comparison Implab/Parallels/DispatchPool.cs @ 24:ee04e1fa78da

fixed dispatch pool race condition
author cin
date Thu, 14 Nov 2013 01:15:07 +0400
parents 5a35900264f5
children 2fad2d1f4b03
comparison
equal deleted inserted replaced
23:f0568ff069a5 24:ee04e1fa78da
149 worker.IsBackground = true; 149 worker.IsBackground = true;
150 worker.Start(); 150 worker.Start();
151 } 151 }
152 } else { 152 } else {
153 // if there is no sleeping threads in the pool 153 // if there is no sleeping threads in the pool
154 StartWorker(); 154 if (!StartWorker())
155 // we haven't started a new thread, but the current can be on the way and it can't process the queue
156 // send it a signal to spin again
157 SignalThread();
155 } 158 }
156 } 159 }
157 160
158 private bool Suspend() { 161 private bool Suspend() {
159 //no tasks left, exit if the thread is no longer needed 162 //no tasks left, exit if the thread is no longer needed
279 Interlocked.Decrement(ref m_activeThreads); 282 Interlocked.Decrement(ref m_activeThreads);
280 if (ReleaseThreadSlotAnyway()) // it was the last worker 283 if (ReleaseThreadSlotAnyway()) // it was the last worker
281 m_hasTasks.Dispose(); 284 m_hasTasks.Dispose();
282 else 285 else
283 SignalThread(); // wake next worker 286 SignalThread(); // wake next worker
284 unit = default(TUnit);
285 break; 287 break;
286 } 288 }
287 289
288 // fetch task 290 // fetch task
289 if (TryDequeue(out unit)) { 291 if (TryDequeue(out unit)) {