blob: b4c51083986b8ffe0e39624067c01bfae48fbc61 [file] [log] [blame]
Hansong Zhang438b08c2018-08-14 14:29:23 -07001/*
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 Pawlowski67f5f372018-07-23 10:00:25 -070021#include <base/location.h>
Hansong Zhang438b08c2018-08-14 14:29:23 -070022#include <future>
23
24namespace bluetooth {
25
26namespace common {
27
28class MessageLoopThread;
29
30/**
31 * An alarm clock that posts a delayed task to a specified MessageLoopThread
Jakub Pawlowski5cf03042018-12-03 16:50:40 +010032 * periodically.
Hansong Zhang438b08c2018-08-14 14:29:23 -070033 *
34 * Warning: MessageLoopThread must be running when any task is scheduled or
35 * being executed
36 */
Jakub Pawlowski5cf03042018-12-03 16:50:40 +010037class RepeatingTimer final {
Hansong Zhang438b08c2018-08-14 14:29:23 -070038 public:
Jakub Pawlowski5cf03042018-12-03 16:50:40 +010039 RepeatingTimer() : expected_time_next_task_us_(0) {}
40 ~RepeatingTimer();
Hansong Zhang438b08c2018-08-14 14:29:23 -070041
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 Pawlowski5cf03042018-12-03 16:50:40 +010055 const base::Location& from_here,
56 base::RepeatingClosure task, base::TimeDelta period);
Hansong Zhang438b08c2018-08-14 14:29:23 -070057
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 Zhang85ea7632018-09-20 10:15:12 -070077 base::CancelableClosure task_wrapper_;
Jakub Pawlowski5cf03042018-12-03 16:50:40 +010078 base::RepeatingClosure task_;
Hansong Zhang438b08c2018-08-14 14:29:23 -070079 base::TimeDelta period_;
Hansong Zhang438b08c2018-08-14 14:29:23 -070080 uint64_t expected_time_next_task_us_; // Using clock boot time in time_util.h
81 mutable std::recursive_mutex api_mutex_;
Hansong Zhang68f91132018-09-20 10:15:12 -070082 void CancelHelper(std::promise<void> promise);
Hansong Zhang438b08c2018-08-14 14:29:23 -070083 void CancelClosure(std::promise<void> promise);
84
Hansong Zhang438b08c2018-08-14 14:29:23 -070085 void RunTask();
Jakub Pawlowski5cf03042018-12-03 16:50:40 +010086
87 DISALLOW_COPY_AND_ASSIGN(RepeatingTimer);
Hansong Zhang438b08c2018-08-14 14:29:23 -070088};
89
90} // namespace common
91
92} // namespace bluetooth