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 | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 15 | #include "rtc_base/task_utils/pending_task_safety_flag.h" |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 16 | #include "rtc_base/task_utils/to_queued_task.h" |
Steve Anton | f380284 | 2019-01-24 19:07:40 -0800 | [diff] [blame] | 17 | #include "rtc_base/time_utils.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | namespace webrtc_repeating_task_impl { |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 21 | |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 22 | RepeatingTaskBase::RepeatingTaskBase( |
| 23 | TaskQueueBase* task_queue, |
| 24 | TimeDelta first_delay, |
| 25 | Clock* clock, |
| 26 | rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag) |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 27 | : task_queue_(task_queue), |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 28 | clock_(clock), |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 29 | next_run_time_(clock_->CurrentTime() + first_delay), |
| 30 | alive_flag_(std::move(alive_flag)) {} |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 31 | |
| 32 | RepeatingTaskBase::~RepeatingTaskBase() = default; |
| 33 | |
| 34 | bool RepeatingTaskBase::Run() { |
Tommi | 29a5fe8 | 2020-05-15 10:12:36 +0200 | [diff] [blame] | 35 | RTC_DCHECK_RUN_ON(task_queue_); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 36 | // Return true to tell the TaskQueue to destruct this object. |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 37 | if (!alive_flag_->alive()) |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 38 | return true; |
| 39 | |
| 40 | TimeDelta delay = RunClosure(); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 41 | |
| 42 | // The closure might have stopped this task, in which case we return true to |
| 43 | // destruct this object. |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 44 | if (!alive_flag_->alive()) |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 45 | return true; |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 46 | |
Sebastian Jansson | d841ea6 | 2019-03-13 16:35:59 +0100 | [diff] [blame] | 47 | RTC_DCHECK(delay.IsFinite()); |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 48 | TimeDelta lost_time = clock_->CurrentTime() - next_run_time_; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 49 | next_run_time_ += delay; |
| 50 | delay -= lost_time; |
Sebastian Jansson | a497d12 | 2019-02-04 16:39:28 +0100 | [diff] [blame] | 51 | delay = std::max(delay, TimeDelta::Zero()); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 52 | |
Sebastian Jansson | a497d12 | 2019-02-04 16:39:28 +0100 | [diff] [blame] | 53 | task_queue_->PostDelayedTask(absl::WrapUnique(this), delay.ms()); |
| 54 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 55 | // Return false to tell the TaskQueue to not destruct this object since we |
| 56 | // have taken ownership with absl::WrapUnique. |
| 57 | return false; |
| 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 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 62 | void RepeatingTaskHandle::Stop() { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 63 | if (repeating_task_) { |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 64 | repeating_task_->SetNotAlive(); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 65 | repeating_task_ = nullptr; |
| 66 | } |
| 67 | } |
| 68 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 69 | bool RepeatingTaskHandle::Running() const { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 70 | return repeating_task_ != nullptr; |
| 71 | } |
| 72 | |
| 73 | } // namespace webrtc |