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 | |
| 14 | #include <type_traits> |
| 15 | #include <utility> |
| 16 | |
| 17 | #include "absl/memory/memory.h" |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 18 | #include "api/task_queue/queued_task.h" |
| 19 | #include "api/task_queue/task_queue_base.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 20 | #include "api/units/time_delta.h" |
| 21 | #include "api/units/timestamp.h" |
| 22 | #include "rtc_base/sequenced_task_checker.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 23 | #include "rtc_base/thread_checker.h" |
| 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 27 | class RepeatingTaskHandle; |
| 28 | |
| 29 | namespace webrtc_repeating_task_impl { |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 30 | class RepeatingTaskBase : public QueuedTask { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 31 | public: |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 32 | RepeatingTaskBase(TaskQueueBase* task_queue, TimeDelta first_delay); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 33 | ~RepeatingTaskBase() override; |
| 34 | virtual TimeDelta RunClosure() = 0; |
| 35 | |
| 36 | private: |
| 37 | friend class ::webrtc::RepeatingTaskHandle; |
| 38 | |
| 39 | bool Run() final; |
| 40 | void Stop() RTC_RUN_ON(task_queue_); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 41 | |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 42 | TaskQueueBase* const task_queue_; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 43 | // This is always finite, except for the special case where it's PlusInfinity |
| 44 | // to signal that the task should stop. |
| 45 | Timestamp next_run_time_ RTC_GUARDED_BY(task_queue_); |
| 46 | }; |
| 47 | |
| 48 | // The template closure pattern is based on rtc::ClosureTask. |
| 49 | template <class Closure> |
| 50 | class RepeatingTaskImpl final : public RepeatingTaskBase { |
| 51 | public: |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 52 | RepeatingTaskImpl(TaskQueueBase* task_queue, |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 53 | TimeDelta first_delay, |
| 54 | Closure&& closure) |
| 55 | : RepeatingTaskBase(task_queue, first_delay), |
| 56 | closure_(std::forward<Closure>(closure)) { |
| 57 | static_assert( |
| 58 | std::is_same<TimeDelta, |
| 59 | typename std::result_of<decltype (&Closure::operator())( |
| 60 | Closure)>::type>::value, |
| 61 | ""); |
| 62 | } |
| 63 | |
| 64 | TimeDelta RunClosure() override { return closure_(); } |
| 65 | |
| 66 | private: |
| 67 | typename std::remove_const< |
| 68 | typename std::remove_reference<Closure>::type>::type closure_; |
| 69 | }; |
| 70 | } // namespace webrtc_repeating_task_impl |
| 71 | |
| 72 | // Allows starting tasks that repeat themselves on a TaskQueue indefinately |
| 73 | // until they are stopped or the TaskQueue is destroyed. It allows starting and |
| 74 | // stopping multiple times, but you must stop one task before starting another |
| 75 | // and it can only be stopped when in the running state. The public interface is |
| 76 | // not thread safe. |
| 77 | class RepeatingTaskHandle { |
| 78 | public: |
Sebastian Jansson | 46b4a0f | 2019-03-26 15:24:23 +0100 | [diff] [blame] | 79 | RepeatingTaskHandle() = default; |
| 80 | ~RepeatingTaskHandle() = default; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 81 | RepeatingTaskHandle(RepeatingTaskHandle&& other); |
| 82 | RepeatingTaskHandle& operator=(RepeatingTaskHandle&& other); |
| 83 | RepeatingTaskHandle(const RepeatingTaskHandle&) = delete; |
| 84 | RepeatingTaskHandle& operator=(const RepeatingTaskHandle&) = delete; |
| 85 | |
| 86 | // Start can be used to start a task that will be reposted with a delay |
| 87 | // determined by the return value of the provided closure. The actual task is |
| 88 | // owned by the TaskQueue and will live until it has been stopped or the |
| 89 | // TaskQueue is destroyed. Note that this means that trying to stop the |
| 90 | // repeating task after the TaskQueue is destroyed is an error. However, it's |
| 91 | // perfectly fine to destroy the handle while the task is running, since the |
| 92 | // repeated task is owned by the TaskQueue. |
| 93 | template <class Closure> |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 94 | static RepeatingTaskHandle Start(TaskQueueBase* task_queue, |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 95 | Closure&& closure) { |
| 96 | auto repeating_task = absl::make_unique< |
| 97 | webrtc_repeating_task_impl::RepeatingTaskImpl<Closure>>( |
| 98 | task_queue, TimeDelta::Zero(), std::forward<Closure>(closure)); |
| 99 | auto* repeating_task_ptr = repeating_task.get(); |
| 100 | task_queue->PostTask(std::move(repeating_task)); |
| 101 | return RepeatingTaskHandle(repeating_task_ptr); |
| 102 | } |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 103 | |
| 104 | // DelayedStart is equivalent to Start except that the first invocation of the |
| 105 | // closure will be delayed by the given amount. |
| 106 | template <class Closure> |
Danil Chapovalov | 4423c36 | 2019-03-06 18:41:39 +0100 | [diff] [blame] | 107 | static RepeatingTaskHandle DelayedStart(TaskQueueBase* task_queue, |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 108 | TimeDelta first_delay, |
| 109 | Closure&& closure) { |
| 110 | auto repeating_task = absl::make_unique< |
| 111 | webrtc_repeating_task_impl::RepeatingTaskImpl<Closure>>( |
| 112 | task_queue, first_delay, std::forward<Closure>(closure)); |
| 113 | auto* repeating_task_ptr = repeating_task.get(); |
| 114 | task_queue->PostDelayedTask(std::move(repeating_task), first_delay.ms()); |
| 115 | return RepeatingTaskHandle(repeating_task_ptr); |
| 116 | } |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 117 | |
| 118 | // Stops future invocations of the repeating task closure. Can only be called |
| 119 | // from the TaskQueue where the task is running. The closure is guaranteed to |
| 120 | // not be running after Stop() returns unless Stop() is called from the |
| 121 | // closure itself. |
| 122 | void Stop(); |
| 123 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 124 | // Returns true if Start() or DelayedStart() was called most recently. Returns |
| 125 | // false initially and if Stop() or PostStop() was called most recently. |
| 126 | bool Running() const; |
| 127 | |
| 128 | private: |
| 129 | explicit RepeatingTaskHandle( |
| 130 | webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 131 | // Owned by the task queue. |
Sebastian Jansson | 46b4a0f | 2019-03-26 15:24:23 +0100 | [diff] [blame] | 132 | webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task_ = nullptr; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | } // namespace webrtc |
| 136 | #endif // RTC_BASE_TASK_UTILS_REPEATING_TASK_H_ |