blob: 8bbd4f59831444c2aabadf95c2ef5a04b5308b16 [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
Ana Krulec75d4ffc2020-10-16 14:56:19 -070019#include <semaphore.h>
Ana Krulecfb772822018-11-30 10:44:07 +010020#include <chrono>
21#include <condition_variable>
22#include <thread>
23
24#include <android-base/thread_annotations.h>
25
26namespace android {
27namespace scheduler {
28
29/*
30 * Class that sets off a timer for a given interval, and fires a callback when the
31 * interval expires.
32 */
Ana Krulecf2c006d2019-06-21 15:37:07 -070033class OneShotTimer {
Ana Krulecfb772822018-11-30 10:44:07 +010034public:
35 using Interval = std::chrono::milliseconds;
Ady Abrahama1a49af2019-02-07 14:36:55 -080036 using ResetCallback = std::function<void()>;
Ana Krulecfb772822018-11-30 10:44:07 +010037 using TimeoutCallback = std::function<void()>;
38
Ana Krulecf2c006d2019-06-21 15:37:07 -070039 OneShotTimer(const Interval& interval, const ResetCallback& resetCallback,
40 const TimeoutCallback& timeoutCallback);
41 ~OneShotTimer();
Ana Krulecfb772822018-11-30 10:44:07 +010042
Alec Mouridc28b372019-04-18 21:17:13 -070043 // Initializes and turns on the idle timer.
Ana Krulecfb772822018-11-30 10:44:07 +010044 void start();
Alec Mouridc28b372019-04-18 21:17:13 -070045 // Stops the idle timer and any held resources.
Ana Krulecfb772822018-11-30 10:44:07 +010046 void stop();
Alec Mouridc28b372019-04-18 21:17:13 -070047 // Resets the wakeup time and fires the reset callback.
Ana Krulecfb772822018-11-30 10:44:07 +010048 void reset();
49
Dominik Laskowski49cea512019-11-12 14:13:23 -080050 std::string dump() const;
51
Ana Krulecfb772822018-11-30 10:44:07 +010052private:
53 // Enum to track in what state is the timer.
Alec Mouridc28b372019-04-18 21:17:13 -070054 enum class TimerState {
55 // The internal timer thread has been destroyed, and no state is
56 // tracked.
57 // Possible state transitions: RESET
58 STOPPED = 0,
59 // An external thread has just reset this timer.
60 // If there is a reset callback, then that callback is fired.
61 // Possible state transitions: STOPPED, WAITING
62 RESET = 1,
63 // This timer is waiting for the timeout interval to expire.
64 // Possible state transaitions: STOPPED, RESET, IDLE
65 WAITING = 2,
66 // The timeout interval has expired, so we are sleeping now.
67 // Possible state transaitions: STOPPED, RESET
68 IDLE = 3
69 };
Ana Krulecfb772822018-11-30 10:44:07 +010070
71 // Function that loops until the condition for stopping is met.
72 void loop();
73
Ana Krulec75d4ffc2020-10-16 14:56:19 -070074 // Checks whether mResetTriggered and mStopTriggered were set and updates
75 // mState if so.
76 TimerState checkForResetAndStop(TimerState state);
77
Ana Krulecfb772822018-11-30 10:44:07 +010078 // Thread waiting for timer to expire.
79 std::thread mThread;
80
Ana Krulec75d4ffc2020-10-16 14:56:19 -070081 // Semaphore to keep mThread synchronized.
82 sem_t mSemaphore;
Ana Krulecfb772822018-11-30 10:44:07 +010083
84 // Interval after which timer expires.
85 const Interval mInterval;
86
Ady Abrahama1a49af2019-02-07 14:36:55 -080087 // Callback that happens when timer resets.
88 const ResetCallback mResetCallback;
89
Ana Krulecfb772822018-11-30 10:44:07 +010090 // Callback that happens when timer expires.
91 const TimeoutCallback mTimeoutCallback;
Ana Krulec75d4ffc2020-10-16 14:56:19 -070092
93 // After removing lock guarding mState, the state can be now accessed at
94 // any time. Keep a bool if the reset or stop were requested, and occasionally
95 // check in the main loop if they were.
96 std::atomic<bool> mResetTriggered = false;
97 std::atomic<bool> mStopTriggered = false;
Ana Krulecfb772822018-11-30 10:44:07 +010098};
99
100} // namespace scheduler
101} // namespace android