blob: 21ec1301432e13b4240c728c155502d2a8e88308 [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"
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"
22#include "rtc_base/sequenced_task_checker.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010023#include "rtc_base/thread_checker.h"
24
25namespace webrtc {
26
27class RepeatingTaskHandle;
28
29namespace webrtc_repeating_task_impl {
Danil Chapovalov4423c362019-03-06 18:41:39 +010030class RepeatingTaskBase : public QueuedTask {
Sebastian Janssonecb68972019-01-18 10:30:54 +010031 public:
Danil Chapovalov4423c362019-03-06 18:41:39 +010032 RepeatingTaskBase(TaskQueueBase* task_queue, TimeDelta first_delay);
Sebastian Janssonecb68972019-01-18 10:30:54 +010033 ~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 Janssonecb68972019-01-18 10:30:54 +010041
Danil Chapovalov4423c362019-03-06 18:41:39 +010042 TaskQueueBase* const task_queue_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010043 // 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:
Danil Chapovalov4423c362019-03-06 18:41:39 +010052 RepeatingTaskImpl(TaskQueueBase* task_queue,
Sebastian Janssonecb68972019-01-18 10:30:54 +010053 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:
Sebastian Jansson46b4a0f2019-03-26 15:24:23 +010079 RepeatingTaskHandle() = default;
80 ~RepeatingTaskHandle() = default;
Sebastian Janssonecb68972019-01-18 10:30:54 +010081 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 Chapovalov4423c362019-03-06 18:41:39 +010094 static RepeatingTaskHandle Start(TaskQueueBase* task_queue,
Sebastian Janssonecb68972019-01-18 10:30:54 +010095 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 Janssonecb68972019-01-18 10:30:54 +0100103
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 Chapovalov4423c362019-03-06 18:41:39 +0100107 static RepeatingTaskHandle DelayedStart(TaskQueueBase* task_queue,
Sebastian Janssonecb68972019-01-18 10:30:54 +0100108 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 Janssonecb68972019-01-18 10:30:54 +0100117
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 Janssonecb68972019-01-18 10:30:54 +0100124 // 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 Janssonecb68972019-01-18 10:30:54 +0100131 // Owned by the task queue.
Sebastian Jansson46b4a0f2019-03-26 15:24:23 +0100132 webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task_ = nullptr;
Sebastian Janssonecb68972019-01-18 10:30:54 +0100133};
134
135} // namespace webrtc
136#endif // RTC_BASE_TASK_UTILS_REPEATING_TASK_H_