Hansong Zhang | 438b08c | 2018-08-14 14:29:23 -0700 | [diff] [blame] | 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> |
Jakub Pawlowski | 67f5f37 | 2018-07-23 10:00:25 -0700 | [diff] [blame] | 21 | #include <base/location.h> |
Hansong Zhang | 438b08c | 2018-08-14 14:29:23 -0700 | [diff] [blame] | 22 | #include <future> |
| 23 | |
| 24 | namespace bluetooth { |
| 25 | |
| 26 | namespace common { |
| 27 | |
| 28 | class MessageLoopThread; |
| 29 | |
| 30 | /** |
| 31 | * An alarm clock that posts a delayed task to a specified MessageLoopThread |
Jakub Pawlowski | 5cf0304 | 2018-12-03 16:50:40 +0100 | [diff] [blame^] | 32 | * periodically. |
Hansong Zhang | 438b08c | 2018-08-14 14:29:23 -0700 | [diff] [blame] | 33 | * |
| 34 | * Warning: MessageLoopThread must be running when any task is scheduled or |
| 35 | * being executed |
| 36 | */ |
Jakub Pawlowski | 5cf0304 | 2018-12-03 16:50:40 +0100 | [diff] [blame^] | 37 | class RepeatingTimer final { |
Hansong Zhang | 438b08c | 2018-08-14 14:29:23 -0700 | [diff] [blame] | 38 | public: |
Jakub Pawlowski | 5cf0304 | 2018-12-03 16:50:40 +0100 | [diff] [blame^] | 39 | RepeatingTimer() : expected_time_next_task_us_(0) {} |
| 40 | ~RepeatingTimer(); |
Hansong Zhang | 438b08c | 2018-08-14 14:29:23 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Schedule a delayed periodic task to the MessageLoopThread. Only one task |
| 44 | * can be scheduled at a time. If another task is scheduled, it will cancel |
| 45 | * the previous task synchronously and schedule the new periodic task; this |
| 46 | * blocks until the previous task is cancelled. |
| 47 | * |
| 48 | * @param thread thread to run the task |
| 49 | * @param from_here location where this task is originated |
| 50 | * @param task task created through base::Bind() |
| 51 | * @param period period for the task to be executed |
| 52 | * @return true iff task is scheduled successfully |
| 53 | */ |
| 54 | bool SchedulePeriodic(const base::WeakPtr<MessageLoopThread>& thread, |
Jakub Pawlowski | 5cf0304 | 2018-12-03 16:50:40 +0100 | [diff] [blame^] | 55 | const base::Location& from_here, |
| 56 | base::RepeatingClosure task, base::TimeDelta period); |
Hansong Zhang | 438b08c | 2018-08-14 14:29:23 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Post an event which cancels the current task asynchronously |
| 60 | */ |
| 61 | void Cancel(); |
| 62 | |
| 63 | /** |
| 64 | * Post an event which cancels the current task and wait for the cancellation |
| 65 | * to be completed |
| 66 | */ |
| 67 | void CancelAndWait(); |
| 68 | |
| 69 | /** |
| 70 | * Returns true when there is a pending task scheduled on a running thread, |
| 71 | * otherwise false. |
| 72 | */ |
| 73 | bool IsScheduled() const; |
| 74 | |
| 75 | private: |
| 76 | base::WeakPtr<MessageLoopThread> message_loop_thread_; |
Hansong Zhang | 85ea763 | 2018-09-20 10:15:12 -0700 | [diff] [blame] | 77 | base::CancelableClosure task_wrapper_; |
Jakub Pawlowski | 5cf0304 | 2018-12-03 16:50:40 +0100 | [diff] [blame^] | 78 | base::RepeatingClosure task_; |
Hansong Zhang | 438b08c | 2018-08-14 14:29:23 -0700 | [diff] [blame] | 79 | base::TimeDelta period_; |
Hansong Zhang | 438b08c | 2018-08-14 14:29:23 -0700 | [diff] [blame] | 80 | uint64_t expected_time_next_task_us_; // Using clock boot time in time_util.h |
| 81 | mutable std::recursive_mutex api_mutex_; |
Hansong Zhang | 68f9113 | 2018-09-20 10:15:12 -0700 | [diff] [blame] | 82 | void CancelHelper(std::promise<void> promise); |
Hansong Zhang | 438b08c | 2018-08-14 14:29:23 -0700 | [diff] [blame] | 83 | void CancelClosure(std::promise<void> promise); |
| 84 | |
Hansong Zhang | 438b08c | 2018-08-14 14:29:23 -0700 | [diff] [blame] | 85 | void RunTask(); |
Jakub Pawlowski | 5cf0304 | 2018-12-03 16:50:40 +0100 | [diff] [blame^] | 86 | |
| 87 | DISALLOW_COPY_AND_ASSIGN(RepeatingTimer); |
Hansong Zhang | 438b08c | 2018-08-14 14:29:23 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | } // namespace common |
| 91 | |
| 92 | } // namespace bluetooth |