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 | |
Yuri Wiitala | ef8fc08 | 2019-09-25 17:21:23 -0700 | [diff] [blame^] | 22 | TaskRunnerImpl::~TaskRunnerImpl() { |
| 23 | // Ensure no thread is currently executing inside RunUntilStopped(). |
| 24 | OSP_DCHECK_EQ(task_runner_thread_id_, std::thread::id()); |
| 25 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 26 | |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 27 | void TaskRunnerImpl::PostPackagedTask(Task task) { |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 28 | std::lock_guard<std::mutex> lock(task_mutex_); |
Ryan Keane | efab2ed | 2019-07-22 12:36:53 -0700 | [diff] [blame] | 29 | tasks_.emplace_back(std::move(task)); |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 30 | if (task_waiter_) { |
| 31 | task_waiter_->OnTaskPosted(); |
| 32 | } else { |
| 33 | run_loop_wakeup_.notify_one(); |
| 34 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 37 | void TaskRunnerImpl::PostPackagedTaskWithDelay(Task task, |
| 38 | Clock::duration delay) { |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 39 | std::lock_guard<std::mutex> lock(task_mutex_); |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 40 | delayed_tasks_.emplace( |
| 41 | std::make_pair(now_function_() + delay, std::move(task))); |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 42 | if (task_waiter_) { |
| 43 | task_waiter_->OnTaskPosted(); |
| 44 | } else { |
| 45 | run_loop_wakeup_.notify_one(); |
| 46 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Max Yakimakha | bf567dc | 2019-09-20 13:37:04 -0700 | [diff] [blame] | 49 | bool TaskRunnerImpl::IsRunningOnTaskRunner() { |
| 50 | return task_runner_thread_id_ == std::this_thread::get_id(); |
| 51 | } |
| 52 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 53 | void TaskRunnerImpl::RunUntilStopped() { |
Yuri Wiitala | ef8fc08 | 2019-09-25 17:21:23 -0700 | [diff] [blame^] | 54 | OSP_DCHECK(!is_running_); |
Max Yakimakha | bf567dc | 2019-09-20 13:37:04 -0700 | [diff] [blame] | 55 | task_runner_thread_id_ = std::this_thread::get_id(); |
Yuri Wiitala | c05ada2 | 2019-09-24 14:25:19 -0700 | [diff] [blame] | 56 | is_running_ = true; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 57 | |
Yuri Wiitala | c05ada2 | 2019-09-24 14:25:19 -0700 | [diff] [blame] | 58 | // Main loop: Run until the |is_running_| flag is set back to false by the |
| 59 | // "quit task" posted by RequestStopSoon(). |
| 60 | while (is_running_) { |
| 61 | ScheduleDelayedTasks(); |
Yuri Wiitala | 71a84bc | 2019-09-25 12:00:23 -0700 | [diff] [blame] | 62 | if (GrabMoreRunnableTasks()) { |
| 63 | RunRunnableTasks(); |
| 64 | } |
Yuri Wiitala | c05ada2 | 2019-09-24 14:25:19 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Flushing phase: Ensure all immediately-runnable tasks are run before |
| 68 | // returning. Since running some tasks might cause more immediately-runnable |
| 69 | // tasks to be posted, loop until there is no more work. |
Yuri Wiitala | 71a84bc | 2019-09-25 12:00:23 -0700 | [diff] [blame] | 70 | // |
| 71 | // If there is bad code that posts tasks indefinitely, this loop will never |
| 72 | // break. However, that also means there is a code path spinning a CPU core at |
| 73 | // 100% all the time. Rather than mitigate this problem scenario, purposely |
| 74 | // let it manifest here in the hopes that unit testing will reveal it (e.g., a |
| 75 | // unit test that never finishes running). |
| 76 | while (GrabMoreRunnableTasks()) { |
| 77 | RunRunnableTasks(); |
Yuri Wiitala | c05ada2 | 2019-09-24 14:25:19 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | task_runner_thread_id_ = std::thread::id(); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void TaskRunnerImpl::RequestStopSoon() { |
Yuri Wiitala | c05ada2 | 2019-09-24 14:25:19 -0700 | [diff] [blame] | 84 | PostTask([this]() { is_running_ = false; }); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Yuri Wiitala | c05ada2 | 2019-09-24 14:25:19 -0700 | [diff] [blame] | 87 | void TaskRunnerImpl::RunRunnableTasks() { |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 88 | OSP_DVLOG << "Running " << running_tasks_.size() << " tasks..."; |
Max Yakimakha | 371bc2b | 2019-09-04 10:49:17 -0700 | [diff] [blame] | 89 | for (TaskWithMetadata& running_task : running_tasks_) { |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 90 | // Move the task to the stack so that its bound state is freed immediately |
| 91 | // after being run. |
Max Yakimakha | 371bc2b | 2019-09-04 10:49:17 -0700 | [diff] [blame] | 92 | TaskWithMetadata task = std::move(running_task); |
| 93 | task(); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 94 | } |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 95 | running_tasks_.clear(); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 98 | void TaskRunnerImpl::ScheduleDelayedTasks() { |
| 99 | std::lock_guard<std::mutex> lock(task_mutex_); |
| 100 | |
| 101 | // Getting the time can be expensive on some platforms, so only get it once. |
| 102 | const auto current_time = now_function_(); |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 103 | const auto end_of_range = delayed_tasks_.upper_bound(current_time); |
| 104 | for (auto it = delayed_tasks_.begin(); it != end_of_range; ++it) { |
| 105 | tasks_.push_back(std::move(it->second)); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 106 | } |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 107 | delayed_tasks_.erase(delayed_tasks_.begin(), end_of_range); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Yuri Wiitala | 71a84bc | 2019-09-25 12:00:23 -0700 | [diff] [blame] | 110 | bool TaskRunnerImpl::GrabMoreRunnableTasks() { |
| 111 | OSP_DCHECK(running_tasks_.empty()); |
| 112 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 113 | std::unique_lock<std::mutex> lock(task_mutex_); |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 114 | if (!tasks_.empty()) { |
Yuri Wiitala | 71a84bc | 2019-09-25 12:00:23 -0700 | [diff] [blame] | 115 | running_tasks_.swap(tasks_); |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | if (!is_running_) { |
| 120 | return false; // Stop was requested. Don't wait for more tasks. |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 121 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 122 | |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 123 | if (task_waiter_) { |
Yuri Wiitala | ef8fc08 | 2019-09-25 17:21:23 -0700 | [diff] [blame^] | 124 | Clock::duration timeout = waiter_timeout_; |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 125 | if (!delayed_tasks_.empty()) { |
Yuri Wiitala | ef8fc08 | 2019-09-25 17:21:23 -0700 | [diff] [blame^] | 126 | Clock::duration next_task_delta = |
| 127 | delayed_tasks_.begin()->first - now_function_(); |
| 128 | if (next_task_delta < timeout) { |
| 129 | timeout = next_task_delta; |
| 130 | } |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 131 | } |
Yuri Wiitala | ef8fc08 | 2019-09-25 17:21:23 -0700 | [diff] [blame^] | 132 | lock.unlock(); |
| 133 | task_waiter_->WaitForTaskToBePosted(timeout); |
| 134 | return false; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Yuri Wiitala | ef8fc08 | 2019-09-25 17:21:23 -0700 | [diff] [blame^] | 137 | if (delayed_tasks_.empty()) { |
| 138 | run_loop_wakeup_.wait(lock); |
| 139 | } else { |
| 140 | run_loop_wakeup_.wait_for(lock, |
| 141 | delayed_tasks_.begin()->first - now_function_()); |
| 142 | } |
Yuri Wiitala | 71a84bc | 2019-09-25 12:00:23 -0700 | [diff] [blame] | 143 | return false; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 144 | } |
Yuri Wiitala | 71a84bc | 2019-09-25 12:00:23 -0700 | [diff] [blame] | 145 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 146 | } // namespace platform |
| 147 | } // namespace openscreen |