blob: 7285427abb39fa812acd38be75301ff07e282951 [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
Ady Abraham018ff0b2021-04-19 23:39:36 -070039 class Clock {
40 public:
41 Clock() = default;
42 virtual ~Clock() = default;
43
44 virtual std::chrono::steady_clock::time_point now() const;
45 };
46
Ady Abrahamdb3dfee2020-11-17 17:07:12 -080047 OneShotTimer(std::string name, const Interval& interval, const ResetCallback& resetCallback,
Ady Abraham018ff0b2021-04-19 23:39:36 -070048 const TimeoutCallback& timeoutCallback,
49 std::unique_ptr<OneShotTimer::Clock> = std::make_unique<OneShotTimer::Clock>());
Ana Krulecf2c006d2019-06-21 15:37:07 -070050 ~OneShotTimer();
Ana Krulecfb772822018-11-30 10:44:07 +010051
Alec Mouridc28b372019-04-18 21:17:13 -070052 // Initializes and turns on the idle timer.
Ana Krulecfb772822018-11-30 10:44:07 +010053 void start();
Alec Mouridc28b372019-04-18 21:17:13 -070054 // Stops the idle timer and any held resources.
Ana Krulecfb772822018-11-30 10:44:07 +010055 void stop();
Alec Mouridc28b372019-04-18 21:17:13 -070056 // Resets the wakeup time and fires the reset callback.
Ana Krulecfb772822018-11-30 10:44:07 +010057 void reset();
58
Dominik Laskowski49cea512019-11-12 14:13:23 -080059 std::string dump() const;
60
Ana Krulecfb772822018-11-30 10:44:07 +010061private:
62 // Enum to track in what state is the timer.
Alec Mouridc28b372019-04-18 21:17:13 -070063 enum class TimerState {
64 // The internal timer thread has been destroyed, and no state is
65 // tracked.
66 // Possible state transitions: RESET
67 STOPPED = 0,
68 // An external thread has just reset this timer.
69 // If there is a reset callback, then that callback is fired.
70 // Possible state transitions: STOPPED, WAITING
71 RESET = 1,
72 // This timer is waiting for the timeout interval to expire.
73 // Possible state transaitions: STOPPED, RESET, IDLE
74 WAITING = 2,
75 // The timeout interval has expired, so we are sleeping now.
76 // Possible state transaitions: STOPPED, RESET
77 IDLE = 3
78 };
Ana Krulecfb772822018-11-30 10:44:07 +010079
80 // Function that loops until the condition for stopping is met.
81 void loop();
82
Ana Krulec75d4ffc2020-10-16 14:56:19 -070083 // Checks whether mResetTriggered and mStopTriggered were set and updates
84 // mState if so.
85 TimerState checkForResetAndStop(TimerState state);
86
Ana Krulecfb772822018-11-30 10:44:07 +010087 // Thread waiting for timer to expire.
88 std::thread mThread;
89
Ady Abraham018ff0b2021-04-19 23:39:36 -070090 // Clock object for the timer. Mocked in unit tests.
91 std::unique_ptr<Clock> mClock;
92
Ana Krulec75d4ffc2020-10-16 14:56:19 -070093 // Semaphore to keep mThread synchronized.
94 sem_t mSemaphore;
Ana Krulecfb772822018-11-30 10:44:07 +010095
Ady Abrahamdb3dfee2020-11-17 17:07:12 -080096 // Timer's name.
97 std::string mName;
98
Ana Krulecfb772822018-11-30 10:44:07 +010099 // Interval after which timer expires.
100 const Interval mInterval;
101
Ady Abrahama1a49af2019-02-07 14:36:55 -0800102 // Callback that happens when timer resets.
103 const ResetCallback mResetCallback;
104
Ana Krulecfb772822018-11-30 10:44:07 +0100105 // Callback that happens when timer expires.
106 const TimeoutCallback mTimeoutCallback;
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700107
108 // After removing lock guarding mState, the state can be now accessed at
109 // any time. Keep a bool if the reset or stop were requested, and occasionally
110 // check in the main loop if they were.
111 std::atomic<bool> mResetTriggered = false;
112 std::atomic<bool> mStopTriggered = false;
Ana Krulecfb772822018-11-30 10:44:07 +0100113};
114
115} // namespace scheduler
116} // namespace android