comparison Implab/Parallels/DispatchPool.cs @ 34:dabf79fde388

fixed race condition in DispatchPool
author cin
date Thu, 10 Apr 2014 04:20:25 +0400
parents 2fad2d1f4b03
children 2fc0fbe7d58b
comparison
equal deleted inserted replaced
33:b255e4aeef17 34:dabf79fde388
153 EnsurePoolIsAlive(); 153 EnsurePoolIsAlive();
154 } 154 }
155 } 155 }
156 } 156 }
157 157
158 private void EnsurePoolIsAlive() { 158 protected void EnsurePoolIsAlive() {
159 if (AllocateThreadSlot(1)) { 159 if (AllocateThreadSlot(1)) {
160 // if there were no threads in the pool 160 // if there were no threads in the pool
161 var worker = new Thread(this.Worker); 161 var worker = new Thread(this.Worker);
162 worker.IsBackground = true; 162 worker.IsBackground = true;
163 worker.Start(); 163 worker.Start();
164 } 164 }
165 } 165 }
166 166
167 private bool Suspend() { 167 protected virtual bool Suspend() {
168 //no tasks left, exit if the thread is no longer needed 168 //no tasks left, exit if the thread is no longer needed
169 bool last; 169 bool last;
170 bool requestExit; 170 bool requestExit;
171 171
172 // if threads have a timeout before releasing 172 // if threads have a timeout before releasing
293 // fetch task 293 // fetch task
294 if (TryDequeue(out unit)) { 294 if (TryDequeue(out unit)) {
295 InvokeUnit(unit); 295 InvokeUnit(unit);
296 continue; 296 continue;
297 } 297 }
298
299 Interlocked.Decrement(ref m_activeThreads); 298 Interlocked.Decrement(ref m_activeThreads);
300 299
301 // entering suspend state 300 // entering suspend state
302 // keep this thread and wait 301 // keep this thread and wait
303 if (!Suspend()) 302 if (!Suspend())