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" |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 12 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 13 | #include "rtc_base/logging.h" |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 14 | #include "rtc_base/task_utils/to_queued_task.h" |
Steve Anton | f380284 | 2019-01-24 19:07:40 -0800 | [diff] [blame] | 15 | #include "rtc_base/time_utils.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | namespace webrtc_repeating_task_impl { |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 19 | RepeatingTaskBase::RepeatingTaskBase(TaskQueueBase* task_queue, |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 20 | TimeDelta first_delay) |
| 21 | : task_queue_(task_queue), |
| 22 | next_run_time_(Timestamp::us(rtc::TimeMicros()) + first_delay) {} |
| 23 | |
| 24 | RepeatingTaskBase::~RepeatingTaskBase() = default; |
| 25 | |
| 26 | bool RepeatingTaskBase::Run() { |
| 27 | RTC_DCHECK_RUN_ON(task_queue_); |
| 28 | // Return true to tell the TaskQueue to destruct this object. |
| 29 | if (next_run_time_.IsPlusInfinity()) |
| 30 | return true; |
| 31 | |
| 32 | TimeDelta delay = RunClosure(); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 33 | |
| 34 | // The closure might have stopped this task, in which case we return true to |
| 35 | // destruct this object. |
| 36 | if (next_run_time_.IsPlusInfinity()) |
| 37 | return true; |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame^] | 38 | |
Sebastian Jansson | d841ea6 | 2019-03-13 16:35:59 +0100 | [diff] [blame] | 39 | RTC_DCHECK(delay.IsFinite()); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 40 | TimeDelta lost_time = Timestamp::us(rtc::TimeMicros()) - next_run_time_; |
| 41 | next_run_time_ += delay; |
| 42 | delay -= lost_time; |
Sebastian Jansson | a497d12 | 2019-02-04 16:39:28 +0100 | [diff] [blame] | 43 | delay = std::max(delay, TimeDelta::Zero()); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 44 | |
Sebastian Jansson | a497d12 | 2019-02-04 16:39:28 +0100 | [diff] [blame] | 45 | task_queue_->PostDelayedTask(absl::WrapUnique(this), delay.ms()); |
| 46 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 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 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 57 | } // namespace webrtc_repeating_task_impl |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 58 | |
| 59 | RepeatingTaskHandle::RepeatingTaskHandle(RepeatingTaskHandle&& other) |
| 60 | : repeating_task_(other.repeating_task_) { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 61 | other.repeating_task_ = nullptr; |
| 62 | } |
| 63 | |
| 64 | RepeatingTaskHandle& RepeatingTaskHandle::operator=( |
| 65 | RepeatingTaskHandle&& other) { |
Sebastian Jansson | 46b4a0f | 2019-03-26 15:24:23 +0100 | [diff] [blame] | 66 | repeating_task_ = other.repeating_task_; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 67 | other.repeating_task_ = nullptr; |
| 68 | return *this; |
| 69 | } |
| 70 | |
| 71 | RepeatingTaskHandle::RepeatingTaskHandle( |
| 72 | webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task) |
| 73 | : repeating_task_(repeating_task) {} |
| 74 | |
| 75 | void RepeatingTaskHandle::Stop() { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 76 | if (repeating_task_) { |
| 77 | RTC_DCHECK_RUN_ON(repeating_task_->task_queue_); |
| 78 | repeating_task_->Stop(); |
| 79 | repeating_task_ = nullptr; |
| 80 | } |
| 81 | } |
| 82 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 83 | bool RepeatingTaskHandle::Running() const { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 84 | return repeating_task_ != nullptr; |
| 85 | } |
| 86 | |
| 87 | } // namespace webrtc |