blob: a90d05e17cf5914c0a0a97e1d678c7ffe33cee9f [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
Ana Krulecf2c006d2019-06-21 15:37:07 -070017#include "OneShotTimer.h"
Ana Krulecfb772822018-11-30 10:44:07 +010018
19#include <chrono>
Dominik Laskowski49cea512019-11-12 14:13:23 -080020#include <sstream>
Ana Krulecfb772822018-11-30 10:44:07 +010021#include <thread>
22
23namespace android {
24namespace scheduler {
25
Ana Krulecf2c006d2019-06-21 15:37:07 -070026OneShotTimer::OneShotTimer(const Interval& interval, const ResetCallback& resetCallback,
27 const TimeoutCallback& timeoutCallback)
Ady Abrahama1a49af2019-02-07 14:36:55 -080028 : mInterval(interval), mResetCallback(resetCallback), mTimeoutCallback(timeoutCallback) {}
Ana Krulecfb772822018-11-30 10:44:07 +010029
Ana Krulecf2c006d2019-06-21 15:37:07 -070030OneShotTimer::~OneShotTimer() {
Ana Krulecfb772822018-11-30 10:44:07 +010031 stop();
32}
33
Ana Krulecf2c006d2019-06-21 15:37:07 -070034void OneShotTimer::start() {
Ana Krulecfb772822018-11-30 10:44:07 +010035 {
36 std::lock_guard<std::mutex> lock(mMutex);
37 mState = TimerState::RESET;
38 }
Ana Krulecf2c006d2019-06-21 15:37:07 -070039 mThread = std::thread(&OneShotTimer::loop, this);
Ana Krulecfb772822018-11-30 10:44:07 +010040}
41
Ana Krulecf2c006d2019-06-21 15:37:07 -070042void OneShotTimer::stop() {
Ana Krulecfb772822018-11-30 10:44:07 +010043 {
44 std::lock_guard<std::mutex> lock(mMutex);
45 mState = TimerState::STOPPED;
46 }
47 mCondition.notify_all();
48 if (mThread.joinable()) {
49 mThread.join();
50 }
51}
52
Ana Krulecf2c006d2019-06-21 15:37:07 -070053void OneShotTimer::loop() {
Ady Abrahama1a49af2019-02-07 14:36:55 -080054 while (true) {
55 bool triggerReset = false;
56 bool triggerTimeout = false;
57 {
58 std::lock_guard<std::mutex> lock(mMutex);
59 if (mState == TimerState::STOPPED) {
60 break;
61 }
62
63 if (mState == TimerState::IDLE) {
64 mCondition.wait(mMutex);
65 continue;
66 }
67
68 if (mState == TimerState::RESET) {
69 triggerReset = true;
70 }
71 }
72 if (triggerReset && mResetCallback) {
73 mResetCallback();
74 }
75
76 { // lock the mutex again. someone might have called stop meanwhile
77 std::lock_guard<std::mutex> lock(mMutex);
78 if (mState == TimerState::STOPPED) {
79 break;
80 }
81
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080082 auto triggerTime = std::chrono::steady_clock::now() + mInterval;
Ana Krulecfb772822018-11-30 10:44:07 +010083 mState = TimerState::WAITING;
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080084 while (mState == TimerState::WAITING) {
85 constexpr auto zero = std::chrono::steady_clock::duration::zero();
86 auto waitTime = triggerTime - std::chrono::steady_clock::now();
87 if (waitTime > zero) mCondition.wait_for(mMutex, waitTime);
Alec Mouridc28b372019-04-18 21:17:13 -070088 if (mState == TimerState::RESET) {
89 triggerTime = std::chrono::steady_clock::now() + mInterval;
90 mState = TimerState::WAITING;
91 } else if (mState == TimerState::WAITING &&
92 (triggerTime - std::chrono::steady_clock::now()) <= zero) {
Ady Abrahama1a49af2019-02-07 14:36:55 -080093 triggerTimeout = true;
Lloyd Pique1f9f1a42019-01-31 13:04:00 -080094 mState = TimerState::IDLE;
Ana Krulecfb772822018-11-30 10:44:07 +010095 }
96 }
Ana Krulecfb772822018-11-30 10:44:07 +010097 }
Ady Abrahama1a49af2019-02-07 14:36:55 -080098 if (triggerTimeout && mTimeoutCallback) {
99 mTimeoutCallback();
100 }
Ana Krulecfb772822018-11-30 10:44:07 +0100101 }
Dominik Laskowski49cea512019-11-12 14:13:23 -0800102}
Ana Krulecfb772822018-11-30 10:44:07 +0100103
Ana Krulecf2c006d2019-06-21 15:37:07 -0700104void OneShotTimer::reset() {
Ana Krulecfb772822018-11-30 10:44:07 +0100105 {
106 std::lock_guard<std::mutex> lock(mMutex);
107 mState = TimerState::RESET;
108 }
109 mCondition.notify_all();
110}
111
Dominik Laskowski49cea512019-11-12 14:13:23 -0800112std::string OneShotTimer::dump() const {
113 std::ostringstream stream;
114 stream << mInterval.count() << " ms";
115 return stream.str();
116}
117
Ana Krulecfb772822018-11-30 10:44:07 +0100118} // namespace scheduler
119} // namespace android