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 | #ifndef RTC_BASE_TASK_UTILS_REPEATING_TASK_H_ |
| 12 | #define RTC_BASE_TASK_UTILS_REPEATING_TASK_H_ |
| 13 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 14 | #include <memory> |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 15 | #include <type_traits> |
| 16 | #include <utility> |
| 17 | |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame^] | 18 | #include "absl/functional/any_invocable.h" |
Artem Titov | c374d11 | 2022-06-16 21:27:45 +0200 | [diff] [blame] | 19 | #include "api/task_queue/pending_task_safety_flag.h" |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 20 | #include "api/task_queue/task_queue_base.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 21 | #include "api/units/time_delta.h" |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 22 | #include "system_wrappers/include/clock.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
Markus Handell | c1c6bef | 2021-07-23 11:11:32 +0200 | [diff] [blame] | 25 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 26 | namespace webrtc_repeating_task_impl { |
Markus Handell | c1c6bef | 2021-07-23 11:11:32 +0200 | [diff] [blame] | 27 | |
| 28 | // Methods simplifying external tracing of RepeatingTaskHandle operations. |
| 29 | void RepeatingTaskHandleDTraceProbeStart(); |
| 30 | void RepeatingTaskHandleDTraceProbeDelayedStart(); |
| 31 | void RepeatingTaskImplDTraceProbeRun(); |
| 32 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 33 | } // namespace webrtc_repeating_task_impl |
| 34 | |
| 35 | // Allows starting tasks that repeat themselves on a TaskQueue indefinately |
| 36 | // until they are stopped or the TaskQueue is destroyed. It allows starting and |
| 37 | // stopping multiple times, but you must stop one task before starting another |
| 38 | // and it can only be stopped when in the running state. The public interface is |
| 39 | // not thread safe. |
| 40 | class RepeatingTaskHandle { |
| 41 | public: |
Sebastian Jansson | 46b4a0f | 2019-03-26 15:24:23 +0100 | [diff] [blame] | 42 | RepeatingTaskHandle() = default; |
| 43 | ~RepeatingTaskHandle() = default; |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 44 | RepeatingTaskHandle(RepeatingTaskHandle&& other) = default; |
| 45 | RepeatingTaskHandle& operator=(RepeatingTaskHandle&& other) = default; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 46 | RepeatingTaskHandle(const RepeatingTaskHandle&) = delete; |
| 47 | RepeatingTaskHandle& operator=(const RepeatingTaskHandle&) = delete; |
| 48 | |
| 49 | // Start can be used to start a task that will be reposted with a delay |
| 50 | // determined by the return value of the provided closure. The actual task is |
| 51 | // owned by the TaskQueue and will live until it has been stopped or the |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 52 | // TaskQueue deletes it. It's perfectly fine to destroy the handle while the |
| 53 | // task is running, since the repeated task is owned by the TaskQueue. |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame] | 54 | // The tasks are scheduled onto the task queue using the specified precision. |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 55 | static RepeatingTaskHandle Start(TaskQueueBase* task_queue, |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame^] | 56 | absl::AnyInvocable<TimeDelta()> closure, |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame] | 57 | TaskQueueBase::DelayPrecision precision = |
| 58 | TaskQueueBase::DelayPrecision::kLow, |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame^] | 59 | Clock* clock = Clock::GetRealTimeClock()); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 60 | |
| 61 | // DelayedStart is equivalent to Start except that the first invocation of the |
| 62 | // closure will be delayed by the given amount. |
Tommi | 532cac5 | 2020-05-18 14:53:42 +0200 | [diff] [blame] | 63 | static RepeatingTaskHandle DelayedStart( |
| 64 | TaskQueueBase* task_queue, |
| 65 | TimeDelta first_delay, |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame^] | 66 | absl::AnyInvocable<TimeDelta()> closure, |
Henrik Boström | 27e8a09 | 2022-01-24 17:12:35 +0100 | [diff] [blame] | 67 | TaskQueueBase::DelayPrecision precision = |
| 68 | TaskQueueBase::DelayPrecision::kLow, |
Danil Chapovalov | 4b97928 | 2022-06-30 10:08:47 +0200 | [diff] [blame^] | 69 | Clock* clock = Clock::GetRealTimeClock()); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 70 | |
| 71 | // Stops future invocations of the repeating task closure. Can only be called |
| 72 | // from the TaskQueue where the task is running. The closure is guaranteed to |
| 73 | // not be running after Stop() returns unless Stop() is called from the |
| 74 | // closure itself. |
| 75 | void Stop(); |
| 76 | |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 77 | // Returns true until Stop() was called. |
| 78 | // Can only be called from the TaskQueue where the task is running. |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 79 | bool Running() const; |
| 80 | |
| 81 | private: |
| 82 | explicit RepeatingTaskHandle( |
Danil Chapovalov | 0f9a8e3 | 2021-06-11 18:39:17 +0200 | [diff] [blame] | 83 | rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag) |
| 84 | : repeating_task_(std::move(alive_flag)) {} |
| 85 | rtc::scoped_refptr<PendingTaskSafetyFlag> repeating_task_; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } // namespace webrtc |
| 89 | #endif // RTC_BASE_TASK_UTILS_REPEATING_TASK_H_ |