| 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 |  | 
|  | 19 | #include <chrono> | 
|  | 20 | #include <condition_variable> | 
|  | 21 | #include <thread> | 
|  | 22 |  | 
|  | 23 | #include <android-base/thread_annotations.h> | 
|  | 24 |  | 
|  | 25 | namespace android { | 
|  | 26 | namespace scheduler { | 
|  | 27 |  | 
|  | 28 | /* | 
|  | 29 | * Class that sets off a timer for a given interval, and fires a callback when the | 
|  | 30 | * interval expires. | 
|  | 31 | */ | 
|  | 32 | class IdleTimer { | 
|  | 33 | public: | 
|  | 34 | using Interval = std::chrono::milliseconds; | 
| Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 35 | using ResetCallback = std::function<void()>; | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 36 | using TimeoutCallback = std::function<void()>; | 
|  | 37 |  | 
| Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 38 | IdleTimer(const Interval& interval, const ResetCallback& resetCallback, | 
|  | 39 | const TimeoutCallback& timeoutCallback); | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 40 | ~IdleTimer(); | 
|  | 41 |  | 
|  | 42 | void start(); | 
|  | 43 | void stop(); | 
|  | 44 | void reset(); | 
|  | 45 |  | 
|  | 46 | private: | 
|  | 47 | // Enum to track in what state is the timer. | 
|  | 48 | enum class TimerState { STOPPED = 0, RESET = 1, WAITING = 2, IDLE = 3 }; | 
|  | 49 |  | 
|  | 50 | // Function that loops until the condition for stopping is met. | 
|  | 51 | void loop(); | 
|  | 52 |  | 
|  | 53 | // Thread waiting for timer to expire. | 
|  | 54 | std::thread mThread; | 
|  | 55 |  | 
|  | 56 | // Condition used to notify mThread. | 
|  | 57 | std::condition_variable_any mCondition; | 
|  | 58 |  | 
|  | 59 | // Lock used for synchronizing the waiting thread with the application thread. | 
|  | 60 | std::mutex mMutex; | 
|  | 61 |  | 
|  | 62 | TimerState mState GUARDED_BY(mMutex) = TimerState::RESET; | 
|  | 63 |  | 
|  | 64 | // Interval after which timer expires. | 
|  | 65 | const Interval mInterval; | 
|  | 66 |  | 
| Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 67 | // Callback that happens when timer resets. | 
|  | 68 | const ResetCallback mResetCallback; | 
|  | 69 |  | 
| Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 70 | // Callback that happens when timer expires. | 
|  | 71 | const TimeoutCallback mTimeoutCallback; | 
|  | 72 | }; | 
|  | 73 |  | 
|  | 74 | } // namespace scheduler | 
|  | 75 | } // namespace android |