• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <base/bind.h>
20 #include <base/cancelable_callback.h>
21 #include <base/location.h>
22 #include <base/time/time.h>
23 
24 #include <future>
25 
26 namespace bluetooth {
27 
28 namespace common {
29 
30 class MessageLoopThread;
31 
32 /**
33  * An alarm clock that posts a delayed task to a specified MessageLoopThread
34  * periodically.
35  *
36  * Warning: MessageLoopThread must be running when any task is scheduled or
37  * being executed
38  */
39 class RepeatingTimer final {
40  public:
RepeatingTimer()41   RepeatingTimer() : expected_time_next_task_us_(0) {}
42   RepeatingTimer(const RepeatingTimer&) = delete;
43   RepeatingTimer& operator=(const RepeatingTimer&) = delete;
44 
45   ~RepeatingTimer();
46 
47   /**
48    * Schedule a delayed periodic task to the MessageLoopThread. Only one task
49    * can be scheduled at a time. If another task is scheduled, it will cancel
50    * the previous task synchronously and schedule the new periodic task; this
51    * blocks until the previous task is cancelled.
52    *
53    * @param thread thread to run the task
54    * @param from_here location where this task is originated
55    * @param task task created through base::Bind()
56    * @param period period for the task to be executed
57    * @return true iff task is scheduled successfully
58    */
59   bool SchedulePeriodic(const base::WeakPtr<MessageLoopThread>& thread,
60                         const base::Location& from_here,
61                         base::RepeatingClosure task, base::TimeDelta period);
62 
63   /**
64    * Post an event which cancels the current task asynchronously
65    */
66   void Cancel();
67 
68   /**
69    * Post an event which cancels the current task and wait for the cancellation
70    * to be completed
71    */
72   void CancelAndWait();
73 
74   /**
75    * Returns true when there is a pending task scheduled on a running thread,
76    * otherwise false.
77    */
78   bool IsScheduled() const;
79 
80  private:
81   base::WeakPtr<MessageLoopThread> message_loop_thread_;
82   base::CancelableClosure task_wrapper_;
83   base::RepeatingClosure task_;
84   base::TimeDelta period_;
85   uint64_t expected_time_next_task_us_;  // Using clock boot time in time_util.h
86   mutable std::recursive_mutex api_mutex_;
87   void CancelHelper(std::promise<void> promise);
88   void CancelClosure(std::promise<void> promise);
89 
90   void RunTask();
91 };
92 
93 }  // namespace common
94 
95 }  // namespace bluetooth
96