| 286 | // ---------------------------------------------------------------------------------------- |
| 287 | |
| 288 | uint64 thread_pool_implementation:: |
| 289 | add_task_internal ( |
| 290 | const bfp_type& bfp, |
| 291 | std::shared_ptr<function_object_copy>& item |
| 292 | ) |
| 293 | { |
| 294 | auto_mutex M(m); |
| 295 | const thread_id_type my_thread_id = get_thread_id(); |
| 296 | |
| 297 | // find a thread that isn't doing anything |
| 298 | long idx = find_empty_task_slot(); |
| 299 | if (idx == -1 && is_worker_thread(my_thread_id)) |
| 300 | { |
| 301 | // this function is being called from within a worker thread and there |
| 302 | // aren't any other worker threads free so just perform the task right |
| 303 | // here |
| 304 | |
| 305 | M.unlock(); |
| 306 | bfp(); |
| 307 | |
| 308 | // return a task id that is both non-zero and also one |
| 309 | // that is never normally returned. This way calls |
| 310 | // to wait_for_task() will never block given this id. |
| 311 | return 1; |
| 312 | } |
| 313 | |
| 314 | // wait until there is a thread that isn't doing anything |
| 315 | while (idx == -1) |
| 316 | { |
| 317 | task_done_signaler.wait(); |
| 318 | idx = find_empty_task_slot(); |
| 319 | } |
| 320 | |
| 321 | tasks[idx].thread_id = my_thread_id; |
| 322 | tasks[idx].task_id = make_next_task_id(idx); |
| 323 | tasks[idx].bfp = bfp; |
| 324 | tasks[idx].function_copy.swap(item); |
| 325 | |
| 326 | task_ready_signaler.signal(); |
| 327 | |
| 328 | return tasks[idx].task_id; |
| 329 | } |
| 330 | |
| 331 | // ---------------------------------------------------------------------------------------- |
| 332 |
nothing calls this directly
no test coverage detected