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), |
Tommi | a0a4480 | 2020-05-13 18:27:26 +0200 | [diff] [blame] | 23 | next_run_time_(Timestamp::Micros(rtc::TimeMicros()) + first_delay) { |
Tommi | a0a4480 | 2020-05-13 18:27:26 +0200 | [diff] [blame] | 24 | } |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 25 | |
| 26 | RepeatingTaskBase::~RepeatingTaskBase() = default; |
| 27 | |
| 28 | bool RepeatingTaskBase::Run() { |
Tommi | 29a5fe8 | 2020-05-15 10:12:36 +0200 | [diff] [blame] | 29 | RTC_DCHECK_RUN_ON(task_queue_); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 30 | // Return true to tell the TaskQueue to destruct this object. |
| 31 | if (next_run_time_.IsPlusInfinity()) |
| 32 | return true; |
| 33 | |
| 34 | TimeDelta delay = RunClosure(); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 35 | |
| 36 | // The closure might have stopped this task, in which case we return true to |
| 37 | // destruct this object. |
| 38 | if (next_run_time_.IsPlusInfinity()) |
| 39 | return true; |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 40 | |
Sebastian Jansson | d841ea6 | 2019-03-13 16:35:59 +0100 | [diff] [blame] | 41 | RTC_DCHECK(delay.IsFinite()); |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 42 | TimeDelta lost_time = Timestamp::Micros(rtc::TimeMicros()) - next_run_time_; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 43 | next_run_time_ += delay; |
| 44 | delay -= lost_time; |
Sebastian Jansson | a497d12 | 2019-02-04 16:39:28 +0100 | [diff] [blame] | 45 | delay = std::max(delay, TimeDelta::Zero()); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 46 | |
Sebastian Jansson | a497d12 | 2019-02-04 16:39:28 +0100 | [diff] [blame] | 47 | task_queue_->PostDelayedTask(absl::WrapUnique(this), delay.ms()); |
| 48 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 49 | // Return false to tell the TaskQueue to not destruct this object since we |
| 50 | // have taken ownership with absl::WrapUnique. |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | void RepeatingTaskBase::Stop() { |
Tommi | 29a5fe8 | 2020-05-15 10:12:36 +0200 | [diff] [blame] | 55 | RTC_DCHECK_RUN_ON(task_queue_); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 56 | RTC_DCHECK(next_run_time_.IsFinite()); |
| 57 | next_run_time_ = Timestamp::PlusInfinity(); |
| 58 | } |
| 59 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 60 | } // namespace webrtc_repeating_task_impl |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 61 | |
| 62 | RepeatingTaskHandle::RepeatingTaskHandle(RepeatingTaskHandle&& other) |
| 63 | : repeating_task_(other.repeating_task_) { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 64 | other.repeating_task_ = nullptr; |
| 65 | } |
| 66 | |
| 67 | RepeatingTaskHandle& RepeatingTaskHandle::operator=( |
| 68 | RepeatingTaskHandle&& other) { |
Sebastian Jansson | 46b4a0f | 2019-03-26 15:24:23 +0100 | [diff] [blame] | 69 | repeating_task_ = other.repeating_task_; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 70 | other.repeating_task_ = nullptr; |
| 71 | return *this; |
| 72 | } |
| 73 | |
| 74 | RepeatingTaskHandle::RepeatingTaskHandle( |
| 75 | webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task) |
| 76 | : repeating_task_(repeating_task) {} |
| 77 | |
| 78 | void RepeatingTaskHandle::Stop() { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 79 | if (repeating_task_) { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 80 | repeating_task_->Stop(); |
| 81 | repeating_task_ = nullptr; |
| 82 | } |
| 83 | } |
| 84 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 85 | bool RepeatingTaskHandle::Running() const { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 86 | return repeating_task_ != nullptr; |
| 87 | } |
| 88 | |
| 89 | } // namespace webrtc |