blob: d5066fdb5ca2d04bcfcb5c82dca5cd0d5e4d6c35 [file] [log] [blame]
Sebastian Janssonecb68972019-01-18 10:30:54 +01001/*
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 Bonadei317a1f02019-09-17 17:06:18 +020014#include <memory>
Sebastian Janssonecb68972019-01-18 10:30:54 +010015#include <type_traits>
16#include <utility>
17
Danil Chapovalov4423c362019-03-06 18:41:39 +010018#include "api/task_queue/queued_task.h"
19#include "api/task_queue/task_queue_base.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010020#include "api/units/time_delta.h"
21#include "api/units/timestamp.h"
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020022#include "rtc_base/task_utils/pending_task_safety_flag.h"
Tommi532cac52020-05-18 14:53:42 +020023#include "system_wrappers/include/clock.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010024
25namespace webrtc {
Sebastian Janssonecb68972019-01-18 10:30:54 +010026namespace webrtc_repeating_task_impl {
Danil Chapovalov4423c362019-03-06 18:41:39 +010027class RepeatingTaskBase : public QueuedTask {
Sebastian Janssonecb68972019-01-18 10:30:54 +010028 public:
Tommi532cac52020-05-18 14:53:42 +020029 RepeatingTaskBase(TaskQueueBase* task_queue,
30 TimeDelta first_delay,
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020031 Clock* clock,
32 rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag);
Sebastian Janssonecb68972019-01-18 10:30:54 +010033 ~RepeatingTaskBase() override;
Tommia0a44802020-05-13 18:27:26 +020034
Sebastian Janssonecb68972019-01-18 10:30:54 +010035 private:
Tommia0a44802020-05-13 18:27:26 +020036 virtual TimeDelta RunClosure() = 0;
Sebastian Janssonecb68972019-01-18 10:30:54 +010037
38 bool Run() final;
Sebastian Janssonecb68972019-01-18 10:30:54 +010039
Danil Chapovalov4423c362019-03-06 18:41:39 +010040 TaskQueueBase* const task_queue_;
Tommi532cac52020-05-18 14:53:42 +020041 Clock* const clock_;
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020042 // This is always finite.
Tommi29a5fe82020-05-15 10:12:36 +020043 Timestamp next_run_time_ RTC_GUARDED_BY(task_queue_);
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020044 rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag_
45 RTC_GUARDED_BY(task_queue_);
Sebastian Janssonecb68972019-01-18 10:30:54 +010046};
47
48// The template closure pattern is based on rtc::ClosureTask.
49template <class Closure>
50class RepeatingTaskImpl final : public RepeatingTaskBase {
51 public:
Danil Chapovalov4423c362019-03-06 18:41:39 +010052 RepeatingTaskImpl(TaskQueueBase* task_queue,
Sebastian Janssonecb68972019-01-18 10:30:54 +010053 TimeDelta first_delay,
Tommi532cac52020-05-18 14:53:42 +020054 Closure&& closure,
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020055 Clock* clock,
56 rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag)
57 : RepeatingTaskBase(task_queue,
58 first_delay,
59 clock,
60 std::move(alive_flag)),
Sebastian Janssonecb68972019-01-18 10:30:54 +010061 closure_(std::forward<Closure>(closure)) {
62 static_assert(
63 std::is_same<TimeDelta,
64 typename std::result_of<decltype (&Closure::operator())(
65 Closure)>::type>::value,
66 "");
67 }
68
Tommia0a44802020-05-13 18:27:26 +020069 private:
Sebastian Janssonecb68972019-01-18 10:30:54 +010070 TimeDelta RunClosure() override { return closure_(); }
71
Sebastian Janssonecb68972019-01-18 10:30:54 +010072 typename std::remove_const<
73 typename std::remove_reference<Closure>::type>::type closure_;
74};
75} // namespace webrtc_repeating_task_impl
76
77// Allows starting tasks that repeat themselves on a TaskQueue indefinately
78// until they are stopped or the TaskQueue is destroyed. It allows starting and
79// stopping multiple times, but you must stop one task before starting another
80// and it can only be stopped when in the running state. The public interface is
81// not thread safe.
82class RepeatingTaskHandle {
83 public:
Sebastian Jansson46b4a0f2019-03-26 15:24:23 +010084 RepeatingTaskHandle() = default;
85 ~RepeatingTaskHandle() = default;
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020086 RepeatingTaskHandle(RepeatingTaskHandle&& other) = default;
87 RepeatingTaskHandle& operator=(RepeatingTaskHandle&& other) = default;
Sebastian Janssonecb68972019-01-18 10:30:54 +010088 RepeatingTaskHandle(const RepeatingTaskHandle&) = delete;
89 RepeatingTaskHandle& operator=(const RepeatingTaskHandle&) = delete;
90
91 // Start can be used to start a task that will be reposted with a delay
92 // determined by the return value of the provided closure. The actual task is
93 // owned by the TaskQueue and will live until it has been stopped or the
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020094 // TaskQueue deletes it. It's perfectly fine to destroy the handle while the
95 // task is running, since the repeated task is owned by the TaskQueue.
Sebastian Janssonecb68972019-01-18 10:30:54 +010096 template <class Closure>
Danil Chapovalov4423c362019-03-06 18:41:39 +010097 static RepeatingTaskHandle Start(TaskQueueBase* task_queue,
Tommi532cac52020-05-18 14:53:42 +020098 Closure&& closure,
99 Clock* clock = Clock::GetRealTimeClock()) {
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +0200100 auto alive_flag = PendingTaskSafetyFlag::CreateDetached();
101 task_queue->PostTask(
102 std::make_unique<
103 webrtc_repeating_task_impl::RepeatingTaskImpl<Closure>>(
104 task_queue, TimeDelta::Zero(), std::forward<Closure>(closure),
105 clock, alive_flag));
106 return RepeatingTaskHandle(std::move(alive_flag));
Sebastian Janssonecb68972019-01-18 10:30:54 +0100107 }
Sebastian Janssonecb68972019-01-18 10:30:54 +0100108
109 // DelayedStart is equivalent to Start except that the first invocation of the
110 // closure will be delayed by the given amount.
111 template <class Closure>
Tommi532cac52020-05-18 14:53:42 +0200112 static RepeatingTaskHandle DelayedStart(
113 TaskQueueBase* task_queue,
114 TimeDelta first_delay,
115 Closure&& closure,
116 Clock* clock = Clock::GetRealTimeClock()) {
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +0200117 auto alive_flag = PendingTaskSafetyFlag::CreateDetached();
118 task_queue->PostDelayedTask(
119 std::make_unique<
120 webrtc_repeating_task_impl::RepeatingTaskImpl<Closure>>(
121 task_queue, first_delay, std::forward<Closure>(closure), clock,
122 alive_flag),
123 first_delay.ms());
124 return RepeatingTaskHandle(std::move(alive_flag));
Sebastian Janssonecb68972019-01-18 10:30:54 +0100125 }
Sebastian Janssonecb68972019-01-18 10:30:54 +0100126
127 // Stops future invocations of the repeating task closure. Can only be called
128 // from the TaskQueue where the task is running. The closure is guaranteed to
129 // not be running after Stop() returns unless Stop() is called from the
130 // closure itself.
131 void Stop();
132
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +0200133 // Returns true until Stop() was called.
134 // Can only be called from the TaskQueue where the task is running.
Sebastian Janssonecb68972019-01-18 10:30:54 +0100135 bool Running() const;
136
137 private:
138 explicit RepeatingTaskHandle(
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +0200139 rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag)
140 : repeating_task_(std::move(alive_flag)) {}
141 rtc::scoped_refptr<PendingTaskSafetyFlag> repeating_task_;
Sebastian Janssonecb68972019-01-18 10:30:54 +0100142};
143
144} // namespace webrtc
145#endif // RTC_BASE_TASK_UTILS_REPEATING_TASK_H_