| 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> | 
 | 23 |  | 
 | 24 | #include <android-base/thread_annotations.h> | 
 | 25 |  | 
 | 26 | namespace android { | 
 | 27 | namespace scheduler { | 
 | 28 |  | 
 | 29 | /* | 
 | 30 |  * Class that sets off a timer for a given interval, and fires a callback when the | 
 | 31 |  * interval expires. | 
 | 32 |  */ | 
| Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 33 | class OneShotTimer { | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 34 | public: | 
 | 35 |     using Interval = std::chrono::milliseconds; | 
| Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 36 |     using ResetCallback = std::function<void()>; | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 37 |     using TimeoutCallback = std::function<void()>; | 
 | 38 |  | 
| Ady Abraham | db3dfee | 2020-11-17 17:07:12 -0800 | [diff] [blame] | 39 |     OneShotTimer(std::string name, const Interval& interval, const ResetCallback& resetCallback, | 
| Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 40 |                  const TimeoutCallback& timeoutCallback); | 
 | 41 |     ~OneShotTimer(); | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 42 |  | 
| Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 43 |     // Initializes and turns on the idle timer. | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 44 |     void start(); | 
| Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 45 |     // Stops the idle timer and any held resources. | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 46 |     void stop(); | 
| Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 47 |     // Resets the wakeup time and fires the reset callback. | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 48 |     void reset(); | 
 | 49 |  | 
| Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 50 |     std::string dump() const; | 
 | 51 |  | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 52 | private: | 
 | 53 |     // Enum to track in what state is the timer. | 
| Alec Mouri | dc28b37 | 2019-04-18 21:17:13 -0700 | [diff] [blame] | 54 |     enum class TimerState { | 
 | 55 |         // The internal timer thread has been destroyed, and no state is | 
 | 56 |         // tracked. | 
 | 57 |         // Possible state transitions: RESET | 
 | 58 |         STOPPED = 0, | 
 | 59 |         // An external thread has just reset this timer. | 
 | 60 |         // If there is a reset callback, then that callback is fired. | 
 | 61 |         // Possible state transitions: STOPPED, WAITING | 
 | 62 |         RESET = 1, | 
 | 63 |         // This timer is waiting for the timeout interval to expire. | 
 | 64 |         // Possible state transaitions: STOPPED, RESET, IDLE | 
 | 65 |         WAITING = 2, | 
 | 66 |         // The timeout interval has expired, so we are sleeping now. | 
 | 67 |         // Possible state transaitions: STOPPED, RESET | 
 | 68 |         IDLE = 3 | 
 | 69 |     }; | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 70 |  | 
 | 71 |     // Function that loops until the condition for stopping is met. | 
 | 72 |     void loop(); | 
 | 73 |  | 
| Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 74 |     // Checks whether mResetTriggered and mStopTriggered were set and updates | 
 | 75 |     // mState if so. | 
 | 76 |     TimerState checkForResetAndStop(TimerState state); | 
 | 77 |  | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 78 |     // Thread waiting for timer to expire. | 
 | 79 |     std::thread mThread; | 
 | 80 |  | 
| Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 81 |     // Semaphore to keep mThread synchronized. | 
 | 82 |     sem_t mSemaphore; | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 83 |  | 
| Ady Abraham | db3dfee | 2020-11-17 17:07:12 -0800 | [diff] [blame] | 84 |     // Timer's name. | 
 | 85 |     std::string mName; | 
 | 86 |  | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 87 |     // Interval after which timer expires. | 
 | 88 |     const Interval mInterval; | 
 | 89 |  | 
| Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 90 |     // Callback that happens when timer resets. | 
 | 91 |     const ResetCallback mResetCallback; | 
 | 92 |  | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 93 |     // Callback that happens when timer expires. | 
 | 94 |     const TimeoutCallback mTimeoutCallback; | 
| Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 95 |  | 
 | 96 |     // After removing lock guarding mState, the state can be now accessed at | 
 | 97 |     // any time. Keep a bool if the reset or stop were requested, and occasionally | 
 | 98 |     // check in the main loop if they were. | 
 | 99 |     std::atomic<bool> mResetTriggered = false; | 
 | 100 |     std::atomic<bool> mStopTriggered = false; | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 101 | }; | 
 | 102 |  | 
 | 103 | } // namespace scheduler | 
 | 104 | } // namespace android |