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 | |
| 19 | #include <chrono> |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 20 | #include <sstream> |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 21 | #include <thread> |
| 22 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | using namespace std::chrono_literals; |
| 25 | |
| 26 | constexpr int64_t kNsToSeconds = std::chrono::duration_cast<std::chrono::nanoseconds>(1s).count(); |
| 27 | |
| 28 | // The syscall interface uses a pair of integers for the timestamp. The first |
| 29 | // (tv_sec) is the whole count of seconds. The second (tv_nsec) is the |
| 30 | // nanosecond part of the count. This function takes care of translation. |
| 31 | void calculateTimeoutTime(std::chrono::nanoseconds timestamp, timespec* spec) { |
| 32 | clock_gettime(CLOCK_MONOTONIC, spec); |
| 33 | spec->tv_sec += static_cast<__kernel_time_t>(timestamp.count() / kNsToSeconds); |
| 34 | spec->tv_nsec += timestamp.count() % kNsToSeconds; |
| 35 | } |
| 36 | } // namespace |
| 37 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 38 | namespace android { |
| 39 | namespace scheduler { |
| 40 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 41 | OneShotTimer::OneShotTimer(const Interval& interval, const ResetCallback& resetCallback, |
| 42 | const TimeoutCallback& timeoutCallback) |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 43 | : mInterval(interval), mResetCallback(resetCallback), mTimeoutCallback(timeoutCallback) {} |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 44 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 45 | OneShotTimer::~OneShotTimer() { |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 46 | stop(); |
| 47 | } |
| 48 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 49 | void OneShotTimer::start() { |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 50 | sem_init(&mSemaphore, 0, 0); |
| 51 | |
| 52 | if (!mThread.joinable()) { |
| 53 | // Only create thread if it has not been created. |
| 54 | mThread = std::thread(&OneShotTimer::loop, this); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 55 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 58 | void OneShotTimer::stop() { |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 59 | mStopTriggered = true; |
| 60 | sem_post(&mSemaphore); |
| 61 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 62 | if (mThread.joinable()) { |
| 63 | mThread.join(); |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 64 | sem_destroy(&mSemaphore); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Ana Krulec | f2c006d | 2019-06-21 15:37:07 -0700 | [diff] [blame] | 68 | void OneShotTimer::loop() { |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 69 | TimerState state = TimerState::RESET; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 70 | while (true) { |
| 71 | bool triggerReset = false; |
| 72 | bool triggerTimeout = false; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 73 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 74 | state = checkForResetAndStop(state); |
| 75 | if (state == TimerState::STOPPED) { |
| 76 | break; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 77 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 78 | |
| 79 | if (state == TimerState::IDLE) { |
| 80 | sem_wait(&mSemaphore); |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | if (state == TimerState::RESET) { |
| 85 | triggerReset = true; |
| 86 | } |
| 87 | |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 88 | if (triggerReset && mResetCallback) { |
| 89 | mResetCallback(); |
| 90 | } |
| 91 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 92 | state = checkForResetAndStop(state); |
| 93 | if (state == TimerState::STOPPED) { |
| 94 | break; |
| 95 | } |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 96 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 97 | auto triggerTime = std::chrono::steady_clock::now() + mInterval; |
| 98 | state = TimerState::WAITING; |
| 99 | while (state == TimerState::WAITING) { |
| 100 | constexpr auto zero = std::chrono::steady_clock::duration::zero(); |
| 101 | // Wait for mInterval time for semaphore signal. |
| 102 | struct timespec ts; |
| 103 | calculateTimeoutTime(std::chrono::nanoseconds(mInterval), &ts); |
| 104 | sem_clockwait(&mSemaphore, CLOCK_MONOTONIC, &ts); |
| 105 | |
| 106 | state = checkForResetAndStop(state); |
| 107 | if (state == TimerState::RESET) { |
| 108 | triggerTime = std::chrono::steady_clock::now() + mInterval; |
| 109 | state = TimerState::WAITING; |
| 110 | } else if (state == TimerState::WAITING && |
| 111 | (triggerTime - std::chrono::steady_clock::now()) <= zero) { |
| 112 | triggerTimeout = true; |
| 113 | state = TimerState::IDLE; |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 114 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 115 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 116 | |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 117 | if (triggerTimeout && mTimeoutCallback) { |
| 118 | mTimeoutCallback(); |
| 119 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 120 | } |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 121 | } |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 122 | |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 123 | OneShotTimer::TimerState OneShotTimer::checkForResetAndStop(TimerState state) { |
| 124 | // Stop takes precedence of the reset. |
| 125 | if (mStopTriggered.exchange(false)) { |
| 126 | return TimerState::STOPPED; |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 127 | } |
Ana Krulec | 75d4ffc | 2020-10-16 14:56:19 -0700 | [diff] [blame] | 128 | // If the state was stopped, the thread was joined, and we cannot reset |
| 129 | // the timer anymore. |
| 130 | if (state != TimerState::STOPPED && mResetTriggered.exchange(false)) { |
| 131 | return TimerState::RESET; |
| 132 | } |
| 133 | return state; |
| 134 | } |
| 135 | |
| 136 | void OneShotTimer::reset() { |
| 137 | mResetTriggered = true; |
| 138 | sem_post(&mSemaphore); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 141 | std::string OneShotTimer::dump() const { |
| 142 | std::ostringstream stream; |
| 143 | stream << mInterval.count() << " ms"; |
| 144 | return stream.str(); |
| 145 | } |
| 146 | |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 147 | } // namespace scheduler |
| 148 | } // namespace android |