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 | #include "rtc_base/task_utils/repeating_task.h" |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 12 | |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame] | 13 | #include "absl/functional/any_invocable.h" |
Artem Titov | c374d11 | 2022-06-16 21:27:45 +0200 | [diff] [blame] | 14 | #include "api/task_queue/pending_task_safety_flag.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 15 | #include "rtc_base/logging.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame] | 18 | namespace { |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 19 | |
Danil Chapovalov | dde7fe4 | 2022-07-05 21:26:06 +0200 | [diff] [blame^] | 20 | class RepeatingTask { |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame] | 21 | public: |
| 22 | RepeatingTask(TaskQueueBase* task_queue, |
| 23 | TaskQueueBase::DelayPrecision precision, |
| 24 | TimeDelta first_delay, |
| 25 | absl::AnyInvocable<TimeDelta()> task, |
| 26 | Clock* clock, |
| 27 | rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag); |
Danil Chapovalov | dde7fe4 | 2022-07-05 21:26:06 +0200 | [diff] [blame^] | 28 | RepeatingTask(RepeatingTask&&) = default; |
| 29 | RepeatingTask& operator=(RepeatingTask&&) = delete; |
| 30 | ~RepeatingTask() = default; |
| 31 | |
| 32 | void operator()() &&; |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame] | 33 | |
| 34 | private: |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame] | 35 | TaskQueueBase* const task_queue_; |
| 36 | const TaskQueueBase::DelayPrecision precision_; |
| 37 | Clock* const clock_; |
| 38 | absl::AnyInvocable<TimeDelta()> task_; |
| 39 | // This is always finite. |
| 40 | Timestamp next_run_time_ RTC_GUARDED_BY(task_queue_); |
| 41 | rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag_ |
| 42 | RTC_GUARDED_BY(task_queue_); |
| 43 | }; |
| 44 | |
| 45 | RepeatingTask::RepeatingTask( |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 46 | TaskQueueBase* task_queue, |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame] | 47 | TaskQueueBase::DelayPrecision precision, |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 48 | TimeDelta first_delay, |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame] | 49 | absl::AnyInvocable<TimeDelta()> task, |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 50 | Clock* clock, |
| 51 | rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag) |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 52 | : task_queue_(task_queue), |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame] | 53 | precision_(precision), |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 54 | clock_(clock), |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame] | 55 | task_(std::move(task)), |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 56 | next_run_time_(clock_->CurrentTime() + first_delay), |
| 57 | alive_flag_(std::move(alive_flag)) {} |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 58 | |
Danil Chapovalov | dde7fe4 | 2022-07-05 21:26:06 +0200 | [diff] [blame^] | 59 | void RepeatingTask::operator()() && { |
Tommi | 29a5fe8 | 2020-05-15 10:12:36 +0200 | [diff] [blame] | 60 | RTC_DCHECK_RUN_ON(task_queue_); |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 61 | if (!alive_flag_->alive()) |
Danil Chapovalov | dde7fe4 | 2022-07-05 21:26:06 +0200 | [diff] [blame^] | 62 | return; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 63 | |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame] | 64 | webrtc_repeating_task_impl::RepeatingTaskImplDTraceProbeRun(); |
| 65 | TimeDelta delay = task_(); |
Niels Möller | 902b554 | 2022-01-17 15:20:24 +0100 | [diff] [blame] | 66 | RTC_DCHECK_GE(delay, TimeDelta::Zero()); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 67 | |
Niels Möller | 902b554 | 2022-01-17 15:20:24 +0100 | [diff] [blame] | 68 | // A delay of +infinity means that the task should not be run again. |
Danil Chapovalov | dde7fe4 | 2022-07-05 21:26:06 +0200 | [diff] [blame^] | 69 | // Alternatively, the closure might have stopped this task. |
Niels Möller | 902b554 | 2022-01-17 15:20:24 +0100 | [diff] [blame] | 70 | if (delay.IsPlusInfinity() || !alive_flag_->alive()) |
Danil Chapovalov | dde7fe4 | 2022-07-05 21:26:06 +0200 | [diff] [blame^] | 71 | return; |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 72 | |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 73 | TimeDelta lost_time = clock_->CurrentTime() - next_run_time_; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 74 | next_run_time_ += delay; |
| 75 | delay -= lost_time; |
Sebastian Jansson | a497d12 | 2019-02-04 16:39:28 +0100 | [diff] [blame] | 76 | delay = std::max(delay, TimeDelta::Zero()); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 77 | |
Danil Chapovalov | dde7fe4 | 2022-07-05 21:26:06 +0200 | [diff] [blame^] | 78 | task_queue_->PostDelayedTaskWithPrecision(precision_, std::move(*this), |
| 79 | delay); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame] | 82 | } // namespace |
| 83 | |
| 84 | RepeatingTaskHandle RepeatingTaskHandle::Start( |
| 85 | TaskQueueBase* task_queue, |
| 86 | absl::AnyInvocable<TimeDelta()> closure, |
| 87 | TaskQueueBase::DelayPrecision precision, |
| 88 | Clock* clock) { |
| 89 | auto alive_flag = PendingTaskSafetyFlag::CreateDetached(); |
| 90 | webrtc_repeating_task_impl::RepeatingTaskHandleDTraceProbeStart(); |
Danil Chapovalov | dde7fe4 | 2022-07-05 21:26:06 +0200 | [diff] [blame^] | 91 | task_queue->PostTask(RepeatingTask(task_queue, precision, TimeDelta::Zero(), |
| 92 | std::move(closure), clock, alive_flag)); |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame] | 93 | return RepeatingTaskHandle(std::move(alive_flag)); |
| 94 | } |
| 95 | |
| 96 | // DelayedStart is equivalent to Start except that the first invocation of the |
| 97 | // closure will be delayed by the given amount. |
| 98 | RepeatingTaskHandle RepeatingTaskHandle::DelayedStart( |
| 99 | TaskQueueBase* task_queue, |
| 100 | TimeDelta first_delay, |
| 101 | absl::AnyInvocable<TimeDelta()> closure, |
| 102 | TaskQueueBase::DelayPrecision precision, |
| 103 | Clock* clock) { |
| 104 | auto alive_flag = PendingTaskSafetyFlag::CreateDetached(); |
| 105 | webrtc_repeating_task_impl::RepeatingTaskHandleDTraceProbeDelayedStart(); |
| 106 | task_queue->PostDelayedTaskWithPrecision( |
| 107 | precision, |
Danil Chapovalov | dde7fe4 | 2022-07-05 21:26:06 +0200 | [diff] [blame^] | 108 | RepeatingTask(task_queue, precision, first_delay, std::move(closure), |
| 109 | clock, alive_flag), |
| 110 | first_delay); |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame] | 111 | return RepeatingTaskHandle(std::move(alive_flag)); |
| 112 | } |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 113 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 114 | void RepeatingTaskHandle::Stop() { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 115 | if (repeating_task_) { |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 116 | repeating_task_->SetNotAlive(); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 117 | repeating_task_ = nullptr; |
| 118 | } |
| 119 | } |
| 120 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 121 | bool RepeatingTaskHandle::Running() const { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 122 | return repeating_task_ != nullptr; |
| 123 | } |
| 124 | |
Markus Handell | c1c6bef | 2021-07-23 11:11:32 +0200 | [diff] [blame] | 125 | namespace webrtc_repeating_task_impl { |
| 126 | // These methods are empty, but can be externally equipped with actions using |
| 127 | // dtrace. |
| 128 | void RepeatingTaskHandleDTraceProbeStart() {} |
| 129 | void RepeatingTaskHandleDTraceProbeDelayedStart() {} |
| 130 | void RepeatingTaskImplDTraceProbeRun() {} |
| 131 | } // namespace webrtc_repeating_task_impl |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 132 | } // namespace webrtc |