blob: 02e8719c0802dc1046df419f6210f21fde8268cc [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>
Alec Mouri07b27ce2021-04-26 16:31:44 -070023#include "../Clock.h"
Ana Krulecfb772822018-11-30 10:44:07 +010024
25#include <android-base/thread_annotations.h>
Dominik Laskowski03cfce82022-11-02 12:13:29 -040026#include <scheduler/Time.h>
Ana Krulecfb772822018-11-30 10:44:07 +010027
28namespace android {
29namespace scheduler {
30
31/*
32 * Class that sets off a timer for a given interval, and fires a callback when the
33 * interval expires.
34 */
Ana Krulecf2c006d2019-06-21 15:37:07 -070035class OneShotTimer {
Ana Krulecfb772822018-11-30 10:44:07 +010036public:
37 using Interval = std::chrono::milliseconds;
Ady Abrahama1a49af2019-02-07 14:36:55 -080038 using ResetCallback = std::function<void()>;
Ana Krulecfb772822018-11-30 10:44:07 +010039 using TimeoutCallback = std::function<void()>;
40
Ady Abrahamdb3dfee2020-11-17 17:07:12 -080041 OneShotTimer(std::string name, const Interval& interval, const ResetCallback& resetCallback,
Ady Abraham018ff0b2021-04-19 23:39:36 -070042 const TimeoutCallback& timeoutCallback,
Leon Scroggins III67388622023-02-06 20:36:20 -050043 std::unique_ptr<android::Clock> clock = std::make_unique<SteadyClock>());
Ana Krulecf2c006d2019-06-21 15:37:07 -070044 ~OneShotTimer();
Ana Krulecfb772822018-11-30 10:44:07 +010045
Dominik Laskowski03cfce82022-11-02 12:13:29 -040046 Duration interval() const { return mInterval; }
47
Alec Mouridc28b372019-04-18 21:17:13 -070048 // Initializes and turns on the idle timer.
Ana Krulecfb772822018-11-30 10:44:07 +010049 void start();
Alec Mouridc28b372019-04-18 21:17:13 -070050 // Stops the idle timer and any held resources.
Ana Krulecfb772822018-11-30 10:44:07 +010051 void stop();
Alec Mouridc28b372019-04-18 21:17:13 -070052 // Resets the wakeup time and fires the reset callback.
Ana Krulecfb772822018-11-30 10:44:07 +010053 void reset();
54
55private:
56 // Enum to track in what state is the timer.
Alec Mouridc28b372019-04-18 21:17:13 -070057 enum class TimerState {
58 // The internal timer thread has been destroyed, and no state is
59 // tracked.
60 // Possible state transitions: RESET
61 STOPPED = 0,
62 // An external thread has just reset this timer.
63 // If there is a reset callback, then that callback is fired.
64 // Possible state transitions: STOPPED, WAITING
65 RESET = 1,
66 // This timer is waiting for the timeout interval to expire.
67 // Possible state transaitions: STOPPED, RESET, IDLE
68 WAITING = 2,
69 // The timeout interval has expired, so we are sleeping now.
70 // Possible state transaitions: STOPPED, RESET
71 IDLE = 3
72 };
Ana Krulecfb772822018-11-30 10:44:07 +010073
74 // Function that loops until the condition for stopping is met.
75 void loop();
76
Ana Krulec75d4ffc2020-10-16 14:56:19 -070077 // Checks whether mResetTriggered and mStopTriggered were set and updates
78 // mState if so.
79 TimerState checkForResetAndStop(TimerState state);
80
Ana Krulecfb772822018-11-30 10:44:07 +010081 // Thread waiting for timer to expire.
82 std::thread mThread;
83
Ady Abraham018ff0b2021-04-19 23:39:36 -070084 // Clock object for the timer. Mocked in unit tests.
Leon Scroggins III67388622023-02-06 20:36:20 -050085 std::unique_ptr<android::Clock> mClock;
Ady Abraham018ff0b2021-04-19 23:39:36 -070086
Ana Krulec75d4ffc2020-10-16 14:56:19 -070087 // Semaphore to keep mThread synchronized.
88 sem_t mSemaphore;
Ana Krulecfb772822018-11-30 10:44:07 +010089
Ady Abrahamdb3dfee2020-11-17 17:07:12 -080090 // Timer's name.
91 std::string mName;
92
Ana Krulecfb772822018-11-30 10:44:07 +010093 // Interval after which timer expires.
94 const Interval mInterval;
95
Ady Abrahama1a49af2019-02-07 14:36:55 -080096 // Callback that happens when timer resets.
97 const ResetCallback mResetCallback;
98
Ana Krulecfb772822018-11-30 10:44:07 +010099 // Callback that happens when timer expires.
100 const TimeoutCallback mTimeoutCallback;
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700101
102 // After removing lock guarding mState, the state can be now accessed at
103 // any time. Keep a bool if the reset or stop were requested, and occasionally
104 // check in the main loop if they were.
105 std::atomic<bool> mResetTriggered = false;
106 std::atomic<bool> mStopTriggered = false;
Alec Mouriaadcf942022-05-11 01:22:42 +0000107 std::atomic<bool> mWaiting = false;
108 std::atomic<std::chrono::steady_clock::time_point> mLastResetTime;
Ana Krulecfb772822018-11-30 10:44:07 +0100109};
110
111} // namespace scheduler
112} // namespace android