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