Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 1 | /* |
| 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 Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 19 | #include <semaphore.h> |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 20 | #include <chrono> |
| 21 | #include <condition_variable> |
| 22 | #include <thread> |
Alec Mouri | 07b27ce | 2021-04-26 16:31:44 -0700 | [diff] [blame] | 23 | #include "../Clock.h" |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 24 | |
| 25 | #include <android-base/thread_annotations.h> |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 26 | #include <scheduler/Time.h> |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace 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 Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 35 | class OneShotTimer { |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 36 | public: |
| 37 | using Interval = std::chrono::milliseconds; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 38 | using ResetCallback = std::function<void()>; |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 39 | using TimeoutCallback = std::function<void()>; |
| 40 | |
Ady Abraham | db3dfee | 2020-11-17 17:07:12 -0800 | [diff] [blame] | 41 | OneShotTimer(std::string name, const Interval& interval, const ResetCallback& resetCallback, |
Ady Abraham | 018ff0b | 2021-04-19 23:39:36 -0700 | [diff] [blame] | 42 | const TimeoutCallback& timeoutCallback, |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 43 | std::unique_ptr<android::Clock> clock = std::make_unique<SteadyClock>()); |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 44 | ~OneShotTimer(); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 45 | |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 46 | Duration interval() const { return mInterval; } |
| 47 | |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 48 | // Initializes and turns on the idle timer. |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 49 | void start(); |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 50 | // Stops the idle timer and any held resources. |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 51 | void stop(); |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 52 | // Resets the wakeup time and fires the reset callback. |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 53 | void reset(); |
| 54 | |
| 55 | private: |
| 56 | // Enum to track in what state is the timer. |
Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 57 | 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 Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 73 | |
| 74 | // Function that loops until the condition for stopping is met. |
| 75 | void loop(); |
| 76 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 77 | // Checks whether mResetTriggered and mStopTriggered were set and updates |
| 78 | // mState if so. |
| 79 | TimerState checkForResetAndStop(TimerState state); |
| 80 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 81 | // Thread waiting for timer to expire. |
| 82 | std::thread mThread; |
| 83 | |
Ady Abraham | 018ff0b | 2021-04-19 23:39:36 -0700 | [diff] [blame] | 84 | // Clock object for the timer. Mocked in unit tests. |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 85 | std::unique_ptr<android::Clock> mClock; |
Ady Abraham | 018ff0b | 2021-04-19 23:39:36 -0700 | [diff] [blame] | 86 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 87 | // Semaphore to keep mThread synchronized. |
| 88 | sem_t mSemaphore; |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 89 | |
Ady Abraham | db3dfee | 2020-11-17 17:07:12 -0800 | [diff] [blame] | 90 | // Timer's name. |
| 91 | std::string mName; |
| 92 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 93 | // Interval after which timer expires. |
| 94 | const Interval mInterval; |
| 95 | |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 96 | // Callback that happens when timer resets. |
| 97 | const ResetCallback mResetCallback; |
| 98 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 99 | // Callback that happens when timer expires. |
| 100 | const TimeoutCallback mTimeoutCallback; |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 101 | |
| 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 Mouri | aadcf94 | 2022-05-11 01:22:42 +0000 | [diff] [blame] | 107 | std::atomic<bool> mWaiting = false; |
| 108 | std::atomic<std::chrono::steady_clock::time_point> mLastResetTime; |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } // namespace scheduler |
| 112 | } // namespace android |