| 216 | } |
| 217 | |
| 218 | SC::Result SC::ThreadPool::queueTask(Task& task) |
| 219 | { |
| 220 | SC_TRY_MSG(numWorkerThreads > 0, "Cannot queue tasks on an uninitialized threadpool"); |
| 221 | |
| 222 | // Function is entirely protected by the mutex |
| 223 | poolMutex.lock(); |
| 224 | auto deferUnlock = MakeDeferred([this] { poolMutex.unlock(); }); |
| 225 | |
| 226 | SC_TRY_MSG(task.threadPool != this, "Trying to queue a task that has already been queued"); |
| 227 | SC_TRY_MSG(task.threadPool == nullptr, "Trying to queue a task that is already in use by another threadpool"); |
| 228 | |
| 229 | task.threadPool = this; |
| 230 | if (taskHead == nullptr) |
| 231 | { |
| 232 | // FIFO was empty, replace head and tail with the task |
| 233 | taskHead = &task; |
| 234 | taskTail = taskHead; |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | // FIFO was not empty, append task to the tail |
| 239 | taskTail->next = &task; |
| 240 | taskTail = &task; |
| 241 | } |
| 242 | taskAvailable.broadcast(); |
| 243 | return Result(true); |
| 244 | } |