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 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 43 | OneShotTimer::OneShotTimer(const Interval& interval, const ResetCallback& resetCallback, |
| 44 | const TimeoutCallback& timeoutCallback) |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 45 | : mInterval(interval), mResetCallback(resetCallback), mTimeoutCallback(timeoutCallback) {} |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 46 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 47 | OneShotTimer::~OneShotTimer() { |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 48 | stop(); |
| 49 | } |
| 50 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 51 | void OneShotTimer::start() { |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame^] | 52 | int result = sem_init(&mSemaphore, 0, 0); |
| 53 | LOG_ALWAYS_FATAL_IF(result, "sem_init failed"); |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 54 | |
| 55 | if (!mThread.joinable()) { |
| 56 | // Only create thread if it has not been created. |
| 57 | mThread = std::thread(&OneShotTimer::loop, this); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 58 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 61 | void OneShotTimer::stop() { |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 62 | mStopTriggered = true; |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame^] | 63 | int result = sem_post(&mSemaphore); |
| 64 | LOG_ALWAYS_FATAL_IF(result, "sem_post failed"); |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 65 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 66 | if (mThread.joinable()) { |
| 67 | mThread.join(); |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame^] | 68 | result = sem_destroy(&mSemaphore); |
| 69 | LOG_ALWAYS_FATAL_IF(result, "sem_destroy failed"); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 73 | void OneShotTimer::loop() { |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 74 | TimerState state = TimerState::RESET; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 75 | while (true) { |
| 76 | bool triggerReset = false; |
| 77 | bool triggerTimeout = false; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 78 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 79 | state = checkForResetAndStop(state); |
| 80 | if (state == TimerState::STOPPED) { |
| 81 | break; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 82 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 83 | |
| 84 | if (state == TimerState::IDLE) { |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame^] | 85 | 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 Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 91 | continue; |
| 92 | } |
| 93 | |
| 94 | if (state == TimerState::RESET) { |
| 95 | triggerReset = true; |
| 96 | } |
| 97 | |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 98 | if (triggerReset && mResetCallback) { |
| 99 | mResetCallback(); |
| 100 | } |
| 101 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 102 | state = checkForResetAndStop(state); |
| 103 | if (state == TimerState::STOPPED) { |
| 104 | break; |
| 105 | } |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 106 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 107 | 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 Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame^] | 114 | 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 Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 120 | |
| 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 Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 129 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 130 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 131 | |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 132 | if (triggerTimeout && mTimeoutCallback) { |
| 133 | mTimeoutCallback(); |
| 134 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 135 | } |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 136 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 137 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 138 | OneShotTimer::TimerState OneShotTimer::checkForResetAndStop(TimerState state) { |
| 139 | // Stop takes precedence of the reset. |
| 140 | if (mStopTriggered.exchange(false)) { |
| 141 | return TimerState::STOPPED; |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 142 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 143 | // 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 | |
| 151 | void OneShotTimer::reset() { |
| 152 | mResetTriggered = true; |
Ady Abraham | 1e19ac9 | 2020-11-17 16:56:00 -0800 | [diff] [blame^] | 153 | int result = sem_post(&mSemaphore); |
| 154 | LOG_ALWAYS_FATAL_IF(result, "sem_post failed"); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 157 | std::string OneShotTimer::dump() const { |
| 158 | std::ostringstream stream; |
| 159 | stream << mInterval.count() << " ms"; |
| 160 | return stream.str(); |
| 161 | } |
| 162 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 163 | } // namespace scheduler |
| 164 | } // namespace android |