@@ -151,7 +151,7 namespace Implab.Test { | |||
|
151 | 151 | var pool = new WorkerPool(5, 20); |
|
152 | 152 | Assert.AreEqual(5, pool.PoolSize); |
|
153 | 153 | pool.Dispose(); |
|
154 |
Thread.Sleep( |
|
|
154 | Thread.Sleep(500); | |
|
155 | 155 | Assert.AreEqual(0, pool.PoolSize); |
|
156 | 156 | pool.Dispose(); |
|
157 | 157 | } |
@@ -244,7 +244,7 namespace Implab.Test { | |||
|
244 | 244 | [TestMethod] |
|
245 | 245 | public void ChainedMapTest() { |
|
246 | 246 | |
|
247 |
using (var pool = new WorkerPool(0,100, |
|
|
247 | using (var pool = new WorkerPool(0,100,1)) { | |
|
248 | 248 | int count = 10000; |
|
249 | 249 | |
|
250 | 250 | double[] args = new double[count]; |
|
1 | NO CONTENT: modified file, binary diff hidden |
@@ -9,12 +9,15 namespace Implab.Parallels { | |||
|
9 | 9 | public abstract class DispatchPool<TUnit> : IDisposable { |
|
10 | 10 | readonly int m_minThreads; |
|
11 | 11 | readonly int m_maxThreads; |
|
12 | int m_createdThreads = 0; | |
|
13 | int m_activeThreads = 0; | |
|
14 | int m_sleepingThreads = 0; | |
|
15 | int m_maxRunningThreads = 0; | |
|
16 | int m_exitRequired = 0; | |
|
17 | int m_releaseTimeout = 100; // timeout while the working thread will wait for the new tasks before exit | |
|
12 | ||
|
13 | int m_createdThreads = 0; // the current size of the pool | |
|
14 | int m_activeThreads = 0; // the count of threads which are active | |
|
15 | int m_sleepingThreads = 0; // the count of currently inactive threads | |
|
16 | int m_maxRunningThreads = 0; // the meximum reached size of the pool | |
|
17 | int m_exitRequired = 0; // the pool is going to shutdown, all unused workers are released | |
|
18 | int m_releaseTimeout = 100; // the timeout while the working thread will wait for the new tasks before exit | |
|
19 | int m_wakeEvents = 0; // the count of wake events | |
|
20 | ||
|
18 | 21 | AutoResetEvent m_hasTasks = new AutoResetEvent(false); |
|
19 | 22 | |
|
20 | 23 | protected DispatchPool(int min, int max) { |
@@ -72,26 +75,43 namespace Implab.Parallels { | |||
|
72 | 75 | |
|
73 | 76 | protected abstract bool TryDequeue(out TUnit unit); |
|
74 | 77 | |
|
75 | protected virtual bool ExtendPool() { | |
|
76 | if (m_sleepingThreads == 0) | |
|
77 | // no sleeping workers are available | |
|
78 | // try create one | |
|
79 |
|
|
|
80 |
|
|
|
81 | // we can get here a race condition when several threads asks to extend pool | |
|
82 | // and some sleaping threads are exited due timeout but they are still counted as sleeping | |
|
83 | // in that case all of this threads could exit except one | |
|
84 | WakePool(); | |
|
85 | return true; | |
|
78 | #region thread execution traits | |
|
79 | int SignalThread() { | |
|
80 | var signals = Interlocked.Increment(ref m_wakeEvents); | |
|
81 | if(signals == 1) | |
|
82 | m_hasTasks.Set(); | |
|
83 | return signals; | |
|
86 | 84 |
|
|
87 | 85 | |
|
86 | bool Sleep(int timeout) { | |
|
87 | Interlocked.Increment(ref m_sleepingThreads); | |
|
88 | if (m_hasTasks.WaitOne(timeout)) { | |
|
89 | // this is autoreset event, only one thread can run this block simultaneously | |
|
90 | var sleeping = Interlocked.Decrement(ref m_sleepingThreads); | |
|
91 | if (Interlocked.Decrement(ref m_wakeEvents) > 0) | |
|
92 | m_hasTasks.Set(); // wake next worker | |
|
93 | ||
|
94 | return true; | |
|
95 | } else { | |
|
96 | Interlocked.Decrement(ref m_sleepingThreads); | |
|
97 | return false; | |
|
88 | 98 | } |
|
99 | } | |
|
100 | #endregion | |
|
89 | 101 | |
|
90 | 102 | /// <summary> |
|
91 | 103 | /// Запускает либо новый поток, если раньше не было ни одного потока, либо устанавливает событие пробуждение одного спящего потока |
|
92 | 104 | /// </summary> |
|
93 |
protected void |
|
|
94 | m_hasTasks.Set(); // wake sleeping thread; | |
|
105 | protected void GrowPool() { | |
|
106 | if (m_exitRequired != 0) | |
|
107 | return; | |
|
108 | if (m_sleepingThreads > m_wakeEvents) { | |
|
109 | // all sleeping threads may gone | |
|
110 | SignalThread(); // wake a sleeping thread; | |
|
111 | ||
|
112 | // we can't check whether signal has been processed | |
|
113 | // anyway it may take some time for the thread to start | |
|
114 | // we will ensure that at least one thread is running | |
|
95 | 115 | |
|
96 | 116 | if (AllocateThreadSlot(1)) { |
|
97 | 117 | // if there were no threads in the pool |
@@ -99,42 +119,45 namespace Implab.Parallels { | |||
|
99 | 119 | worker.IsBackground = true; |
|
100 | 120 | worker.Start(); |
|
101 | 121 | } |
|
122 | } else { | |
|
123 | // if there is no sleeping threads in the pool | |
|
124 | StartWorker(); | |
|
125 | } | |
|
102 | 126 | } |
|
103 | 127 | |
|
104 | bool Sleep(int timeout) { | |
|
105 | Interlocked.Increment(ref m_sleepingThreads); | |
|
106 | var result = m_hasTasks.WaitOne(timeout); | |
|
107 | Interlocked.Decrement(ref m_sleepingThreads); | |
|
108 | return result; | |
|
109 | } | |
|
110 | ||
|
111 | protected virtual bool Suspend() { | |
|
128 | private bool Suspend() { | |
|
112 | 129 | //no tasks left, exit if the thread is no longer needed |
|
113 | 130 | bool last; |
|
114 | 131 | bool requestExit; |
|
115 | 132 | |
|
133 | ||
|
134 | ||
|
135 | // if threads have a timeout before releasing | |
|
116 | 136 | if (m_releaseTimeout > 0) |
|
117 | 137 | requestExit = !Sleep(m_releaseTimeout); |
|
118 | 138 | else |
|
119 | 139 | requestExit = true; |
|
120 | 140 | |
|
141 | if (!requestExit) | |
|
142 | return true; | |
|
121 | 143 | |
|
144 | // release unsused thread | |
|
122 | 145 | if (requestExit && ReleaseThreadSlot(out last)) { |
|
123 | 146 | // in case at the moment the last thread was being released |
|
124 | 147 | // a new task was added to the queue, we need to try |
|
125 | 148 | // to revoke the thread to avoid the situation when the task is left unprocessed |
|
126 | if (last && m_hasTasks.WaitOne(0)) { | |
|
149 | if (last && Sleep(0)) { // Sleep(0) will fetch pending task or will return false | |
|
127 | 150 | if (AllocateThreadSlot(1)) |
|
128 | 151 | return true; // spin again... |
|
129 | 152 | else |
|
130 | // we failed to reallocate the first slot for this thread | |
|
131 | // therefore we need to release the event | |
|
132 | m_hasTasks.Set(); | |
|
153 | SignalThread(); // since Sleep(0) has fetched the signal we neet to reschedule it | |
|
154 | ||
|
133 | 155 | } |
|
134 | 156 | |
|
135 | 157 | return false; |
|
136 | 158 | } |
|
137 | 159 | |
|
160 | // wait till infinity | |
|
138 | 161 | Sleep(-1); |
|
139 | 162 | |
|
140 | 163 | return true; |
@@ -215,7 +238,12 namespace Implab.Parallels { | |||
|
215 | 238 | } |
|
216 | 239 | } |
|
217 | 240 | |
|
218 | bool FetchTask(out TUnit unit) { | |
|
241 | protected abstract void InvokeUnit(TUnit unit); | |
|
242 | ||
|
243 | void Worker() { | |
|
244 | TUnit unit; | |
|
245 | Interlocked.Increment(ref m_activeThreads); | |
|
246 | Sleep(0); // remove wake request if the new thread is started | |
|
219 | 247 | do { |
|
220 | 248 | // exit if requested |
|
221 | 249 | if (m_exitRequired != 0) { |
@@ -224,15 +252,15 namespace Implab.Parallels { | |||
|
224 | 252 | if (ReleaseThreadSlotAnyway()) // it was the last worker |
|
225 | 253 | m_hasTasks.Dispose(); |
|
226 | 254 | else |
|
227 |
|
|
|
255 | SignalThread(); // wake next worker | |
|
228 | 256 | unit = default(TUnit); |
|
229 |
|
|
|
257 | break; | |
|
230 | 258 | } |
|
231 | 259 | |
|
232 | 260 | // fetch task |
|
233 | 261 | if (TryDequeue(out unit)) { |
|
234 |
|
|
|
235 |
|
|
|
262 | InvokeUnit(unit); | |
|
263 | continue; | |
|
236 | 264 | } |
|
237 | 265 | |
|
238 | 266 | Interlocked.Decrement(ref m_activeThreads); |
@@ -240,19 +268,11 namespace Implab.Parallels { | |||
|
240 | 268 | // entering suspend state |
|
241 | 269 | // keep this thread and wait |
|
242 | 270 | if (!Suspend()) |
|
243 |
|
|
|
271 | break; | |
|
244 | 272 | |
|
245 | 273 | Interlocked.Increment(ref m_activeThreads); |
|
246 | 274 | } while (true); |
|
247 | } | |
|
248 | 275 | |
|
249 | protected abstract void InvokeUnit(TUnit unit); | |
|
250 | ||
|
251 | void Worker() { | |
|
252 | TUnit unit; | |
|
253 | Interlocked.Increment(ref m_activeThreads); | |
|
254 | while (FetchTask(out unit)) | |
|
255 | InvokeUnit(unit); | |
|
256 | 276 | } |
|
257 | 277 | |
|
258 | 278 | protected virtual void Dispose(bool disposing) { |
@@ -262,7 +282,10 namespace Implab.Parallels { | |||
|
262 | 282 | return; |
|
263 | 283 | |
|
264 | 284 | // wake sleeping threads |
|
265 |
|
|
|
285 | if (m_createdThreads > 0) | |
|
286 | SignalThread(); | |
|
287 | else | |
|
288 | m_hasTasks.Dispose(); | |
|
266 | 289 | GC.SuppressFinalize(this); |
|
267 | 290 | } |
|
268 | 291 | } |
@@ -57,15 +57,8 namespace Implab.Parallels { | |||
|
57 | 57 | var len = Interlocked.Increment(ref m_queueLength); |
|
58 | 58 | m_queue.Enqueue(unit); |
|
59 | 59 | |
|
60 | ExtendPool(); | |
|
61 | } | |
|
62 | ||
|
63 | protected override bool ExtendPool() { | |
|
64 | if (m_queueLength <= m_threshold*ActiveThreads) | |
|
65 | // in this case we are in active thread and it request for additional workers | |
|
66 | // satisfy it only when queue is longer than threshold | |
|
67 | return false; | |
|
68 | return base.ExtendPool(); | |
|
60 | if (len > m_threshold*ActiveThreads) | |
|
61 | GrowPool(); | |
|
69 | 62 | } |
|
70 | 63 | |
|
71 | 64 | protected override bool TryDequeue(out Action unit) { |
@@ -80,11 +73,5 namespace Implab.Parallels { | |||
|
80 | 73 | unit(); |
|
81 | 74 | } |
|
82 | 75 | |
|
83 | protected override bool Suspend() { | |
|
84 | if (m_queueLength == 0) | |
|
85 | return base.Suspend(); | |
|
86 | else | |
|
87 | return true; // spin again without locks... | |
|
88 | 76 |
|
|
89 | 77 | } |
|
90 | } |
General Comments 0
You need to be logged in to leave comments.
Login now