blob: 09265bb94723a98a31607438f92f71c66175fdc3 [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>
26
27namespace android {
28namespace scheduler {
29
30/*
31 * Class that sets off a timer for a given interval, and fires a callback when the
32 * interval expires.
33 */
Ana Krulecf2c006d2019-06-21 15:37:07 -070034class OneShotTimer {
Ana Krulecfb772822018-11-30 10:44:07 +010035public:
36 using Interval = std::chrono::milliseconds;
Ady Abrahama1a49af2019-02-07 14:36:55 -080037 using ResetCallback = std::function<void()>;
Ana Krulecfb772822018-11-30 10:44:07 +010038 using TimeoutCallback = std::function<void()>;
39
Ady Abrahamdb3dfee2020-11-17 17:07:12 -080040 OneShotTimer(std::string name, const Interval& interval, const ResetCallback& resetCallback,
Ady Abraham018ff0b2021-04-19 23:39:36 -070041 const TimeoutCallback& timeoutCallback,
Alec Mouri07b27ce2021-04-26 16:31:44 -070042 std::unique_ptr<Clock> clock = std::make_unique<SteadyClock>());
Ana Krulecf2c006d2019-06-21 15:37:07 -070043 ~OneShotTimer();
Ana Krulecfb772822018-11-30 10:44:07 +010044
Alec Mouridc28b372019-04-18 21:17:13 -070045 // Initializes and turns on the idle timer.
Ana Krulecfb772822018-11-30 10:44:07 +010046 void start();
Alec Mouridc28b372019-04-18 21:17:13 -070047 // Stops the idle timer and any held resources.
Ana Krulecfb772822018-11-30 10:44:07 +010048 void stop();
Alec Mouridc28b372019-04-18 21:17:13 -070049 // Resets the wakeup time and fires the reset callback.
Ana Krulecfb772822018-11-30 10:44:07 +010050 void reset();
51
Dominik Laskowski49cea512019-11-12 14:13:23 -080052 std::string dump() const;
53
Ana Krulecfb772822018-11-30 10:44:07 +010054private:
55 // Enum to track in what state is the timer.
Alec Mouridc28b372019-04-18 21:17:13 -070056 enum class TimerState {
57 // The internal timer thread has been destroyed, and no state is
58 // tracked.
59 // Possible state transitions: RESET
60 STOPPED = 0,
61 // An external thread has just reset this timer.
62 // If there is a reset callback, then that callback is fired.
63 // Possible state transitions: STOPPED, WAITING
64 RESET = 1,
65 // This timer is waiting for the timeout interval to expire.
66 // Possible state transaitions: STOPPED, RESET, IDLE
67 WAITING = 2,
68 // The timeout interval has expired, so we are sleeping now.
69 // Possible state transaitions: STOPPED, RESET
70 IDLE = 3
71 };
Ana Krulecfb772822018-11-30 10:44:07 +010072
73 // Function that loops until the condition for stopping is met.
74 void loop();
75
Ana Krulec75d4ffc2020-10-16 14:56:19 -070076 // Checks whether mResetTriggered and mStopTriggered were set and updates
77 // mState if so.
78 TimerState checkForResetAndStop(TimerState state);
79
Ana Krulecfb772822018-11-30 10:44:07 +010080 // Thread waiting for timer to expire.
81 std::thread mThread;
82
Ady Abraham018ff0b2021-04-19 23:39:36 -070083 // Clock object for the timer. Mocked in unit tests.
84 std::unique_ptr<Clock> mClock;
85
Ana Krulec75d4ffc2020-10-16 14:56:19 -070086 // Semaphore to keep mThread synchronized.
87 sem_t mSemaphore;
Ana Krulecfb772822018-11-30 10:44:07 +010088
Ady Abrahamdb3dfee2020-11-17 17:07:12 -080089 // Timer's name.
90 std::string mName;
91
Ana Krulecfb772822018-11-30 10:44:07 +010092 // Interval after which timer expires.
93 const Interval mInterval;
94
Ady Abrahama1a49af2019-02-07 14:36:55 -080095 // Callback that happens when timer resets.
96 const ResetCallback mResetCallback;
97
Ana Krulecfb772822018-11-30 10:44:07 +010098 // Callback that happens when timer expires.
99 const TimeoutCallback mTimeoutCallback;
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700100
101 // After removing lock guarding mState, the state can be now accessed at
102 // any time. Keep a bool if the reset or stop were requested, and occasionally
103 // check in the main loop if they were.
104 std::atomic<bool> mResetTriggered = false;
105 std::atomic<bool> mStopTriggered = false;
Ana Krulecfb772822018-11-30 10:44:07 +0100106};
107
108} // namespace scheduler
109} // namespace android