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