blob: 75d03bfe5e6e33cdab68f7cf7093e2bf84496eed [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 Janssonb55015e2019-04-09 13:44:04 +020022#include "rtc_base/synchronization/sequence_checker.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010023
24namespace webrtc {
25
26class RepeatingTaskHandle;
27
28namespace webrtc_repeating_task_impl {
Danil Chapovalov4423c362019-03-06 18:41:39 +010029class RepeatingTaskBase : public QueuedTask {
Sebastian Janssonecb68972019-01-18 10:30:54 +010030 public:
Danil Chapovalov4423c362019-03-06 18:41:39 +010031 RepeatingTaskBase(TaskQueueBase* task_queue, TimeDelta first_delay);
Sebastian Janssonecb68972019-01-18 10:30:54 +010032 ~RepeatingTaskBase() override;
Tommia0a44802020-05-13 18:27:26 +020033
34 void Stop();
Sebastian Janssonecb68972019-01-18 10:30:54 +010035
36 private:
Tommia0a44802020-05-13 18:27:26 +020037 virtual TimeDelta RunClosure() = 0;
Sebastian Janssonecb68972019-01-18 10:30:54 +010038
39 bool Run() final;
Sebastian Janssonecb68972019-01-18 10:30:54 +010040
Danil Chapovalov4423c362019-03-06 18:41:39 +010041 TaskQueueBase* const task_queue_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010042 // This is always finite, except for the special case where it's PlusInfinity
43 // to signal that the task should stop.
Tommia0a44802020-05-13 18:27:26 +020044 Timestamp next_run_time_ RTC_GUARDED_BY(sequence_checker_);
45 // We use a SequenceChecker to check for correct usage instead of using
46 // RTC_DCHECK_RUN_ON(task_queue_). This is to work around a compatibility
47 // issue with some TQ implementations such as rtc::Thread that don't
48 // consistently set themselves as the 'current' TQ when running tasks.
49 // The SequenceChecker detects those implementations differently but gives
50 // the same effect as far as thread safety goes.
51 SequenceChecker sequence_checker_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010052};
53
54// The template closure pattern is based on rtc::ClosureTask.
55template <class Closure>
56class RepeatingTaskImpl final : public RepeatingTaskBase {
57 public:
Danil Chapovalov4423c362019-03-06 18:41:39 +010058 RepeatingTaskImpl(TaskQueueBase* task_queue,
Sebastian Janssonecb68972019-01-18 10:30:54 +010059 TimeDelta first_delay,
60 Closure&& closure)
61 : RepeatingTaskBase(task_queue, first_delay),
62 closure_(std::forward<Closure>(closure)) {
63 static_assert(
64 std::is_same<TimeDelta,
65 typename std::result_of<decltype (&Closure::operator())(
66 Closure)>::type>::value,
67 "");
68 }
69
Tommia0a44802020-05-13 18:27:26 +020070 private:
Sebastian Janssonecb68972019-01-18 10:30:54 +010071 TimeDelta RunClosure() override { return closure_(); }
72
Sebastian Janssonecb68972019-01-18 10:30:54 +010073 typename std::remove_const<
74 typename std::remove_reference<Closure>::type>::type closure_;
75};
76} // namespace webrtc_repeating_task_impl
77
78// Allows starting tasks that repeat themselves on a TaskQueue indefinately
79// until they are stopped or the TaskQueue is destroyed. It allows starting and
80// stopping multiple times, but you must stop one task before starting another
81// and it can only be stopped when in the running state. The public interface is
82// not thread safe.
83class RepeatingTaskHandle {
84 public:
Sebastian Jansson46b4a0f2019-03-26 15:24:23 +010085 RepeatingTaskHandle() = default;
86 ~RepeatingTaskHandle() = default;
Sebastian Janssonecb68972019-01-18 10:30:54 +010087 RepeatingTaskHandle(RepeatingTaskHandle&& other);
88 RepeatingTaskHandle& operator=(RepeatingTaskHandle&& other);
89 RepeatingTaskHandle(const RepeatingTaskHandle&) = delete;
90 RepeatingTaskHandle& operator=(const RepeatingTaskHandle&) = delete;
91
92 // Start can be used to start a task that will be reposted with a delay
93 // determined by the return value of the provided closure. The actual task is
94 // owned by the TaskQueue and will live until it has been stopped or the
95 // TaskQueue is destroyed. Note that this means that trying to stop the
96 // repeating task after the TaskQueue is destroyed is an error. However, it's
97 // perfectly fine to destroy the handle while the task is running, since the
98 // repeated task is owned by the TaskQueue.
99 template <class Closure>
Danil Chapovalov4423c362019-03-06 18:41:39 +0100100 static RepeatingTaskHandle Start(TaskQueueBase* task_queue,
Sebastian Janssonecb68972019-01-18 10:30:54 +0100101 Closure&& closure) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200102 auto repeating_task = std::make_unique<
Sebastian Janssonecb68972019-01-18 10:30:54 +0100103 webrtc_repeating_task_impl::RepeatingTaskImpl<Closure>>(
104 task_queue, TimeDelta::Zero(), std::forward<Closure>(closure));
105 auto* repeating_task_ptr = repeating_task.get();
106 task_queue->PostTask(std::move(repeating_task));
107 return RepeatingTaskHandle(repeating_task_ptr);
108 }
Sebastian Janssonecb68972019-01-18 10:30:54 +0100109
110 // DelayedStart is equivalent to Start except that the first invocation of the
111 // closure will be delayed by the given amount.
112 template <class Closure>
Danil Chapovalov4423c362019-03-06 18:41:39 +0100113 static RepeatingTaskHandle DelayedStart(TaskQueueBase* task_queue,
Sebastian Janssonecb68972019-01-18 10:30:54 +0100114 TimeDelta first_delay,
115 Closure&& closure) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200116 auto repeating_task = std::make_unique<
Sebastian Janssonecb68972019-01-18 10:30:54 +0100117 webrtc_repeating_task_impl::RepeatingTaskImpl<Closure>>(
118 task_queue, first_delay, std::forward<Closure>(closure));
119 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 10:30:54 +0100123
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 10:30:54 +0100130 // 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 10:30:54 +0100137 // Owned by the task queue.
Sebastian Jansson46b4a0f2019-03-26 15:24:23 +0100138 webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task_ = nullptr;
Sebastian Janssonecb68972019-01-18 10:30:54 +0100139};
140
141} // namespace webrtc
142#endif // RTC_BASE_TASK_UTILS_REPEATING_TASK_H_