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> |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 20 | #include <sstream> |
| 21 | #include <unistd.h> |
| 22 | #include <vector> |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 23 | |
Andy Hung | d426582 | 2022-04-01 18:54:32 -0700 | [diff] [blame] | 24 | #include <mediautils/MediaUtilsDelayed.h> |
Atneya Nair | f5b6851 | 2022-05-23 20:02:49 -0400 | [diff] [blame] | 25 | #include <mediautils/TidWrapper.h> |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 26 | #include <mediautils/TimerThread.h> |
Andy Hung | f8ab093 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 27 | #include <utils/Log.h> |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 28 | #include <utils/ThreadDefs.h> |
| 29 | |
Andy Hung | 2aa1510 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 30 | using namespace std::chrono_literals; |
| 31 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 32 | namespace android::mediautils { |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 33 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 34 | extern std::string formatTime(std::chrono::system_clock::time_point t); |
| 35 | extern std::string_view timeSuffix(std::string_view time1, std::string_view time2); |
| 36 | |
| 37 | TimerThread::Handle TimerThread::scheduleTask( |
Andy Hung | f8ab093 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 38 | std::string_view tag, TimerCallback&& func, |
| 39 | Duration timeoutDuration, Duration secondChanceDuration) { |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 40 | const auto now = std::chrono::system_clock::now(); |
Andy Hung | 2aa1510 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 41 | auto request = std::make_shared<const Request>(now, now + |
| 42 | std::chrono::duration_cast<std::chrono::system_clock::duration>(timeoutDuration), |
Atneya Nair | f5b6851 | 2022-05-23 20:02:49 -0400 | [diff] [blame] | 43 | secondChanceDuration, getThreadIdWrapper(), tag); |
Andy Hung | 2aa1510 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 44 | return mMonitorThread.add(std::move(request), std::move(func), timeoutDuration); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Andy Hung | 35f9615 | 2022-07-15 15:18:59 -0700 | [diff] [blame] | 47 | TimerThread::Handle TimerThread::trackTask(std::string_view tag) { |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 48 | const auto now = std::chrono::system_clock::now(); |
Andy Hung | f8ab093 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 49 | auto request = std::make_shared<const Request>(now, now, |
Atneya Nair | f5b6851 | 2022-05-23 20:02:49 -0400 | [diff] [blame] | 50 | Duration{} /* secondChanceDuration */, getThreadIdWrapper(), tag); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 51 | return mNoTimeoutMap.add(std::move(request)); |
| 52 | } |
| 53 | |
| 54 | bool TimerThread::cancelTask(Handle handle) { |
Andy Hung | 2aa1510 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 55 | std::shared_ptr<const Request> request = isNoTimeoutHandle(handle) ? |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 56 | mNoTimeoutMap.remove(handle) : mMonitorThread.remove(handle); |
| 57 | if (!request) return false; |
| 58 | mRetiredQueue.add(std::move(request)); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | std::string TimerThread::toString(size_t retiredCount) const { |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 63 | // Note: These request queues are snapshot very close together but |
| 64 | // not at "identical" times as we don't use a class-wide lock. |
| 65 | |
| 66 | std::vector<std::shared_ptr<const Request>> timeoutRequests; |
| 67 | std::vector<std::shared_ptr<const Request>> retiredRequests; |
| 68 | mTimeoutQueue.copyRequests(timeoutRequests); |
| 69 | mRetiredQueue.copyRequests(retiredRequests, retiredCount); |
| 70 | std::vector<std::shared_ptr<const Request>> pendingRequests = |
| 71 | getPendingRequests(); |
| 72 | |
| 73 | struct Analysis analysis = analyzeTimeout(timeoutRequests, pendingRequests); |
| 74 | std::string analysisSummary; |
| 75 | if (!analysis.summary.empty()) { |
| 76 | analysisSummary = std::string("\nanalysis [ ").append(analysis.summary).append(" ]"); |
| 77 | } |
Andy Hung | 10ac711 | 2022-03-28 08:00:40 -0700 | [diff] [blame] | 78 | std::string timeoutStack; |
| 79 | if (analysis.timeoutTid != -1) { |
| 80 | timeoutStack = std::string("\ntimeout(") |
| 81 | .append(std::to_string(analysis.timeoutTid)).append(") callstack [\n") |
Andy Hung | d426582 | 2022-04-01 18:54:32 -0700 | [diff] [blame] | 82 | .append(getCallStackStringForTid(analysis.timeoutTid)).append("]"); |
Andy Hung | 10ac711 | 2022-03-28 08:00:40 -0700 | [diff] [blame] | 83 | } |
| 84 | std::string blockedStack; |
| 85 | if (analysis.HALBlockedTid != -1) { |
| 86 | blockedStack = std::string("\nblocked(") |
| 87 | .append(std::to_string(analysis.HALBlockedTid)).append(") callstack [\n") |
Andy Hung | d426582 | 2022-04-01 18:54:32 -0700 | [diff] [blame] | 88 | .append(getCallStackStringForTid(analysis.HALBlockedTid)).append("]"); |
Andy Hung | 10ac711 | 2022-03-28 08:00:40 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 91 | return std::string("now ") |
| 92 | .append(formatTime(std::chrono::system_clock::now())) |
Andy Hung | f8ab093 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 93 | .append("\nsecondChanceCount ") |
| 94 | .append(std::to_string(mMonitorThread.getSecondChanceCount())) |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 95 | .append(analysisSummary) |
| 96 | .append("\ntimeout [ ") |
| 97 | .append(requestsToString(timeoutRequests)) |
| 98 | .append(" ]\npending [ ") |
| 99 | .append(requestsToString(pendingRequests)) |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 100 | .append(" ]\nretired [ ") |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 101 | .append(requestsToString(retiredRequests)) |
Andy Hung | 10ac711 | 2022-03-28 08:00:40 -0700 | [diff] [blame] | 102 | .append(" ]") |
| 103 | .append(timeoutStack) |
| 104 | .append(blockedStack); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 107 | // A HAL method is where the substring "Hidl" is in the class name. |
| 108 | // The tag should look like: ... Hidl ... :: ... |
| 109 | // When the audio HAL is updated to AIDL perhaps we will use instead |
| 110 | // a global directory of HAL classes. |
| 111 | // |
| 112 | // See MethodStatistics.cpp: |
| 113 | // mediautils::getStatisticsClassesForModule(METHOD_STATISTICS_MODULE_NAME_AUDIO_HIDL) |
| 114 | // |
| 115 | /* static */ |
| 116 | bool TimerThread::isRequestFromHal(const std::shared_ptr<const Request>& request) { |
Andy Hung | 35f9615 | 2022-07-15 15:18:59 -0700 | [diff] [blame] | 117 | const size_t hidlPos = request->tag.asStringView().find("Hidl"); |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 118 | if (hidlPos == std::string::npos) return false; |
| 119 | // should be a separator afterwards Hidl which indicates the string was in the class. |
Andy Hung | 35f9615 | 2022-07-15 15:18:59 -0700 | [diff] [blame] | 120 | const size_t separatorPos = request->tag.asStringView().find("::", hidlPos); |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 121 | return separatorPos != std::string::npos; |
| 122 | } |
| 123 | |
| 124 | /* static */ |
| 125 | struct TimerThread::Analysis TimerThread::analyzeTimeout( |
| 126 | const std::vector<std::shared_ptr<const Request>>& timeoutRequests, |
| 127 | const std::vector<std::shared_ptr<const Request>>& pendingRequests) { |
| 128 | |
| 129 | if (timeoutRequests.empty() || pendingRequests.empty()) return {}; // nothing to say. |
| 130 | |
| 131 | // for now look at last timeout (in our case, the only timeout) |
| 132 | const std::shared_ptr<const Request> timeout = timeoutRequests.back(); |
| 133 | |
| 134 | // pending Requests that are problematic. |
| 135 | std::vector<std::shared_ptr<const Request>> pendingExact; |
| 136 | std::vector<std::shared_ptr<const Request>> pendingPossible; |
| 137 | |
Andy Hung | 2aa1510 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 138 | // We look at pending requests that were scheduled no later than kPendingDuration |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 139 | // after the timeout request. This prevents false matches with calls |
| 140 | // that naturally block for a short period of time |
| 141 | // such as HAL write() and read(). |
| 142 | // |
Andy Hung | 2aa1510 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 143 | constexpr Duration kPendingDuration = 1000ms; |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 144 | for (const auto& pending : pendingRequests) { |
| 145 | // If the pending tid is the same as timeout tid, problem identified. |
| 146 | if (pending->tid == timeout->tid) { |
| 147 | pendingExact.emplace_back(pending); |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | // if the pending tid is scheduled within time limit |
Andy Hung | 2aa1510 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 152 | if (pending->scheduled - timeout->scheduled < kPendingDuration) { |
Andy Hung | f45f34c | 2022-03-25 13:09:03 -0700 | [diff] [blame] | 153 | pendingPossible.emplace_back(pending); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | struct Analysis analysis{}; |
| 158 | |
| 159 | analysis.timeoutTid = timeout->tid; |
| 160 | std::string& summary = analysis.summary; |
| 161 | if (!pendingExact.empty()) { |
| 162 | const auto& request = pendingExact.front(); |
| 163 | const bool hal = isRequestFromHal(request); |
| 164 | |
| 165 | if (hal) { |
| 166 | summary = std::string("Blocked directly due to HAL call: ") |
| 167 | .append(request->toString()); |
| 168 | } |
| 169 | } |
| 170 | if (summary.empty() && !pendingPossible.empty()) { |
| 171 | for (const auto& request : pendingPossible) { |
| 172 | const bool hal = isRequestFromHal(request); |
| 173 | if (hal) { |
| 174 | // The first blocked call is the most likely one. |
| 175 | // Recent calls might be temporarily blocked |
| 176 | // calls such as write() or read() depending on kDuration. |
| 177 | summary = std::string("Blocked possibly due to HAL call: ") |
| 178 | .append(request->toString()); |
| 179 | analysis.HALBlockedTid = request->tid; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | return analysis; |
| 184 | } |
| 185 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 186 | std::vector<std::shared_ptr<const TimerThread::Request>> TimerThread::getPendingRequests() const { |
| 187 | constexpr size_t kEstimatedPendingRequests = 8; // approx 128 byte alloc. |
| 188 | std::vector<std::shared_ptr<const Request>> pendingRequests; |
| 189 | pendingRequests.reserve(kEstimatedPendingRequests); // preallocate vector out of lock. |
| 190 | |
| 191 | // following are internally locked calls, which add to our local pendingRequests. |
| 192 | mMonitorThread.copyRequests(pendingRequests); |
| 193 | mNoTimeoutMap.copyRequests(pendingRequests); |
| 194 | |
| 195 | // Sort in order of scheduled time. |
| 196 | std::sort(pendingRequests.begin(), pendingRequests.end(), |
| 197 | [](const std::shared_ptr<const Request>& r1, |
| 198 | const std::shared_ptr<const Request>& r2) { |
| 199 | return r1->scheduled < r2->scheduled; |
| 200 | }); |
| 201 | return pendingRequests; |
| 202 | } |
| 203 | |
| 204 | std::string TimerThread::pendingToString() const { |
| 205 | return requestsToString(getPendingRequests()); |
| 206 | } |
| 207 | |
| 208 | std::string TimerThread::retiredToString(size_t n) const { |
| 209 | std::vector<std::shared_ptr<const Request>> retiredRequests; |
| 210 | mRetiredQueue.copyRequests(retiredRequests, n); |
| 211 | |
| 212 | // Dump to string |
| 213 | return requestsToString(retiredRequests); |
| 214 | } |
| 215 | |
| 216 | std::string TimerThread::timeoutToString(size_t n) const { |
| 217 | std::vector<std::shared_ptr<const Request>> timeoutRequests; |
| 218 | mTimeoutQueue.copyRequests(timeoutRequests, n); |
| 219 | |
| 220 | // Dump to string |
| 221 | return requestsToString(timeoutRequests); |
| 222 | } |
| 223 | |
| 224 | std::string TimerThread::Request::toString() const { |
| 225 | const auto scheduledString = formatTime(scheduled); |
| 226 | const auto deadlineString = formatTime(deadline); |
| 227 | return std::string(tag) |
| 228 | .append(" scheduled ").append(scheduledString) |
| 229 | .append(" deadline ").append(timeSuffix(scheduledString, deadlineString)) |
| 230 | .append(" tid ").append(std::to_string(tid)); |
| 231 | } |
| 232 | |
| 233 | void TimerThread::RequestQueue::add(std::shared_ptr<const Request> request) { |
| 234 | std::lock_guard lg(mRQMutex); |
| 235 | mRequestQueue.emplace_back(std::chrono::system_clock::now(), std::move(request)); |
| 236 | if (mRequestQueue.size() > mRequestQueueMax) { |
| 237 | mRequestQueue.pop_front(); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | void TimerThread::RequestQueue::copyRequests( |
| 242 | std::vector<std::shared_ptr<const Request>>& requests, size_t n) const { |
| 243 | std::lock_guard lg(mRQMutex); |
| 244 | const size_t size = mRequestQueue.size(); |
| 245 | size_t i = n >= size ? 0 : size - n; |
| 246 | for (; i < size; ++i) { |
| 247 | const auto &[time, request] = mRequestQueue[i]; |
| 248 | requests.emplace_back(request); |
| 249 | } |
| 250 | } |
| 251 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 252 | TimerThread::Handle TimerThread::NoTimeoutMap::add(std::shared_ptr<const Request> request) { |
| 253 | std::lock_guard lg(mNTMutex); |
| 254 | // A unique handle is obtained by mNoTimeoutRequests.fetch_add(1), |
| 255 | // This need not be under a lock, but we do so anyhow. |
Andy Hung | 2aa1510 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 256 | const Handle handle = getUniqueHandle_l(); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 257 | mMap[handle] = request; |
| 258 | return handle; |
| 259 | } |
| 260 | |
| 261 | std::shared_ptr<const TimerThread::Request> TimerThread::NoTimeoutMap::remove(Handle handle) { |
| 262 | std::lock_guard lg(mNTMutex); |
| 263 | auto it = mMap.find(handle); |
| 264 | if (it == mMap.end()) return {}; |
| 265 | auto request = it->second; |
| 266 | mMap.erase(it); |
| 267 | return request; |
| 268 | } |
| 269 | |
| 270 | void TimerThread::NoTimeoutMap::copyRequests( |
| 271 | std::vector<std::shared_ptr<const Request>>& requests) const { |
| 272 | std::lock_guard lg(mNTMutex); |
| 273 | for (const auto &[handle, request] : mMap) { |
| 274 | requests.emplace_back(request); |
| 275 | } |
| 276 | } |
| 277 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 278 | TimerThread::MonitorThread::MonitorThread(RequestQueue& timeoutQueue) |
| 279 | : mTimeoutQueue(timeoutQueue) |
| 280 | , mThread([this] { threadFunc(); }) { |
| 281 | pthread_setname_np(mThread.native_handle(), "TimerThread"); |
| 282 | pthread_setschedprio(mThread.native_handle(), PRIORITY_URGENT_AUDIO); |
| 283 | } |
| 284 | |
| 285 | TimerThread::MonitorThread::~MonitorThread() { |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 286 | { |
| 287 | std::lock_guard _l(mMutex); |
| 288 | mShouldExit = true; |
| 289 | mCond.notify_all(); |
| 290 | } |
| 291 | mThread.join(); |
| 292 | } |
| 293 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 294 | void TimerThread::MonitorThread::threadFunc() { |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 295 | std::unique_lock _l(mMutex); |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 296 | while (!mShouldExit) { |
Andy Hung | f8ab093 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 297 | Handle nextDeadline = INVALID_HANDLE; |
| 298 | Handle now = INVALID_HANDLE; |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 299 | if (!mMonitorRequests.empty()) { |
Andy Hung | f8ab093 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 300 | nextDeadline = mMonitorRequests.begin()->first; |
| 301 | now = std::chrono::steady_clock::now(); |
| 302 | if (nextDeadline < now) { |
| 303 | auto node = mMonitorRequests.extract(mMonitorRequests.begin()); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 304 | // Deadline has expired, handle the request. |
Andy Hung | f8ab093 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 305 | auto secondChanceDuration = node.mapped().first->secondChanceDuration; |
| 306 | if (secondChanceDuration.count() != 0) { |
| 307 | // We now apply the second chance duration to find the clock |
| 308 | // monotonic second deadline. The unique key is then the |
| 309 | // pair<second_deadline, first_deadline>. |
| 310 | // |
| 311 | // The second chance prevents a false timeout should there be |
| 312 | // any clock monotonic advancement during suspend. |
| 313 | auto newHandle = now + secondChanceDuration; |
| 314 | ALOGD("%s: TimeCheck second chance applied for %s", |
| 315 | __func__, node.mapped().first->tag.c_str()); // should be rare event. |
| 316 | mSecondChanceRequests.emplace_hint(mSecondChanceRequests.end(), |
| 317 | std::make_pair(newHandle, nextDeadline), |
| 318 | std::move(node.mapped())); |
| 319 | // increment second chance counter. |
| 320 | mSecondChanceCount.fetch_add(1 /* arg */, std::memory_order_relaxed); |
| 321 | } else { |
| 322 | { |
| 323 | _l.unlock(); |
| 324 | // We add Request to retired queue early so that it can be dumped out. |
| 325 | mTimeoutQueue.add(std::move(node.mapped().first)); |
| 326 | node.mapped().second(nextDeadline); |
| 327 | // Caution: we don't hold lock when we call TimerCallback, |
| 328 | // but this is the timeout case! We will crash soon, |
| 329 | // maybe before returning. |
| 330 | // anything left over is released here outside lock. |
| 331 | } |
| 332 | // reacquire the lock - if something was added, we loop immediately to check. |
| 333 | _l.lock(); |
| 334 | } |
| 335 | // always process expiring monitor requests first. |
| 336 | continue; |
| 337 | } |
| 338 | } |
| 339 | // now process any second chance requests. |
| 340 | if (!mSecondChanceRequests.empty()) { |
| 341 | Handle secondDeadline = mSecondChanceRequests.begin()->first.first; |
| 342 | if (now == INVALID_HANDLE) now = std::chrono::steady_clock::now(); |
| 343 | if (secondDeadline < now) { |
| 344 | auto node = mSecondChanceRequests.extract(mSecondChanceRequests.begin()); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 345 | { |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 346 | _l.unlock(); |
| 347 | // We add Request to retired queue early so that it can be dumped out. |
| 348 | mTimeoutQueue.add(std::move(node.mapped().first)); |
Andy Hung | f8ab093 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 349 | const Handle originalHandle = node.key().second; |
| 350 | node.mapped().second(originalHandle); |
Andy Hung | 2aa1510 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 351 | // Caution: we don't hold lock when we call TimerCallback. |
| 352 | // This is benign issue - we permit concurrent operations |
| 353 | // while in the callback to the MonitorQueue. |
| 354 | // |
| 355 | // Anything left over is released here outside lock. |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 356 | } |
| 357 | // reacquire the lock - if something was added, we loop immediately to check. |
| 358 | _l.lock(); |
| 359 | continue; |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 360 | } |
Andy Hung | f8ab093 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 361 | // update the deadline. |
| 362 | if (nextDeadline == INVALID_HANDLE) { |
| 363 | nextDeadline = secondDeadline; |
| 364 | } else { |
| 365 | nextDeadline = std::min(nextDeadline, secondDeadline); |
| 366 | } |
| 367 | } |
| 368 | if (nextDeadline != INVALID_HANDLE) { |
Ytai Ben-Tsvi | 1ea62c9 | 2021-11-10 14:38:27 -0800 | [diff] [blame] | 369 | mCond.wait_until(_l, nextDeadline); |
| 370 | } else { |
| 371 | mCond.wait(_l); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 376 | TimerThread::Handle TimerThread::MonitorThread::add( |
Andy Hung | 2aa1510 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 377 | std::shared_ptr<const Request> request, TimerCallback&& func, Duration timeout) { |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 378 | std::lock_guard _l(mMutex); |
| 379 | const Handle handle = getUniqueHandle_l(timeout); |
Andy Hung | f8ab093 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 380 | mMonitorRequests.emplace_hint(mMonitorRequests.end(), |
| 381 | handle, std::make_pair(std::move(request), std::move(func))); |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 382 | mCond.notify_all(); |
| 383 | return handle; |
| 384 | } |
| 385 | |
| 386 | std::shared_ptr<const TimerThread::Request> TimerThread::MonitorThread::remove(Handle handle) { |
Andy Hung | f8ab093 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 387 | std::pair<std::shared_ptr<const Request>, TimerCallback> data; |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 388 | std::unique_lock ul(mMutex); |
Andy Hung | f8ab093 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 389 | if (const auto it = mMonitorRequests.find(handle); |
| 390 | it != mMonitorRequests.end()) { |
| 391 | data = std::move(it->second); |
| 392 | mMonitorRequests.erase(it); |
| 393 | ul.unlock(); // manually release lock here so func (data.second) |
| 394 | // is released outside of lock. |
| 395 | return data.first; // request |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 396 | } |
Andy Hung | f8ab093 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 397 | |
| 398 | // this check is O(N), but since the second chance requests are ordered |
| 399 | // in terms of earliest expiration time, we would expect better than average results. |
| 400 | for (auto it = mSecondChanceRequests.begin(); it != mSecondChanceRequests.end(); ++it) { |
| 401 | if (it->first.second == handle) { |
| 402 | data = std::move(it->second); |
| 403 | mSecondChanceRequests.erase(it); |
| 404 | ul.unlock(); // manually release lock here so func (data.second) |
| 405 | // is released outside of lock. |
| 406 | return data.first; // request |
| 407 | } |
| 408 | } |
| 409 | return {}; |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void TimerThread::MonitorThread::copyRequests( |
| 413 | std::vector<std::shared_ptr<const Request>>& requests) const { |
| 414 | std::lock_guard lg(mMutex); |
| 415 | for (const auto &[deadline, monitorpair] : mMonitorRequests) { |
| 416 | requests.emplace_back(monitorpair.first); |
| 417 | } |
Andy Hung | f8ab093 | 2022-06-13 19:49:43 -0700 | [diff] [blame] | 418 | // we combine the second map with the first map - this is |
| 419 | // everything that is pending on the monitor thread. |
| 420 | // The second map will be older than the first map so this |
| 421 | // is in order. |
| 422 | for (const auto &[deadline, monitorpair] : mSecondChanceRequests) { |
| 423 | requests.emplace_back(monitorpair.first); |
| 424 | } |
Andy Hung | a2a1ac3 | 2022-03-18 16:12:11 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | } // namespace android::mediautils |