blob: 222ab1ad676c65ed8eb04bf85b4d61b8c78fbc8d [file] [log] [blame]
Sebastian Janssonecb68972019-01-18 10:30:54 +01001/*
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 Chapovalov4423c362019-03-06 18:41:39 +010012
Danil Chapovalov4b979282022-06-30 10:08:47 +020013#include "absl/functional/any_invocable.h"
Artem Titovc374d112022-06-16 21:27:45 +020014#include "api/task_queue/pending_task_safety_flag.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010015#include "rtc_base/logging.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010016
17namespace webrtc {
Danil Chapovalov4b979282022-06-30 10:08:47 +020018namespace {
Tommi532cac52020-05-18 14:53:42 +020019
Danil Chapovalovdde7fe42022-07-05 21:26:06 +020020class RepeatingTask {
Danil Chapovalov4b979282022-06-30 10:08:47 +020021 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 Chapovalovdde7fe42022-07-05 21:26:06 +020028 RepeatingTask(RepeatingTask&&) = default;
29 RepeatingTask& operator=(RepeatingTask&&) = delete;
30 ~RepeatingTask() = default;
31
32 void operator()() &&;
Danil Chapovalov4b979282022-06-30 10:08:47 +020033
34 private:
Danil Chapovalov4b979282022-06-30 10:08:47 +020035 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
45RepeatingTask::RepeatingTask(
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020046 TaskQueueBase* task_queue,
Henrik Boström27e8a092022-01-24 17:12:35 +010047 TaskQueueBase::DelayPrecision precision,
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020048 TimeDelta first_delay,
Danil Chapovalov4b979282022-06-30 10:08:47 +020049 absl::AnyInvocable<TimeDelta()> task,
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020050 Clock* clock,
51 rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag)
Sebastian Janssonecb68972019-01-18 10:30:54 +010052 : task_queue_(task_queue),
Henrik Boström27e8a092022-01-24 17:12:35 +010053 precision_(precision),
Tommi532cac52020-05-18 14:53:42 +020054 clock_(clock),
Danil Chapovalov4b979282022-06-30 10:08:47 +020055 task_(std::move(task)),
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020056 next_run_time_(clock_->CurrentTime() + first_delay),
57 alive_flag_(std::move(alive_flag)) {}
Sebastian Janssonecb68972019-01-18 10:30:54 +010058
Danil Chapovalovdde7fe42022-07-05 21:26:06 +020059void RepeatingTask::operator()() && {
Tommi29a5fe82020-05-15 10:12:36 +020060 RTC_DCHECK_RUN_ON(task_queue_);
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020061 if (!alive_flag_->alive())
Danil Chapovalovdde7fe42022-07-05 21:26:06 +020062 return;
Sebastian Janssonecb68972019-01-18 10:30:54 +010063
Danil Chapovalov4b979282022-06-30 10:08:47 +020064 webrtc_repeating_task_impl::RepeatingTaskImplDTraceProbeRun();
65 TimeDelta delay = task_();
Niels Möller902b5542022-01-17 15:20:24 +010066 RTC_DCHECK_GE(delay, TimeDelta::Zero());
Sebastian Janssonecb68972019-01-18 10:30:54 +010067
Niels Möller902b5542022-01-17 15:20:24 +010068 // A delay of +infinity means that the task should not be run again.
Danil Chapovalovdde7fe42022-07-05 21:26:06 +020069 // Alternatively, the closure might have stopped this task.
Niels Möller902b5542022-01-17 15:20:24 +010070 if (delay.IsPlusInfinity() || !alive_flag_->alive())
Danil Chapovalovdde7fe42022-07-05 21:26:06 +020071 return;
Jonas Olssona4d87372019-07-05 19:08:33 +020072
Tommi532cac52020-05-18 14:53:42 +020073 TimeDelta lost_time = clock_->CurrentTime() - next_run_time_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010074 next_run_time_ += delay;
75 delay -= lost_time;
Sebastian Janssona497d122019-02-04 16:39:28 +010076 delay = std::max(delay, TimeDelta::Zero());
Sebastian Janssonecb68972019-01-18 10:30:54 +010077
Danil Chapovalovdde7fe42022-07-05 21:26:06 +020078 task_queue_->PostDelayedTaskWithPrecision(precision_, std::move(*this),
79 delay);
Sebastian Janssonecb68972019-01-18 10:30:54 +010080}
81
Danil Chapovalov4b979282022-06-30 10:08:47 +020082} // namespace
83
84RepeatingTaskHandle 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 Chapovalovdde7fe42022-07-05 21:26:06 +020091 task_queue->PostTask(RepeatingTask(task_queue, precision, TimeDelta::Zero(),
92 std::move(closure), clock, alive_flag));
Danil Chapovalov4b979282022-06-30 10:08:47 +020093 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.
98RepeatingTaskHandle 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 Chapovalovdde7fe42022-07-05 21:26:06 +0200108 RepeatingTask(task_queue, precision, first_delay, std::move(closure),
109 clock, alive_flag),
110 first_delay);
Danil Chapovalov4b979282022-06-30 10:08:47 +0200111 return RepeatingTaskHandle(std::move(alive_flag));
112}
Sebastian Janssonecb68972019-01-18 10:30:54 +0100113
Sebastian Janssonecb68972019-01-18 10:30:54 +0100114void RepeatingTaskHandle::Stop() {
Sebastian Janssonecb68972019-01-18 10:30:54 +0100115 if (repeating_task_) {
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +0200116 repeating_task_->SetNotAlive();
Sebastian Janssonecb68972019-01-18 10:30:54 +0100117 repeating_task_ = nullptr;
118 }
119}
120
Sebastian Janssonecb68972019-01-18 10:30:54 +0100121bool RepeatingTaskHandle::Running() const {
Sebastian Janssonecb68972019-01-18 10:30:54 +0100122 return repeating_task_ != nullptr;
123}
124
Markus Handellc1c6bef2021-07-23 11:11:32 +0200125namespace webrtc_repeating_task_impl {
126// These methods are empty, but can be externally equipped with actions using
127// dtrace.
128void RepeatingTaskHandleDTraceProbeStart() {}
129void RepeatingTaskHandleDTraceProbeDelayedStart() {}
130void RepeatingTaskImplDTraceProbeRun() {}
131} // namespace webrtc_repeating_task_impl
Sebastian Janssonecb68972019-01-18 10:30:54 +0100132} // namespace webrtc