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