Main task stealing loop which is used by worker threads when they have nothing to do.
| 172 | // Main task stealing loop which is used by worker threads when they have |
| 173 | // nothing to do. |
| 174 | static void thread_task_loop(threadpool_data* impl, std::size_t thread_id, task_wait_handle wait_task) |
| 175 | { |
| 176 | // Get our thread's data |
| 177 | thread_data_t& current_thread = impl->thread_data[thread_id]; |
| 178 | |
| 179 | // Flag indicating if we have added a continuation to the task |
| 180 | bool added_continuation = false; |
| 181 | |
| 182 | // Event to wait on |
| 183 | task_wait_event event; |
| 184 | |
| 185 | // Loop while waiting for the task to complete |
| 186 | while (true) { |
| 187 | // Check if the task has finished. If we have added a continuation, we |
| 188 | // need to make sure the event has been signaled, otherwise the other |
| 189 | // thread may try to signal it after we have freed it. |
| 190 | if (wait_task && (added_continuation ? event.try_wait(wait_type::task_finished) : wait_task.ready())) |
| 191 | return; |
| 192 | |
| 193 | // Try to get a task from the local queue |
| 194 | if (task_run_handle t = current_thread.queue.pop()) { |
| 195 | t.run(); |
| 196 | continue; |
| 197 | } |
| 198 | |
| 199 | // Stealing loop |
| 200 | while (true) { |
| 201 | // Try to steal a task |
| 202 | if (task_run_handle t = steal_task(impl, thread_id)) { |
| 203 | t.run(); |
| 204 | break; |
| 205 | } |
| 206 | |
| 207 | // Try to fetch from the public queue |
| 208 | std::unique_lock<std::mutex> locked(impl->lock); |
| 209 | if (task_run_handle t = impl->public_queue.pop()) { |
| 210 | // Don't hold the lock while running the task |
| 211 | locked.unlock(); |
| 212 | t.run(); |
| 213 | break; |
| 214 | } |
| 215 | |
| 216 | // If shutting down and we don't have a task to wait for, return. |
| 217 | if (!wait_task && impl->shutdown) { |
| 218 | #ifdef BROKEN_JOIN_IN_DESTRUCTOR |
| 219 | // Notify once all worker threads have exited |
| 220 | if (--impl->shutdown_num_threads == 0) |
| 221 | impl->shutdown_complete_event.notify_one(); |
| 222 | #endif |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | // Initialize the event object |
| 227 | event.init(); |
| 228 | |
| 229 | // No tasks found, so sleep until something happens. |
| 230 | // If a continuation has not been added yet, add it. |
| 231 | if (wait_task && !added_continuation) { |