blob: c4e760a55d5ee1adce0b5dba524728733ed55782 [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
14#include <type_traits>
15#include <utility>
16
17#include "absl/memory/memory.h"
18#include "api/units/time_delta.h"
19#include "api/units/timestamp.h"
20#include "rtc_base/sequenced_task_checker.h"
21#include "rtc_base/task_queue.h"
22#include "rtc_base/thread_checker.h"
23
24namespace webrtc {
25
26class RepeatingTaskHandle;
27
28namespace webrtc_repeating_task_impl {
29class RepeatingTaskBase : public rtc::QueuedTask {
30 public:
31 RepeatingTaskBase(rtc::TaskQueue* task_queue, TimeDelta first_delay);
32 ~RepeatingTaskBase() override;
33 virtual TimeDelta RunClosure() = 0;
34
35 private:
36 friend class ::webrtc::RepeatingTaskHandle;
37
38 bool Run() final;
39 void Stop() RTC_RUN_ON(task_queue_);
40 void PostStop();
41
42 rtc::TaskQueue* const task_queue_;
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.
49template <class Closure>
50class RepeatingTaskImpl final : public RepeatingTaskBase {
51 public:
52 RepeatingTaskImpl(rtc::TaskQueue* task_queue,
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.
77class RepeatingTaskHandle {
78 public:
79 RepeatingTaskHandle();
80 ~RepeatingTaskHandle();
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>
94 static RepeatingTaskHandle Start(rtc::TaskQueue* task_queue,
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 }
103 template <class Closure>
104 static RepeatingTaskHandle Start(Closure&& closure) {
105 return Start(rtc::TaskQueue::Current(), std::forward<Closure>(closure));
106 }
107
108 // DelayedStart is equivalent to Start except that the first invocation of the
109 // closure will be delayed by the given amount.
110 template <class Closure>
111 static RepeatingTaskHandle DelayedStart(rtc::TaskQueue* task_queue,
112 TimeDelta first_delay,
113 Closure&& closure) {
114 auto repeating_task = absl::make_unique<
115 webrtc_repeating_task_impl::RepeatingTaskImpl<Closure>>(
116 task_queue, first_delay, std::forward<Closure>(closure));
117 auto* repeating_task_ptr = repeating_task.get();
118 task_queue->PostDelayedTask(std::move(repeating_task), first_delay.ms());
119 return RepeatingTaskHandle(repeating_task_ptr);
120 }
121 template <class Closure>
122 static RepeatingTaskHandle DelayedStart(TimeDelta first_delay,
123 Closure&& closure) {
124 return DelayedStart(rtc::TaskQueue::Current(), first_delay,
125 std::forward<Closure>(closure));
126 }
127
128 // Stops future invocations of the repeating task closure. Can only be called
129 // from the TaskQueue where the task is running. The closure is guaranteed to
130 // not be running after Stop() returns unless Stop() is called from the
131 // closure itself.
132 void Stop();
133
134 // Stops future invocations of the repeating task closure. The closure might
135 // still be running when PostStop() returns, but there will be no future
136 // invocation.
137 void PostStop();
138
139 // Returns true if Start() or DelayedStart() was called most recently. Returns
140 // false initially and if Stop() or PostStop() was called most recently.
141 bool Running() const;
142
143 private:
144 explicit RepeatingTaskHandle(
145 webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task);
146 rtc::SequencedTaskChecker sequence_checker_;
147 // Owned by the task queue.
148 webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task_
149 RTC_GUARDED_BY(sequence_checker_) = nullptr;
150};
151
152} // namespace webrtc
153#endif // RTC_BASE_TASK_UTILS_REPEATING_TASK_H_