blob: fa6ae79321246794f46d6fd525d0a11b29112bbd [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"
Mirko Bonadei317a1f02019-09-17 17:06:18 +020014#include "absl/memory/memory.h"
Artem Titovc374d112022-06-16 21:27:45 +020015#include "api/task_queue/pending_task_safety_flag.h"
16#include "api/task_queue/to_queued_task.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010017#include "rtc_base/logging.h"
Steve Antonf3802842019-01-24 19:07:40 -080018#include "rtc_base/time_utils.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010019
20namespace webrtc {
Danil Chapovalov4b979282022-06-30 10:08:47 +020021namespace {
Tommi532cac52020-05-18 14:53:42 +020022
Danil Chapovalov4b979282022-06-30 10:08:47 +020023class 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
46RepeatingTask::RepeatingTask(
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020047 TaskQueueBase* task_queue,
Henrik Boström27e8a092022-01-24 17:12:35 +010048 TaskQueueBase::DelayPrecision precision,
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020049 TimeDelta first_delay,
Danil Chapovalov4b979282022-06-30 10:08:47 +020050 absl::AnyInvocable<TimeDelta()> task,
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020051 Clock* clock,
52 rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag)
Sebastian Janssonecb68972019-01-18 10:30:54 +010053 : task_queue_(task_queue),
Henrik Boström27e8a092022-01-24 17:12:35 +010054 precision_(precision),
Tommi532cac52020-05-18 14:53:42 +020055 clock_(clock),
Danil Chapovalov4b979282022-06-30 10:08:47 +020056 task_(std::move(task)),
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020057 next_run_time_(clock_->CurrentTime() + first_delay),
58 alive_flag_(std::move(alive_flag)) {}
Sebastian Janssonecb68972019-01-18 10:30:54 +010059
Danil Chapovalov4b979282022-06-30 10:08:47 +020060bool RepeatingTask::Run() {
Tommi29a5fe82020-05-15 10:12:36 +020061 RTC_DCHECK_RUN_ON(task_queue_);
Sebastian Janssonecb68972019-01-18 10:30:54 +010062 // Return true to tell the TaskQueue to destruct this object.
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020063 if (!alive_flag_->alive())
Sebastian Janssonecb68972019-01-18 10:30:54 +010064 return true;
65
Danil Chapovalov4b979282022-06-30 10:08:47 +020066 webrtc_repeating_task_impl::RepeatingTaskImplDTraceProbeRun();
67 TimeDelta delay = task_();
Niels Möller902b5542022-01-17 15:20:24 +010068 RTC_DCHECK_GE(delay, TimeDelta::Zero());
Sebastian Janssonecb68972019-01-18 10:30:54 +010069
Niels Möller902b5542022-01-17 15:20:24 +010070 // 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 Janssonecb68972019-01-18 10:30:54 +010074 return true;
Jonas Olssona4d87372019-07-05 19:08:33 +020075
Tommi532cac52020-05-18 14:53:42 +020076 TimeDelta lost_time = clock_->CurrentTime() - next_run_time_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010077 next_run_time_ += delay;
78 delay -= lost_time;
Sebastian Janssona497d122019-02-04 16:39:28 +010079 delay = std::max(delay, TimeDelta::Zero());
Sebastian Janssonecb68972019-01-18 10:30:54 +010080
Henrik Boström27e8a092022-01-24 17:12:35 +010081 task_queue_->PostDelayedTaskWithPrecision(precision_, absl::WrapUnique(this),
82 delay.ms());
Sebastian Janssona497d122019-02-04 16:39:28 +010083
Sebastian Janssonecb68972019-01-18 10:30:54 +010084 // 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 Chapovalov4b979282022-06-30 10:08:47 +020089} // namespace
90
91RepeatingTaskHandle 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.
106RepeatingTaskHandle 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 Janssonecb68972019-01-18 10:30:54 +0100121
Sebastian Janssonecb68972019-01-18 10:30:54 +0100122void RepeatingTaskHandle::Stop() {
Sebastian Janssonecb68972019-01-18 10:30:54 +0100123 if (repeating_task_) {
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +0200124 repeating_task_->SetNotAlive();
Sebastian Janssonecb68972019-01-18 10:30:54 +0100125 repeating_task_ = nullptr;
126 }
127}
128
Sebastian Janssonecb68972019-01-18 10:30:54 +0100129bool RepeatingTaskHandle::Running() const {
Sebastian Janssonecb68972019-01-18 10:30:54 +0100130 return repeating_task_ != nullptr;
131}
132
Markus Handellc1c6bef2021-07-23 11:11:32 +0200133namespace webrtc_repeating_task_impl {
134// These methods are empty, but can be externally equipped with actions using
135// dtrace.
136void RepeatingTaskHandleDTraceProbeStart() {}
137void RepeatingTaskHandleDTraceProbeDelayedStart() {}
138void RepeatingTaskImplDTraceProbeRun() {}
139} // namespace webrtc_repeating_task_impl
Sebastian Janssonecb68972019-01-18 10:30:54 +0100140} // namespace webrtc