| 126 | |
| 127 | _condition.notifyAll(); |
| 128 | } |
| 129 | |
| 130 | DispatchFunction TaskQueue::lockFreeRemoveTask(task_id_t taskId) { |
| 131 | for (auto i = _tasks.begin(); i != _tasks.end(); ++i) { |
| 132 | if (i->id == taskId) { |
| 133 | auto task = std::move(*i); |
| 134 | _tasks.erase(i); |
| 135 | return std::move(task.function); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return DispatchFunction(); |
| 140 | } |
| 141 | |
| 142 | void TaskQueue::barrier(const DispatchFunction& function) { |
| 143 | auto executeTime = std::chrono::steady_clock::now(); |
| 144 | |
| 145 | std::unique_lock<Mutex> lockGuard(_mutex); |
| 146 | auto id = insertTask(DispatchFunction(), executeTime, true, 0); |
| 147 | |
| 148 | while (!_tasks.empty()) { |
| 149 | // Wait until we have no currently running tasks, and that the task at the front is our barrier task |
| 150 | if (_currentRunningTasks != 0 || _tasks.front().id != id) { |
| 151 | _condition.wait(lockGuard); |
| 152 | continue; |
| 153 | } |
| 154 | |
| 155 | // We have no running tasks, and our barrier task is at the front. |
| 156 | // We can now execute our barrier |