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/cancelable_callback.h>
20 #include <base/functional/bind.h>
21 #include <base/location.h>
22 
23 #include <chrono>
24 #include <future>
25 
26 #include "time_util.h"
27 
28 namespace bluetooth {
29 
30 namespace common {
31 
32 class MessageLoopThread;
33 
34 /**
35  * An alarm clock that posts a delayed task to a specified MessageLoopThread
36  * periodically.
37  *
38  * Warning: MessageLoopThread must be running when any task is scheduled or
39  * being executed
40  */
41 class RepeatingTimer final {
42  public:
43   RepeatingTimer(uint64_t (*clock_tick_us)(void) =
44                      bluetooth::common::time_get_os_boottime_us)
45       : expected_time_next_task_us_(0), clock_tick_us_(clock_tick_us) {}
46   RepeatingTimer(const RepeatingTimer&) = delete;
47   RepeatingTimer& operator=(const RepeatingTimer&) = delete;
48 
49   ~RepeatingTimer();
50 
51   /**
52    * Schedule a delayed periodic task to the MessageLoopThread. Only one task
53    * can be scheduled at a time. If another task is scheduled, it will cancel
54    * the previous task synchronously and schedule the new periodic task; this
55    * blocks until the previous task is cancelled.
56    *
57    * @param thread thread to run the task
58    * @param from_here location where this task is originated
59    * @param task task created through base::Bind()
60    * @param period period for the task to be executed
61    * @return true iff task is scheduled successfully
62    */
63   bool SchedulePeriodic(const base::WeakPtr<MessageLoopThread>& thread,
64                         const base::Location& from_here,
65                         base::RepeatingClosure task,
66                         std::chrono::microseconds period);
67 
68   /**
69    * Post an event which cancels the current task asynchronously
70    */
71   void Cancel();
72 
73   /**
74    * Post an event which cancels the current task and wait for the cancellation
75    * to be completed
76    */
77   void CancelAndWait();
78 
79   /**
80    * Returns true when there is a pending task scheduled on a running thread,
81    * otherwise false.
82    */
83   bool IsScheduled() const;
84 
85  private:
86   base::WeakPtr<MessageLoopThread> message_loop_thread_;
87   base::CancelableClosure task_wrapper_;
88   base::RepeatingClosure task_;
89   std::chrono::microseconds period_;
90   uint64_t expected_time_next_task_us_;
91   uint64_t (*clock_tick_us_)(void);
92   mutable std::recursive_mutex api_mutex_;
93   void CancelHelper(std::promise<void> promise);
94   void CancelClosure(std::promise<void> promise);
95 
96   void RunTask();
97 };
98 
99 }  // namespace common
100 
101 }  // namespace bluetooth
102