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 | #ifndef PLATFORM_IMPL_TASK_RUNNER_H_ |
| 6 | #define PLATFORM_IMPL_TASK_RUNNER_H_ |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 7 | |
| 8 | #include <atomic> |
| 9 | #include <condition_variable> // NOLINT |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 10 | #include <map> |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 11 | #include <memory> |
Ryan Keane | a973b51 | 2019-07-29 15:50:39 -0700 | [diff] [blame] | 12 | #include <mutex> // NOLINT |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 13 | #include <utility> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "absl/base/thread_annotations.h" |
| 17 | #include "absl/types/optional.h" |
Ryan Keane | 230920e | 2019-08-29 13:21:40 -0700 | [diff] [blame] | 18 | #include "platform/api/task_runner.h" |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 19 | #include "platform/api/time.h" |
Ryan Keane | efab2ed | 2019-07-22 12:36:53 -0700 | [diff] [blame] | 20 | #include "platform/api/trace_logging.h" |
Jordan Bayles | a26582d | 2019-07-10 14:44:58 -0700 | [diff] [blame] | 21 | #include "platform/base/error.h" |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 22 | |
| 23 | namespace openscreen { |
| 24 | namespace platform { |
| 25 | |
btolsch | 4051e72 | 2019-06-07 16:15:17 -0700 | [diff] [blame] | 26 | class TaskRunnerImpl final : public TaskRunner { |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 27 | public: |
btolsch | 4051e72 | 2019-06-07 16:15:17 -0700 | [diff] [blame] | 28 | using Task = TaskRunner::Task; |
| 29 | |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 30 | class TaskWaiter { |
| 31 | public: |
| 32 | virtual ~TaskWaiter() = default; |
| 33 | |
| 34 | // These calls should be thread-safe. The absolute minimum is that |
| 35 | // OnTaskPosted must be safe to call from another thread while this is |
| 36 | // inside WaitForTaskToBePosted. NOTE: There may be spurious wakeups from |
| 37 | // WaitForTaskToBePosted depending on whether the specific implementation |
| 38 | // chooses to clear queued WakeUps before entering WaitForTaskToBePosted. |
| 39 | |
| 40 | // Blocks until some event occurs, which means new tasks may have been |
| 41 | // posted. Wait may only block up to |timeout| where 0 means don't block at |
| 42 | // all (not block forever). |
| 43 | virtual Error WaitForTaskToBePosted(Clock::duration timeout) = 0; |
| 44 | |
| 45 | // If a WaitForTaskToBePosted call is currently blocking, unblock it |
| 46 | // immediately. |
| 47 | virtual void OnTaskPosted() = 0; |
| 48 | }; |
| 49 | |
| 50 | explicit TaskRunnerImpl( |
| 51 | platform::ClockNowFunctionPtr now_function, |
| 52 | TaskWaiter* event_waiter = nullptr, |
| 53 | Clock::duration waiter_timeout = std::chrono::milliseconds(100)); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 54 | |
| 55 | // TaskRunner overrides |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 56 | ~TaskRunnerImpl() final; |
| 57 | void PostPackagedTask(Task task) final; |
| 58 | void PostPackagedTaskWithDelay(Task task, Clock::duration delay) final; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 59 | |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 60 | // Tasks will only be executed if RunUntilStopped has been called, and |
btolsch | 4051e72 | 2019-06-07 16:15:17 -0700 | [diff] [blame] | 61 | // RequestStopSoon has not. Important note: TaskRunner does NOT do any |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 62 | // threading, so calling "RunUntilStopped()" will block whatever thread you |
| 63 | // are calling it on. |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 64 | void RunUntilStopped(); |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 65 | |
btolsch | 4051e72 | 2019-06-07 16:15:17 -0700 | [diff] [blame] | 66 | // Thread-safe method for requesting the TaskRunner to stop running. This sets |
| 67 | // a flag that will get checked in the run loop, typically after completing |
| 68 | // the current task. |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 69 | void RequestStopSoon(); |
| 70 | |
| 71 | // Execute all tasks immediately, useful for testing only. Note: this method |
| 72 | // will schedule any delayed tasks that are ready to run, but does not block |
| 73 | // waiting for delayed tasks to become eligible. |
| 74 | void RunUntilIdleForTesting(); |
| 75 | |
| 76 | private: |
Ryan Keane | 1d9c0c4 | 2019-07-29 15:22:19 -0700 | [diff] [blame] | 77 | #ifndef TRACE_FORCE_DISABLE |
Ryan Keane | efab2ed | 2019-07-22 12:36:53 -0700 | [diff] [blame] | 78 | // Wrapper around a Task used to store the TraceId Metadata along with the |
| 79 | // task itself, and to set the current TraceIdHierarchy before executing the |
| 80 | // task. |
| 81 | class TaskWithMetadata { |
| 82 | public: |
Ryan Keane | 1d9c0c4 | 2019-07-29 15:22:19 -0700 | [diff] [blame] | 83 | // NOTE: 'explicit' keyword omitted so that conversion construtor can be |
| 84 | // used. This simplifies switching between 'Task' and 'TaskWithMetadata' |
| 85 | // based on the compilation flag. |
| 86 | TaskWithMetadata(Task task) |
| 87 | : task_(std::move(task)), trace_ids_(TRACE_HIERARCHY){}; |
Ryan Keane | efab2ed | 2019-07-22 12:36:53 -0700 | [diff] [blame] | 88 | |
Ryan Keane | 1d9c0c4 | 2019-07-29 15:22:19 -0700 | [diff] [blame] | 89 | void operator()() { |
| 90 | TRACE_SET_HIERARCHY(trace_ids_); |
| 91 | std::move(task_)(); |
| 92 | } |
Ryan Keane | efab2ed | 2019-07-22 12:36:53 -0700 | [diff] [blame] | 93 | |
| 94 | private: |
| 95 | Task task_; |
| 96 | TraceIdHierarchy trace_ids_; |
| 97 | }; |
Ryan Keane | 1d9c0c4 | 2019-07-29 15:22:19 -0700 | [diff] [blame] | 98 | #else // TRACE_FORCE_DISABLE defined |
| 99 | using TaskWithMetadata = Task; |
| 100 | #endif // TRACE_FORCE_DISABLE |
Ryan Keane | efab2ed | 2019-07-22 12:36:53 -0700 | [diff] [blame] | 101 | |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 102 | // Run all tasks already in the task queue. If the queue is empty, wait for |
| 103 | // either (1) a delayed task to become available, or (2) a task to be added |
| 104 | // to the queue. |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 105 | void RunCurrentTasksBlocking(); |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 106 | |
| 107 | // Run tasks already in the queue, for testing. If the queue is empty, this |
| 108 | // method does not block but instead returns immediately. |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 109 | void RunCurrentTasksForTesting(); |
| 110 | |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 111 | // Loop method that runs tasks in the current thread, until the |
| 112 | // RequestStopSoon method is called. |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 113 | void RunTasksUntilStopped(); |
| 114 | |
| 115 | // Look at all tasks in the delayed task queue, then schedule them if the |
| 116 | // minimum delay time has elapsed. |
| 117 | void ScheduleDelayedTasks(); |
| 118 | |
Jordan Bayles | 5d72bc2 | 2019-04-09 13:33:52 -0700 | [diff] [blame] | 119 | // Look at the current state of the TaskRunner and determine if the run loop |
| 120 | // should be woken up |
| 121 | bool ShouldWakeUpRunLoop(); |
| 122 | |
Jordan Bayles | a8e9677 | 2019-04-08 10:53:54 -0700 | [diff] [blame] | 123 | // Takes the task_mutex_ lock, returning immediately if work is available. If |
| 124 | // no work is available, this places the task running thread into a waiting |
| 125 | // state until we stop running or work is available. |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 126 | std::unique_lock<std::mutex> WaitForWorkAndAcquireLock(); |
| 127 | |
| 128 | const platform::ClockNowFunctionPtr now_function_; |
| 129 | |
| 130 | // Atomic so that we can perform atomic exchanges. |
| 131 | std::atomic_bool is_running_; |
| 132 | |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 133 | // This mutex is used for |tasks_| and |delayed_tasks_|, and also for |
| 134 | // notifying the run loop to wake up when it is waiting for a task to be added |
| 135 | // to the queue in |run_loop_wakeup_|. |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 136 | std::mutex task_mutex_; |
Ryan Keane | efab2ed | 2019-07-22 12:36:53 -0700 | [diff] [blame] | 137 | std::vector<TaskWithMetadata> tasks_ GUARDED_BY(task_mutex_); |
| 138 | std::multimap<Clock::time_point, TaskWithMetadata> delayed_tasks_ |
| 139 | GUARDED_BY(task_mutex_); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 140 | |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 141 | // When |task_waiter_| is nullptr, |run_loop_wakeup_| is used for sleeping the |
| 142 | // task runner. Otherwise, |run_loop_wakeup_| isn't used and |task_waiter_| |
| 143 | // is used instead (along with |waiter_timeout_|). |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 144 | std::condition_variable run_loop_wakeup_; |
btolsch | d94fe62 | 2019-05-09 14:21:40 -0700 | [diff] [blame] | 145 | TaskWaiter* const task_waiter_; |
| 146 | Clock::duration waiter_timeout_; |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 147 | |
| 148 | // To prevent excessive re-allocation of the underlying array of the |tasks_| |
| 149 | // vector, use an A/B vector-swap mechanism. |running_tasks_| starts out |
| 150 | // empty, and is swapped with |tasks_| when it is time to run the Tasks. |
Ryan Keane | efab2ed | 2019-07-22 12:36:53 -0700 | [diff] [blame] | 151 | std::vector<TaskWithMetadata> running_tasks_; |
Ryan Keane | 32c88d0 | 2019-07-02 18:46:14 -0700 | [diff] [blame] | 152 | |
| 153 | OSP_DISALLOW_COPY_AND_ASSIGN(TaskRunnerImpl); |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 154 | }; |
| 155 | } // namespace platform |
| 156 | } // namespace openscreen |
| 157 | |
Jordan Bayles | a26582d | 2019-07-10 14:44:58 -0700 | [diff] [blame] | 158 | #endif // PLATFORM_IMPL_TASK_RUNNER_H_ |