xref: /aosp_15_r20/frameworks/wilhelm/src/locks.h (revision bebae9c0e76121f8312ccb50385c080b3a0b023c)
1*bebae9c0SAndroid Build Coastguard Worker /*
2*bebae9c0SAndroid Build Coastguard Worker  * Copyright (C) 2010 The Android Open Source Project
3*bebae9c0SAndroid Build Coastguard Worker  *
4*bebae9c0SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*bebae9c0SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*bebae9c0SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*bebae9c0SAndroid Build Coastguard Worker  *
8*bebae9c0SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*bebae9c0SAndroid Build Coastguard Worker  *
10*bebae9c0SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*bebae9c0SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*bebae9c0SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*bebae9c0SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*bebae9c0SAndroid Build Coastguard Worker  * limitations under the License.
15*bebae9c0SAndroid Build Coastguard Worker  */
16*bebae9c0SAndroid Build Coastguard Worker 
17*bebae9c0SAndroid Build Coastguard Worker #include "Configuration.h"
18*bebae9c0SAndroid Build Coastguard Worker 
19*bebae9c0SAndroid Build Coastguard Worker /** \file locks.h Mutual exclusion and condition variables */
20*bebae9c0SAndroid Build Coastguard Worker 
21*bebae9c0SAndroid Build Coastguard Worker #ifdef USE_DEBUG
22*bebae9c0SAndroid Build Coastguard Worker extern void object_lock_exclusive_(IObject *thiz, const char *file, int line);
23*bebae9c0SAndroid Build Coastguard Worker extern void object_unlock_exclusive_(IObject *thiz, const char *file, int line);
24*bebae9c0SAndroid Build Coastguard Worker extern void object_unlock_exclusive_attributes_(IObject *thiz, unsigned attr,
25*bebae9c0SAndroid Build Coastguard Worker     const char *file, int line);
26*bebae9c0SAndroid Build Coastguard Worker extern void object_cond_wait_(IObject *thiz, const char *file, int line);
27*bebae9c0SAndroid Build Coastguard Worker #else
28*bebae9c0SAndroid Build Coastguard Worker extern void object_lock_exclusive(IObject *thiz);
29*bebae9c0SAndroid Build Coastguard Worker extern void object_unlock_exclusive(IObject *thiz);
30*bebae9c0SAndroid Build Coastguard Worker extern void object_unlock_exclusive_attributes(IObject *thiz, unsigned attr);
31*bebae9c0SAndroid Build Coastguard Worker extern void object_cond_wait(IObject *thiz);
32*bebae9c0SAndroid Build Coastguard Worker #endif
33*bebae9c0SAndroid Build Coastguard Worker extern void object_cond_signal(IObject *thiz);
34*bebae9c0SAndroid Build Coastguard Worker extern void object_cond_broadcast(IObject *thiz);
35*bebae9c0SAndroid Build Coastguard Worker 
36*bebae9c0SAndroid Build Coastguard Worker #ifdef USE_DEBUG
37*bebae9c0SAndroid Build Coastguard Worker #define object_lock_exclusive(thiz) object_lock_exclusive_((thiz), __FILE__, __LINE__)
38*bebae9c0SAndroid Build Coastguard Worker #define object_unlock_exclusive(thiz) object_unlock_exclusive_((thiz), __FILE__, __LINE__)
39*bebae9c0SAndroid Build Coastguard Worker #define object_unlock_exclusive_attributes(thiz, attr) \
40*bebae9c0SAndroid Build Coastguard Worker     object_unlock_exclusive_attributes_((thiz), (attr), __FILE__, __LINE__)
41*bebae9c0SAndroid Build Coastguard Worker #define object_cond_wait(thiz) object_cond_wait_((thiz), __FILE__, __LINE__)
42*bebae9c0SAndroid Build Coastguard Worker #endif
43*bebae9c0SAndroid Build Coastguard Worker 
44*bebae9c0SAndroid Build Coastguard Worker // Currently shared locks are implemented as exclusive, but don't count on it
45*bebae9c0SAndroid Build Coastguard Worker 
46*bebae9c0SAndroid Build Coastguard Worker #define object_lock_shared(thiz)   object_lock_exclusive(thiz)
47*bebae9c0SAndroid Build Coastguard Worker #define object_unlock_shared(thiz) object_unlock_exclusive(thiz)
48*bebae9c0SAndroid Build Coastguard Worker 
49*bebae9c0SAndroid Build Coastguard Worker // Currently interface locks are actually on whole object, but don't count on it.
50*bebae9c0SAndroid Build Coastguard Worker // These operations are undefined on IObject, as it lacks an mThis.
51*bebae9c0SAndroid Build Coastguard Worker // If you have an IObject, then use the object_ functions instead.
52*bebae9c0SAndroid Build Coastguard Worker 
53*bebae9c0SAndroid Build Coastguard Worker #define interface_lock_exclusive(thiz)   object_lock_exclusive(InterfaceToIObject(thiz))
54*bebae9c0SAndroid Build Coastguard Worker #define interface_unlock_exclusive(thiz) object_unlock_exclusive(InterfaceToIObject(thiz))
55*bebae9c0SAndroid Build Coastguard Worker #define interface_unlock_exclusive_attributes(thiz, attr) \
56*bebae9c0SAndroid Build Coastguard Worker     object_unlock_exclusive_attributes(InterfaceToIObject(thiz), (attr))
57*bebae9c0SAndroid Build Coastguard Worker #define interface_lock_shared(thiz)      object_lock_shared(InterfaceToIObject(thiz))
58*bebae9c0SAndroid Build Coastguard Worker #define interface_unlock_shared(thiz)    object_unlock_shared(InterfaceToIObject(thiz))
59*bebae9c0SAndroid Build Coastguard Worker #define interface_cond_wait(thiz)        object_cond_wait(InterfaceToIObject(thiz))
60*bebae9c0SAndroid Build Coastguard Worker #define interface_cond_signal(thiz)      object_cond_signal(InterfaceToIObject(thiz))
61*bebae9c0SAndroid Build Coastguard Worker #define interface_cond_broadcast(thiz)   object_cond_broadcast(InterfaceToIObject(thiz))
62*bebae9c0SAndroid Build Coastguard Worker 
63*bebae9c0SAndroid Build Coastguard Worker // Peek and poke are an optimization for small atomic fields that don't "matter".
64*bebae9c0SAndroid Build Coastguard Worker // Don't use for struct, as struct copy might not be atomic.
65*bebae9c0SAndroid Build Coastguard Worker // On uniprocessor they can be no-ops, on SMP they could be memory barriers but locks are easier.
66*bebae9c0SAndroid Build Coastguard Worker 
67*bebae9c0SAndroid Build Coastguard Worker #define object_lock_peek(thiz)      object_lock_shared(thiz)
68*bebae9c0SAndroid Build Coastguard Worker #define object_unlock_peek(thiz)    object_unlock_shared(thiz)
69*bebae9c0SAndroid Build Coastguard Worker #define interface_lock_poke(thiz)   interface_lock_exclusive(thiz)
70*bebae9c0SAndroid Build Coastguard Worker #define interface_unlock_poke(thiz) interface_unlock_exclusive(thiz)
71*bebae9c0SAndroid Build Coastguard Worker #define interface_lock_peek(thiz)   interface_lock_shared(thiz)
72*bebae9c0SAndroid Build Coastguard Worker #define interface_unlock_peek(thiz) interface_unlock_shared(thiz)
73