blob: 96f78023a381c0e12c5599d984c307f3704efddf [file] [log] [blame]
Eric Laurent3528c932018-02-23 17:17:22 -08001/*
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 Laurent39b09b52018-06-29 12:24:40 -070018#include <utils/Log.h>
Marco Nelissencf90b492019-09-26 11:20:54 -070019#include <mediautils/TimeCheck.h>
20#include <mediautils/EventLog.h>
Eric Laurent3528c932018-02-23 17:17:22 -080021
22namespace android {
23
24/* static */
25sp<TimeCheck::TimeCheckThread> TimeCheck::getTimeCheckThread()
26{
27 static sp<TimeCheck::TimeCheckThread> sTimeCheckThread = new TimeCheck::TimeCheckThread();
28 return sTimeCheckThread;
29}
30
31TimeCheck::TimeCheck(const char *tag, uint32_t timeoutMs)
32 : mEndTimeNs(getTimeCheckThread()->startMonitoring(tag, timeoutMs))
33{
34}
35
36TimeCheck::~TimeCheck() {
37 getTimeCheckThread()->stopMonitoring(mEndTimeNs);
38}
39
40TimeCheck::TimeCheckThread::~TimeCheckThread()
41{
42 AutoMutex _l(mMutex);
43 requestExit();
44 mMonitorRequests.clear();
45 mCond.signal();
46}
47
48nsecs_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
57void TimeCheck::TimeCheckThread::stopMonitoring(nsecs_t endTimeNs) {
58 Mutex::Autolock _l(mMutex);
59 mMonitorRequests.removeItem(endTimeNs);
60 mCond.signal();
61}
62
63bool 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 Laurent9b9b3de2019-10-14 18:47:34 -070085 if (status != NO_ERROR) {
86 LOG_EVENT_STRING(LOGTAG_AUDIO_BINDER_TIMEOUT, tag);
87 LOG_ALWAYS_FATAL("TimeCheck timeout for %s", tag);
88 }
Eric Laurent39b09b52018-06-29 12:24:40 -070089 }
Eric Laurent3528c932018-02-23 17:17:22 -080090 return true;
91}
92
93}; // namespace android