| 190 | } |
| 191 | |
| 192 | SC::Result SC::ThreadPool::waitForTask(Task& task) |
| 193 | { |
| 194 | SC_TRY_MSG(numWorkerThreads > 0, "Cannot wait for tasks on an uninitialized threadpool"); |
| 195 | |
| 196 | // Function is entirely protected by the mutex |
| 197 | poolMutex.lock(); |
| 198 | auto deferUnlock = MakeDeferred([this] { poolMutex.unlock(); }); |
| 199 | |
| 200 | for (;;) |
| 201 | { |
| 202 | if (task.threadPool == nullptr) |
| 203 | { |
| 204 | break; // The task being waited has been flagged as completed |
| 205 | } |
| 206 | if (taskHead != nullptr or numRunningTasks != 0) |
| 207 | { |
| 208 | taskCompleted.wait(poolMutex); |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | break; // all tasks have completed (including the task being waited) |
| 213 | } |
| 214 | } |
| 215 | return Result(true); |
| 216 | } |
| 217 | |
| 218 | SC::Result SC::ThreadPool::queueTask(Task& task) |
| 219 | { |