blob: c07df4567c1b87df354e39e1a00ac6488c7ef1ec [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,
Henrik Boström27e8a092022-01-24 17:12:35 +010024 TaskQueueBase::DelayPrecision precision,
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020025 TimeDelta first_delay,
26 Clock* clock,
27 rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag)
Sebastian Janssonecb68972019-01-18 10:30:54 +010028 : task_queue_(task_queue),
Henrik Boström27e8a092022-01-24 17:12:35 +010029 precision_(precision),
Tommi532cac52020-05-18 14:53:42 +020030 clock_(clock),
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020031 next_run_time_(clock_->CurrentTime() + first_delay),
32 alive_flag_(std::move(alive_flag)) {}
Sebastian Janssonecb68972019-01-18 10:30:54 +010033
34RepeatingTaskBase::~RepeatingTaskBase() = default;
35
36bool RepeatingTaskBase::Run() {
Tommi29a5fe82020-05-15 10:12:36 +020037 RTC_DCHECK_RUN_ON(task_queue_);
Sebastian Janssonecb68972019-01-18 10:30:54 +010038 // Return true to tell the TaskQueue to destruct this object.
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020039 if (!alive_flag_->alive())
Sebastian Janssonecb68972019-01-18 10:30:54 +010040 return true;
41
42 TimeDelta delay = RunClosure();
Niels Möller902b5542022-01-17 15:20:24 +010043 RTC_DCHECK_GE(delay, TimeDelta::Zero());
Sebastian Janssonecb68972019-01-18 10:30:54 +010044
Niels Möller902b5542022-01-17 15:20:24 +010045 // A delay of +infinity means that the task should not be run again.
46 // Alternatively, the closure might have stopped this task. In either which
47 // case we return true to destruct this object.
48 if (delay.IsPlusInfinity() || !alive_flag_->alive())
Sebastian Janssonecb68972019-01-18 10:30:54 +010049 return true;
Jonas Olssona4d87372019-07-05 19:08:33 +020050
Tommi532cac52020-05-18 14:53:42 +020051 TimeDelta lost_time = clock_->CurrentTime() - next_run_time_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010052 next_run_time_ += delay;
53 delay -= lost_time;
Sebastian Janssona497d122019-02-04 16:39:28 +010054 delay = std::max(delay, TimeDelta::Zero());
Sebastian Janssonecb68972019-01-18 10:30:54 +010055
Henrik Boström27e8a092022-01-24 17:12:35 +010056 task_queue_->PostDelayedTaskWithPrecision(precision_, absl::WrapUnique(this),
57 delay.ms());
Sebastian Janssona497d122019-02-04 16:39:28 +010058
Sebastian Janssonecb68972019-01-18 10:30:54 +010059 // Return false to tell the TaskQueue to not destruct this object since we
60 // have taken ownership with absl::WrapUnique.
61 return false;
62}
63
Sebastian Janssonecb68972019-01-18 10:30:54 +010064} // namespace webrtc_repeating_task_impl
Sebastian Janssonecb68972019-01-18 10:30:54 +010065
Sebastian Janssonecb68972019-01-18 10:30:54 +010066void RepeatingTaskHandle::Stop() {
Sebastian Janssonecb68972019-01-18 10:30:54 +010067 if (repeating_task_) {
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020068 repeating_task_->SetNotAlive();
Sebastian Janssonecb68972019-01-18 10:30:54 +010069 repeating_task_ = nullptr;
70 }
71}
72
Sebastian Janssonecb68972019-01-18 10:30:54 +010073bool RepeatingTaskHandle::Running() const {
Sebastian Janssonecb68972019-01-18 10:30:54 +010074 return repeating_task_ != nullptr;
75}
76
Markus Handellc1c6bef2021-07-23 11:11:32 +020077namespace webrtc_repeating_task_impl {
78// These methods are empty, but can be externally equipped with actions using
79// dtrace.
80void RepeatingTaskHandleDTraceProbeStart() {}
81void RepeatingTaskHandleDTraceProbeDelayedStart() {}
82void RepeatingTaskImplDTraceProbeRun() {}
83} // namespace webrtc_repeating_task_impl
Sebastian Janssonecb68972019-01-18 10:30:54 +010084} // namespace webrtc