blob: 1f3eb1d064a29ad5ac2c9f1b0a2d8b56690236ab [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();
Sebastian Janssonecb68972019-01-18 10:30:54 +010041
42 // The closure might have stopped this task, in which case we return true to
43 // destruct this object.
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020044 if (!alive_flag_->alive())
Sebastian Janssonecb68972019-01-18 10:30:54 +010045 return true;
Jonas Olssona4d87372019-07-05 19:08:33 +020046
Sebastian Janssond841ea62019-03-13 16:35:59 +010047 RTC_DCHECK(delay.IsFinite());
Tommi532cac52020-05-18 14:53:42 +020048 TimeDelta lost_time = clock_->CurrentTime() - next_run_time_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010049 next_run_time_ += delay;
50 delay -= lost_time;
Sebastian Janssona497d122019-02-04 16:39:28 +010051 delay = std::max(delay, TimeDelta::Zero());
Sebastian Janssonecb68972019-01-18 10:30:54 +010052
Sebastian Janssona497d122019-02-04 16:39:28 +010053 task_queue_->PostDelayedTask(absl::WrapUnique(this), delay.ms());
54
Sebastian Janssonecb68972019-01-18 10:30:54 +010055 // Return false to tell the TaskQueue to not destruct this object since we
56 // have taken ownership with absl::WrapUnique.
57 return false;
58}
59
Sebastian Janssonecb68972019-01-18 10:30:54 +010060} // namespace webrtc_repeating_task_impl
Sebastian Janssonecb68972019-01-18 10:30:54 +010061
Sebastian Janssonecb68972019-01-18 10:30:54 +010062void RepeatingTaskHandle::Stop() {
Sebastian Janssonecb68972019-01-18 10:30:54 +010063 if (repeating_task_) {
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020064 repeating_task_->SetNotAlive();
Sebastian Janssonecb68972019-01-18 10:30:54 +010065 repeating_task_ = nullptr;
66 }
67}
68
Sebastian Janssonecb68972019-01-18 10:30:54 +010069bool RepeatingTaskHandle::Running() const {
Sebastian Janssonecb68972019-01-18 10:30:54 +010070 return repeating_task_ != nullptr;
71}
72
Markus Handellc1c6bef2021-07-23 11:11:32 +020073namespace webrtc_repeating_task_impl {
74// These methods are empty, but can be externally equipped with actions using
75// dtrace.
76void RepeatingTaskHandleDTraceProbeStart() {}
77void RepeatingTaskHandleDTraceProbeDelayedStart() {}
78void RepeatingTaskImplDTraceProbeRun() {}
79} // namespace webrtc_repeating_task_impl
Sebastian Janssonecb68972019-01-18 10:30:54 +010080} // namespace webrtc