| 29 | |
| 30 | template <bool cooperative> |
| 31 | void JobBase<cooperative>::DoWait() { |
| 32 | if (wait_started_) |
| 33 | throw std::logic_error("This job has already been waited for."); |
| 34 | |
| 35 | if (total_tasks_ == 0) { |
| 36 | // If there are no tasks, it's legal to skip a call to Run, therefore executor_ can be null. |
| 37 | wait_started_ = true; |
| 38 | wait_completed_ = true; |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | if (this->executor_ == nullptr) |
| 43 | throw std::logic_error("This job hasn't been run - cannot wait for it."); |
| 44 | |
| 45 | if (ThreadPoolBase::this_thread_pool() == this->executor_) { |
| 46 | if constexpr (cooperative) { |
| 47 | auto ready = [&]() { return num_pending_tasks_ == 0; }; |
| 48 | wait_started_ = true; |
| 49 | bool result = ThreadPoolBase::this_thread_pool()->WaitOrRunTasks(this->cv_, ready); |
| 50 | wait_completed_ = true; |
| 51 | if (!result) |
| 52 | throw std::logic_error("The thread pool was stopped"); |
| 53 | } else { |
| 54 | throw std::logic_error("Cannot wait for this job from inside the thread pool."); |
| 55 | } |
| 56 | } else { |
| 57 | wait_started_ = true; |
| 58 | int old = num_pending_tasks_.load(); |
| 59 | while (old != 0) { |
| 60 | num_pending_tasks_.wait(old); |
| 61 | old = num_pending_tasks_.load(); |
| 62 | assert(old >= 0); |
| 63 | } |
| 64 | wait_completed_ = true; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | template <bool cooperative> |
| 69 | void JobBase<cooperative>::DoNotify() { |
no test coverage detected