blob: 71911e6982c240a132fdaec3c56c1eced71a7100 [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 {
Danil Chapovalov4423c362019-03-06 18:41:39 +010020RepeatingTaskBase::RepeatingTaskBase(TaskQueueBase* task_queue,
Sebastian Janssonecb68972019-01-18 10:30:54 +010021 TimeDelta first_delay)
22 : task_queue_(task_queue),
Tommia0a44802020-05-13 18:27:26 +020023 next_run_time_(Timestamp::Micros(rtc::TimeMicros()) + first_delay) {
Artem Titovaf1b9ce2020-05-14 13:52:14 +000024 sequence_checker_.Detach();
Tommia0a44802020-05-13 18:27:26 +020025}
Sebastian Janssonecb68972019-01-18 10:30:54 +010026
27RepeatingTaskBase::~RepeatingTaskBase() = default;
28
29bool RepeatingTaskBase::Run() {
Artem Titovaf1b9ce2020-05-14 13:52:14 +000030 RTC_DCHECK_RUN_ON(&sequence_checker_);
Sebastian Janssonecb68972019-01-18 10:30:54 +010031 // Return true to tell the TaskQueue to destruct this object.
32 if (next_run_time_.IsPlusInfinity())
33 return true;
34
35 TimeDelta delay = RunClosure();
Sebastian Janssonecb68972019-01-18 10:30:54 +010036
37 // The closure might have stopped this task, in which case we return true to
38 // destruct this object.
39 if (next_run_time_.IsPlusInfinity())
40 return true;
Jonas Olssona4d87372019-07-05 19:08:33 +020041
Sebastian Janssond841ea62019-03-13 16:35:59 +010042 RTC_DCHECK(delay.IsFinite());
Danil Chapovalov0c626af2020-02-10 11:16:00 +010043 TimeDelta lost_time = Timestamp::Micros(rtc::TimeMicros()) - next_run_time_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010044 next_run_time_ += delay;
45 delay -= lost_time;
Sebastian Janssona497d122019-02-04 16:39:28 +010046 delay = std::max(delay, TimeDelta::Zero());
Sebastian Janssonecb68972019-01-18 10:30:54 +010047
Sebastian Janssona497d122019-02-04 16:39:28 +010048 task_queue_->PostDelayedTask(absl::WrapUnique(this), delay.ms());
49
Sebastian Janssonecb68972019-01-18 10:30:54 +010050 // Return false to tell the TaskQueue to not destruct this object since we
51 // have taken ownership with absl::WrapUnique.
52 return false;
53}
54
55void RepeatingTaskBase::Stop() {
Artem Titovaf1b9ce2020-05-14 13:52:14 +000056 RTC_DCHECK_RUN_ON(&sequence_checker_);
Sebastian Janssonecb68972019-01-18 10:30:54 +010057 RTC_DCHECK(next_run_time_.IsFinite());
58 next_run_time_ = Timestamp::PlusInfinity();
59}
60
Sebastian Janssonecb68972019-01-18 10:30:54 +010061} // namespace webrtc_repeating_task_impl
Sebastian Janssonecb68972019-01-18 10:30:54 +010062
63RepeatingTaskHandle::RepeatingTaskHandle(RepeatingTaskHandle&& other)
64 : repeating_task_(other.repeating_task_) {
Sebastian Janssonecb68972019-01-18 10:30:54 +010065 other.repeating_task_ = nullptr;
66}
67
68RepeatingTaskHandle& RepeatingTaskHandle::operator=(
69 RepeatingTaskHandle&& other) {
Sebastian Jansson46b4a0f2019-03-26 15:24:23 +010070 repeating_task_ = other.repeating_task_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010071 other.repeating_task_ = nullptr;
72 return *this;
73}
74
75RepeatingTaskHandle::RepeatingTaskHandle(
76 webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task)
77 : repeating_task_(repeating_task) {}
78
79void RepeatingTaskHandle::Stop() {
Sebastian Janssonecb68972019-01-18 10:30:54 +010080 if (repeating_task_) {
Sebastian Janssonecb68972019-01-18 10:30:54 +010081 repeating_task_->Stop();
82 repeating_task_ = nullptr;
83 }
84}
85
Sebastian Janssonecb68972019-01-18 10:30:54 +010086bool RepeatingTaskHandle::Running() const {
Sebastian Janssonecb68972019-01-18 10:30:54 +010087 return repeating_task_ != nullptr;
88}
89
90} // namespace webrtc