blob: f7ae55ee97b85cf2a8e0de9bb326dd57a7e8e8cd [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"
Sebastian Janssonecb68972019-01-18 10:30:54 +010022
23namespace webrtc {
24
25class RepeatingTaskHandle;
26
27namespace webrtc_repeating_task_impl {
Danil Chapovalov4423c362019-03-06 18:41:39 +010028class RepeatingTaskBase : public QueuedTask {
Sebastian Janssonecb68972019-01-18 10:30:54 +010029 public:
Danil Chapovalov4423c362019-03-06 18:41:39 +010030 RepeatingTaskBase(TaskQueueBase* task_queue, TimeDelta first_delay);
Sebastian Janssonecb68972019-01-18 10:30:54 +010031 ~RepeatingTaskBase() override;
Tommia0a44802020-05-13 18:27:26 +020032
33 void Stop();
Sebastian Janssonecb68972019-01-18 10:30:54 +010034
35 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_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010041 // This is always finite, except for the special case where it's PlusInfinity
42 // to signal that the task should stop.
Tommi29a5fe82020-05-15 10:12:36 +020043 Timestamp next_run_time_ RTC_GUARDED_BY(task_queue_);
Sebastian Janssonecb68972019-01-18 10:30:54 +010044};
45
46// The template closure pattern is based on rtc::ClosureTask.
47template <class Closure>
48class RepeatingTaskImpl final : public RepeatingTaskBase {
49 public:
Danil Chapovalov4423c362019-03-06 18:41:39 +010050 RepeatingTaskImpl(TaskQueueBase* task_queue,
Sebastian Janssonecb68972019-01-18 10:30:54 +010051 TimeDelta first_delay,
52 Closure&& closure)
53 : RepeatingTaskBase(task_queue, first_delay),
54 closure_(std::forward<Closure>(closure)) {
55 static_assert(
56 std::is_same<TimeDelta,
57 typename std::result_of<decltype (&Closure::operator())(
58 Closure)>::type>::value,
59 "");
60 }
61
Tommia0a44802020-05-13 18:27:26 +020062 private:
Sebastian Janssonecb68972019-01-18 10:30:54 +010063 TimeDelta RunClosure() override { return closure_(); }
64
Sebastian Janssonecb68972019-01-18 10:30:54 +010065 typename std::remove_const<
66 typename std::remove_reference<Closure>::type>::type closure_;
67};
68} // namespace webrtc_repeating_task_impl
69
70// Allows starting tasks that repeat themselves on a TaskQueue indefinately
71// until they are stopped or the TaskQueue is destroyed. It allows starting and
72// stopping multiple times, but you must stop one task before starting another
73// and it can only be stopped when in the running state. The public interface is
74// not thread safe.
75class RepeatingTaskHandle {
76 public:
Sebastian Jansson46b4a0f2019-03-26 15:24:23 +010077 RepeatingTaskHandle() = default;
78 ~RepeatingTaskHandle() = default;
Sebastian Janssonecb68972019-01-18 10:30:54 +010079 RepeatingTaskHandle(RepeatingTaskHandle&& other);
80 RepeatingTaskHandle& operator=(RepeatingTaskHandle&& other);
81 RepeatingTaskHandle(const RepeatingTaskHandle&) = delete;
82 RepeatingTaskHandle& operator=(const RepeatingTaskHandle&) = delete;
83
84 // Start can be used to start a task that will be reposted with a delay
85 // determined by the return value of the provided closure. The actual task is
86 // owned by the TaskQueue and will live until it has been stopped or the
87 // TaskQueue is destroyed. Note that this means that trying to stop the
88 // repeating task after the TaskQueue is destroyed is an error. However, it's
89 // perfectly fine to destroy the handle while the task is running, since the
90 // repeated task is owned by the TaskQueue.
91 template <class Closure>
Danil Chapovalov4423c362019-03-06 18:41:39 +010092 static RepeatingTaskHandle Start(TaskQueueBase* task_queue,
Sebastian Janssonecb68972019-01-18 10:30:54 +010093 Closure&& closure) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020094 auto repeating_task = std::make_unique<
Sebastian Janssonecb68972019-01-18 10:30:54 +010095 webrtc_repeating_task_impl::RepeatingTaskImpl<Closure>>(
96 task_queue, TimeDelta::Zero(), std::forward<Closure>(closure));
97 auto* repeating_task_ptr = repeating_task.get();
98 task_queue->PostTask(std::move(repeating_task));
99 return RepeatingTaskHandle(repeating_task_ptr);
100 }
Sebastian Janssonecb68972019-01-18 10:30:54 +0100101
102 // DelayedStart is equivalent to Start except that the first invocation of the
103 // closure will be delayed by the given amount.
104 template <class Closure>
Danil Chapovalov4423c362019-03-06 18:41:39 +0100105 static RepeatingTaskHandle DelayedStart(TaskQueueBase* task_queue,
Sebastian Janssonecb68972019-01-18 10:30:54 +0100106 TimeDelta first_delay,
107 Closure&& closure) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200108 auto repeating_task = std::make_unique<
Sebastian Janssonecb68972019-01-18 10:30:54 +0100109 webrtc_repeating_task_impl::RepeatingTaskImpl<Closure>>(
110 task_queue, first_delay, std::forward<Closure>(closure));
111 auto* repeating_task_ptr = repeating_task.get();
112 task_queue->PostDelayedTask(std::move(repeating_task), first_delay.ms());
113 return RepeatingTaskHandle(repeating_task_ptr);
114 }
Sebastian Janssonecb68972019-01-18 10:30:54 +0100115
116 // Stops future invocations of the repeating task closure. Can only be called
117 // from the TaskQueue where the task is running. The closure is guaranteed to
118 // not be running after Stop() returns unless Stop() is called from the
119 // closure itself.
120 void Stop();
121
Sebastian Janssonecb68972019-01-18 10:30:54 +0100122 // Returns true if Start() or DelayedStart() was called most recently. Returns
123 // false initially and if Stop() or PostStop() was called most recently.
124 bool Running() const;
125
126 private:
127 explicit RepeatingTaskHandle(
128 webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task);
Sebastian Janssonecb68972019-01-18 10:30:54 +0100129 // Owned by the task queue.
Sebastian Jansson46b4a0f2019-03-26 15:24:23 +0100130 webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task_ = nullptr;
Sebastian Janssonecb68972019-01-18 10:30:54 +0100131};
132
133} // namespace webrtc
134#endif // RTC_BASE_TASK_UTILS_REPEATING_TASK_H_