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 | |
Yuri Wiitala | 341085a | 2020-03-11 17:13:54 -0700 | [diff] [blame] | 7 | #include <csignal> |
Ryan Keane | a973b51 | 2019-07-29 15:50:39 -0700 | [diff] [blame] | 8 | #include <thread> |
| 9 | |
Jordan Bayles | a44d04a | 2020-05-06 16:59:39 -0700 | [diff] [blame] | 10 | #include "util/osp_logging.h" |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 11 | |
| 12 | namespace openscreen { |
btolsch | c92ba2f | 2019-04-10 11:46:01 -0700 | [diff] [blame] | 13 | |
Yuri Wiitala | 341085a | 2020-03-11 17:13:54 -0700 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | // This is mutated by the signal handler installed by RunUntilSignaled(), and is |
| 17 | // checked by RunUntilStopped(). |
| 18 | // |
| 19 | // Per the C++14 spec, passing visible changes to memory between a signal |
| 20 | // handler and a program thread must be done through a volatile variable. |
| 21 | volatile enum { |
| 22 | kNotRunning, |
| 23 | kNotSignaled, |
| 24 | kSignaled |
| 25 | } g_signal_state = kNotRunning; |
| 26 | |
| 27 | void OnReceivedSignal(int signal) { |
| 28 | g_signal_state = kSignaled; |
| 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
Yuri Wiitala | 2b02e32 | 2019-12-03 16:59:40 -0800 | [diff] [blame] | 33 | TaskRunnerImpl::TaskRunnerImpl(ClockNowFunctionPtr now_function, |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 34 | TaskWaiter* event_waiter, |
| 35 | Clock::duration waiter_timeout) |
| 36 | : now_function_(now_function), |
| 37 | is_running_(false), |
| 38 | task_waiter_(event_waiter), |
| 39 | waiter_timeout_(waiter_timeout) {} |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 40 | |
Yuri Wiitala | ef8fc08 | 2019-09-25 17:21:23 -0700 | [diff] [blame] | 41 | TaskRunnerImpl::~TaskRunnerImpl() { |
| 42 | // Ensure no thread is currently executing inside RunUntilStopped(). |
| 43 | OSP_DCHECK_EQ(task_runner_thread_id_, std::thread::id()); |
| 44 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 45 | |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 46 | void TaskRunnerImpl::PostPackagedTask(Task task) { |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 47 | std::lock_guard<std::mutex> lock(task_mutex_); |
Ryan Keane | efab2ed | 2019-07-22 12:36:53 -0700 | [diff] [blame] | 48 | tasks_.emplace_back(std::move(task)); |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 49 | if (task_waiter_) { |
| 50 | task_waiter_->OnTaskPosted(); |
| 51 | } else { |
| 52 | run_loop_wakeup_.notify_one(); |
| 53 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 56 | void TaskRunnerImpl::PostPackagedTaskWithDelay(Task task, |
| 57 | Clock::duration delay) { |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 58 | std::lock_guard<std::mutex> lock(task_mutex_); |
Yuri Wiitala | c681b47 | 2020-02-20 13:41:54 -0800 | [diff] [blame] | 59 | if (delay <= Clock::duration::zero()) { |
| 60 | tasks_.emplace_back(std::move(task)); |
| 61 | } else { |
| 62 | delayed_tasks_.emplace( |
| 63 | std::make_pair(now_function_() + delay, std::move(task))); |
| 64 | } |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 65 | if (task_waiter_) { |
| 66 | task_waiter_->OnTaskPosted(); |
| 67 | } else { |
| 68 | run_loop_wakeup_.notify_one(); |
| 69 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Max Yakimakha | bf567dc | 2019-09-20 13:37:04 -0700 | [diff] [blame] | 72 | bool TaskRunnerImpl::IsRunningOnTaskRunner() { |
| 73 | return task_runner_thread_id_ == std::this_thread::get_id(); |
| 74 | } |
| 75 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 76 | void TaskRunnerImpl::RunUntilStopped() { |
Yuri Wiitala | ef8fc08 | 2019-09-25 17:21:23 -0700 | [diff] [blame] | 77 | OSP_DCHECK(!is_running_); |
Max Yakimakha | bf567dc | 2019-09-20 13:37:04 -0700 | [diff] [blame] | 78 | task_runner_thread_id_ = std::this_thread::get_id(); |
Yuri Wiitala | c05ada2 | 2019-09-24 14:25:19 -0700 | [diff] [blame] | 79 | is_running_ = true; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 80 | |
Yuri Wiitala | c05ada2 | 2019-09-24 14:25:19 -0700 | [diff] [blame] | 81 | // Main loop: Run until the |is_running_| flag is set back to false by the |
Yuri Wiitala | 341085a | 2020-03-11 17:13:54 -0700 | [diff] [blame] | 82 | // "quit task" posted by RequestStopSoon(), or the process received a |
| 83 | // termination signal. |
Yuri Wiitala | c05ada2 | 2019-09-24 14:25:19 -0700 | [diff] [blame] | 84 | while (is_running_) { |
| 85 | ScheduleDelayedTasks(); |
Yuri Wiitala | 71a84bc | 2019-09-25 12:00:23 -0700 | [diff] [blame] | 86 | if (GrabMoreRunnableTasks()) { |
| 87 | RunRunnableTasks(); |
| 88 | } |
Yuri Wiitala | 341085a | 2020-03-11 17:13:54 -0700 | [diff] [blame] | 89 | if (g_signal_state == kSignaled) { |
| 90 | is_running_ = false; |
| 91 | } |
Yuri Wiitala | c05ada2 | 2019-09-24 14:25:19 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // Flushing phase: Ensure all immediately-runnable tasks are run before |
| 95 | // returning. Since running some tasks might cause more immediately-runnable |
| 96 | // tasks to be posted, loop until there is no more work. |
Yuri Wiitala | 71a84bc | 2019-09-25 12:00:23 -0700 | [diff] [blame] | 97 | // |
| 98 | // If there is bad code that posts tasks indefinitely, this loop will never |
| 99 | // break. However, that also means there is a code path spinning a CPU core at |
| 100 | // 100% all the time. Rather than mitigate this problem scenario, purposely |
| 101 | // let it manifest here in the hopes that unit testing will reveal it (e.g., a |
| 102 | // unit test that never finishes running). |
| 103 | while (GrabMoreRunnableTasks()) { |
| 104 | RunRunnableTasks(); |
Yuri Wiitala | c05ada2 | 2019-09-24 14:25:19 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | task_runner_thread_id_ = std::thread::id(); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Yuri Wiitala | 341085a | 2020-03-11 17:13:54 -0700 | [diff] [blame] | 110 | void TaskRunnerImpl::RunUntilSignaled() { |
| 111 | OSP_CHECK_EQ(g_signal_state, kNotRunning) |
| 112 | << __func__ << " may not be invoked concurrently."; |
| 113 | g_signal_state = kNotSignaled; |
| 114 | const auto old_sigint_handler = std::signal(SIGINT, &OnReceivedSignal); |
| 115 | const auto old_sigterm_handler = std::signal(SIGTERM, &OnReceivedSignal); |
| 116 | |
| 117 | RunUntilStopped(); |
| 118 | |
| 119 | std::signal(SIGINT, old_sigint_handler); |
| 120 | std::signal(SIGTERM, old_sigterm_handler); |
| 121 | g_signal_state = kNotRunning; |
| 122 | } |
| 123 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 124 | void TaskRunnerImpl::RequestStopSoon() { |
Yuri Wiitala | c05ada2 | 2019-09-24 14:25:19 -0700 | [diff] [blame] | 125 | PostTask([this]() { is_running_ = false; }); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Yuri Wiitala | c05ada2 | 2019-09-24 14:25:19 -0700 | [diff] [blame] | 128 | void TaskRunnerImpl::RunRunnableTasks() { |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 129 | OSP_DVLOG << "Running " << running_tasks_.size() << " tasks..."; |
Max Yakimakha | 371bc2b | 2019-09-04 10:49:17 -0700 | [diff] [blame] | 130 | for (TaskWithMetadata& running_task : running_tasks_) { |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 131 | // Move the task to the stack so that its bound state is freed immediately |
| 132 | // after being run. |
Max Yakimakha | 371bc2b | 2019-09-04 10:49:17 -0700 | [diff] [blame] | 133 | TaskWithMetadata task = std::move(running_task); |
| 134 | task(); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 135 | } |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 136 | running_tasks_.clear(); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 139 | void TaskRunnerImpl::ScheduleDelayedTasks() { |
| 140 | std::lock_guard<std::mutex> lock(task_mutex_); |
| 141 | |
| 142 | // Getting the time can be expensive on some platforms, so only get it once. |
| 143 | const auto current_time = now_function_(); |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 144 | const auto end_of_range = delayed_tasks_.upper_bound(current_time); |
| 145 | for (auto it = delayed_tasks_.begin(); it != end_of_range; ++it) { |
| 146 | tasks_.push_back(std::move(it->second)); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 147 | } |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 148 | delayed_tasks_.erase(delayed_tasks_.begin(), end_of_range); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Yuri Wiitala | 71a84bc | 2019-09-25 12:00:23 -0700 | [diff] [blame] | 151 | bool TaskRunnerImpl::GrabMoreRunnableTasks() { |
| 152 | OSP_DCHECK(running_tasks_.empty()); |
| 153 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 154 | std::unique_lock<std::mutex> lock(task_mutex_); |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 155 | if (!tasks_.empty()) { |
Yuri Wiitala | 71a84bc | 2019-09-25 12:00:23 -0700 | [diff] [blame] | 156 | running_tasks_.swap(tasks_); |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | if (!is_running_) { |
| 161 | return false; // Stop was requested. Don't wait for more tasks. |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 162 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 163 | |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 164 | if (task_waiter_) { |
Yuri Wiitala | ef8fc08 | 2019-09-25 17:21:23 -0700 | [diff] [blame] | 165 | Clock::duration timeout = waiter_timeout_; |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 166 | if (!delayed_tasks_.empty()) { |
Yuri Wiitala | ef8fc08 | 2019-09-25 17:21:23 -0700 | [diff] [blame] | 167 | Clock::duration next_task_delta = |
| 168 | delayed_tasks_.begin()->first - now_function_(); |
| 169 | if (next_task_delta < timeout) { |
| 170 | timeout = next_task_delta; |
| 171 | } |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 172 | } |
Yuri Wiitala | ef8fc08 | 2019-09-25 17:21:23 -0700 | [diff] [blame] | 173 | lock.unlock(); |
| 174 | task_waiter_->WaitForTaskToBePosted(timeout); |
| 175 | return false; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Yuri Wiitala | ef8fc08 | 2019-09-25 17:21:23 -0700 | [diff] [blame] | 178 | if (delayed_tasks_.empty()) { |
| 179 | run_loop_wakeup_.wait(lock); |
| 180 | } else { |
| 181 | run_loop_wakeup_.wait_for(lock, |
| 182 | delayed_tasks_.begin()->first - now_function_()); |
| 183 | } |
Yuri Wiitala | 71a84bc | 2019-09-25 12:00:23 -0700 | [diff] [blame] | 184 | return false; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 185 | } |
Yuri Wiitala | 71a84bc | 2019-09-25 12:00:23 -0700 | [diff] [blame] | 186 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 187 | } // namespace openscreen |