blob: 4e1e2eef00983ee465d32a931456b16df01a05a3 [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
Ady Abraham67231722024-03-21 18:06:21 -070046 Duration interval() const { return mInterval.load(); }
47 void setInterval(Interval value) { mInterval = value; }
Dominik Laskowski03cfce82022-11-02 12:13:29 -040048
Alec Mouridc28b372019-04-18 21:17:13 -070049 // Initializes and turns on the idle timer.
Ana Krulecfb772822018-11-30 10:44:07 +010050 void start();
Alec Mouridc28b372019-04-18 21:17:13 -070051 // Stops the idle timer and any held resources.
Ana Krulecfb772822018-11-30 10:44:07 +010052 void stop();
Alec Mouridc28b372019-04-18 21:17:13 -070053 // Resets the wakeup time and fires the reset callback.
Ana Krulecfb772822018-11-30 10:44:07 +010054 void reset();
Ady Abraham67231722024-03-21 18:06:21 -070055 // Pauses the timer. reset calls will get ignored.
56 void pause();
57 // Resumes the timer.
58 void resume();
Ana Krulecfb772822018-11-30 10:44:07 +010059
60private:
61 // Enum to track in what state is the timer.
Alec Mouridc28b372019-04-18 21:17:13 -070062 enum class TimerState {
63 // The internal timer thread has been destroyed, and no state is
64 // tracked.
65 // Possible state transitions: RESET
66 STOPPED = 0,
67 // An external thread has just reset this timer.
68 // If there is a reset callback, then that callback is fired.
69 // Possible state transitions: STOPPED, WAITING
70 RESET = 1,
71 // This timer is waiting for the timeout interval to expire.
72 // Possible state transaitions: STOPPED, RESET, IDLE
73 WAITING = 2,
74 // The timeout interval has expired, so we are sleeping now.
75 // Possible state transaitions: STOPPED, RESET
76 IDLE = 3
77 };
Ana Krulecfb772822018-11-30 10:44:07 +010078
79 // Function that loops until the condition for stopping is met.
80 void loop();
81
Ana Krulec75d4ffc2020-10-16 14:56:19 -070082 // Checks whether mResetTriggered and mStopTriggered were set and updates
83 // mState if so.
84 TimerState checkForResetAndStop(TimerState state);
85
Ana Krulecfb772822018-11-30 10:44:07 +010086 // Thread waiting for timer to expire.
87 std::thread mThread;
88
Ady Abraham018ff0b2021-04-19 23:39:36 -070089 // Clock object for the timer. Mocked in unit tests.
Leon Scroggins III67388622023-02-06 20:36:20 -050090 std::unique_ptr<android::Clock> mClock;
Ady Abraham018ff0b2021-04-19 23:39:36 -070091
Ana Krulec75d4ffc2020-10-16 14:56:19 -070092 // Semaphore to keep mThread synchronized.
93 sem_t mSemaphore;
Ana Krulecfb772822018-11-30 10:44:07 +010094
Ady Abrahamdb3dfee2020-11-17 17:07:12 -080095 // Timer's name.
96 std::string mName;
97
Ana Krulecfb772822018-11-30 10:44:07 +010098 // Interval after which timer expires.
Ady Abraham67231722024-03-21 18:06:21 -070099 std::atomic<Interval> mInterval;
Ana Krulecfb772822018-11-30 10:44:07 +0100100
Ady Abrahama1a49af2019-02-07 14:36:55 -0800101 // Callback that happens when timer resets.
102 const ResetCallback mResetCallback;
103
Ana Krulecfb772822018-11-30 10:44:07 +0100104 // Callback that happens when timer expires.
105 const TimeoutCallback mTimeoutCallback;
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700106
107 // After removing lock guarding mState, the state can be now accessed at
108 // any time. Keep a bool if the reset or stop were requested, and occasionally
109 // check in the main loop if they were.
110 std::atomic<bool> mResetTriggered = false;
111 std::atomic<bool> mStopTriggered = false;
Alec Mouriaadcf942022-05-11 01:22:42 +0000112 std::atomic<bool> mWaiting = false;
Ady Abraham67231722024-03-21 18:06:21 -0700113 std::atomic<bool> mPaused = false;
Alec Mouriaadcf942022-05-11 01:22:42 +0000114 std::atomic<std::chrono::steady_clock::time_point> mLastResetTime;
Ana Krulecfb772822018-11-30 10:44:07 +0100115};
116
117} // namespace scheduler
118} // namespace android