blob: e5ea3d8174854a9d6098372de80252a00337b536 [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 Chapovalov4b979282022-06-30 10:08:47 +020018#include "absl/functional/any_invocable.h"
Artem Titovc374d112022-06-16 21:27:45 +020019#include "api/task_queue/pending_task_safety_flag.h"
Danil Chapovalov4423c362019-03-06 18:41:39 +010020#include "api/task_queue/task_queue_base.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010021#include "api/units/time_delta.h"
Tommi532cac52020-05-18 14:53:42 +020022#include "system_wrappers/include/clock.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010023
24namespace webrtc {
Markus Handellc1c6bef2021-07-23 11:11:32 +020025
Sebastian Janssonecb68972019-01-18 10:30:54 +010026namespace webrtc_repeating_task_impl {
Markus Handellc1c6bef2021-07-23 11:11:32 +020027
28// Methods simplifying external tracing of RepeatingTaskHandle operations.
29void RepeatingTaskHandleDTraceProbeStart();
30void RepeatingTaskHandleDTraceProbeDelayedStart();
31void RepeatingTaskImplDTraceProbeRun();
32
Sebastian Janssonecb68972019-01-18 10:30:54 +010033} // namespace webrtc_repeating_task_impl
34
35// Allows starting tasks that repeat themselves on a TaskQueue indefinately
36// until they are stopped or the TaskQueue is destroyed. It allows starting and
37// stopping multiple times, but you must stop one task before starting another
38// and it can only be stopped when in the running state. The public interface is
39// not thread safe.
40class RepeatingTaskHandle {
41 public:
Sebastian Jansson46b4a0f2019-03-26 15:24:23 +010042 RepeatingTaskHandle() = default;
43 ~RepeatingTaskHandle() = default;
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020044 RepeatingTaskHandle(RepeatingTaskHandle&& other) = default;
45 RepeatingTaskHandle& operator=(RepeatingTaskHandle&& other) = default;
Sebastian Janssonecb68972019-01-18 10:30:54 +010046 RepeatingTaskHandle(const RepeatingTaskHandle&) = delete;
47 RepeatingTaskHandle& operator=(const RepeatingTaskHandle&) = delete;
48
49 // Start can be used to start a task that will be reposted with a delay
50 // determined by the return value of the provided closure. The actual task is
51 // owned by the TaskQueue and will live until it has been stopped or the
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020052 // TaskQueue deletes it. It's perfectly fine to destroy the handle while the
53 // task is running, since the repeated task is owned by the TaskQueue.
Henrik Boström27e8a092022-01-24 17:12:35 +010054 // The tasks are scheduled onto the task queue using the specified precision.
Danil Chapovalov4423c362019-03-06 18:41:39 +010055 static RepeatingTaskHandle Start(TaskQueueBase* task_queue,
Danil Chapovalov4b979282022-06-30 10:08:47 +020056 absl::AnyInvocable<TimeDelta()> closure,
Henrik Boström27e8a092022-01-24 17:12:35 +010057 TaskQueueBase::DelayPrecision precision =
58 TaskQueueBase::DelayPrecision::kLow,
Danil Chapovalov4b979282022-06-30 10:08:47 +020059 Clock* clock = Clock::GetRealTimeClock());
Sebastian Janssonecb68972019-01-18 10:30:54 +010060
61 // DelayedStart is equivalent to Start except that the first invocation of the
62 // closure will be delayed by the given amount.
Tommi532cac52020-05-18 14:53:42 +020063 static RepeatingTaskHandle DelayedStart(
64 TaskQueueBase* task_queue,
65 TimeDelta first_delay,
Danil Chapovalov4b979282022-06-30 10:08:47 +020066 absl::AnyInvocable<TimeDelta()> closure,
Henrik Boström27e8a092022-01-24 17:12:35 +010067 TaskQueueBase::DelayPrecision precision =
68 TaskQueueBase::DelayPrecision::kLow,
Danil Chapovalov4b979282022-06-30 10:08:47 +020069 Clock* clock = Clock::GetRealTimeClock());
Sebastian Janssonecb68972019-01-18 10:30:54 +010070
71 // Stops future invocations of the repeating task closure. Can only be called
72 // from the TaskQueue where the task is running. The closure is guaranteed to
73 // not be running after Stop() returns unless Stop() is called from the
74 // closure itself.
75 void Stop();
76
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020077 // Returns true until Stop() was called.
78 // Can only be called from the TaskQueue where the task is running.
Sebastian Janssonecb68972019-01-18 10:30:54 +010079 bool Running() const;
80
81 private:
82 explicit RepeatingTaskHandle(
Danil Chapovalov0f9a8e32021-06-11 18:39:17 +020083 rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag)
84 : repeating_task_(std::move(alive_flag)) {}
85 rtc::scoped_refptr<PendingTaskSafetyFlag> repeating_task_;
Sebastian Janssonecb68972019-01-18 10:30:54 +010086};
87
88} // namespace webrtc
89#endif // RTC_BASE_TASK_UTILS_REPEATING_TASK_H_