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) = bluetooth::common::time_get_os_boottime_us)
44       : expected_time_next_task_us_(0), clock_tick_us_(clock_tick_us) {}
45   RepeatingTimer(const RepeatingTimer&) = delete;
46   RepeatingTimer& operator=(const RepeatingTimer&) = delete;
47 
48   ~RepeatingTimer();
49 
50   /**
51    * Schedule a delayed periodic task to the MessageLoopThread. Only one task
52    * can be scheduled at a time. If another task is scheduled, it will cancel
53    * the previous task synchronously and schedule the new periodic task; this
54    * blocks until the previous task is cancelled.
55    *
56    * @param thread thread to run the task
57    * @param from_here location where this task is originated
58    * @param task task created through base::Bind()
59    * @param period period for the task to be executed
60    * @return true iff task is scheduled successfully
61    */
62   bool SchedulePeriodic(const base::WeakPtr<MessageLoopThread>& thread,
63                         const base::Location& from_here, base::RepeatingClosure task,
64                         std::chrono::microseconds period);
65 
66   /**
67    * Post an event which cancels the current task asynchronously
68    */
69   void Cancel();
70 
71   /**
72    * Post an event which cancels the current task and wait for the cancellation
73    * to be completed
74    */
75   void CancelAndWait();
76 
77   /**
78    * Returns true when there is a pending task scheduled on a running thread,
79    * otherwise false.
80    */
81   bool IsScheduled() const;
82 
83 private:
84   base::WeakPtr<MessageLoopThread> message_loop_thread_;
85   base::CancelableClosure task_wrapper_;
86   base::RepeatingClosure task_;
87   std::chrono::microseconds period_;
88   uint64_t expected_time_next_task_us_;
89   uint64_t (*clock_tick_us_)(void);
90   mutable std::recursive_mutex api_mutex_;
91   void CancelHelper(std::promise<void> promise);
92   void CancelClosure(std::promise<void> promise);
93 
94   void RunTask();
95 };
96 
97 }  // namespace common
98 
99 }  // namespace bluetooth
100