blob: 574e6331f14112c7e58907a35a9ba19d0f626c66 [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 Chapovalov4423c362019-03-06 18:41:39 +010015#include "rtc_base/task_utils/to_queued_task.h"
Steve Antonf3802842019-01-24 19:07:40 -080016#include "rtc_base/time_utils.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010017
18namespace webrtc {
19namespace webrtc_repeating_task_impl {
Tommi532cac52020-05-18 14:53:42 +020020
Danil Chapovalov4423c362019-03-06 18:41:39 +010021RepeatingTaskBase::RepeatingTaskBase(TaskQueueBase* task_queue,
Tommi532cac52020-05-18 14:53:42 +020022 TimeDelta first_delay,
23 Clock* clock)
Sebastian Janssonecb68972019-01-18 10:30:54 +010024 : task_queue_(task_queue),
Tommi532cac52020-05-18 14:53:42 +020025 clock_(clock),
26 next_run_time_(clock_->CurrentTime() + first_delay) {}
Sebastian Janssonecb68972019-01-18 10:30:54 +010027
28RepeatingTaskBase::~RepeatingTaskBase() = default;
29
30bool RepeatingTaskBase::Run() {
Tommi29a5fe82020-05-15 10:12:36 +020031 RTC_DCHECK_RUN_ON(task_queue_);
Sebastian Janssonecb68972019-01-18 10:30:54 +010032 // Return true to tell the TaskQueue to destruct this object.
33 if (next_run_time_.IsPlusInfinity())
34 return true;
35
36 TimeDelta delay = RunClosure();
Sebastian Janssonecb68972019-01-18 10:30:54 +010037
38 // The closure might have stopped this task, in which case we return true to
39 // destruct this object.
40 if (next_run_time_.IsPlusInfinity())
41 return true;
Jonas Olssona4d87372019-07-05 19:08:33 +020042
Sebastian Janssond841ea62019-03-13 16:35:59 +010043 RTC_DCHECK(delay.IsFinite());
Tommi532cac52020-05-18 14:53:42 +020044 TimeDelta lost_time = clock_->CurrentTime() - next_run_time_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010045 next_run_time_ += delay;
46 delay -= lost_time;
Sebastian Janssona497d122019-02-04 16:39:28 +010047 delay = std::max(delay, TimeDelta::Zero());
Sebastian Janssonecb68972019-01-18 10:30:54 +010048
Sebastian Janssona497d122019-02-04 16:39:28 +010049 task_queue_->PostDelayedTask(absl::WrapUnique(this), delay.ms());
50
Sebastian Janssonecb68972019-01-18 10:30:54 +010051 // Return false to tell the TaskQueue to not destruct this object since we
52 // have taken ownership with absl::WrapUnique.
53 return false;
54}
55
56void RepeatingTaskBase::Stop() {
Tommi29a5fe82020-05-15 10:12:36 +020057 RTC_DCHECK_RUN_ON(task_queue_);
Sebastian Janssonecb68972019-01-18 10:30:54 +010058 RTC_DCHECK(next_run_time_.IsFinite());
59 next_run_time_ = Timestamp::PlusInfinity();
60}
61
Sebastian Janssonecb68972019-01-18 10:30:54 +010062} // namespace webrtc_repeating_task_impl
Sebastian Janssonecb68972019-01-18 10:30:54 +010063
64RepeatingTaskHandle::RepeatingTaskHandle(RepeatingTaskHandle&& other)
65 : repeating_task_(other.repeating_task_) {
Sebastian Janssonecb68972019-01-18 10:30:54 +010066 other.repeating_task_ = nullptr;
67}
68
69RepeatingTaskHandle& RepeatingTaskHandle::operator=(
70 RepeatingTaskHandle&& other) {
Sebastian Jansson46b4a0f2019-03-26 15:24:23 +010071 repeating_task_ = other.repeating_task_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010072 other.repeating_task_ = nullptr;
73 return *this;
74}
75
76RepeatingTaskHandle::RepeatingTaskHandle(
77 webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task)
78 : repeating_task_(repeating_task) {}
79
80void RepeatingTaskHandle::Stop() {
Sebastian Janssonecb68972019-01-18 10:30:54 +010081 if (repeating_task_) {
Sebastian Janssonecb68972019-01-18 10:30:54 +010082 repeating_task_->Stop();
83 repeating_task_ = nullptr;
84 }
85}
86
Sebastian Janssonecb68972019-01-18 10:30:54 +010087bool RepeatingTaskHandle::Running() const {
Sebastian Janssonecb68972019-01-18 10:30:54 +010088 return repeating_task_ != nullptr;
89}
90
91} // namespace webrtc