blob: 4e460bb082fd7997f1b5ab3f4e9a4fd6c286e33b [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),
Danil Chapovalov0c626af2020-02-10 11:16:00 +010023 next_run_time_(Timestamp::Micros(rtc::TimeMicros()) + first_delay) {}
Sebastian Janssonecb68972019-01-18 10:30:54 +010024
25RepeatingTaskBase::~RepeatingTaskBase() = default;
26
27bool RepeatingTaskBase::Run() {
28 RTC_DCHECK_RUN_ON(task_queue_);
29 // Return true to tell the TaskQueue to destruct this object.
30 if (next_run_time_.IsPlusInfinity())
31 return true;
32
33 TimeDelta delay = RunClosure();
Sebastian Janssonecb68972019-01-18 10:30:54 +010034
35 // The closure might have stopped this task, in which case we return true to
36 // destruct this object.
37 if (next_run_time_.IsPlusInfinity())
38 return true;
Jonas Olssona4d87372019-07-05 19:08:33 +020039
Sebastian Janssond841ea62019-03-13 16:35:59 +010040 RTC_DCHECK(delay.IsFinite());
Danil Chapovalov0c626af2020-02-10 11:16:00 +010041 TimeDelta lost_time = Timestamp::Micros(rtc::TimeMicros()) - next_run_time_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010042 next_run_time_ += delay;
43 delay -= lost_time;
Sebastian Janssona497d122019-02-04 16:39:28 +010044 delay = std::max(delay, TimeDelta::Zero());
Sebastian Janssonecb68972019-01-18 10:30:54 +010045
Sebastian Janssona497d122019-02-04 16:39:28 +010046 task_queue_->PostDelayedTask(absl::WrapUnique(this), delay.ms());
47
Sebastian Janssonecb68972019-01-18 10:30:54 +010048 // Return false to tell the TaskQueue to not destruct this object since we
49 // have taken ownership with absl::WrapUnique.
50 return false;
51}
52
53void RepeatingTaskBase::Stop() {
54 RTC_DCHECK(next_run_time_.IsFinite());
55 next_run_time_ = Timestamp::PlusInfinity();
56}
57
Sebastian Janssonecb68972019-01-18 10:30:54 +010058} // namespace webrtc_repeating_task_impl
Sebastian Janssonecb68972019-01-18 10:30:54 +010059
60RepeatingTaskHandle::RepeatingTaskHandle(RepeatingTaskHandle&& other)
61 : repeating_task_(other.repeating_task_) {
Sebastian Janssonecb68972019-01-18 10:30:54 +010062 other.repeating_task_ = nullptr;
63}
64
65RepeatingTaskHandle& RepeatingTaskHandle::operator=(
66 RepeatingTaskHandle&& other) {
Sebastian Jansson46b4a0f2019-03-26 15:24:23 +010067 repeating_task_ = other.repeating_task_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010068 other.repeating_task_ = nullptr;
69 return *this;
70}
71
72RepeatingTaskHandle::RepeatingTaskHandle(
73 webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task)
74 : repeating_task_(repeating_task) {}
75
76void RepeatingTaskHandle::Stop() {
Sebastian Janssonecb68972019-01-18 10:30:54 +010077 if (repeating_task_) {
78 RTC_DCHECK_RUN_ON(repeating_task_->task_queue_);
79 repeating_task_->Stop();
80 repeating_task_ = nullptr;
81 }
82}
83
Sebastian Janssonecb68972019-01-18 10:30:54 +010084bool RepeatingTaskHandle::Running() const {
Sebastian Janssonecb68972019-01-18 10:30:54 +010085 return repeating_task_ != nullptr;
86}
87
88} // namespace webrtc