blob: 487b7d19d466fdc6e2319fe60dba03cbbcaa3230 [file] [log] [blame]
Sebastian Janssonecb68972019-01-18 09:30:541/*
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 15:06:1814#include <memory>
Sebastian Janssonecb68972019-01-18 09:30:5415#include <type_traits>
16#include <utility>
17
Danil Chapovalov4423c362019-03-06 17:41:3918#include "api/task_queue/queued_task.h"
19#include "api/task_queue/task_queue_base.h"
Sebastian Janssonecb68972019-01-18 09:30:5420#include "api/units/time_delta.h"
21#include "api/units/timestamp.h"
Tommi532cac52020-05-18 12:53:4222#include "system_wrappers/include/clock.h"
Sebastian Janssonecb68972019-01-18 09:30:5423
24namespace webrtc {
25
26class RepeatingTaskHandle;
27
28namespace webrtc_repeating_task_impl {
Danil Chapovalov4423c362019-03-06 17:41:3929class RepeatingTaskBase : public QueuedTask {
Sebastian Janssonecb68972019-01-18 09:30:5430 public:
Tommi532cac52020-05-18 12:53:4231 RepeatingTaskBase(TaskQueueBase* task_queue,
32 TimeDelta first_delay,
33 Clock* clock);
Sebastian Janssonecb68972019-01-18 09:30:5434 ~RepeatingTaskBase() override;
Tommia0a44802020-05-13 16:27:2635
36 void Stop();
Sebastian Janssonecb68972019-01-18 09:30:5437
38 private:
Tommia0a44802020-05-13 16:27:2639 virtual TimeDelta RunClosure() = 0;
Sebastian Janssonecb68972019-01-18 09:30:5440
41 bool Run() final;
Sebastian Janssonecb68972019-01-18 09:30:5442
Danil Chapovalov4423c362019-03-06 17:41:3943 TaskQueueBase* const task_queue_;
Tommi532cac52020-05-18 12:53:4244 Clock* const clock_;
Sebastian Janssonecb68972019-01-18 09:30:5445 // This is always finite, except for the special case where it's PlusInfinity
46 // to signal that the task should stop.
Tommi29a5fe82020-05-15 08:12:3647 Timestamp next_run_time_ RTC_GUARDED_BY(task_queue_);
Sebastian Janssonecb68972019-01-18 09:30:5448};
49
50// The template closure pattern is based on rtc::ClosureTask.
51template <class Closure>
52class RepeatingTaskImpl final : public RepeatingTaskBase {
53 public:
Danil Chapovalov4423c362019-03-06 17:41:3954 RepeatingTaskImpl(TaskQueueBase* task_queue,
Sebastian Janssonecb68972019-01-18 09:30:5455 TimeDelta first_delay,
Tommi532cac52020-05-18 12:53:4256 Closure&& closure,
57 Clock* clock)
58 : RepeatingTaskBase(task_queue, first_delay, clock),
Sebastian Janssonecb68972019-01-18 09:30:5459 closure_(std::forward<Closure>(closure)) {
60 static_assert(
61 std::is_same<TimeDelta,
62 typename std::result_of<decltype (&Closure::operator())(
63 Closure)>::type>::value,
64 "");
65 }
66
Tommia0a44802020-05-13 16:27:2667 private:
Sebastian Janssonecb68972019-01-18 09:30:5468 TimeDelta RunClosure() override { return closure_(); }
69
Sebastian Janssonecb68972019-01-18 09:30:5470 typename std::remove_const<
71 typename std::remove_reference<Closure>::type>::type closure_;
72};
73} // namespace webrtc_repeating_task_impl
74
75// Allows starting tasks that repeat themselves on a TaskQueue indefinately
76// until they are stopped or the TaskQueue is destroyed. It allows starting and
77// stopping multiple times, but you must stop one task before starting another
78// and it can only be stopped when in the running state. The public interface is
79// not thread safe.
80class RepeatingTaskHandle {
81 public:
Sebastian Jansson46b4a0f2019-03-26 14:24:2382 RepeatingTaskHandle() = default;
83 ~RepeatingTaskHandle() = default;
Sebastian Janssonecb68972019-01-18 09:30:5484 RepeatingTaskHandle(RepeatingTaskHandle&& other);
85 RepeatingTaskHandle& operator=(RepeatingTaskHandle&& other);
86 RepeatingTaskHandle(const RepeatingTaskHandle&) = delete;
87 RepeatingTaskHandle& operator=(const RepeatingTaskHandle&) = delete;
88
89 // Start can be used to start a task that will be reposted with a delay
90 // determined by the return value of the provided closure. The actual task is
91 // owned by the TaskQueue and will live until it has been stopped or the
92 // TaskQueue is destroyed. Note that this means that trying to stop the
93 // repeating task after the TaskQueue is destroyed is an error. However, it's
94 // perfectly fine to destroy the handle while the task is running, since the
95 // repeated task is owned by the TaskQueue.
96 template <class Closure>
Danil Chapovalov4423c362019-03-06 17:41:3997 static RepeatingTaskHandle Start(TaskQueueBase* task_queue,
Tommi532cac52020-05-18 12:53:4298 Closure&& closure,
99 Clock* clock = Clock::GetRealTimeClock()) {
Mirko Bonadei317a1f02019-09-17 15:06:18100 auto repeating_task = std::make_unique<
Sebastian Janssonecb68972019-01-18 09:30:54101 webrtc_repeating_task_impl::RepeatingTaskImpl<Closure>>(
Tommi532cac52020-05-18 12:53:42102 task_queue, TimeDelta::Zero(), std::forward<Closure>(closure), clock);
Sebastian Janssonecb68972019-01-18 09:30:54103 auto* repeating_task_ptr = repeating_task.get();
104 task_queue->PostTask(std::move(repeating_task));
105 return RepeatingTaskHandle(repeating_task_ptr);
106 }
Sebastian Janssonecb68972019-01-18 09:30:54107
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>
Tommi532cac52020-05-18 12:53:42111 static RepeatingTaskHandle DelayedStart(
112 TaskQueueBase* task_queue,
113 TimeDelta first_delay,
114 Closure&& closure,
115 Clock* clock = Clock::GetRealTimeClock()) {
Mirko Bonadei317a1f02019-09-17 15:06:18116 auto repeating_task = std::make_unique<
Sebastian Janssonecb68972019-01-18 09:30:54117 webrtc_repeating_task_impl::RepeatingTaskImpl<Closure>>(
Tommi532cac52020-05-18 12:53:42118 task_queue, first_delay, std::forward<Closure>(closure), clock);
Sebastian Janssonecb68972019-01-18 09:30:54119 auto* repeating_task_ptr = repeating_task.get();
120 task_queue->PostDelayedTask(std::move(repeating_task), first_delay.ms());
121 return RepeatingTaskHandle(repeating_task_ptr);
122 }
Sebastian Janssonecb68972019-01-18 09:30:54123
124 // Stops future invocations of the repeating task closure. Can only be called
125 // from the TaskQueue where the task is running. The closure is guaranteed to
126 // not be running after Stop() returns unless Stop() is called from the
127 // closure itself.
128 void Stop();
129
Sebastian Janssonecb68972019-01-18 09:30:54130 // Returns true if Start() or DelayedStart() was called most recently. Returns
131 // false initially and if Stop() or PostStop() was called most recently.
132 bool Running() const;
133
134 private:
135 explicit RepeatingTaskHandle(
136 webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task);
Sebastian Janssonecb68972019-01-18 09:30:54137 // Owned by the task queue.
Sebastian Jansson46b4a0f2019-03-26 14:24:23138 webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task_ = nullptr;
Sebastian Janssonecb68972019-01-18 09:30:54139};
140
141} // namespace webrtc
142#endif // RTC_BASE_TASK_UTILS_REPEATING_TASK_H_