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