Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef RTC_BASE_TASK_UTILS_REPEATING_TASK_H_ |
| 12 | #define RTC_BASE_TASK_UTILS_REPEATING_TASK_H_ |
| 13 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 14 | #include <memory> |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 15 | #include <type_traits> |
| 16 | #include <utility> |
| 17 | |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 18 | #include "api/task_queue/queued_task.h" |
| 19 | #include "api/task_queue/task_queue_base.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 20 | #include "api/units/time_delta.h" |
| 21 | #include "api/units/timestamp.h" |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 22 | #include "rtc_base/task_utils/pending_task_safety_flag.h" |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 23 | #include "system_wrappers/include/clock.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
Markus Handell | c1c6bef | 2021-07-23 11:11:32 +0200 | [diff] [blame] | 26 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 27 | namespace webrtc_repeating_task_impl { |
Markus Handell | c1c6bef | 2021-07-23 11:11:32 +0200 | [diff] [blame] | 28 | |
| 29 | // Methods simplifying external tracing of RepeatingTaskHandle operations. |
| 30 | void RepeatingTaskHandleDTraceProbeStart(); |
| 31 | void RepeatingTaskHandleDTraceProbeDelayedStart(); |
| 32 | void RepeatingTaskImplDTraceProbeRun(); |
| 33 | |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 34 | class RepeatingTaskBase : public QueuedTask { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 35 | public: |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 36 | RepeatingTaskBase(TaskQueueBase* task_queue, |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame^] | 37 | TaskQueueBase::DelayPrecision precision, |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 38 | TimeDelta first_delay, |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 39 | Clock* clock, |
| 40 | rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 41 | ~RepeatingTaskBase() override; |
Tommi | a0a4480 | 2020-05-13 18:27:26 +0200 | [diff] [blame] | 42 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 43 | private: |
Tommi | a0a4480 | 2020-05-13 18:27:26 +0200 | [diff] [blame] | 44 | virtual TimeDelta RunClosure() = 0; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 45 | |
| 46 | bool Run() final; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 47 | |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 48 | TaskQueueBase* const task_queue_; |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame^] | 49 | const TaskQueueBase::DelayPrecision precision_; |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 50 | Clock* const clock_; |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 51 | // This is always finite. |
Tommi | 29a5fe8 | 2020-05-15 10:12:36 +0200 | [diff] [blame] | 52 | Timestamp next_run_time_ RTC_GUARDED_BY(task_queue_); |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 53 | rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag_ |
| 54 | RTC_GUARDED_BY(task_queue_); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 55 | }; |
| 56 | |
Niels Möller | 902b554 | 2022-01-17 15:20:24 +0100 | [diff] [blame] | 57 | // The template closure pattern is based on rtc::ClosureTask. The provided |
| 58 | // closure should have a TimeDelta return value, specifing the desired |
| 59 | // non-negative interval to next repetition, or TimeDelta::PlusInfinity to |
| 60 | // indicate that the task should be deleted and not called again. |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 61 | template <class Closure> |
| 62 | class RepeatingTaskImpl final : public RepeatingTaskBase { |
| 63 | public: |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 64 | RepeatingTaskImpl(TaskQueueBase* task_queue, |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame^] | 65 | TaskQueueBase::DelayPrecision precision, |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 66 | TimeDelta first_delay, |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 67 | Closure&& closure, |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 68 | Clock* clock, |
| 69 | rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag) |
| 70 | : RepeatingTaskBase(task_queue, |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame^] | 71 | precision, |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 72 | first_delay, |
| 73 | clock, |
| 74 | std::move(alive_flag)), |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 75 | closure_(std::forward<Closure>(closure)) { |
| 76 | static_assert( |
| 77 | std::is_same<TimeDelta, |
| 78 | typename std::result_of<decltype (&Closure::operator())( |
| 79 | Closure)>::type>::value, |
| 80 | ""); |
| 81 | } |
| 82 | |
Tommi | a0a4480 | 2020-05-13 18:27:26 +0200 | [diff] [blame] | 83 | private: |
Markus Handell | c1c6bef | 2021-07-23 11:11:32 +0200 | [diff] [blame] | 84 | TimeDelta RunClosure() override { |
| 85 | RepeatingTaskImplDTraceProbeRun(); |
| 86 | return closure_(); |
| 87 | } |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 88 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 89 | typename std::remove_const< |
| 90 | typename std::remove_reference<Closure>::type>::type closure_; |
| 91 | }; |
| 92 | } // namespace webrtc_repeating_task_impl |
| 93 | |
| 94 | // Allows starting tasks that repeat themselves on a TaskQueue indefinately |
| 95 | // until they are stopped or the TaskQueue is destroyed. It allows starting and |
| 96 | // stopping multiple times, but you must stop one task before starting another |
| 97 | // and it can only be stopped when in the running state. The public interface is |
| 98 | // not thread safe. |
| 99 | class RepeatingTaskHandle { |
| 100 | public: |
Sebastian Jansson | 46b4a0f | 2019-03-26 15:24:23 +0100 | [diff] [blame] | 101 | RepeatingTaskHandle() = default; |
| 102 | ~RepeatingTaskHandle() = default; |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 103 | RepeatingTaskHandle(RepeatingTaskHandle&& other) = default; |
| 104 | RepeatingTaskHandle& operator=(RepeatingTaskHandle&& other) = default; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 105 | RepeatingTaskHandle(const RepeatingTaskHandle&) = delete; |
| 106 | RepeatingTaskHandle& operator=(const RepeatingTaskHandle&) = delete; |
| 107 | |
| 108 | // Start can be used to start a task that will be reposted with a delay |
| 109 | // determined by the return value of the provided closure. The actual task is |
| 110 | // owned by the TaskQueue and will live until it has been stopped or the |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 111 | // TaskQueue deletes it. It's perfectly fine to destroy the handle while the |
| 112 | // task is running, since the repeated task is owned by the TaskQueue. |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame^] | 113 | // The tasks are scheduled onto the task queue using the specified precision. |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 114 | template <class Closure> |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 115 | static RepeatingTaskHandle Start(TaskQueueBase* task_queue, |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 116 | Closure&& closure, |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame^] | 117 | TaskQueueBase::DelayPrecision precision = |
| 118 | TaskQueueBase::DelayPrecision::kLow, |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 119 | Clock* clock = Clock::GetRealTimeClock()) { |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 120 | auto alive_flag = PendingTaskSafetyFlag::CreateDetached(); |
Markus Handell | c1c6bef | 2021-07-23 11:11:32 +0200 | [diff] [blame] | 121 | webrtc_repeating_task_impl::RepeatingTaskHandleDTraceProbeStart(); |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 122 | task_queue->PostTask( |
| 123 | std::make_unique< |
| 124 | webrtc_repeating_task_impl::RepeatingTaskImpl<Closure>>( |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame^] | 125 | task_queue, precision, TimeDelta::Zero(), |
| 126 | std::forward<Closure>(closure), clock, alive_flag)); |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 127 | return RepeatingTaskHandle(std::move(alive_flag)); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 128 | } |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 129 | |
| 130 | // DelayedStart is equivalent to Start except that the first invocation of the |
| 131 | // closure will be delayed by the given amount. |
| 132 | template <class Closure> |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 133 | static RepeatingTaskHandle DelayedStart( |
| 134 | TaskQueueBase* task_queue, |
| 135 | TimeDelta first_delay, |
| 136 | Closure&& closure, |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame^] | 137 | TaskQueueBase::DelayPrecision precision = |
| 138 | TaskQueueBase::DelayPrecision::kLow, |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 139 | Clock* clock = Clock::GetRealTimeClock()) { |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 140 | auto alive_flag = PendingTaskSafetyFlag::CreateDetached(); |
Markus Handell | c1c6bef | 2021-07-23 11:11:32 +0200 | [diff] [blame] | 141 | webrtc_repeating_task_impl::RepeatingTaskHandleDTraceProbeDelayedStart(); |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame^] | 142 | task_queue->PostDelayedTaskWithPrecision( |
| 143 | precision, |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 144 | std::make_unique< |
| 145 | webrtc_repeating_task_impl::RepeatingTaskImpl<Closure>>( |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame^] | 146 | task_queue, precision, first_delay, std::forward<Closure>(closure), |
| 147 | clock, alive_flag), |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 148 | first_delay.ms()); |
| 149 | return RepeatingTaskHandle(std::move(alive_flag)); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 150 | } |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 151 | |
| 152 | // Stops future invocations of the repeating task closure. Can only be called |
| 153 | // from the TaskQueue where the task is running. The closure is guaranteed to |
| 154 | // not be running after Stop() returns unless Stop() is called from the |
| 155 | // closure itself. |
| 156 | void Stop(); |
| 157 | |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 158 | // Returns true until Stop() was called. |
| 159 | // Can only be called from the TaskQueue where the task is running. |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 160 | bool Running() const; |
| 161 | |
| 162 | private: |
| 163 | explicit RepeatingTaskHandle( |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 164 | rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag) |
| 165 | : repeating_task_(std::move(alive_flag)) {} |
| 166 | rtc::scoped_refptr<PendingTaskSafetyFlag> repeating_task_; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | } // namespace webrtc |
| 170 | #endif // RTC_BASE_TASK_UTILS_REPEATING_TASK_H_ |