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