blob: b28b1aa051d30891e02f04fb958ecda1245f76f5 [file] [log] [blame]
Ana Krulecfb772822018-11-30 10:44:07 +01001/*
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#include "IdleTimer.h"
18
19#include <chrono>
20#include <thread>
21
22namespace android {
23namespace scheduler {
24
Ady Abrahama1a49af2019-02-07 14:36:55 -080025IdleTimer::IdleTimer(const Interval& interval, const ResetCallback& resetCallback,
26 const TimeoutCallback& timeoutCallback)
27 : mInterval(interval), mResetCallback(resetCallback), mTimeoutCallback(timeoutCallback) {}
Ana Krulecfb772822018-11-30 10:44:07 +010028
29IdleTimer::~IdleTimer() {
30 stop();
31}
32
33void IdleTimer::start() {
34 {
35 std::lock_guard<std::mutex> lock(mMutex);
36 mState = TimerState::RESET;
37 }
38 mThread = std::thread(&IdleTimer::loop, this);
39}
40
41void IdleTimer::stop() {
42 {
43 std::lock_guard<std::mutex> lock(mMutex);
44 mState = TimerState::STOPPED;
45 }
46 mCondition.notify_all();
47 if (mThread.joinable()) {
48 mThread.join();
49 }
50}
51
52void IdleTimer::loop() {
Ady Abrahama1a49af2019-02-07 14:36:55 -080053 while (true) {
54 bool triggerReset = false;
55 bool triggerTimeout = false;
56 {
57 std::lock_guard<std::mutex> lock(mMutex);
58 if (mState == TimerState::STOPPED) {
59 break;
60 }
61
62 if (mState == TimerState::IDLE) {
63 mCondition.wait(mMutex);
64 continue;
65 }
66
67 if (mState == TimerState::RESET) {
68 triggerReset = true;
69 }
70 }
71 if (triggerReset && mResetCallback) {
72 mResetCallback();
73 }
74
75 { // lock the mutex again. someone might have called stop meanwhile
76 std::lock_guard<std::mutex> lock(mMutex);
77 if (mState == TimerState::STOPPED) {
78 break;
79 }
80
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080081 auto triggerTime = std::chrono::steady_clock::now() + mInterval;
Ana Krulecfb772822018-11-30 10:44:07 +010082 mState = TimerState::WAITING;
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080083 while (mState == TimerState::WAITING) {
84 constexpr auto zero = std::chrono::steady_clock::duration::zero();
85 auto waitTime = triggerTime - std::chrono::steady_clock::now();
86 if (waitTime > zero) mCondition.wait_for(mMutex, waitTime);
87 if (mState == TimerState::WAITING &&
88 (triggerTime - std::chrono::steady_clock::now()) <= zero) {
Ady Abrahama1a49af2019-02-07 14:36:55 -080089 triggerTimeout = true;
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080090 mState = TimerState::IDLE;
Ana Krulecfb772822018-11-30 10:44:07 +010091 }
92 }
Ana Krulecfb772822018-11-30 10:44:07 +010093 }
Ady Abrahama1a49af2019-02-07 14:36:55 -080094 if (triggerTimeout && mTimeoutCallback) {
95 mTimeoutCallback();
96 }
Ana Krulecfb772822018-11-30 10:44:07 +010097 }
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080098} // namespace scheduler
Ana Krulecfb772822018-11-30 10:44:07 +010099
100void IdleTimer::reset() {
101 {
102 std::lock_guard<std::mutex> lock(mMutex);
103 mState = TimerState::RESET;
104 }
105 mCondition.notify_all();
106}
107
108} // namespace scheduler
109} // namespace android