1 /* 2 * 3 * Copyright 2024, 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 20 #include <android-base/thread_annotations.h> 21 #include <audio_utils/mutex.h> 22 #include <utils/Mutex.h> 23 24 #include <functional> 25 26 namespace android::afutils { 27 28 // Lock guard with a acquire timeout, which for the purpose of thread safety annotations acts as if 29 // it has the capability (i.e. the thread annotations *lie*). Used for dump utilities, where if we 30 // are deadlocked, we access without the lock since we are about to abort due to watchdog anyway. 31 // If the lock was truly successfully acquired, unlock on dtor. Like all guards (if successful), 32 // this guard is solely responsible for locking on ctor/unlocking on dtor, and the mutex reference 33 // must be valid for the lifetime of the object 34 class [[nodiscard]] SCOPED_CAPABILITY FallibleLockGuard { 35 public: 36 static constexpr int kDefaultTimeout = 1'000'000'000; 37 ACQUIRE(mutex)38 explicit FallibleLockGuard(Mutex& mutex, int64_t timeoutNs = kDefaultTimeout) ACQUIRE(mutex) { 39 if (mutex.timedLock(timeoutNs) == NO_ERROR) { 40 mUnlockFunc = [&mutex]() NO_THREAD_SAFETY_ANALYSIS { mutex.unlock(); }; 41 } 42 } 43 44 explicit FallibleLockGuard(audio_utils::mutex& mutex, int64_t timeoutNs = kDefaultTimeout) 45 ACQUIRE(mutex) { 46 if (mutex.try_lock(timeoutNs)) { 47 mUnlockFunc = [&mutex]() NO_THREAD_SAFETY_ANALYSIS { mutex.unlock(); }; 48 } 49 } 50 51 FallibleLockGuard(const FallibleLockGuard& other) = delete; 52 53 FallibleLockGuard(FallibleLockGuard&& other) { 54 mUnlockFunc.swap(other.mUnlockFunc); 55 } 56 57 FallibleLockGuard& operator=(const FallibleLockGuard& other) = delete; 58 59 // Return if the underlying lock was successfully locked 60 explicit operator bool() const { return static_cast<bool>(mUnlockFunc); } 61 62 ~FallibleLockGuard() RELEASE() { 63 if (mUnlockFunc) mUnlockFunc(); 64 } 65 66 private: 67 std::function<void()> mUnlockFunc; 68 }; 69 } // android::afutils 70