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 task task created through base::Bind() 58 * @param period period for the task to be executed 59 * @return true iff task is scheduled successfully 60 */ 61 bool SchedulePeriodic(const base::WeakPtr<MessageLoopThread>& thread, base::RepeatingClosure task, 62 std::chrono::microseconds period); 63 64 /** 65 * Post an event which cancels the current task asynchronously 66 */ 67 void Cancel(); 68 69 /** 70 * Post an event which cancels the current task and wait for the cancellation 71 * to be completed 72 */ 73 void CancelAndWait(); 74 75 /** 76 * Returns true when there is a pending task scheduled on a running thread, 77 * otherwise false. 78 */ 79 bool IsScheduled() const; 80 81 private: 82 base::WeakPtr<MessageLoopThread> message_loop_thread_; 83 base::CancelableClosure task_wrapper_; 84 base::RepeatingClosure task_; 85 std::chrono::microseconds period_; 86 uint64_t expected_time_next_task_us_; 87 uint64_t (*clock_tick_us_)(void); 88 mutable std::recursive_mutex api_mutex_; 89 void CancelHelper(std::promise<void> promise); 90 void CancelClosure(std::promise<void> promise); 91 92 void RunTask(); 93 }; 94 95 } // namespace common 96 97 } // namespace bluetooth 98