Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 1 | /* |
| 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" |
| 12 | #include "rtc_base/logging.h" |
Steve Anton | f380284 | 2019-01-24 19:07:40 -0800 | [diff] [blame^] | 13 | #include "rtc_base/time_utils.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 14 | |
| 15 | namespace webrtc { |
| 16 | namespace webrtc_repeating_task_impl { |
| 17 | RepeatingTaskBase::RepeatingTaskBase(rtc::TaskQueue* task_queue, |
| 18 | TimeDelta first_delay) |
| 19 | : task_queue_(task_queue), |
| 20 | next_run_time_(Timestamp::us(rtc::TimeMicros()) + first_delay) {} |
| 21 | |
| 22 | RepeatingTaskBase::~RepeatingTaskBase() = default; |
| 23 | |
| 24 | bool RepeatingTaskBase::Run() { |
| 25 | RTC_DCHECK_RUN_ON(task_queue_); |
| 26 | // Return true to tell the TaskQueue to destruct this object. |
| 27 | if (next_run_time_.IsPlusInfinity()) |
| 28 | return true; |
| 29 | |
| 30 | TimeDelta delay = RunClosure(); |
| 31 | RTC_DCHECK(delay.IsFinite()); |
| 32 | |
| 33 | // The closure might have stopped this task, in which case we return true to |
| 34 | // destruct this object. |
| 35 | if (next_run_time_.IsPlusInfinity()) |
| 36 | return true; |
| 37 | |
| 38 | TimeDelta lost_time = Timestamp::us(rtc::TimeMicros()) - next_run_time_; |
| 39 | next_run_time_ += delay; |
| 40 | delay -= lost_time; |
| 41 | |
| 42 | if (delay <= TimeDelta::Zero()) { |
| 43 | task_queue_->PostTask(absl::WrapUnique(this)); |
| 44 | } else { |
| 45 | task_queue_->PostDelayedTask(absl::WrapUnique(this), delay.ms()); |
| 46 | } |
| 47 | // Return false to tell the TaskQueue to not destruct this object since we |
| 48 | // have taken ownership with absl::WrapUnique. |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | void RepeatingTaskBase::Stop() { |
| 53 | RTC_DCHECK(next_run_time_.IsFinite()); |
| 54 | next_run_time_ = Timestamp::PlusInfinity(); |
| 55 | } |
| 56 | |
| 57 | void RepeatingTaskBase::PostStop() { |
| 58 | if (task_queue_->IsCurrent()) { |
| 59 | RTC_DLOG(LS_INFO) << "Using PostStop() from the task queue running the " |
| 60 | "repeated task. Consider calling Stop() instead."; |
| 61 | } |
| 62 | task_queue_->PostTask([this] { |
| 63 | RTC_DCHECK_RUN_ON(task_queue_); |
| 64 | Stop(); |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | } // namespace webrtc_repeating_task_impl |
| 69 | RepeatingTaskHandle::RepeatingTaskHandle() { |
| 70 | sequence_checker_.Detach(); |
| 71 | } |
| 72 | RepeatingTaskHandle::~RepeatingTaskHandle() { |
| 73 | sequence_checker_.Detach(); |
| 74 | } |
| 75 | |
| 76 | RepeatingTaskHandle::RepeatingTaskHandle(RepeatingTaskHandle&& other) |
| 77 | : repeating_task_(other.repeating_task_) { |
| 78 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 79 | other.repeating_task_ = nullptr; |
| 80 | } |
| 81 | |
| 82 | RepeatingTaskHandle& RepeatingTaskHandle::operator=( |
| 83 | RepeatingTaskHandle&& other) { |
| 84 | RTC_DCHECK_RUN_ON(&other.sequence_checker_); |
| 85 | { |
| 86 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 87 | repeating_task_ = other.repeating_task_; |
| 88 | } |
| 89 | other.repeating_task_ = nullptr; |
| 90 | return *this; |
| 91 | } |
| 92 | |
| 93 | RepeatingTaskHandle::RepeatingTaskHandle( |
| 94 | webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task) |
| 95 | : repeating_task_(repeating_task) {} |
| 96 | |
| 97 | void RepeatingTaskHandle::Stop() { |
| 98 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 99 | if (repeating_task_) { |
| 100 | RTC_DCHECK_RUN_ON(repeating_task_->task_queue_); |
| 101 | repeating_task_->Stop(); |
| 102 | repeating_task_ = nullptr; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void RepeatingTaskHandle::PostStop() { |
| 107 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 108 | if (repeating_task_) { |
| 109 | repeating_task_->PostStop(); |
| 110 | repeating_task_ = nullptr; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | bool RepeatingTaskHandle::Running() const { |
| 115 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 116 | return repeating_task_ != nullptr; |
| 117 | } |
| 118 | |
| 119 | } // namespace webrtc |