Mercurial > pub > ImplabNet
comparison Implab/Parallels/DispatchPool.cs @ 30:2fad2d1f4b03
small refactoring, cleanup.
| author | cin |
|---|---|
| date | Mon, 07 Apr 2014 03:25:57 +0400 |
| parents | ee04e1fa78da |
| children | dabf79fde388 |
comparison
equal
deleted
inserted
replaced
| 29:768f7deeb55b | 30:2fad2d1f4b03 |
|---|---|
| 141 | 141 |
| 142 // we can't check whether signal has been processed | 142 // we can't check whether signal has been processed |
| 143 // anyway it may take some time for the thread to start | 143 // anyway it may take some time for the thread to start |
| 144 // we will ensure that at least one thread is running | 144 // we will ensure that at least one thread is running |
| 145 | 145 |
| 146 if (AllocateThreadSlot(1)) { | 146 EnsurePoolIsAlive(); |
| 147 // if there were no threads in the pool | |
| 148 var worker = new Thread(this.Worker); | |
| 149 worker.IsBackground = true; | |
| 150 worker.Start(); | |
| 151 } | |
| 152 } else { | 147 } else { |
| 153 // if there is no sleeping threads in the pool | 148 // if there is no sleeping threads in the pool |
| 154 if (!StartWorker()) | 149 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 | 150 // we haven't started a new thread, but the current can be on the way to terminate and it can't process the queue |
| 156 // send it a signal to spin again | 151 // send it a signal to spin again |
| 157 SignalThread(); | 152 SignalThread(); |
| 153 EnsurePoolIsAlive(); | |
| 154 } | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 private void EnsurePoolIsAlive() { | |
| 159 if (AllocateThreadSlot(1)) { | |
| 160 // if there were no threads in the pool | |
| 161 var worker = new Thread(this.Worker); | |
| 162 worker.IsBackground = true; | |
| 163 worker.Start(); | |
| 158 } | 164 } |
| 159 } | 165 } |
| 160 | 166 |
| 161 private bool Suspend() { | 167 private bool Suspend() { |
| 162 //no tasks left, exit if the thread is no longer needed | 168 //no tasks left, exit if the thread is no longer needed |
| 175 // release unsused thread | 181 // release unsused thread |
| 176 if (requestExit && ReleaseThreadSlot(out last)) { | 182 if (requestExit && ReleaseThreadSlot(out last)) { |
| 177 // in case at the moment the last thread was being released | 183 // in case at the moment the last thread was being released |
| 178 // a new task was added to the queue, we need to try | 184 // a new task was added to the queue, we need to try |
| 179 // to revoke the thread to avoid the situation when the task is left unprocessed | 185 // to revoke the thread to avoid the situation when the task is left unprocessed |
| 180 if (last && Sleep(0)) { // Sleep(0) will fetch pending task or will return false | 186 if (last && FetchSignalOrWait(0)) { // FetchSignalOrWait(0) will fetch pending task or will return false |
| 181 if (AllocateThreadSlot(1)) | 187 SignalThread(); // since FetchSignalOrWait(0) has fetched the signal we need to reschedule it |
| 182 return true; // spin again... | 188 return AllocateThreadSlot(1); // ensure that at least one thread is alive |
| 183 else | |
| 184 SignalThread(); // since Sleep(0) has fetched the signal we neet to reschedule it | |
| 185 | |
| 186 } | 189 } |
| 187 | 190 |
| 188 return false; | 191 return false; |
| 189 } | 192 } |
| 190 | 193 |
