blob: fd1aa022265238bfd7194cbb843afc14f67f04fb [file] [log] [blame]
Ana Krulecfb772822018-11-30 10:44:07 +01001/*
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 <chrono>
20#include <condition_variable>
21#include <thread>
22
23#include <android-base/thread_annotations.h>
24
25namespace android {
26namespace scheduler {
27
28/*
29 * Class that sets off a timer for a given interval, and fires a callback when the
30 * interval expires.
31 */
Ana Krulecf2c006d2019-06-21 15:37:07 -070032class OneShotTimer {
Ana Krulecfb772822018-11-30 10:44:07 +010033public:
34 using Interval = std::chrono::milliseconds;
Ady Abrahama1a49af2019-02-07 14:36:55 -080035 using ResetCallback = std::function<void()>;
Ana Krulecfb772822018-11-30 10:44:07 +010036 using TimeoutCallback = std::function<void()>;
37
Ana Krulecf2c006d2019-06-21 15:37:07 -070038 OneShotTimer(const Interval& interval, const ResetCallback& resetCallback,
39 const TimeoutCallback& timeoutCallback);
40 ~OneShotTimer();
Ana Krulecfb772822018-11-30 10:44:07 +010041
Alec Mouridc28b372019-04-18 21:17:13 -070042 // Initializes and turns on the idle timer.
Ana Krulecfb772822018-11-30 10:44:07 +010043 void start();
Alec Mouridc28b372019-04-18 21:17:13 -070044 // Stops the idle timer and any held resources.
Ana Krulecfb772822018-11-30 10:44:07 +010045 void stop();
Alec Mouridc28b372019-04-18 21:17:13 -070046 // Resets the wakeup time and fires the reset callback.
Ana Krulecfb772822018-11-30 10:44:07 +010047 void reset();
48
49private:
50 // Enum to track in what state is the timer.
Alec Mouridc28b372019-04-18 21:17:13 -070051 enum class TimerState {
52 // The internal timer thread has been destroyed, and no state is
53 // tracked.
54 // Possible state transitions: RESET
55 STOPPED = 0,
56 // An external thread has just reset this timer.
57 // If there is a reset callback, then that callback is fired.
58 // Possible state transitions: STOPPED, WAITING
59 RESET = 1,
60 // This timer is waiting for the timeout interval to expire.
61 // Possible state transaitions: STOPPED, RESET, IDLE
62 WAITING = 2,
63 // The timeout interval has expired, so we are sleeping now.
64 // Possible state transaitions: STOPPED, RESET
65 IDLE = 3
66 };
Ana Krulecfb772822018-11-30 10:44:07 +010067
68 // Function that loops until the condition for stopping is met.
69 void loop();
70
71 // Thread waiting for timer to expire.
72 std::thread mThread;
73
74 // Condition used to notify mThread.
75 std::condition_variable_any mCondition;
76
77 // Lock used for synchronizing the waiting thread with the application thread.
78 std::mutex mMutex;
79
Alec Mouridc28b372019-04-18 21:17:13 -070080 // Current timer state
Ana Krulecfb772822018-11-30 10:44:07 +010081 TimerState mState GUARDED_BY(mMutex) = TimerState::RESET;
82
83 // Interval after which timer expires.
84 const Interval mInterval;
85
Ady Abrahama1a49af2019-02-07 14:36:55 -080086 // Callback that happens when timer resets.
87 const ResetCallback mResetCallback;
88
Ana Krulecfb772822018-11-30 10:44:07 +010089 // Callback that happens when timer expires.
90 const TimeoutCallback mTimeoutCallback;
91};
92
93} // namespace scheduler
94} // namespace android