| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- // ThreadWorker.cs
- //
- // Copyright (c) 2008 Jérémie "Garuma" Laval
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- //
- #if NET_4_0 || BOOTSTRAP_NET_4_0
- using System;
- using System.Threading;
- using System.Collections.Concurrent;
- namespace System.Threading.Tasks
- {
- internal class ThreadWorker : IDisposable
- {
- Thread workerThread;
- /* This field is used when a TheadWorker have to call Task.Wait
- * which bring him back here with the static WorkerMethod although
- * it's more optimized for him to continue calling its own WorkerMethod
- */
- [ThreadStatic]
- static ThreadWorker autoReference;
-
- readonly IDequeOperations<Task> dDeque;
- readonly ThreadWorker[] others;
- readonly ManualResetEvent waitHandle;
- readonly IProducerConsumerCollection<Task> sharedWorkQueue;
- readonly IScheduler sched;
- readonly ThreadPriority threadPriority;
- // Flag to tell if workerThread is running
- int started = 0;
-
- readonly int workerLength;
- readonly int workerPosition;
- const int maxRetry = 5;
-
- const int sleepThreshold = 100;
- int deepSleepTime = 8;
-
- public ThreadWorker (IScheduler sched,
- ThreadWorker[] others,
- int workerPosition,
- IProducerConsumerCollection<Task> sharedWorkQueue,
- ThreadPriority priority,
- ManualResetEvent handle)
- {
- this.others = others;
- this.dDeque = new CyclicDeque<Task> ();
- this.sched = sched;
- this.sharedWorkQueue = sharedWorkQueue;
- this.workerLength = others.Length;
- this.workerPosition = workerPosition;
- this.waitHandle = handle;
- this.threadPriority = priority;
- InitializeUnderlyingThread ();
- }
-
- void InitializeUnderlyingThread ()
- {
- this.workerThread = new Thread (WorkerMethodWrapper);
-
- this.workerThread.IsBackground = true;
- this.workerThread.Priority = threadPriority;
- this.workerThread.Name = "ParallelFxThreadWorker";
- }
- public void Dispose ()
- {
- Stop ();
- if (workerThread.ThreadState != ThreadState.Stopped)
- workerThread.Abort ();
- }
-
- public void Pulse ()
- {
- if (started == 1)
- return;
- // If the thread was stopped then set it in use and restart it
- int result = Interlocked.Exchange (ref started, 1);
- if (result != 0)
- return;
- if (this.workerThread.ThreadState != ThreadState.Unstarted) {
- InitializeUnderlyingThread ();
- }
- workerThread.Start ();
- }
-
- public void Stop ()
- {
- // Set the flag to stop so that the while in the thread will stop
- // doing its infinite loop.
- started = 0;
- }
-
- // This is the actual method called in the Thread
- void WorkerMethodWrapper ()
- {
- int sleepTime = 0;
- autoReference = this;
-
- // Main loop
- while (started == 1) {
- bool result = false;
- result = WorkerMethod ();
- if (!result)
- waitHandle.Reset ();
- Thread.Yield ();
- if (result) {
- deepSleepTime = 8;
- sleepTime = 0;
- continue;
- }
- // If we are spinning too much, have a deeper sleep
- if (++sleepTime > sleepThreshold && sharedWorkQueue.Count == 0) {
- waitHandle.WaitOne ((deepSleepTime = deepSleepTime >= 0x4000 ? 0x4000 : deepSleepTime << 1));
- }
- }
- started = 0;
- }
-
- // Main method, used to do all the logic of retrieving, processing and stealing work.
- bool WorkerMethod ()
- {
- bool result = false;
- bool hasStolenFromOther;
- do {
- hasStolenFromOther = false;
-
- Task value;
-
- // We fill up our work deque concurrently with other ThreadWorker
- while (sharedWorkQueue.Count > 0) {
- while (sharedWorkQueue.TryTake (out value)) {
- dDeque.PushBottom (value);
- waitHandle.Set ();
- }
- // Now we process our work
- while (dDeque.PopBottom (out value) == PopResult.Succeed) {
- waitHandle.Set ();
- if (value != null) {
- value.Execute (ChildWorkAdder);
- result = true;
- }
- }
- }
- // When we have finished, steal from other worker
- ThreadWorker other;
-
- // Repeat the operation a little so that we can let other things process.
- for (int j = 0; j < maxRetry; ++j) {
- int len = workerLength + workerPosition;
- // Start stealing with the ThreadWorker at our right to minimize contention
- for (int it = workerPosition + 1; it < len; ++it) {
- int i = it % workerLength;
- if ((other = others [i]) == null || other == this)
- continue;
-
- // Maybe make this steal more than one item at a time, see TODO.
- if (other.dDeque.PopTop (out value) == PopResult.Succeed) {
- waitHandle.Set ();
- hasStolenFromOther = true;
- if (value != null) {
- value.Execute (ChildWorkAdder);
- result = true;
- }
- }
- }
- }
- } while (sharedWorkQueue.Count > 0 || hasStolenFromOther);
-
- return result;
- }
-
- // Almost same as above but with an added predicate and treating one item at a time.
- // It's used by Scheduler Participate(...) method for special waiting case like
- // Task.WaitAll(someTasks) or Task.WaitAny(someTasks)
- // Predicate should be really fast and not blocking as it is called a good deal of time
- // Also, the method skip tasks that are LongRunning to avoid blocking (Task are not LongRunning by default)
- public static void WorkerMethod (Func<bool> predicate, IProducerConsumerCollection<Task> sharedWorkQueue,
- ThreadWorker[] others, ManualResetEvent evt)
- {
- while (!predicate ()) {
- Task value;
-
- // If we are in fact a normal ThreadWorker, use our own deque
- if (autoReference != null) {
- while (autoReference.dDeque.PopBottom (out value) == PopResult.Succeed && value != null) {
- evt.Set ();
- if (CheckTaskFitness (value))
- value.Execute (autoReference.ChildWorkAdder);
- else {
- autoReference.dDeque.PushBottom (value);
- evt.Set ();
- }
- if (predicate ())
- return;
- }
- }
- // Dequeue only one item as we have restriction
- while (sharedWorkQueue.TryTake (out value) && value != null) {
- evt.Set ();
- if (CheckTaskFitness (value))
- value.Execute (null);
- else {
- sharedWorkQueue.TryAdd (value);
- evt.Set ();
- }
- if (predicate ())
- return;
- }
- // First check to see if we comply to predicate
- if (predicate ())
- return;
-
- // Try to complete other work by stealing since our desired tasks may be in other worker
- ThreadWorker other;
- for (int i = 0; i < others.Length; i++) {
- if ((other = others [i]) == null)
- continue;
-
- if (other.dDeque.PopTop (out value) == PopResult.Succeed && value != null) {
- evt.Set ();
- if (CheckTaskFitness (value))
- value.Execute (null);
- else {
- sharedWorkQueue.TryAdd (value);
- evt.Set ();
- }
- }
-
- if (predicate ())
- return;
- }
- Thread.Yield ();
- }
- }
- internal void ChildWorkAdder (Task t)
- {
- dDeque.PushBottom (t);
- sched.PulseAll ();
- }
-
- static bool CheckTaskFitness (Task t)
- {
- return (t.CreationOptions | TaskCreationOptions.LongRunning) > 0;
- }
-
- public bool Finished {
- get {
- return started == 0;
- }
- }
- public int Id {
- get {
- return workerThread.ManagedThreadId;
- }
- }
-
- public bool Equals (ThreadWorker other)
- {
- return (other == null) ? false : object.ReferenceEquals (this.dDeque, other.dDeque);
- }
-
- public override bool Equals (object obj)
- {
- ThreadWorker temp = obj as ThreadWorker;
- return temp == null ? false : Equals (temp);
- }
-
- public override int GetHashCode ()
- {
- return workerThread.ManagedThreadId.GetHashCode ();
- }
- }
- }
- #endif
|