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) { |
| 50 | LOG_ALWAYS_FATAL_IF(!mClock, "Clock must not be provided"); |
| 51 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 52 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 53 | OneShotTimer::~OneShotTimer() { |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 54 | stop(); |
| 55 | } |
| 56 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 57 | void OneShotTimer::start() { |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame] | 58 | int result = sem_init(&mSemaphore, 0, 0); |
| 59 | LOG_ALWAYS_FATAL_IF(result, "sem_init failed"); |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 60 | |
| 61 | if (!mThread.joinable()) { |
| 62 | // Only create thread if it has not been created. |
| 63 | mThread = std::thread(&OneShotTimer::loop, this); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 64 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 67 | void OneShotTimer::stop() { |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 68 | mStopTriggered = true; |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame] | 69 | int result = sem_post(&mSemaphore); |
| 70 | LOG_ALWAYS_FATAL_IF(result, "sem_post failed"); |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 71 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 72 | if (mThread.joinable()) { |
| 73 | mThread.join(); |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame] | 74 | result = sem_destroy(&mSemaphore); |
| 75 | LOG_ALWAYS_FATAL_IF(result, "sem_destroy failed"); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 79 | void OneShotTimer::loop() { |
Ady Abraham | db3dfee | 2020-11-17 17:07:12 -0800 | [diff] [blame] | 80 | if (pthread_setname_np(pthread_self(), mName.c_str())) { |
| 81 | ALOGW("Failed to set thread name on dispatch thread"); |
| 82 | } |
| 83 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 84 | TimerState state = TimerState::RESET; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 85 | while (true) { |
| 86 | bool triggerReset = false; |
| 87 | bool triggerTimeout = false; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 88 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 89 | state = checkForResetAndStop(state); |
| 90 | if (state == TimerState::STOPPED) { |
| 91 | break; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 92 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 93 | |
| 94 | if (state == TimerState::IDLE) { |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame] | 95 | int result = sem_wait(&mSemaphore); |
| 96 | if (result && errno != EINTR) { |
| 97 | std::stringstream ss; |
| 98 | ss << "sem_wait failed (" << errno << ")"; |
| 99 | LOG_ALWAYS_FATAL("%s", ss.str().c_str()); |
| 100 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 101 | continue; |
| 102 | } |
| 103 | |
| 104 | if (state == TimerState::RESET) { |
| 105 | triggerReset = true; |
| 106 | } |
| 107 | |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 108 | if (triggerReset && mResetCallback) { |
| 109 | mResetCallback(); |
| 110 | } |
| 111 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 112 | state = checkForResetAndStop(state); |
| 113 | if (state == TimerState::STOPPED) { |
| 114 | break; |
| 115 | } |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 116 | |
Ady Abraham | 018ff0b | 2021-04-19 23:39:36 -0700 | [diff] [blame] | 117 | auto triggerTime = mClock->now() + mInterval; |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 118 | state = TimerState::WAITING; |
| 119 | while (state == TimerState::WAITING) { |
| 120 | constexpr auto zero = std::chrono::steady_clock::duration::zero(); |
| 121 | // Wait for mInterval time for semaphore signal. |
| 122 | struct timespec ts; |
| 123 | calculateTimeoutTime(std::chrono::nanoseconds(mInterval), &ts); |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame] | 124 | int result = sem_clockwait(&mSemaphore, CLOCK_MONOTONIC, &ts); |
| 125 | if (result && errno != ETIMEDOUT && errno != EINTR) { |
| 126 | std::stringstream ss; |
| 127 | ss << "sem_clockwait failed (" << errno << ")"; |
| 128 | LOG_ALWAYS_FATAL("%s", ss.str().c_str()); |
| 129 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 130 | |
| 131 | state = checkForResetAndStop(state); |
| 132 | if (state == TimerState::RESET) { |
Ady Abraham | 018ff0b | 2021-04-19 23:39:36 -0700 | [diff] [blame] | 133 | triggerTime = mClock->now() + mInterval; |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 134 | state = TimerState::WAITING; |
Ady Abraham | 018ff0b | 2021-04-19 23:39:36 -0700 | [diff] [blame] | 135 | } else if (state == TimerState::WAITING && (triggerTime - mClock->now()) <= zero) { |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 136 | triggerTimeout = true; |
| 137 | state = TimerState::IDLE; |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 138 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 139 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 140 | |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 141 | if (triggerTimeout && mTimeoutCallback) { |
| 142 | mTimeoutCallback(); |
| 143 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 144 | } |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 145 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 146 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 147 | OneShotTimer::TimerState OneShotTimer::checkForResetAndStop(TimerState state) { |
| 148 | // Stop takes precedence of the reset. |
| 149 | if (mStopTriggered.exchange(false)) { |
| 150 | return TimerState::STOPPED; |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 151 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 152 | // If the state was stopped, the thread was joined, and we cannot reset |
| 153 | // the timer anymore. |
| 154 | if (state != TimerState::STOPPED && mResetTriggered.exchange(false)) { |
| 155 | return TimerState::RESET; |
| 156 | } |
| 157 | return state; |
| 158 | } |
| 159 | |
| 160 | void OneShotTimer::reset() { |
| 161 | mResetTriggered = true; |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame] | 162 | int result = sem_post(&mSemaphore); |
| 163 | LOG_ALWAYS_FATAL_IF(result, "sem_post failed"); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 166 | std::string OneShotTimer::dump() const { |
| 167 | std::ostringstream stream; |
| 168 | stream << mInterval.count() << " ms"; |
| 169 | return stream.str(); |
| 170 | } |
| 171 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 172 | } // namespace scheduler |
| 173 | } // namespace android |