blob: 8e807d6c763917d87d0a83ed6e6da26f4ce13753 [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
Ady Abraham1e19ac92020-11-17 16:56:00 -080019#include <utils/Log.h>
20#include <utils/Timers.h>
Ana Krulecfb772822018-11-30 10:44:07 +010021#include <chrono>
Dominik Laskowski49cea512019-11-12 14:13:23 -080022#include <sstream>
Ana Krulecfb772822018-11-30 10:44:07 +010023#include <thread>
24
Ana Krulec75d4ffc2020-10-16 14:56:19 -070025namespace {
26using namespace std::chrono_literals;
27
28constexpr int64_t kNsToSeconds = std::chrono::duration_cast<std::chrono::nanoseconds>(1s).count();
29
30// The syscall interface uses a pair of integers for the timestamp. The first
31// (tv_sec) is the whole count of seconds. The second (tv_nsec) is the
32// nanosecond part of the count. This function takes care of translation.
33void calculateTimeoutTime(std::chrono::nanoseconds timestamp, timespec* spec) {
Ady Abraham1e19ac92020-11-17 16:56:00 -080034 const nsecs_t timeout = systemTime(CLOCK_MONOTONIC) + timestamp.count();
35 spec->tv_sec = static_cast<__kernel_time_t>(timeout / kNsToSeconds);
36 spec->tv_nsec = timeout % kNsToSeconds;
Ana Krulec75d4ffc2020-10-16 14:56:19 -070037}
38} // namespace
39
Ana Krulecfb772822018-11-30 10:44:07 +010040namespace android {
41namespace scheduler {
42
Ana Krulecf2c006d2019-06-21 15:37:07 -070043OneShotTimer::OneShotTimer(const Interval& interval, const ResetCallback& resetCallback,
44 const TimeoutCallback& timeoutCallback)
Ady Abrahama1a49af2019-02-07 14:36:55 -080045 : mInterval(interval), mResetCallback(resetCallback), mTimeoutCallback(timeoutCallback) {}
Ana Krulecfb772822018-11-30 10:44:07 +010046
Ana Krulecf2c006d2019-06-21 15:37:07 -070047OneShotTimer::~OneShotTimer() {
Ana Krulecfb772822018-11-30 10:44:07 +010048 stop();
49}
50
Ana Krulecf2c006d2019-06-21 15:37:07 -070051void OneShotTimer::start() {
Ady Abraham1e19ac92020-11-17 16:56:00 -080052 int result = sem_init(&mSemaphore, 0, 0);
53 LOG_ALWAYS_FATAL_IF(result, "sem_init failed");
Ana Krulec75d4ffc2020-10-16 14:56:19 -070054
55 if (!mThread.joinable()) {
56 // Only create thread if it has not been created.
57 mThread = std::thread(&OneShotTimer::loop, this);
Ana Krulecfb772822018-11-30 10:44:07 +010058 }
Ana Krulecfb772822018-11-30 10:44:07 +010059}
60
Ana Krulecf2c006d2019-06-21 15:37:07 -070061void OneShotTimer::stop() {
Ana Krulec75d4ffc2020-10-16 14:56:19 -070062 mStopTriggered = true;
Ady Abraham1e19ac92020-11-17 16:56:00 -080063 int result = sem_post(&mSemaphore);
64 LOG_ALWAYS_FATAL_IF(result, "sem_post failed");
Ana Krulec75d4ffc2020-10-16 14:56:19 -070065
Ana Krulecfb772822018-11-30 10:44:07 +010066 if (mThread.joinable()) {
67 mThread.join();
Ady Abraham1e19ac92020-11-17 16:56:00 -080068 result = sem_destroy(&mSemaphore);
69 LOG_ALWAYS_FATAL_IF(result, "sem_destroy failed");
Ana Krulecfb772822018-11-30 10:44:07 +010070 }
71}
72
Ana Krulecf2c006d2019-06-21 15:37:07 -070073void OneShotTimer::loop() {
Ana Krulec75d4ffc2020-10-16 14:56:19 -070074 TimerState state = TimerState::RESET;
Ady Abrahama1a49af2019-02-07 14:36:55 -080075 while (true) {
76 bool triggerReset = false;
77 bool triggerTimeout = false;
Ady Abrahama1a49af2019-02-07 14:36:55 -080078
Ana Krulec75d4ffc2020-10-16 14:56:19 -070079 state = checkForResetAndStop(state);
80 if (state == TimerState::STOPPED) {
81 break;
Ady Abrahama1a49af2019-02-07 14:36:55 -080082 }
Ana Krulec75d4ffc2020-10-16 14:56:19 -070083
84 if (state == TimerState::IDLE) {
Ady Abraham1e19ac92020-11-17 16:56:00 -080085 int result = sem_wait(&mSemaphore);
86 if (result && errno != EINTR) {
87 std::stringstream ss;
88 ss << "sem_wait failed (" << errno << ")";
89 LOG_ALWAYS_FATAL("%s", ss.str().c_str());
90 }
Ana Krulec75d4ffc2020-10-16 14:56:19 -070091 continue;
92 }
93
94 if (state == TimerState::RESET) {
95 triggerReset = true;
96 }
97
Ady Abrahama1a49af2019-02-07 14:36:55 -080098 if (triggerReset && mResetCallback) {
99 mResetCallback();
100 }
101
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700102 state = checkForResetAndStop(state);
103 if (state == TimerState::STOPPED) {
104 break;
105 }
Ady Abrahama1a49af2019-02-07 14:36:55 -0800106
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700107 auto triggerTime = std::chrono::steady_clock::now() + mInterval;
108 state = TimerState::WAITING;
109 while (state == TimerState::WAITING) {
110 constexpr auto zero = std::chrono::steady_clock::duration::zero();
111 // Wait for mInterval time for semaphore signal.
112 struct timespec ts;
113 calculateTimeoutTime(std::chrono::nanoseconds(mInterval), &ts);
Ady Abraham1e19ac92020-11-17 16:56:00 -0800114 int result = sem_clockwait(&mSemaphore, CLOCK_MONOTONIC, &ts);
115 if (result && errno != ETIMEDOUT && errno != EINTR) {
116 std::stringstream ss;
117 ss << "sem_clockwait failed (" << errno << ")";
118 LOG_ALWAYS_FATAL("%s", ss.str().c_str());
119 }
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700120
121 state = checkForResetAndStop(state);
122 if (state == TimerState::RESET) {
123 triggerTime = std::chrono::steady_clock::now() + mInterval;
124 state = TimerState::WAITING;
125 } else if (state == TimerState::WAITING &&
126 (triggerTime - std::chrono::steady_clock::now()) <= zero) {
127 triggerTimeout = true;
128 state = TimerState::IDLE;
Ana Krulecfb772822018-11-30 10:44:07 +0100129 }
Ana Krulecfb772822018-11-30 10:44:07 +0100130 }
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700131
Ady Abrahama1a49af2019-02-07 14:36:55 -0800132 if (triggerTimeout && mTimeoutCallback) {
133 mTimeoutCallback();
134 }
Ana Krulecfb772822018-11-30 10:44:07 +0100135 }
Dominik Laskowski49cea512019-11-12 14:13:23 -0800136}
Ana Krulecfb772822018-11-30 10:44:07 +0100137
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700138OneShotTimer::TimerState OneShotTimer::checkForResetAndStop(TimerState state) {
139 // Stop takes precedence of the reset.
140 if (mStopTriggered.exchange(false)) {
141 return TimerState::STOPPED;
Ana Krulecfb772822018-11-30 10:44:07 +0100142 }
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700143 // If the state was stopped, the thread was joined, and we cannot reset
144 // the timer anymore.
145 if (state != TimerState::STOPPED && mResetTriggered.exchange(false)) {
146 return TimerState::RESET;
147 }
148 return state;
149}
150
151void OneShotTimer::reset() {
152 mResetTriggered = true;
Ady Abraham1e19ac92020-11-17 16:56:00 -0800153 int result = sem_post(&mSemaphore);
154 LOG_ALWAYS_FATAL_IF(result, "sem_post failed");
Ana Krulecfb772822018-11-30 10:44:07 +0100155}
156
Dominik Laskowski49cea512019-11-12 14:13:23 -0800157std::string OneShotTimer::dump() const {
158 std::ostringstream stream;
159 stream << mInterval.count() << " ms";
160 return stream.str();
161}
162
Ana Krulecfb772822018-11-30 10:44:07 +0100163} // namespace scheduler
164} // namespace android