Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file |
| 4 | |
Jordan Bayles | a26582d | 2019-07-10 14:44:58 -0700 | [diff] [blame] | 5 | #include "platform/impl/task_runner.h" |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 6 | |
Ryan Keane | a973b51 | 2019-07-29 15:50:39 -0700 | [diff] [blame] | 7 | #include <thread> |
| 8 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 9 | #include "platform/api/logging.h" |
| 10 | |
| 11 | namespace openscreen { |
| 12 | namespace platform { |
btolsch | c92ba2f | 2019-04-10 11:46:01 -0700 | [diff] [blame] | 13 | |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 14 | TaskRunnerImpl::TaskRunnerImpl(platform::ClockNowFunctionPtr now_function, |
| 15 | TaskWaiter* event_waiter, |
| 16 | Clock::duration waiter_timeout) |
| 17 | : now_function_(now_function), |
| 18 | is_running_(false), |
| 19 | task_waiter_(event_waiter), |
| 20 | waiter_timeout_(waiter_timeout) {} |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 21 | |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 22 | TaskRunnerImpl::~TaskRunnerImpl() = default; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 23 | |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 24 | void TaskRunnerImpl::PostPackagedTask(Task task) { |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 25 | std::lock_guard<std::mutex> lock(task_mutex_); |
Ryan Keane | efab2ed | 2019-07-22 12:36:53 -0700 | [diff] [blame] | 26 | tasks_.emplace_back(std::move(task)); |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 27 | if (task_waiter_) { |
| 28 | task_waiter_->OnTaskPosted(); |
| 29 | } else { |
| 30 | run_loop_wakeup_.notify_one(); |
| 31 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 34 | void TaskRunnerImpl::PostPackagedTaskWithDelay(Task task, |
| 35 | Clock::duration delay) { |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 36 | std::lock_guard<std::mutex> lock(task_mutex_); |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 37 | delayed_tasks_.emplace( |
| 38 | std::make_pair(now_function_() + delay, std::move(task))); |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 39 | if (task_waiter_) { |
| 40 | task_waiter_->OnTaskPosted(); |
| 41 | } else { |
| 42 | run_loop_wakeup_.notify_one(); |
| 43 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Max Yakimakha | bf567dc | 2019-09-20 13:37:04 -0700 | [diff] [blame] | 46 | bool TaskRunnerImpl::IsRunningOnTaskRunner() { |
| 47 | return task_runner_thread_id_ == std::this_thread::get_id(); |
| 48 | } |
| 49 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 50 | void TaskRunnerImpl::RunUntilStopped() { |
Max Yakimakha | bf567dc | 2019-09-20 13:37:04 -0700 | [diff] [blame] | 51 | task_runner_thread_id_ = std::this_thread::get_id(); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 52 | const bool was_running = is_running_.exchange(true); |
| 53 | OSP_CHECK(!was_running); |
| 54 | |
| 55 | RunTasksUntilStopped(); |
| 56 | } |
| 57 | |
| 58 | void TaskRunnerImpl::RequestStopSoon() { |
| 59 | const bool was_running = is_running_.exchange(false); |
| 60 | |
| 61 | if (was_running) { |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame] | 62 | OSP_DVLOG << "Requesting stop..."; |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 63 | if (task_waiter_) { |
| 64 | task_waiter_->OnTaskPosted(); |
| 65 | } else { |
| 66 | std::lock_guard<std::mutex> lock(task_mutex_); |
| 67 | run_loop_wakeup_.notify_one(); |
| 68 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | void TaskRunnerImpl::RunUntilIdleForTesting() { |
| 73 | ScheduleDelayedTasks(); |
| 74 | RunCurrentTasksForTesting(); |
| 75 | } |
| 76 | |
| 77 | void TaskRunnerImpl::RunCurrentTasksForTesting() { |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 78 | { |
| 79 | // Unlike in the RunCurrentTasksBlocking method, here we just immediately |
| 80 | // take the lock and drain the tasks_ queue. This allows tests to avoid |
| 81 | // having to do any multithreading to interact with the queue. |
| 82 | std::unique_lock<std::mutex> lock(task_mutex_); |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 83 | OSP_DCHECK(running_tasks_.empty()); |
| 84 | running_tasks_.swap(tasks_); |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 85 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 86 | |
Ryan Keane | efab2ed | 2019-07-22 12:36:53 -0700 | [diff] [blame] | 87 | for (TaskWithMetadata& task : running_tasks_) { |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 88 | // Move the task to the stack so that its bound state is freed immediately |
| 89 | // after being run. |
| 90 | std::move(task)(); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 91 | } |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 92 | running_tasks_.clear(); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void TaskRunnerImpl::RunCurrentTasksBlocking() { |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 96 | { |
| 97 | // Wait for the lock. If there are no current tasks, we will wait until |
| 98 | // a delayed task is ready or a task gets added to the queue. |
| 99 | auto lock = WaitForWorkAndAcquireLock(); |
| 100 | if (!lock) { |
| 101 | return; |
| 102 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 103 | |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 104 | OSP_DCHECK(running_tasks_.empty()); |
| 105 | running_tasks_.swap(tasks_); |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 106 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 107 | |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 108 | OSP_DVLOG << "Running " << running_tasks_.size() << " tasks..."; |
Max Yakimakha | 371bc2b | 2019-09-04 10:49:17 -0700 | [diff] [blame] | 109 | for (TaskWithMetadata& running_task : running_tasks_) { |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 110 | // Move the task to the stack so that its bound state is freed immediately |
| 111 | // after being run. |
Max Yakimakha | 371bc2b | 2019-09-04 10:49:17 -0700 | [diff] [blame] | 112 | TaskWithMetadata task = std::move(running_task); |
| 113 | task(); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 114 | } |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 115 | running_tasks_.clear(); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void TaskRunnerImpl::RunTasksUntilStopped() { |
| 119 | while (is_running_) { |
| 120 | ScheduleDelayedTasks(); |
| 121 | RunCurrentTasksBlocking(); |
| 122 | } |
Max Yakimakha | bf567dc | 2019-09-20 13:37:04 -0700 | [diff] [blame] | 123 | task_runner_thread_id_ = std::thread::id(); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void TaskRunnerImpl::ScheduleDelayedTasks() { |
| 127 | std::lock_guard<std::mutex> lock(task_mutex_); |
| 128 | |
| 129 | // Getting the time can be expensive on some platforms, so only get it once. |
| 130 | const auto current_time = now_function_(); |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 131 | const auto end_of_range = delayed_tasks_.upper_bound(current_time); |
| 132 | for (auto it = delayed_tasks_.begin(); it != end_of_range; ++it) { |
| 133 | tasks_.push_back(std::move(it->second)); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 134 | } |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 135 | delayed_tasks_.erase(delayed_tasks_.begin(), end_of_range); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame] | 138 | bool TaskRunnerImpl::ShouldWakeUpRunLoop() { |
| 139 | if (!is_running_) { |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | if (!tasks_.empty()) { |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | return !delayed_tasks_.empty() && |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 148 | (delayed_tasks_.begin()->first <= now_function_()); |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 151 | std::unique_lock<std::mutex> TaskRunnerImpl::WaitForWorkAndAcquireLock() { |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 152 | // These checks are redundant, as they are a subset of predicates in the |
| 153 | // below wait predicate. However, this is more readable and a slight |
| 154 | // optimization, as we don't need to take a lock if we aren't running. |
| 155 | if (!is_running_) { |
btolsch | 4051e72 | 2019-06-07 16:15:17 -0700 | [diff] [blame] | 156 | OSP_DVLOG << "TaskRunnerImpl not running. Returning empty lock."; |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 157 | return {}; |
| 158 | } |
| 159 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 160 | std::unique_lock<std::mutex> lock(task_mutex_); |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 161 | if (!tasks_.empty()) { |
btolsch | 4051e72 | 2019-06-07 16:15:17 -0700 | [diff] [blame] | 162 | OSP_DVLOG << "TaskRunnerImpl lock acquired."; |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 163 | return lock; |
| 164 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 165 | |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 166 | if (task_waiter_) { |
| 167 | do { |
| 168 | Clock::duration timeout = waiter_timeout_; |
| 169 | if (!delayed_tasks_.empty()) { |
| 170 | Clock::duration next_task_delta = |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 171 | delayed_tasks_.begin()->first - now_function_(); |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 172 | if (next_task_delta < timeout) { |
| 173 | timeout = next_task_delta; |
| 174 | } |
| 175 | } |
| 176 | lock.unlock(); |
| 177 | task_waiter_->WaitForTaskToBePosted(timeout); |
| 178 | lock.lock(); |
| 179 | } while (!ShouldWakeUpRunLoop()); |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame] | 180 | } else { |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 181 | // Pass a wait predicate to avoid lost or spurious wakeups. |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 182 | if (!delayed_tasks_.empty()) { |
| 183 | // We don't have any work to do currently, but have some in the |
| 184 | // pipe. |
btolsch | 61045d1 | 2019-05-09 16:33:58 -0700 | [diff] [blame] | 185 | const auto wait_predicate = [this] { return ShouldWakeUpRunLoop(); }; |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 186 | OSP_DVLOG << "TaskRunner waiting for lock until delayed task ready..."; |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 187 | run_loop_wakeup_.wait_for(lock, |
| 188 | delayed_tasks_.begin()->first - now_function_(), |
| 189 | wait_predicate); |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 190 | } else { |
| 191 | // We don't have any work queued. |
btolsch | 61045d1 | 2019-05-09 16:33:58 -0700 | [diff] [blame] | 192 | const auto wait_predicate = [this] { |
| 193 | return !delayed_tasks_.empty() || ShouldWakeUpRunLoop(); |
| 194 | }; |
btolsch | 4051e72 | 2019-06-07 16:15:17 -0700 | [diff] [blame] | 195 | OSP_DVLOG << "TaskRunnerImpl waiting for lock..."; |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 196 | run_loop_wakeup_.wait(lock, wait_predicate); |
| 197 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 198 | } |
| 199 | |
btolsch | 4051e72 | 2019-06-07 16:15:17 -0700 | [diff] [blame] | 200 | OSP_DVLOG << "TaskRunnerImpl lock acquired."; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 201 | return lock; |
| 202 | } |
| 203 | } // namespace platform |
| 204 | } // namespace openscreen |