blob: d65939839471eee2eed3fcc4e6ebe8bda7046422 [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
Ady Abraham018ff0b2021-04-19 23:39:36 -070043std::chrono::steady_clock::time_point OneShotTimer::Clock::now() const {
44 return std::chrono::steady_clock::now();
45}
46
Ady Abrahamdb3dfee2020-11-17 17:07:12 -080047OneShotTimer::OneShotTimer(std::string name, const Interval& interval,
48 const ResetCallback& resetCallback,
Ady Abraham018ff0b2021-04-19 23:39:36 -070049 const TimeoutCallback& timeoutCallback,
50 std::unique_ptr<OneShotTimer::Clock> clock)
51 : mClock(std::move(clock)),
52 mName(std::move(name)),
Ady Abrahamdb3dfee2020-11-17 17:07:12 -080053 mInterval(interval),
54 mResetCallback(resetCallback),
Ady Abraham018ff0b2021-04-19 23:39:36 -070055 mTimeoutCallback(timeoutCallback) {
56 LOG_ALWAYS_FATAL_IF(!mClock, "Clock must not be provided");
57}
Ana Krulecfb772822018-11-30 10:44:07 +010058
Ana Krulecf2c006d2019-06-21 15:37:07 -070059OneShotTimer::~OneShotTimer() {
Ana Krulecfb772822018-11-30 10:44:07 +010060 stop();
61}
62
Ana Krulecf2c006d2019-06-21 15:37:07 -070063void OneShotTimer::start() {
Ady Abraham1e19ac92020-11-17 16:56:00 -080064 int result = sem_init(&mSemaphore, 0, 0);
65 LOG_ALWAYS_FATAL_IF(result, "sem_init failed");
Ana Krulec75d4ffc2020-10-16 14:56:19 -070066
67 if (!mThread.joinable()) {
68 // Only create thread if it has not been created.
69 mThread = std::thread(&OneShotTimer::loop, this);
Ana Krulecfb772822018-11-30 10:44:07 +010070 }
Ana Krulecfb772822018-11-30 10:44:07 +010071}
72
Ana Krulecf2c006d2019-06-21 15:37:07 -070073void OneShotTimer::stop() {
Ana Krulec75d4ffc2020-10-16 14:56:19 -070074 mStopTriggered = true;
Ady Abraham1e19ac92020-11-17 16:56:00 -080075 int result = sem_post(&mSemaphore);
76 LOG_ALWAYS_FATAL_IF(result, "sem_post failed");
Ana Krulec75d4ffc2020-10-16 14:56:19 -070077
Ana Krulecfb772822018-11-30 10:44:07 +010078 if (mThread.joinable()) {
79 mThread.join();
Ady Abraham1e19ac92020-11-17 16:56:00 -080080 result = sem_destroy(&mSemaphore);
81 LOG_ALWAYS_FATAL_IF(result, "sem_destroy failed");
Ana Krulecfb772822018-11-30 10:44:07 +010082 }
83}
84
Ana Krulecf2c006d2019-06-21 15:37:07 -070085void OneShotTimer::loop() {
Ady Abrahamdb3dfee2020-11-17 17:07:12 -080086 if (pthread_setname_np(pthread_self(), mName.c_str())) {
87 ALOGW("Failed to set thread name on dispatch thread");
88 }
89
Ana Krulec75d4ffc2020-10-16 14:56:19 -070090 TimerState state = TimerState::RESET;
Ady Abrahama1a49af2019-02-07 14:36:55 -080091 while (true) {
92 bool triggerReset = false;
93 bool triggerTimeout = false;
Ady Abrahama1a49af2019-02-07 14:36:55 -080094
Ana Krulec75d4ffc2020-10-16 14:56:19 -070095 state = checkForResetAndStop(state);
96 if (state == TimerState::STOPPED) {
97 break;
Ady Abrahama1a49af2019-02-07 14:36:55 -080098 }
Ana Krulec75d4ffc2020-10-16 14:56:19 -070099
100 if (state == TimerState::IDLE) {
Ady Abraham1e19ac92020-11-17 16:56:00 -0800101 int result = sem_wait(&mSemaphore);
102 if (result && errno != EINTR) {
103 std::stringstream ss;
104 ss << "sem_wait failed (" << errno << ")";
105 LOG_ALWAYS_FATAL("%s", ss.str().c_str());
106 }
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700107 continue;
108 }
109
110 if (state == TimerState::RESET) {
111 triggerReset = true;
112 }
113
Ady Abrahama1a49af2019-02-07 14:36:55 -0800114 if (triggerReset && mResetCallback) {
115 mResetCallback();
116 }
117
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700118 state = checkForResetAndStop(state);
119 if (state == TimerState::STOPPED) {
120 break;
121 }
Ady Abrahama1a49af2019-02-07 14:36:55 -0800122
Ady Abraham018ff0b2021-04-19 23:39:36 -0700123 auto triggerTime = mClock->now() + mInterval;
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700124 state = TimerState::WAITING;
125 while (state == TimerState::WAITING) {
126 constexpr auto zero = std::chrono::steady_clock::duration::zero();
127 // Wait for mInterval time for semaphore signal.
128 struct timespec ts;
129 calculateTimeoutTime(std::chrono::nanoseconds(mInterval), &ts);
Ady Abraham1e19ac92020-11-17 16:56:00 -0800130 int result = sem_clockwait(&mSemaphore, CLOCK_MONOTONIC, &ts);
131 if (result && errno != ETIMEDOUT && errno != EINTR) {
132 std::stringstream ss;
133 ss << "sem_clockwait failed (" << errno << ")";
134 LOG_ALWAYS_FATAL("%s", ss.str().c_str());
135 }
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700136
137 state = checkForResetAndStop(state);
138 if (state == TimerState::RESET) {
Ady Abraham018ff0b2021-04-19 23:39:36 -0700139 triggerTime = mClock->now() + mInterval;
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700140 state = TimerState::WAITING;
Ady Abraham018ff0b2021-04-19 23:39:36 -0700141 } else if (state == TimerState::WAITING && (triggerTime - mClock->now()) <= zero) {
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700142 triggerTimeout = true;
143 state = TimerState::IDLE;
Ana Krulecfb772822018-11-30 10:44:07 +0100144 }
Ana Krulecfb772822018-11-30 10:44:07 +0100145 }
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700146
Ady Abrahama1a49af2019-02-07 14:36:55 -0800147 if (triggerTimeout && mTimeoutCallback) {
148 mTimeoutCallback();
149 }
Ana Krulecfb772822018-11-30 10:44:07 +0100150 }
Dominik Laskowski49cea512019-11-12 14:13:23 -0800151}
Ana Krulecfb772822018-11-30 10:44:07 +0100152
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700153OneShotTimer::TimerState OneShotTimer::checkForResetAndStop(TimerState state) {
154 // Stop takes precedence of the reset.
155 if (mStopTriggered.exchange(false)) {
156 return TimerState::STOPPED;
Ana Krulecfb772822018-11-30 10:44:07 +0100157 }
Ana Krulec75d4ffc2020-10-16 14:56:19 -0700158 // If the state was stopped, the thread was joined, and we cannot reset
159 // the timer anymore.
160 if (state != TimerState::STOPPED && mResetTriggered.exchange(false)) {
161 return TimerState::RESET;
162 }
163 return state;
164}
165
166void OneShotTimer::reset() {
167 mResetTriggered = true;
Ady Abraham1e19ac92020-11-17 16:56:00 -0800168 int result = sem_post(&mSemaphore);
169 LOG_ALWAYS_FATAL_IF(result, "sem_post failed");
Ana Krulecfb772822018-11-30 10:44:07 +0100170}
171
Dominik Laskowski49cea512019-11-12 14:13:23 -0800172std::string OneShotTimer::dump() const {
173 std::ostringstream stream;
174 stream << mInterval.count() << " ms";
175 return stream.str();
176}
177
Ana Krulecfb772822018-11-30 10:44:07 +0100178} // namespace scheduler
179} // namespace android