xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/MutexUtils.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2022 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <log/log.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <utils/Mutex.h>
21*38e8c45fSAndroid Build Coastguard Worker 
22*38e8c45fSAndroid Build Coastguard Worker namespace android {
23*38e8c45fSAndroid Build Coastguard Worker 
24*38e8c45fSAndroid Build Coastguard Worker struct SCOPED_CAPABILITY ConditionalLock {
ConditionalLockConditionalLock25*38e8c45fSAndroid Build Coastguard Worker     ConditionalLock(Mutex& mutex, bool lock) ACQUIRE(mutex) : mutex(mutex), lock(lock) {
26*38e8c45fSAndroid Build Coastguard Worker         if (lock) mutex.lock();
27*38e8c45fSAndroid Build Coastguard Worker     }
28*38e8c45fSAndroid Build Coastguard Worker 
RELEASEConditionalLock29*38e8c45fSAndroid Build Coastguard Worker     ~ConditionalLock() RELEASE() {
30*38e8c45fSAndroid Build Coastguard Worker         if (lock) mutex.unlock();
31*38e8c45fSAndroid Build Coastguard Worker     }
32*38e8c45fSAndroid Build Coastguard Worker 
33*38e8c45fSAndroid Build Coastguard Worker     Mutex& mutex;
34*38e8c45fSAndroid Build Coastguard Worker     const bool lock;
35*38e8c45fSAndroid Build Coastguard Worker };
36*38e8c45fSAndroid Build Coastguard Worker 
37*38e8c45fSAndroid Build Coastguard Worker struct SCOPED_CAPABILITY TimedLock {
TimedLockTimedLock38*38e8c45fSAndroid Build Coastguard Worker     TimedLock(Mutex& mutex, nsecs_t timeout, const char* whence) ACQUIRE(mutex)
39*38e8c45fSAndroid Build Coastguard Worker           : mutex(mutex), status(mutex.timedLock(timeout)) {
40*38e8c45fSAndroid Build Coastguard Worker         ALOGE_IF(!locked(), "%s timed out locking: %s (%d)", whence, strerror(-status), status);
41*38e8c45fSAndroid Build Coastguard Worker     }
42*38e8c45fSAndroid Build Coastguard Worker 
RELEASETimedLock43*38e8c45fSAndroid Build Coastguard Worker     ~TimedLock() RELEASE() {
44*38e8c45fSAndroid Build Coastguard Worker         if (locked()) mutex.unlock();
45*38e8c45fSAndroid Build Coastguard Worker     }
46*38e8c45fSAndroid Build Coastguard Worker 
lockedTimedLock47*38e8c45fSAndroid Build Coastguard Worker     bool locked() const { return status == NO_ERROR; }
48*38e8c45fSAndroid Build Coastguard Worker 
49*38e8c45fSAndroid Build Coastguard Worker     Mutex& mutex;
50*38e8c45fSAndroid Build Coastguard Worker     const status_t status;
51*38e8c45fSAndroid Build Coastguard Worker };
52*38e8c45fSAndroid Build Coastguard Worker 
53*38e8c45fSAndroid Build Coastguard Worker // Require, under penalty of compilation failure, that the compiler thinks that a mutex is held.
54*38e8c45fSAndroid Build Coastguard Worker #define REQUIRE_MUTEX(expr) ([]() REQUIRES(expr) {})()
55*38e8c45fSAndroid Build Coastguard Worker 
56*38e8c45fSAndroid Build Coastguard Worker // Tell the compiler that we know that a mutex is held.
57*38e8c45fSAndroid Build Coastguard Worker #define ASSERT_MUTEX(expr) ([]() ASSERT_CAPABILITY(expr) {})()
58*38e8c45fSAndroid Build Coastguard Worker 
59*38e8c45fSAndroid Build Coastguard Worker // Specify that one mutex is an alias for another.
60*38e8c45fSAndroid Build Coastguard Worker // (e.g. SurfaceFlinger::mStateLock and Layer::mFlinger->mStateLock)
61*38e8c45fSAndroid Build Coastguard Worker #define MUTEX_ALIAS(held, alias) (REQUIRE_MUTEX(held), ASSERT_MUTEX(alias))
62*38e8c45fSAndroid Build Coastguard Worker 
63*38e8c45fSAndroid Build Coastguard Worker } // namespace android
64