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 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 13 | #include "absl/memory/memory.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 14 | #include "rtc_base/logging.h" |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 15 | #include "rtc_base/task_utils/to_queued_task.h" |
Steve Anton | f380284 | 2019-01-24 19:07:40 -0800 | [diff] [blame] | 16 | #include "rtc_base/time_utils.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | namespace webrtc_repeating_task_impl { |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 20 | RepeatingTaskBase::RepeatingTaskBase(TaskQueueBase* task_queue, |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 21 | TimeDelta first_delay) |
| 22 | : task_queue_(task_queue), |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame^] | 23 | next_run_time_(Timestamp::Micros(rtc::TimeMicros()) + first_delay) {} |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 24 | |
| 25 | RepeatingTaskBase::~RepeatingTaskBase() = default; |
| 26 | |
| 27 | bool 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 Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 34 | |
| 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 Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 39 | |
Sebastian Jansson | d841ea6 | 2019-03-13 16:35:59 +0100 | [diff] [blame] | 40 | RTC_DCHECK(delay.IsFinite()); |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame^] | 41 | TimeDelta lost_time = Timestamp::Micros(rtc::TimeMicros()) - next_run_time_; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 42 | next_run_time_ += delay; |
| 43 | delay -= lost_time; |
Sebastian Jansson | a497d12 | 2019-02-04 16:39:28 +0100 | [diff] [blame] | 44 | delay = std::max(delay, TimeDelta::Zero()); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 45 | |
Sebastian Jansson | a497d12 | 2019-02-04 16:39:28 +0100 | [diff] [blame] | 46 | task_queue_->PostDelayedTask(absl::WrapUnique(this), delay.ms()); |
| 47 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 48 | // 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 | |
| 53 | void RepeatingTaskBase::Stop() { |
| 54 | RTC_DCHECK(next_run_time_.IsFinite()); |
| 55 | next_run_time_ = Timestamp::PlusInfinity(); |
| 56 | } |
| 57 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 58 | } // namespace webrtc_repeating_task_impl |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 59 | |
| 60 | RepeatingTaskHandle::RepeatingTaskHandle(RepeatingTaskHandle&& other) |
| 61 | : repeating_task_(other.repeating_task_) { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 62 | other.repeating_task_ = nullptr; |
| 63 | } |
| 64 | |
| 65 | RepeatingTaskHandle& RepeatingTaskHandle::operator=( |
| 66 | RepeatingTaskHandle&& other) { |
Sebastian Jansson | 46b4a0f | 2019-03-26 15:24:23 +0100 | [diff] [blame] | 67 | repeating_task_ = other.repeating_task_; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 68 | other.repeating_task_ = nullptr; |
| 69 | return *this; |
| 70 | } |
| 71 | |
| 72 | RepeatingTaskHandle::RepeatingTaskHandle( |
| 73 | webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task) |
| 74 | : repeating_task_(repeating_task) {} |
| 75 | |
| 76 | void RepeatingTaskHandle::Stop() { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 77 | if (repeating_task_) { |
| 78 | RTC_DCHECK_RUN_ON(repeating_task_->task_queue_); |
| 79 | repeating_task_->Stop(); |
| 80 | repeating_task_ = nullptr; |
| 81 | } |
| 82 | } |
| 83 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 84 | bool RepeatingTaskHandle::Running() const { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 85 | return repeating_task_ != nullptr; |
| 86 | } |
| 87 | |
| 88 | } // namespace webrtc |