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