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 | |
| 5 | #include "api/impl/task_runner_impl.h" |
| 6 | |
| 7 | #include "platform/api/logging.h" |
| 8 | |
| 9 | namespace openscreen { |
| 10 | namespace platform { |
| 11 | TaskRunnerImpl::TaskRunnerImpl(platform::ClockNowFunctionPtr now_function) |
| 12 | : now_function_(now_function), is_running_(false) {} |
| 13 | |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 14 | TaskRunnerImpl::~TaskRunnerImpl() = default; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 15 | |
| 16 | void TaskRunnerImpl::PostTask(Task task) { |
| 17 | std::lock_guard<std::mutex> lock(task_mutex_); |
| 18 | tasks_.push_back(std::move(task)); |
| 19 | run_loop_wakeup_.notify_one(); |
| 20 | } |
| 21 | |
| 22 | void TaskRunnerImpl::PostTaskWithDelay(Task task, Clock::duration delay) { |
| 23 | std::lock_guard<std::mutex> lock(task_mutex_); |
| 24 | delayed_tasks_.emplace(std::move(task), now_function_() + delay); |
| 25 | run_loop_wakeup_.notify_one(); |
| 26 | } |
| 27 | |
| 28 | void TaskRunnerImpl::RunUntilStopped() { |
| 29 | const bool was_running = is_running_.exchange(true); |
| 30 | OSP_CHECK(!was_running); |
| 31 | |
| 32 | RunTasksUntilStopped(); |
| 33 | } |
| 34 | |
| 35 | void TaskRunnerImpl::RequestStopSoon() { |
| 36 | const bool was_running = is_running_.exchange(false); |
| 37 | |
| 38 | if (was_running) { |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame^] | 39 | OSP_DVLOG << "Requesting stop..."; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 40 | std::lock_guard<std::mutex> lock(task_mutex_); |
| 41 | run_loop_wakeup_.notify_one(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void TaskRunnerImpl::RunUntilIdleForTesting() { |
| 46 | ScheduleDelayedTasks(); |
| 47 | RunCurrentTasksForTesting(); |
| 48 | } |
| 49 | |
| 50 | void TaskRunnerImpl::RunCurrentTasksForTesting() { |
| 51 | std::deque<Task> current_tasks; |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 52 | { |
| 53 | // Unlike in the RunCurrentTasksBlocking method, here we just immediately |
| 54 | // take the lock and drain the tasks_ queue. This allows tests to avoid |
| 55 | // having to do any multithreading to interact with the queue. |
| 56 | std::unique_lock<std::mutex> lock(task_mutex_); |
| 57 | tasks_.swap(current_tasks); |
| 58 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 59 | |
| 60 | for (Task& task : current_tasks) { |
| 61 | task(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void TaskRunnerImpl::RunCurrentTasksBlocking() { |
| 66 | std::deque<Task> current_tasks; |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 67 | { |
| 68 | // Wait for the lock. If there are no current tasks, we will wait until |
| 69 | // a delayed task is ready or a task gets added to the queue. |
| 70 | auto lock = WaitForWorkAndAcquireLock(); |
| 71 | if (!lock) { |
| 72 | return; |
| 73 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 74 | |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 75 | tasks_.swap(current_tasks); |
| 76 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 77 | |
| 78 | for (Task& task : current_tasks) { |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame^] | 79 | OSP_DVLOG << "Running " << current_tasks.size() << " current tasks..."; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 80 | task(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void TaskRunnerImpl::RunTasksUntilStopped() { |
| 85 | while (is_running_) { |
| 86 | ScheduleDelayedTasks(); |
| 87 | RunCurrentTasksBlocking(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void TaskRunnerImpl::ScheduleDelayedTasks() { |
| 92 | std::lock_guard<std::mutex> lock(task_mutex_); |
| 93 | |
| 94 | // Getting the time can be expensive on some platforms, so only get it once. |
| 95 | const auto current_time = now_function_(); |
| 96 | while (!delayed_tasks_.empty() && |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame^] | 97 | (delayed_tasks_.top().runnable_after <= current_time)) { |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 98 | tasks_.push_back(std::move(delayed_tasks_.top().task)); |
| 99 | delayed_tasks_.pop(); |
| 100 | } |
| 101 | } |
| 102 | |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame^] | 103 | bool TaskRunnerImpl::ShouldWakeUpRunLoop() { |
| 104 | if (!is_running_) { |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | if (!tasks_.empty()) { |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | return !delayed_tasks_.empty() && |
| 113 | (delayed_tasks_.top().runnable_after <= now_function_()); |
| 114 | } |
| 115 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 116 | std::unique_lock<std::mutex> TaskRunnerImpl::WaitForWorkAndAcquireLock() { |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 117 | // These checks are redundant, as they are a subset of predicates in the |
| 118 | // below wait predicate. However, this is more readable and a slight |
| 119 | // optimization, as we don't need to take a lock if we aren't running. |
| 120 | if (!is_running_) { |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame^] | 121 | OSP_DVLOG << "TaskRunner not running. Returning empty lock."; |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 122 | return {}; |
| 123 | } |
| 124 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 125 | std::unique_lock<std::mutex> lock(task_mutex_); |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 126 | if (!tasks_.empty()) { |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame^] | 127 | OSP_DVLOG << "TaskRunner lock acquired."; |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 128 | return lock; |
| 129 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 130 | |
| 131 | // Pass a wait predicate to avoid lost or spurious wakeups. |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame^] | 132 | const auto wait_predicate = [this] { return ShouldWakeUpRunLoop(); }; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 133 | if (!delayed_tasks_.empty()) { |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame^] | 134 | // We don't have any work to do currently, but have some in the |
| 135 | // pipe. |
| 136 | OSP_DVLOG << "TaskRunner waiting for lock until delayed task ready..."; |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 137 | run_loop_wakeup_.wait_until(lock, delayed_tasks_.top().runnable_after, |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 138 | wait_predicate); |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame^] | 139 | } else { |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 140 | // We don't have any work queued. |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame^] | 141 | OSP_DVLOG << "TaskRunner waiting for lock..."; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 142 | run_loop_wakeup_.wait(lock, wait_predicate); |
| 143 | } |
| 144 | |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame^] | 145 | OSP_DVLOG << "TaskRunner lock acquired."; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 146 | return lock; |
| 147 | } |
| 148 | } // namespace platform |
| 149 | } // namespace openscreen |