3 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 using System.Threading;
28 using System.Threading.Tasks;
29 using System.Collections.Concurrent;
31 namespace Mono.Threading.Tasks
33 public class FixedTaskScheduler: TaskScheduler, IMonoTaskScheduler
35 readonly IProducerConsumerCollection<Task> workQueue;
36 readonly ThreadWorker[] workers;
37 readonly ManualResetEvent pulseHandle = new ManualResetEvent (false);
39 public FixedTaskScheduler ()
40 : this (Environment.ProcessorCount, ThreadPriority.Normal)
45 public FixedTaskScheduler (int maxWorker, ThreadPriority priority)
47 workQueue = new ConcurrentQueue<Task> ();
48 workers = new ThreadWorker [maxWorker];
50 for (int i = 0; i < maxWorker; i++) {
51 workers [i] = new ThreadWorker (workers, i, workQueue, new CyclicDeque<Task> (), priority, pulseHandle);
56 protected override void QueueTask (Task t)
58 // Add to the shared work pool
60 // Wake up some worker if they were asleep
64 public void MonoParticipateUntil (Task task)
69 ManualResetEventSlim evt = new ManualResetEventSlim (false);
70 task.ContinueWith (_ => evt.Set (), TaskContinuationOptions.ExecuteSynchronously);
71 if (evt.IsSet || task.IsCompleted)
74 ParticipateUntilInternal (task, evt, -1);
77 public bool MonoParticipateUntil (Task task, ManualResetEventSlim evt, int millisecondsTimeout)
82 bool isFromPredicate = true;
83 task.ContinueWith (_ => { isFromPredicate = false; evt.Set (); }, TaskContinuationOptions.ExecuteSynchronously);
85 ParticipateUntilInternal (task, evt, millisecondsTimeout);
90 return isFromPredicate;
93 public void ParticipateUntilInternal (Task self, ManualResetEventSlim evt, int millisecondsTimeout)
95 ThreadWorker.ParticipativeWorkerMethod (self, evt, millisecondsTimeout, workQueue, workers, pulseHandle, (a, b) => true);
98 static bool TaskCompletedPredicate (Task self)
100 return self.IsCompleted;
103 public void PulseAll ()
108 public void Dispose ()
110 foreach (ThreadWorker w in workers)
114 #region Scheduler dummy stubs
115 protected override System.Collections.Generic.IEnumerable<Task> GetScheduledTasks ()
117 throw new System.NotImplementedException();
120 protected override bool TryDequeue (Task task)
122 throw new System.NotImplementedException();
125 protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
131 public override int MaximumConcurrencyLevel {
133 return base.MaximumConcurrencyLevel;