Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
| 17 | #include <chrono> |
| 18 | #include <thread> |
| 19 | |
| 20 | #include <vibratorservice/VibratorCallbackScheduler.h> |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | namespace vibrator { |
| 25 | |
| 26 | // ------------------------------------------------------------------------------------------------- |
| 27 | |
| 28 | bool DelayedCallback::isExpired() const { |
| 29 | return mExpiration <= std::chrono::steady_clock::now(); |
| 30 | } |
| 31 | |
| 32 | DelayedCallback::Timestamp DelayedCallback::getExpiration() const { |
| 33 | return mExpiration; |
| 34 | } |
| 35 | |
| 36 | void DelayedCallback::run() const { |
| 37 | mCallback(); |
| 38 | } |
| 39 | |
| 40 | bool DelayedCallback::operator<(const DelayedCallback& other) const { |
| 41 | return mExpiration < other.mExpiration; |
| 42 | } |
| 43 | |
| 44 | bool DelayedCallback::operator>(const DelayedCallback& other) const { |
| 45 | return mExpiration > other.mExpiration; |
| 46 | } |
| 47 | |
| 48 | // ------------------------------------------------------------------------------------------------- |
| 49 | |
| 50 | CallbackScheduler::~CallbackScheduler() { |
| 51 | { |
| 52 | std::lock_guard<std::mutex> lock(mMutex); |
| 53 | mFinished = true; |
| 54 | } |
| 55 | mCondition.notify_all(); |
| 56 | if (mCallbackThread && mCallbackThread->joinable()) { |
| 57 | mCallbackThread->join(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void CallbackScheduler::schedule(std::function<void()> callback, std::chrono::milliseconds delay) { |
| 62 | { |
| 63 | std::lock_guard<std::mutex> lock(mMutex); |
| 64 | if (mCallbackThread == nullptr) { |
| 65 | mCallbackThread = std::make_unique<std::thread>(&CallbackScheduler::loop, this); |
| 66 | } |
| 67 | mQueue.emplace(DelayedCallback(callback, delay)); |
| 68 | } |
| 69 | mCondition.notify_all(); |
| 70 | } |
| 71 | |
| 72 | void CallbackScheduler::loop() { |
| 73 | while (true) { |
Colin Cross | 0d5e129 | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 74 | std::unique_lock<std::mutex> lock(mMutex); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 75 | if (mFinished) { |
| 76 | // Destructor was called, so let the callback thread die. |
| 77 | break; |
| 78 | } |
| 79 | while (!mQueue.empty() && mQueue.top().isExpired()) { |
Colin Cross | 0d5e129 | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 80 | DelayedCallback callback = mQueue.top(); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 81 | mQueue.pop(); |
Colin Cross | 0d5e129 | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 82 | lock.unlock(); |
| 83 | callback.run(); |
| 84 | lock.lock(); |
Lais Andrade | 10d9dc7 | 2020-05-20 12:00:49 +0000 | [diff] [blame] | 85 | } |
| 86 | if (mQueue.empty()) { |
| 87 | // Wait until a new callback is scheduled. |
| 88 | mCondition.wait(mMutex); |
| 89 | } else { |
| 90 | // Wait until next callback expires, or a new one is scheduled. |
| 91 | mCondition.wait_until(mMutex, mQueue.top().getExpiration()); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // ------------------------------------------------------------------------------------------------- |
| 97 | |
| 98 | }; // namespace vibrator |
| 99 | |
| 100 | }; // namespace android |