Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | #define LOG_TAG "TimerThread" |
| 18 | |
| 19 | #include <optional> |
| 20 | |
| 21 | #include <mediautils/TimerThread.h> |
| 22 | #include <utils/ThreadDefs.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | TimerThread::TimerThread() : mThread([this] { threadFunc(); }) { |
| 27 | pthread_setname_np(mThread.native_handle(), "TimeCheckThread"); |
| 28 | pthread_setschedprio(mThread.native_handle(), PRIORITY_URGENT_AUDIO); |
| 29 | } |
| 30 | |
| 31 | TimerThread::~TimerThread() { |
| 32 | { |
| 33 | std::lock_guard _l(mMutex); |
| 34 | mShouldExit = true; |
| 35 | mCond.notify_all(); |
| 36 | } |
| 37 | mThread.join(); |
| 38 | } |
| 39 | |
| 40 | TimerThread::Handle TimerThread::scheduleTaskAtDeadline(std::function<void()>&& func, |
| 41 | TimePoint deadline) { |
| 42 | std::lock_guard _l(mMutex); |
| 43 | |
| 44 | // To avoid key collisions, advance by 1 tick until the key is unique. |
| 45 | for (; mMonitorRequests.find(deadline) != mMonitorRequests.end(); |
| 46 | deadline += TimePoint::duration(1)) |
| 47 | ; |
| 48 | mMonitorRequests.emplace(deadline, std::move(func)); |
| 49 | mCond.notify_all(); |
| 50 | return deadline; |
| 51 | } |
| 52 | |
Andy Hung | 5c6d68a | 2022-03-09 21:54:59 -0800 | [diff] [blame] | 53 | // Returns true if cancelled, false if handle doesn't exist. |
| 54 | // Beware of lock inversion with cancelTask() as the callback |
| 55 | // is called while holding mMutex. |
| 56 | bool TimerThread::cancelTask(Handle handle) { |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 57 | std::lock_guard _l(mMutex); |
Andy Hung | 5c6d68a | 2022-03-09 21:54:59 -0800 | [diff] [blame] | 58 | return mMonitorRequests.erase(handle) != 0; |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void TimerThread::threadFunc() { |
| 62 | std::unique_lock _l(mMutex); |
| 63 | |
| 64 | while (!mShouldExit) { |
| 65 | if (!mMonitorRequests.empty()) { |
| 66 | TimePoint nextDeadline = mMonitorRequests.begin()->first; |
| 67 | if (nextDeadline < std::chrono::steady_clock::now()) { |
| 68 | // Deadline expired. |
| 69 | mMonitorRequests.begin()->second(); |
| 70 | mMonitorRequests.erase(mMonitorRequests.begin()); |
| 71 | } |
| 72 | mCond.wait_until(_l, nextDeadline); |
| 73 | } else { |
| 74 | mCond.wait(_l); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | } // namespace android |