/// <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(); }
/// <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(); } }
/// <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(); } }
private void EnsureHelperInstance() { if (dispatcher == null) dispatcher = new UnityThreading.Dispatcher(); if (taskDistributor == null) taskDistributor = new UnityThreading.TaskDistributor(); }
/// <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)); }
void OnDestroy() { foreach (var thread in registeredThreads) { thread.Dispose(); } if (dispatcher != null) { dispatcher.Dispose(); } dispatcher = null; if (taskDistributor != null) { taskDistributor.Dispose(); } taskDistributor = null; }
/// <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; }
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); }
/// <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(); }
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; }
/// <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; } }
/// <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); }
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; }
/// <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); }
/// <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)); }
private void EnsureHelperInstance() { dispatcher = UnityThreading.Dispatcher.MainNoThrow ?? new UnityThreading.Dispatcher(); taskDistributor = UnityThreading.TaskDistributor.MainNoThrow ?? new UnityThreading.TaskDistributor("TaskDistributor"); }
/// <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))); }
/// <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)); }
/// <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)); }
/// <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)); }
public TaskWorker(string name, TaskDistributor taskDistributor) : base(name, false) { this.TaskDistributor = taskDistributor; this.Dispatcher = new Dispatcher(false); }
public TaskWorker(TaskDistributor taskDistributor) : base(false) { this.TaskDistributor = taskDistributor; this.Dispatcher = new Dispatcher(false); }
/// <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)); }
private void EnsureHelperInstance() { if (dispatcher == null) dispatcher = new UnityThreading.Dispatcher(); if (taskDistributor == null) taskDistributor = new UnityThreading.TaskDistributor(); isWebPlayer = Application.isWebPlayer; }
/// <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)); }
/// <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; }
/// <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; }