Eric Laurent | 3528c93 | 2018-02-23 17:17:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | |
| 17 | |
Eric Laurent | 39b09b5 | 2018-06-29 12:24:40 -0700 | [diff] [blame] | 18 | #include <utils/Log.h> |
Marco Nelissen | cf90b49 | 2019-09-26 11:20:54 -0700 | [diff] [blame] | 19 | #include <mediautils/TimeCheck.h> |
| 20 | #include <mediautils/EventLog.h> |
Eric Laurent | 3528c93 | 2018-02-23 17:17:22 -0800 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | /* static */ |
| 25 | sp<TimeCheck::TimeCheckThread> TimeCheck::getTimeCheckThread() |
| 26 | { |
| 27 | static sp<TimeCheck::TimeCheckThread> sTimeCheckThread = new TimeCheck::TimeCheckThread(); |
| 28 | return sTimeCheckThread; |
| 29 | } |
| 30 | |
| 31 | TimeCheck::TimeCheck(const char *tag, uint32_t timeoutMs) |
| 32 | : mEndTimeNs(getTimeCheckThread()->startMonitoring(tag, timeoutMs)) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | TimeCheck::~TimeCheck() { |
| 37 | getTimeCheckThread()->stopMonitoring(mEndTimeNs); |
| 38 | } |
| 39 | |
| 40 | TimeCheck::TimeCheckThread::~TimeCheckThread() |
| 41 | { |
| 42 | AutoMutex _l(mMutex); |
| 43 | requestExit(); |
| 44 | mMonitorRequests.clear(); |
| 45 | mCond.signal(); |
| 46 | } |
| 47 | |
| 48 | nsecs_t TimeCheck::TimeCheckThread::startMonitoring(const char *tag, uint32_t timeoutMs) { |
| 49 | Mutex::Autolock _l(mMutex); |
| 50 | nsecs_t endTimeNs = systemTime() + milliseconds(timeoutMs); |
| 51 | for (; mMonitorRequests.indexOfKey(endTimeNs) >= 0; ++endTimeNs); |
| 52 | mMonitorRequests.add(endTimeNs, tag); |
| 53 | mCond.signal(); |
| 54 | return endTimeNs; |
| 55 | } |
| 56 | |
| 57 | void TimeCheck::TimeCheckThread::stopMonitoring(nsecs_t endTimeNs) { |
| 58 | Mutex::Autolock _l(mMutex); |
| 59 | mMonitorRequests.removeItem(endTimeNs); |
| 60 | mCond.signal(); |
| 61 | } |
| 62 | |
| 63 | bool TimeCheck::TimeCheckThread::threadLoop() |
| 64 | { |
| 65 | status_t status = TIMED_OUT; |
| 66 | const char *tag; |
| 67 | { |
| 68 | AutoMutex _l(mMutex); |
| 69 | |
| 70 | if (exitPending()) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | nsecs_t endTimeNs = INT64_MAX; |
| 75 | // KeyedVector mMonitorRequests is ordered so take first entry as next timeout |
| 76 | if (mMonitorRequests.size() != 0) { |
| 77 | endTimeNs = mMonitorRequests.keyAt(0); |
| 78 | tag = mMonitorRequests.valueAt(0); |
| 79 | } |
| 80 | |
| 81 | const nsecs_t waitTimeNs = endTimeNs - systemTime(); |
| 82 | if (waitTimeNs > 0) { |
| 83 | status = mCond.waitRelative(mMutex, waitTimeNs); |
| 84 | } |
Eric Laurent | 9b9b3de | 2019-10-14 18:47:34 -0700 | [diff] [blame] | 85 | if (status != NO_ERROR) { |
| 86 | LOG_EVENT_STRING(LOGTAG_AUDIO_BINDER_TIMEOUT, tag); |
| 87 | LOG_ALWAYS_FATAL("TimeCheck timeout for %s", tag); |
| 88 | } |
Eric Laurent | 39b09b5 | 2018-06-29 12:24:40 -0700 | [diff] [blame] | 89 | } |
Eric Laurent | 3528c93 | 2018-02-23 17:17:22 -0800 | [diff] [blame] | 90 | return true; |
| 91 | } |
| 92 | |
| 93 | }; // namespace android |