• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <memory>
15 #include <type_traits>
16 #include <utility>
17 
18 #include "absl/functional/any_invocable.h"
19 #include "api/task_queue/pending_task_safety_flag.h"
20 #include "api/task_queue/task_queue_base.h"
21 #include "api/units/time_delta.h"
22 #include "system_wrappers/include/clock.h"
23 
24 namespace webrtc {
25 
26 namespace webrtc_repeating_task_impl {
27 
28 // Methods simplifying external tracing of RepeatingTaskHandle operations.
29 void RepeatingTaskHandleDTraceProbeStart();
30 void RepeatingTaskHandleDTraceProbeDelayedStart();
31 void RepeatingTaskImplDTraceProbeRun();
32 
33 }  // 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.
40 class RepeatingTaskHandle {
41  public:
42   RepeatingTaskHandle() = default;
43   ~RepeatingTaskHandle() = default;
44   RepeatingTaskHandle(RepeatingTaskHandle&& other) = default;
45   RepeatingTaskHandle& operator=(RepeatingTaskHandle&& other) = default;
46   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
52   // 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.
54   // The tasks are scheduled onto the task queue using the specified precision.
55   static RepeatingTaskHandle Start(TaskQueueBase* task_queue,
56                                    absl::AnyInvocable<TimeDelta()> closure,
57                                    TaskQueueBase::DelayPrecision precision =
58                                        TaskQueueBase::DelayPrecision::kLow,
59                                    Clock* clock = Clock::GetRealTimeClock());
60 
61   // DelayedStart is equivalent to Start except that the first invocation of the
62   // closure will be delayed by the given amount.
63   static RepeatingTaskHandle DelayedStart(
64       TaskQueueBase* task_queue,
65       TimeDelta first_delay,
66       absl::AnyInvocable<TimeDelta()> closure,
67       TaskQueueBase::DelayPrecision precision =
68           TaskQueueBase::DelayPrecision::kLow,
69       Clock* clock = Clock::GetRealTimeClock());
70 
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 
77   // Returns true until Stop() was called.
78   // Can only be called from the TaskQueue where the task is running.
79   bool Running() const;
80 
81  private:
RepeatingTaskHandle(rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag)82   explicit RepeatingTaskHandle(
83       rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag)
84       : repeating_task_(std::move(alive_flag)) {}
85   rtc::scoped_refptr<PendingTaskSafetyFlag> repeating_task_;
86 };
87 
88 }  // namespace webrtc
89 #endif  // RTC_BASE_TASK_UTILS_REPEATING_TASK_H_
90