00481ffe79cc9b817560cccd78fd5b193a46f0b2
[mono.git] / mcs / class / Mono.Parallel / Mono.Threading.Tasks / FixedTaskScheduler.cs
1 // Scheduler.cs
2 //
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
4 //
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:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
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
21 // THE SOFTWARE.
22 //
23 //
24
25 #if NET_4_0 || MOBILE
26 using System;
27 using System.Threading;
28 using System.Threading.Tasks;
29 using System.Collections.Concurrent;
30
31 namespace Mono.Threading.Tasks
32 {
33         public class FixedTaskScheduler: TaskScheduler, IMonoTaskScheduler
34         {
35                 readonly IProducerConsumerCollection<Task> workQueue;
36                 readonly ThreadWorker[]        workers;
37                 readonly ManualResetEvent      pulseHandle = new ManualResetEvent (false);
38
39                 public FixedTaskScheduler ()
40                         : this (Environment.ProcessorCount, ThreadPriority.Normal)
41                 {
42                         
43                 }
44                 
45                 public FixedTaskScheduler (int maxWorker, ThreadPriority priority)
46                 {
47                         workQueue = new ConcurrentQueue<Task> ();
48                         workers = new ThreadWorker [maxWorker];
49                         
50                         for (int i = 0; i < maxWorker; i++) {
51                                 workers [i] = new ThreadWorker (workers, i, workQueue, new CyclicDeque<Task> (), priority, pulseHandle);
52                                 workers [i].Pulse ();
53                         }
54                 }
55
56                 protected override void QueueTask (Task t)
57                 {
58                         // Add to the shared work pool
59                         workQueue.TryAdd (t);
60                         // Wake up some worker if they were asleep
61                         PulseAll ();
62                 }
63
64                 public void MonoParticipateUntil (Task task)
65                 {
66                         if (task.IsCompleted)
67                                 return;
68
69                         ManualResetEventSlim evt = new ManualResetEventSlim (false);
70                         task.ContinueWith (_ => evt.Set (), TaskContinuationOptions.ExecuteSynchronously);
71                         if (evt.IsSet || task.IsCompleted)
72                                 return;
73                         
74                         ParticipateUntilInternal (task, evt, -1);
75                 }
76                 
77                 public bool MonoParticipateUntil (Task task, ManualResetEventSlim evt, int millisecondsTimeout)
78                 {
79                         if (task.IsCompleted)
80                                 return false;
81
82                         bool isFromPredicate = true;
83                         task.ContinueWith (_ => { isFromPredicate = false; evt.Set (); }, TaskContinuationOptions.ExecuteSynchronously);
84
85                         ParticipateUntilInternal (task, evt, millisecondsTimeout);
86
87                         if (task.IsCompleted)
88                                 return false;
89
90                         return isFromPredicate;
91                 }
92                 
93                 public void ParticipateUntilInternal (Task self, ManualResetEventSlim evt, int millisecondsTimeout)
94                 {
95                         ThreadWorker.ParticipativeWorkerMethod (self, evt, millisecondsTimeout, workQueue, workers, pulseHandle, (a, b) => true);
96                 }
97
98                 static bool TaskCompletedPredicate (Task self)
99                 {
100                         return self.IsCompleted;
101                 }
102                 
103                 public void PulseAll ()
104                 {
105                         pulseHandle.Set ();
106                 }
107                 
108                 public void Dispose ()
109                 {
110                         foreach (ThreadWorker w in workers)
111                                 w.Dispose ();
112                 }
113
114                 #region Scheduler dummy stubs
115                 protected override System.Collections.Generic.IEnumerable<Task> GetScheduledTasks ()
116                 {
117                         throw new System.NotImplementedException();
118                 }
119
120                 protected override bool TryDequeue (Task task)
121                 {
122                         throw new System.NotImplementedException();
123                 }
124
125                 protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
126                 {
127                         task.Execute (null);
128                         return true;
129                 }
130                 
131                 public override int MaximumConcurrencyLevel {
132                         get {
133                                 return base.MaximumConcurrencyLevel;
134                         }
135                 }
136                 #endregion
137         }
138 }
139 #endif