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