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