Inheritance: DispatcherBase
コード例 #1
0
		/// <summary>
		/// Creates a new instance of the TaskDistributor.
		/// </summary>
        /// <param name="workerThreadCount">The number of worker threads, a value below one will create ProcessorCount x3 worker threads.</param>
		/// <param name="autoStart">Should the instance auto start the worker threads.</param>
		public TaskDistributor(int workerThreadCount, bool autoStart)
			: base()
		{
            if (workerThreadCount <= 0)
            {
#if !NO_UNITY
                workerThreadCount = UnityEngine.SystemInfo.processorCount * 2;
#else
                workerThreadCount = Environment.ProcessorCount * 2;
#endif
            }

			workerThreads = new TaskWorker[workerThreadCount];
			lock (workerThreads)
			{
				for (var i = 0; i < workerThreadCount; ++i)
					workerThreads[i] = new TaskWorker(this);
			}

			if (mainTaskDistributor == null)
				mainTaskDistributor = this;

			if (autoStart)
				Start();
		}
コード例 #2
0
        /// <summary>
        /// Creates a new instance of the TaskDistributor.
        /// </summary>
        /// <param name="workerThreadCount">The number of worker threads, a value below one will create ProcessorCount x3 worker threads.</param>
        /// <param name="autoStart">Should the instance auto start the worker threads.</param>
        public TaskDistributor(int workerThreadCount, bool autoStart)
            : base()
        {
            if (workerThreadCount <= 0)
            {
#if !NO_UNITY
                workerThreadCount = UnityEngine.SystemInfo.processorCount * 3;
#else
                workerThreadCount = Environment.ProcessorCount * 3;
#endif
            }

            workerThreads = new TaskWorker[workerThreadCount];
            lock (workerThreads)
            {
                for (var i = 0; i < workerThreadCount; ++i)
                {
                    workerThreads[i] = new TaskWorker(this);
                }
            }

            if (mainTaskDistributor == null)
            {
                mainTaskDistributor = this;
            }

            if (autoStart)
            {
                Start();
            }
        }
コード例 #3
0
 /// <summary>
 /// Creates a new instance of the TaskDistributor.
 /// </summary>
 /// <param name="workerThreadCount">The number of worker threads, a value below one will create ProcessorCount x2 worker threads.</param>
 /// <param name="autoStart">Should the instance auto start the worker threads.</param>
 public TaskDistributor(string name, int workerThreadCount, bool autoStart)
     : base()
 {
     this.name = name;
     if (workerThreadCount <= 0)
     {
         workerThreadCount = ThreadBase.AvailableProcessors * 2;
     }
     workerThreads = new TaskWorker[workerThreadCount];
     lock (workerThreads)
     {
         for (var i = 0; i < workerThreadCount; ++i)
         {
             workerThreads[i] = new TaskWorker(name, this);
         }
     }
     if (mainTaskDistributor == null)
     {
         mainTaskDistributor = this;
     }
     if (autoStart)
     {
         Start();
     }
 }
コード例 #4
0
    private void EnsureHelperInstance()
    {
        if (dispatcher == null)
            dispatcher = new UnityThreading.Dispatcher();

        if (taskDistributor == null)
            taskDistributor = new UnityThreading.TaskDistributor();
    }
コード例 #5
0
        /// <summary>
        /// Starts the given Task when this Task ended successfully.
        /// </summary>
        /// <param name="followingTask">The task to start.</param>
        /// <returns>This task.</returns>
        public static Task Then(this Task that, Task followingTask)
        {
            TaskDistributor target = null;

            if (ThreadBase.CurrentThread is TaskWorker)
            {
                target = ((TaskWorker)ThreadBase.CurrentThread).TaskDistributor;
            }
            return(that.Then(followingTask, target));
        }
コード例 #6
0
    void OnDestroy()
    {
        foreach (var thread in registeredThreads)
        {
            thread.Dispose();
        }

        if (dispatcher != null)
        {
            dispatcher.Dispose();
        }
        dispatcher = null;

        if (taskDistributor != null)
        {
            taskDistributor.Dispose();
        }
        taskDistributor = null;
    }
コード例 #7
0
ファイル: TaskDistributer.cs プロジェクト: zh5243a/libpomelo2
        /// <summary>
        /// Disposes all TaskDistributor, worker threads, resources and remaining tasks.
        /// </summary>
        public override void Dispose()
        {
            if (isDisposed)
            {
                return;
            }

            while (true)
            {
                Task currentTask;
                lock (taskListSyncRoot)
                {
                    if (taskList.Count != 0)
                    {
                        currentTask = taskList.Dequeue();
                    }
                    else
                    {
                        break;
                    }
                }
                currentTask.Dispose();
            }

            lock (workerThreads)
            {
                for (var i = 0; i < workerThreads.Length; ++i)
                {
                    workerThreads[i].Dispose();
                }
                workerThreads = new TaskWorker[0];
            }

            dataEvent.Close();
            dataEvent = null;

            if (mainTaskDistributor == this)
            {
                mainTaskDistributor = null;
            }

            isDisposed = true;
        }
コード例 #8
0
 protected override IEnumerator Do()
 {
     while (!exitEvent.InterWaitOne(0))
     {
         if (!Dispatcher.ProcessNextTask())
         {
             TaskDistributor.FillTasks(Dispatcher);
             if (Dispatcher.TaskCount == 0)
             {
                 var result = WaitHandle.WaitAny(new WaitHandle[] { exitEvent, TaskDistributor.NewDataWaitHandle });
                 if (result == 0)
                 {
                     return(null);
                 }
                 TaskDistributor.FillTasks(Dispatcher);
             }
         }
     }
     return(null);
 }
コード例 #9
0
        /// <summary>
        /// Creates a new instance of the TaskDistributor.
        /// </summary>
        /// <param name="workerThreadCount">The number of worker threads, a value below one will create ProcessorCount x2 worker threads.</param>
        /// <param name="autoStart">Should the instance auto start the worker threads.</param>
        public TaskDistributor(string name, int workerThreadCount, bool autoStart)
            : base()
        {
            this.name = name;
            if (workerThreadCount <= 0)
                workerThreadCount = ThreadBase.AvailableProcessors * 2;

            workerThreads = new TaskWorker[workerThreadCount];
            lock (workerThreads)
            {
                for (var i = 0; i < workerThreadCount; ++i)
                    workerThreads[i] = new TaskWorker(name, this);
            }

            if (mainTaskDistributor == null)
                mainTaskDistributor = this;

            if (autoStart)
                Start();
        }
コード例 #10
0
    void OnDestroy()
    {
        foreach (var thread in registeredThreads)
        {
            thread.Dispose();
        }

        if (dispatcher != null)
        {
            dispatcher.Dispose();
        }
        dispatcher = null;

        if (taskDistributor != null)
        {
            taskDistributor.Dispose();
        }
        taskDistributor = null;

        //maybe must be fixed! :P
//        if (instance == this)
//            instance = null;
    }
コード例 #11
0
        /// <summary>
        /// Disposes all TaskDistributor, worker threads, resources and remaining tasks.
        /// </summary>
        public override void Dispose()
        {
            while (true)
            {
                TaskBase currentTask;
                lock (taskList)
                {
                    if (taskList.Count != 0)
                    {
                        currentTask = taskList[0];
                        taskList.RemoveAt(0);
                    }
                    else
                    {
                        break;
                    }
                }
                currentTask.Dispose();
            }

            lock (workerThreads)
            {
                for (var i = 0; i < workerThreads.Length; ++i)
                {
                    workerThreads[i].Dispose();
                }
                workerThreads = new TaskWorker[0];
            }

            dataEvent.Close();
            dataEvent = null;

            if (mainTaskDistributor == this)
            {
                mainTaskDistributor = null;
            }
        }
コード例 #12
0
        /// <summary>
        /// Performs the given Func sequential for each element in the enumerable.
        /// </summary>
        /// <param name="action">The Func to perform for each element.</param>
        /// <param name="target">The TaskDistributor instance on which the operation should perform.</param>
        /// <returns>IEnumerable of created tasks.</returns>
        public static IEnumerable <Task <TResult> > SequentialForEach <TResult, T>(this IEnumerable <T> that, Func <T, TResult> action, TaskDistributor target)
        {
            var  result   = new List <Task <TResult> >();
            Task lastTask = null;

            foreach (var element in that)
            {
                var tmp  = element;
                var task = Task.Create(() => action(tmp));
                if (lastTask == null)
                {
                    task.Run(target);
                }
                else
                {
                    lastTask.WhenEnded(() => task.Run(target));
                }
                lastTask = task;
                result.Add(task);
            }
            return(result);
        }
コード例 #13
0
    void OnDestroy()
    {
        foreach (var thread in registeredThreads)
            thread.Dispose();

        if (dispatcher != null)
            dispatcher.Dispose();
        dispatcher = null;

        if (taskDistributor != null)
            taskDistributor.Dispose();
        taskDistributor = null;

        if (instance == this)
            instance = null;
    }
コード例 #14
0
        /// <summary>
        /// Performs the given Func parallel for each element in the enumerable.
        /// </summary>
        /// <param name="action">The Func to perform for each element.</param>
        /// <param name="target">The TaskDistributor instance on which the operation should perform.</param>
        /// <returns>IEnumerable of created tasks.</returns>
        public static IEnumerable <Task <TResult> > ParallelForEach <TResult, T>(this IEnumerable <T> that, Func <T, TResult> action, TaskDistributor target)
        {
            var result = new List <Task <TResult> >();

            foreach (var element in that)
            {
                var tmp  = element;
                var task = Task.Create(() => action(tmp)).Run(target);
                result.Add(task);
            }
            return(result);
        }
コード例 #15
0
 /// <summary>
 /// Performs the given Action sequential for each element in the enumerable.
 /// </summary>
 /// <param name="action">The Action to perform for each element.</param>
 /// <param name="target">The TaskDistributor instance on which the operation should perform.</param>
 /// <returns>IEnumerable of created tasks.</returns>
 public static IEnumerable <Task> SequentialForEach <T>(this IEnumerable <T> that, Action <T> action, TaskDistributor target)
 {
     return((IEnumerable <Task>)that.SequentialForEach(element => { action(element); return default(UnityThreading.Task.Unit); }, target));
 }
コード例 #16
0
 private void EnsureHelperInstance()
 {
     dispatcher      = UnityThreading.Dispatcher.MainNoThrow ?? new UnityThreading.Dispatcher();
     taskDistributor = UnityThreading.TaskDistributor.MainNoThrow ?? new UnityThreading.TaskDistributor("TaskDistributor");
 }
コード例 #17
0
 /// <summary>
 /// Starts the Enumerator as async Task on the given TaskDistributor.
 /// </summary>
 /// <param name="target">The TaskDistributor instance on which the operation should perform.</param>
 /// <returns>The task.</returns>
 public static Task RunAsync(this IEnumerator that, TaskDistributor target)
 {
     return(target.Dispatch(Task.Create(that)));
 }
コード例 #18
0
 /// <summary>
 /// Starts the given Method as async Task on the given TaskDistributor.
 /// </summary>
 /// <param name="target">The TaskDistributor instance on which the operation should perform.</param>
 /// <param name="args">Optional arguments passed to the method.</param>
 /// <returns>The task.</returns>
 public static Task <T> RunAsync <T>(this object that, string methodName, TaskDistributor target, params object[] args)
 {
     return(Task.Create <T>(that, methodName, args).Run(target));
 }
コード例 #19
0
 /// <summary>
 /// Starts the Func as async Task on the given TaskDistributor.
 /// </summary>
 /// <param name="target">The TaskDistributor instance on which the operation should perform.</param>
 /// <returns>The task.</returns>
 public static Task <T> RunAsync <T>(this Func <T> that, TaskDistributor target)
 {
     return(target.Dispatch(that));
 }
コード例 #20
0
 /// <summary>
 /// Starts the Enumerator as async Task on the given TaskDistributor.
 /// </summary>
 /// <param name="target">The TaskDistributor instance on which the operation should perform.</param>
 /// <returns>The task.</returns>
 public static Task RunAsync(this IEnumerator that, TaskDistributor target)
 {
     return target.Dispatch(Task.Create(that));
 }
コード例 #21
0
 public TaskWorker(string name, TaskDistributor taskDistributor)
     : base(name, false)
 {
     this.TaskDistributor = taskDistributor;
     this.Dispatcher      = new Dispatcher(false);
 }
コード例 #22
0
		public TaskWorker(TaskDistributor taskDistributor)
            : base(false)
        {
			this.TaskDistributor = taskDistributor;
			this.Dispatcher = new Dispatcher(false);
		}
コード例 #23
0
 /// <summary>
 /// Starts the Action as async Task on the given TaskDistributor.
 /// </summary>
 /// <param name="target">The TaskDistributor instance on which the operation should perform.</param>
 /// <returns>The task.</returns>
 public static Task RunAsync(this Action that, TaskDistributor target)
 {
     return(target.Dispatch(that));
 }
コード例 #24
0
    private void EnsureHelperInstance()
    {
		dispatcher = UnityThreading.Dispatcher.MainNoThrow ?? new UnityThreading.Dispatcher();
		taskDistributor = UnityThreading.TaskDistributor.MainNoThrow ?? new UnityThreading.TaskDistributor("TaskDistributor");
    }
コード例 #25
0
    private void EnsureHelperInstance()
    {
        if (dispatcher == null)
            dispatcher = new UnityThreading.Dispatcher();

        if (taskDistributor == null)
            taskDistributor = new UnityThreading.TaskDistributor();

        isWebPlayer = Application.isWebPlayer;
    }
コード例 #26
0
 /// <summary>
 /// Starts the given Method as async Task on the given TaskDistributor.
 /// </summary>
 /// <param name="target">The TaskDistributor instance on which the operation should perform.</param>
 /// <param name="args">Optional arguments passed to the method.</param>
 /// <returns>The task.</returns>
 public static Task RunAsync(this object that, string methodName, TaskDistributor target, params object[] args)
 {
     return(that.RunAsync <object>(methodName, target, args));
 }
コード例 #27
0
		/// <summary>
		/// Disposes all TaskDistributor, worker threads, resources and remaining tasks.
		/// </summary>
        public override void Dispose()
        {
			while (true)
			{
				TaskBase currentTask;
                lock (taskList)
                {
                    if (taskList.Count != 0)
                    {
                        currentTask = taskList[0];
                        taskList.RemoveAt(0);
                    }
                    else
                        break;
                }
				currentTask.Dispose();
			}

			lock (workerThreads)
			{
				for (var i = 0; i < workerThreads.Length; ++i)
					workerThreads[i].Dispose();
				workerThreads = new TaskWorker[0];
			}

			dataEvent.Close();
			dataEvent = null;

			if (mainTaskDistributor == this)
				mainTaskDistributor = null;
        }
コード例 #28
0
        /// <summary>
        /// Disposes all TaskDistributor, worker threads, resources and remaining tasks.
        /// </summary>
        public override void Dispose()
        {
            if (isDisposed)
                return;

            while (true)
            {
                Task currentTask;
                lock (taskListSyncRoot)
                {
                    if (taskList.Count != 0)
                        currentTask = taskList.Dequeue();
                    else
                        break;
                }
                currentTask.Dispose();
            }

            lock (workerThreads)
            {
                for (var i = 0; i < workerThreads.Length; ++i)
                    workerThreads[i].Dispose();
                workerThreads = new TaskWorker[0];
            }

            dataEvent.Close();
            dataEvent = null;

            if (mainTaskDistributor == this)
                mainTaskDistributor = null;

            isDisposed = true;
        }