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
18 #include <utils/Log.h>
19 #include <media/TimeCheck.h>
20 #include <media/EventLog.h>
21
22 namespace android {
23
24 /* static */
getTimeCheckThread()25 sp<TimeCheck::TimeCheckThread> TimeCheck::getTimeCheckThread()
26 {
27 static sp<TimeCheck::TimeCheckThread> sTimeCheckThread = new TimeCheck::TimeCheckThread();
28 return sTimeCheckThread;
29 }
30
TimeCheck(const char * tag,uint32_t timeoutMs)31 TimeCheck::TimeCheck(const char *tag, uint32_t timeoutMs)
32 : mEndTimeNs(getTimeCheckThread()->startMonitoring(tag, timeoutMs))
33 {
34 }
35
~TimeCheck()36 TimeCheck::~TimeCheck() {
37 getTimeCheckThread()->stopMonitoring(mEndTimeNs);
38 }
39
~TimeCheckThread()40 TimeCheck::TimeCheckThread::~TimeCheckThread()
41 {
42 AutoMutex _l(mMutex);
43 requestExit();
44 mMonitorRequests.clear();
45 mCond.signal();
46 }
47
startMonitoring(const char * tag,uint32_t timeoutMs)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
stopMonitoring(nsecs_t endTimeNs)57 void TimeCheck::TimeCheckThread::stopMonitoring(nsecs_t endTimeNs) {
58 Mutex::Autolock _l(mMutex);
59 mMonitorRequests.removeItem(endTimeNs);
60 mCond.signal();
61 }
62
threadLoop()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 }
85 }
86 if (status != NO_ERROR) {
87 LOG_EVENT_STRING(LOGTAG_AUDIO_BINDER_TIMEOUT, tag);
88 LOG_ALWAYS_FATAL("TimeCheck timeout for %s", tag);
89 }
90 return true;
91 }
92
93 }; // namespace android
94