blob: e4ad112dd43e993cef113033ab37e6166d878870 [file] [log] [blame]
Andy Hung21ff9672023-07-18 20:54:44 -07001/*
2 *
3 * Copyright 2023, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
Andy Hung2ac52f12023-08-28 18:36:53 -070020#include <audio_utils/mutex.h>
Andy Hung21ff9672023-07-18 20:54:44 -070021#include <utils/Mutex.h>
Andy Hung2ac52f12023-08-28 18:36:53 -070022#include <utils/Timers.h>
Andy Hung21ff9672023-07-18 20:54:44 -070023
24namespace android::afutils {
25
26inline bool dumpTryLock(Mutex& mutex)
27{
28 static constexpr int kDumpLockTimeoutNs = 1'000'000'000;
29 const status_t err = mutex.timedLock(kDumpLockTimeoutNs);
30 return err == NO_ERROR;
31}
32
Andy Hung2ac52f12023-08-28 18:36:53 -070033// Note: the std::timed_mutex try_lock_for and try_lock_until methods are inefficient.
34// It is better to use std::mutex and call this method.
35//
36inline bool dumpTryLock(audio_utils::mutex& mutex) TRY_ACQUIRE(true, mutex)
37{
38 static constexpr int64_t kDumpLockTimeoutNs = 1'000'000'000;
39
40 const int64_t timeoutNs = kDumpLockTimeoutNs + systemTime(SYSTEM_TIME_REALTIME);
41 const struct timespec ts = {
42 .tv_sec = static_cast<time_t>(timeoutNs / 1000000000),
43 .tv_nsec = static_cast<long>(timeoutNs % 1000000000),
44 };
45 return pthread_mutex_timedlock(mutex.native_handle(), &ts) == 0;
46}
47
48} // android::afutils