Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 1 | /* |
| 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 Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 17 | #include "OneShotTimer.h" |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame] | 18 | #include <utils/Log.h> |
| 19 | #include <utils/Timers.h> |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 20 | #include <chrono> |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 21 | #include <sstream> |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 22 | #include <thread> |
| 23 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 24 | namespace { |
| 25 | using namespace std::chrono_literals; |
| 26 | |
| 27 | constexpr int64_t kNsToSeconds = std::chrono::duration_cast<std::chrono::nanoseconds>(1s).count(); |
| 28 | |
| 29 | // The syscall interface uses a pair of integers for the timestamp. The first |
| 30 | // (tv_sec) is the whole count of seconds. The second (tv_nsec) is the |
| 31 | // nanosecond part of the count. This function takes care of translation. |
| 32 | void calculateTimeoutTime(std::chrono::nanoseconds timestamp, timespec* spec) { |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame] | 33 | const nsecs_t timeout = systemTime(CLOCK_MONOTONIC) + timestamp.count(); |
| 34 | spec->tv_sec = static_cast<__kernel_time_t>(timeout / kNsToSeconds); |
| 35 | spec->tv_nsec = timeout % kNsToSeconds; |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 36 | } |
| 37 | } // namespace |
| 38 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 39 | namespace android { |
| 40 | namespace scheduler { |
| 41 | |
Ady Abraham | db3dfee | 2020-11-17 17:07:12 -0800 | [diff] [blame] | 42 | OneShotTimer::OneShotTimer(std::string name, const Interval& interval, |
| 43 | const ResetCallback& resetCallback, |
Alec Mouri | 07b27ce | 2021-04-26 16:31:44 -0700 | [diff] [blame] | 44 | const TimeoutCallback& timeoutCallback, std::unique_ptr<Clock> clock) |
Ady Abraham | 018ff0b | 2021-04-19 23:39:36 -0700 | [diff] [blame] | 45 | : mClock(std::move(clock)), |
| 46 | mName(std::move(name)), |
Ady Abraham | db3dfee | 2020-11-17 17:07:12 -0800 | [diff] [blame] | 47 | mInterval(interval), |
| 48 | mResetCallback(resetCallback), |
Ady Abraham | 018ff0b | 2021-04-19 23:39:36 -0700 | [diff] [blame] | 49 | mTimeoutCallback(timeoutCallback) { |
Alec Mouri | aadcf94 | 2022-05-11 01:22:42 +0000 | [diff] [blame^] | 50 | mLastResetTime = std::chrono::steady_clock::time_point::min(); |
Ady Abraham | 018ff0b | 2021-04-19 23:39:36 -0700 | [diff] [blame] | 51 | LOG_ALWAYS_FATAL_IF(!mClock, "Clock must not be provided"); |
| 52 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 53 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 54 | OneShotTimer::~OneShotTimer() { |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 55 | stop(); |
| 56 | } |
| 57 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 58 | void OneShotTimer::start() { |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame] | 59 | int result = sem_init(&mSemaphore, 0, 0); |
| 60 | LOG_ALWAYS_FATAL_IF(result, "sem_init failed"); |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 61 | |
| 62 | if (!mThread.joinable()) { |
| 63 | // Only create thread if it has not been created. |
| 64 | mThread = std::thread(&OneShotTimer::loop, this); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 65 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 68 | void OneShotTimer::stop() { |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 69 | mStopTriggered = true; |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame] | 70 | int result = sem_post(&mSemaphore); |
| 71 | LOG_ALWAYS_FATAL_IF(result, "sem_post failed"); |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 72 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 73 | if (mThread.joinable()) { |
| 74 | mThread.join(); |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame] | 75 | result = sem_destroy(&mSemaphore); |
| 76 | LOG_ALWAYS_FATAL_IF(result, "sem_destroy failed"); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 80 | void OneShotTimer::loop() { |
Ady Abraham | db3dfee | 2020-11-17 17:07:12 -0800 | [diff] [blame] | 81 | if (pthread_setname_np(pthread_self(), mName.c_str())) { |
| 82 | ALOGW("Failed to set thread name on dispatch thread"); |
| 83 | } |
| 84 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 85 | TimerState state = TimerState::RESET; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 86 | while (true) { |
| 87 | bool triggerReset = false; |
| 88 | bool triggerTimeout = false; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 89 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 90 | state = checkForResetAndStop(state); |
| 91 | if (state == TimerState::STOPPED) { |
| 92 | break; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 93 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 94 | |
| 95 | if (state == TimerState::IDLE) { |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame] | 96 | int result = sem_wait(&mSemaphore); |
| 97 | if (result && errno != EINTR) { |
| 98 | std::stringstream ss; |
| 99 | ss << "sem_wait failed (" << errno << ")"; |
| 100 | LOG_ALWAYS_FATAL("%s", ss.str().c_str()); |
| 101 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 102 | continue; |
| 103 | } |
| 104 | |
| 105 | if (state == TimerState::RESET) { |
| 106 | triggerReset = true; |
| 107 | } |
| 108 | |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 109 | if (triggerReset && mResetCallback) { |
| 110 | mResetCallback(); |
| 111 | } |
| 112 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 113 | state = checkForResetAndStop(state); |
| 114 | if (state == TimerState::STOPPED) { |
| 115 | break; |
| 116 | } |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 117 | |
Ady Abraham | 018ff0b | 2021-04-19 23:39:36 -0700 | [diff] [blame] | 118 | auto triggerTime = mClock->now() + mInterval; |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 119 | state = TimerState::WAITING; |
Alec Mouri | aadcf94 | 2022-05-11 01:22:42 +0000 | [diff] [blame^] | 120 | while (true) { |
| 121 | mWaiting = true; |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 122 | constexpr auto zero = std::chrono::steady_clock::duration::zero(); |
Alec Mouri | aadcf94 | 2022-05-11 01:22:42 +0000 | [diff] [blame^] | 123 | // Wait for mInterval time to check if we need to reset or drop into the idle state. |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 124 | struct timespec ts; |
| 125 | calculateTimeoutTime(std::chrono::nanoseconds(mInterval), &ts); |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame] | 126 | int result = sem_clockwait(&mSemaphore, CLOCK_MONOTONIC, &ts); |
| 127 | if (result && errno != ETIMEDOUT && errno != EINTR) { |
| 128 | std::stringstream ss; |
| 129 | ss << "sem_clockwait failed (" << errno << ")"; |
| 130 | LOG_ALWAYS_FATAL("%s", ss.str().c_str()); |
| 131 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 132 | |
Alec Mouri | aadcf94 | 2022-05-11 01:22:42 +0000 | [diff] [blame^] | 133 | mWaiting = false; |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 134 | state = checkForResetAndStop(state); |
Alec Mouri | aadcf94 | 2022-05-11 01:22:42 +0000 | [diff] [blame^] | 135 | if (state == TimerState::STOPPED) { |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | if (state == TimerState::WAITING && (triggerTime - mClock->now()) <= zero) { |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 140 | triggerTimeout = true; |
| 141 | state = TimerState::IDLE; |
Alec Mouri | aadcf94 | 2022-05-11 01:22:42 +0000 | [diff] [blame^] | 142 | break; |
| 143 | } |
| 144 | |
| 145 | if (state == TimerState::RESET) { |
| 146 | triggerTime = mLastResetTime.load() + mInterval; |
| 147 | state = TimerState::WAITING; |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 148 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 149 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 150 | |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 151 | if (triggerTimeout && mTimeoutCallback) { |
| 152 | mTimeoutCallback(); |
| 153 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 154 | } |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 155 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 156 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 157 | OneShotTimer::TimerState OneShotTimer::checkForResetAndStop(TimerState state) { |
| 158 | // Stop takes precedence of the reset. |
| 159 | if (mStopTriggered.exchange(false)) { |
| 160 | return TimerState::STOPPED; |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 161 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 162 | // If the state was stopped, the thread was joined, and we cannot reset |
| 163 | // the timer anymore. |
| 164 | if (state != TimerState::STOPPED && mResetTriggered.exchange(false)) { |
| 165 | return TimerState::RESET; |
| 166 | } |
| 167 | return state; |
| 168 | } |
| 169 | |
| 170 | void OneShotTimer::reset() { |
Alec Mouri | aadcf94 | 2022-05-11 01:22:42 +0000 | [diff] [blame^] | 171 | mLastResetTime = mClock->now(); |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 172 | mResetTriggered = true; |
Alec Mouri | aadcf94 | 2022-05-11 01:22:42 +0000 | [diff] [blame^] | 173 | // If mWaiting is true, then we are guaranteed to be in a block where we are waiting on |
| 174 | // mSemaphore for a timeout, rather than idling. So we can avoid a sem_post call since we can |
| 175 | // just check that we triggered a reset on timeout. |
| 176 | if (!mWaiting) { |
| 177 | LOG_ALWAYS_FATAL_IF(sem_post(&mSemaphore), "sem_post failed"); |
| 178 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 181 | std::string OneShotTimer::dump() const { |
| 182 | std::ostringstream stream; |
| 183 | stream << mInterval.count() << " ms"; |
| 184 | return stream.str(); |
| 185 | } |
| 186 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 187 | } // namespace scheduler |
| 188 | } // namespace android |