Andy Hung | 21ff967 | 2023-07-18 20:54:44 -0700 | [diff] [blame] | 1 | /* |
| 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 Hung | 2ac52f1 | 2023-08-28 18:36:53 -0700 | [diff] [blame^] | 20 | #include <audio_utils/mutex.h> |
Andy Hung | 21ff967 | 2023-07-18 20:54:44 -0700 | [diff] [blame] | 21 | #include <utils/Mutex.h> |
Andy Hung | 2ac52f1 | 2023-08-28 18:36:53 -0700 | [diff] [blame^] | 22 | #include <utils/Timers.h> |
Andy Hung | 21ff967 | 2023-07-18 20:54:44 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android::afutils { |
| 25 | |
| 26 | inline 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 Hung | 2ac52f1 | 2023-08-28 18:36:53 -0700 | [diff] [blame^] | 33 | // 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 | // |
| 36 | inline 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 |