blob: 27838003c43d8c8337838a1a3931e9332553b1d4 [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
Ana Krulec75d4ffc2020-10-16 14:56:19 -070023namespace {
24using namespace std::chrono_literals;
25
26constexpr int64_t kNsToSeconds = std::chrono::duration_cast<std::chrono::nanoseconds>(1s).count();
27
28// The syscall interface uses a pair of integers for the timestamp. The first
29// (tv_sec) is the whole count of seconds. The second (tv_nsec) is the
30// nanosecond part of the count. This function takes care of translation.
31void calculateTimeoutTime(std::chrono::nanoseconds timestamp, timespec* spec) {
32 clock_gettime(CLOCK_MONOTONIC, spec);
33 spec->tv_sec += static_cast<__kernel_time_t>(timestamp.count() / kNsToSeconds);
34 spec->tv_nsec += timestamp.count() % kNsToSeconds;
35}
36} // namespace
37
Ana Krulecfb772822018-11-30 10:44:07 +010038namespace android {
39namespace scheduler {
40
Ana Krulecf2c006d2019-06-21 15:37:07 -070041OneShotTimer::OneShotTimer(const Interval& interval, const ResetCallback& resetCallback,
42 const TimeoutCallback& timeoutCallback)
Ady Abrahama1a49af2019-02-07 14:36:55 -080043 : mInterval(interval), mResetCallback(resetCallback), mTimeoutCallback(timeoutCallback) {}
Ana Krulecfb772822018-11-30 10:44:07 +010044
Ana Krulecf2c006d2019-06-21 15:37:07 -070045OneShotTimer::~OneShotTimer() {
Ana Krulecfb772822018-11-30 10:44:07 +010046 stop();
47}
48
Ana Krulecf2c006d2019-06-21 15:37:07 -070049void OneShotTimer::start() {
Ana Krulec75d4ffc2020-10-16 14:56:19 -070050 sem_init(&mSemaphore, 0, 0);
51
52 if (!mThread.joinable()) {
53 // Only create thread if it has not been created.
54 mThread = std::thread(&OneShotTimer::loop, this);
Ana Krulecfb772822018-11-30 10:44:07 +010055 }
Ana Krulecfb772822018-11-30 10:44:07 +010056}
57
Ana Krulecf2c006d2019-06-21 15:37:07 -070058void OneShotTimer::stop() {
Ana Krulec75d4ffc2020-10-16 14:56:19 -070059 mStopTriggered = true;
60 sem_post(&mSemaphore);
61
Ana Krulecfb772822018-11-30 10:44:07 +010062 if (mThread.joinable()) {
63 mThread.join();
Ana Krulec75d4ffc2020-10-16 14:56:19 -070064 sem_destroy(&mSemaphore);
Ana Krulecfb772822018-11-30 10:44:07 +010065 }
66}
67
Ana Krulecf2c006d2019-06-21 15:37:07 -070068void OneShotTimer::loop() {
Ana Krulec75d4ffc2020-10-16 14:56:19 -070069 TimerState state = TimerState::RESET;
Ady Abrahama1a49af2019-02-07 14:36:55 -080070 while (true) {
71 bool triggerReset = false;
72 bool triggerTimeout = false;
Ady Abrahama1a49af2019-02-07 14:36:55 -080073
Ana Krulec75d4ffc2020-10-16 14:56:19 -070074 state = checkForResetAndStop(state);
75 if (state == TimerState::STOPPED) {
76 break;
Ady Abrahama1a49af2019-02-07 14:36:55 -080077 }
Ana Krulec75d4ffc2020-10-16 14:56:19 -070078
79 if (state == TimerState::IDLE) {
80 sem_wait(&mSemaphore);
81 continue;
82 }
83
84 if (state == TimerState::RESET) {
85 triggerReset = true;
86 }
87
Ady Abrahama1a49af2019-02-07 14:36:55 -080088 if (triggerReset && mResetCallback) {
89 mResetCallback();
90 }
91
Ana Krulec75d4ffc2020-10-16 14:56:19 -070092 state = checkForResetAndStop(state);
93 if (state == TimerState::STOPPED) {
94 break;
95 }
Ady Abrahama1a49af2019-02-07 14:36:55 -080096
Ana Krulec75d4ffc2020-10-16 14:56:19 -070097 auto triggerTime = std::chrono::steady_clock::now() + mInterval;
98 state = TimerState::WAITING;
99 while (state == TimerState::WAITING) {
100 constexpr auto zero = std::chrono::steady_clock::duration::zero();
101 // Wait for mInterval time for semaphore signal.
102 struct timespec ts;
103 calculateTimeoutTime(std::chrono::nanoseconds(mInterval), &ts);
104 sem_clockwait(&mSemaphore, CLOCK_MONOTONIC, &ts);
105
106 state = checkForResetAndStop(state);
107 if (state == TimerState::RESET) {
108 triggerTime = std::chrono::steady_clock::now() + mInterval;
109 state = TimerState::WAITING;
110 } else if (state == TimerState::WAITING &&
111 (triggerTime - std::chrono::steady_clock::now()) <= zero) {
112 triggerTimeout = true;
113 state = TimerState::IDLE;
Ana Krulecfb772822018-11-30 10:44:07 +0100114 }
Ana Krulecfb772822018-11-30 10:44:07 +0100115 }
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700116
Ady Abrahama1a49af2019-02-07 14:36:55 -0800117 if (triggerTimeout && mTimeoutCallback) {
118 mTimeoutCallback();
119 }
Ana Krulecfb772822018-11-30 10:44:07 +0100120 }
Dominik Laskowski49cea512019-11-12 14:13:23 -0800121}
Ana Krulecfb772822018-11-30 10:44:07 +0100122
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700123OneShotTimer::TimerState OneShotTimer::checkForResetAndStop(TimerState state) {
124 // Stop takes precedence of the reset.
125 if (mStopTriggered.exchange(false)) {
126 return TimerState::STOPPED;
Ana Krulecfb772822018-11-30 10:44:07 +0100127 }
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700128 // If the state was stopped, the thread was joined, and we cannot reset
129 // the timer anymore.
130 if (state != TimerState::STOPPED && mResetTriggered.exchange(false)) {
131 return TimerState::RESET;
132 }
133 return state;
134}
135
136void OneShotTimer::reset() {
137 mResetTriggered = true;
138 sem_post(&mSemaphore);
Ana Krulecfb772822018-11-30 10:44:07 +0100139}
140
Dominik Laskowski49cea512019-11-12 14:13:23 -0800141std::string OneShotTimer::dump() const {
142 std::ostringstream stream;
143 stream << mInterval.count() << " ms";
144 return stream.str();
145}
146
Ana Krulecfb772822018-11-30 10:44:07 +0100147} // namespace scheduler
148} // namespace android