blob: b9bfdd87a491510b761d793a6d5156f964030f40 [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
Mirko Bonadei317a1f02019-09-17 17:06:18 +020013#include "absl/memory/memory.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010014#include "rtc_base/logging.h"
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020015#include "rtc_base/task_utils/pending_task_safety_flag.h"
Danil Chapovalov4423c362019-03-06 18:41:39 +010016#include "rtc_base/task_utils/to_queued_task.h"
Steve Antonf3802842019-01-24 19:07:40 -080017#include "rtc_base/time_utils.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010018
19namespace webrtc {
20namespace webrtc_repeating_task_impl {
Tommi532cac52020-05-18 14:53:42 +020021
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020022RepeatingTaskBase::RepeatingTaskBase(
23 TaskQueueBase* task_queue,
24 TimeDelta first_delay,
25 Clock* clock,
26 rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag)
Sebastian Janssonecb68972019-01-18 10:30:54 +010027 : task_queue_(task_queue),
Tommi532cac52020-05-18 14:53:42 +020028 clock_(clock),
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020029 next_run_time_(clock_->CurrentTime() + first_delay),
30 alive_flag_(std::move(alive_flag)) {}
Sebastian Janssonecb68972019-01-18 10:30:54 +010031
32RepeatingTaskBase::~RepeatingTaskBase() = default;
33
34bool RepeatingTaskBase::Run() {
Tommi29a5fe82020-05-15 10:12:36 +020035 RTC_DCHECK_RUN_ON(task_queue_);
Sebastian Janssonecb68972019-01-18 10:30:54 +010036 // Return true to tell the TaskQueue to destruct this object.
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020037 if (!alive_flag_->alive())
Sebastian Janssonecb68972019-01-18 10:30:54 +010038 return true;
39
40 TimeDelta delay = RunClosure();
Niels Möller902b5542022-01-17 15:20:24 +010041 RTC_DCHECK_GE(delay, TimeDelta::Zero());
Sebastian Janssonecb68972019-01-18 10:30:54 +010042
Niels Möller902b5542022-01-17 15:20:24 +010043 // A delay of +infinity means that the task should not be run again.
44 // Alternatively, the closure might have stopped this task. In either which
45 // case we return true to destruct this object.
46 if (delay.IsPlusInfinity() || !alive_flag_->alive())
Sebastian Janssonecb68972019-01-18 10:30:54 +010047 return true;
Jonas Olssona4d87372019-07-05 19:08:33 +020048
Tommi532cac52020-05-18 14:53:42 +020049 TimeDelta lost_time = clock_->CurrentTime() - next_run_time_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010050 next_run_time_ += delay;
51 delay -= lost_time;
Sebastian Janssona497d122019-02-04 16:39:28 +010052 delay = std::max(delay, TimeDelta::Zero());
Sebastian Janssonecb68972019-01-18 10:30:54 +010053
Sebastian Janssona497d122019-02-04 16:39:28 +010054 task_queue_->PostDelayedTask(absl::WrapUnique(this), delay.ms());
55
Sebastian Janssonecb68972019-01-18 10:30:54 +010056 // Return false to tell the TaskQueue to not destruct this object since we
57 // have taken ownership with absl::WrapUnique.
58 return false;
59}
60
Sebastian Janssonecb68972019-01-18 10:30:54 +010061} // namespace webrtc_repeating_task_impl
Sebastian Janssonecb68972019-01-18 10:30:54 +010062
Sebastian Janssonecb68972019-01-18 10:30:54 +010063void RepeatingTaskHandle::Stop() {
Sebastian Janssonecb68972019-01-18 10:30:54 +010064 if (repeating_task_) {
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020065 repeating_task_->SetNotAlive();
Sebastian Janssonecb68972019-01-18 10:30:54 +010066 repeating_task_ = nullptr;
67 }
68}
69
Sebastian Janssonecb68972019-01-18 10:30:54 +010070bool RepeatingTaskHandle::Running() const {
Sebastian Janssonecb68972019-01-18 10:30:54 +010071 return repeating_task_ != nullptr;
72}
73
Markus Handellc1c6bef2021-07-23 11:11:32 +020074namespace webrtc_repeating_task_impl {
75// These methods are empty, but can be externally equipped with actions using
76// dtrace.
77void RepeatingTaskHandleDTraceProbeStart() {}
78void RepeatingTaskHandleDTraceProbeDelayedStart() {}
79void RepeatingTaskImplDTraceProbeRun() {}
80} // namespace webrtc_repeating_task_impl
Sebastian Janssonecb68972019-01-18 10:30:54 +010081} // namespace webrtc