locks.h revision 5c8bd83295b2752bb7ac34f83dfb19dcdb1be5e5
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
20extern void object_lock_exclusive_(IObject *this, const char *file, int line);
21extern void object_unlock_exclusive_(IObject *this, const char *file, int line);
22extern void object_unlock_exclusive_attributes_(IObject *this, unsigned attr,
23    const char *file, int line);
24extern void object_cond_wait_(IObject *this, const char *file, int line);
25#else
26extern void object_lock_exclusive(IObject *this);
27extern void object_unlock_exclusive(IObject *this);
28extern void object_unlock_exclusive_attributes(IObject *this, unsigned attr);
29extern void object_cond_wait(IObject *this);
30#endif
31extern void object_cond_signal(IObject *this);
32extern void object_cond_broadcast(IObject *this);
33
34#ifdef USE_DEBUG
35#define object_lock_exclusive(this) object_lock_exclusive_((this), __FILE__, __LINE__)
36#define object_unlock_exclusive(this) object_unlock_exclusive_((this), __FILE__, __LINE__)
37#define object_unlock_exclusive_attributes(this, attr) \
38    object_unlock_exclusive_attributes_((this), (attr), __FILE__, __LINE__)
39#define object_cond_wait(this) object_cond_wait_((this), __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(this)   object_lock_exclusive(this)
45#define object_unlock_shared(this) object_unlock_exclusive(this)
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(this)   object_lock_exclusive(InterfaceToIObject(this))
52#define interface_unlock_exclusive(this) object_unlock_exclusive(InterfaceToIObject(this))
53#define interface_unlock_exclusive_attributes(this, attr) \
54    object_unlock_exclusive_attributes(InterfaceToIObject(this), (attr))
55#define interface_lock_shared(this)      object_lock_shared(InterfaceToIObject(this))
56#define interface_unlock_shared(this)    object_unlock_shared(InterfaceToIObject(this))
57#define interface_cond_wait(this)        object_cond_wait(InterfaceToIObject(this))
58#define interface_cond_signal(this)      object_cond_signal(InterfaceToIObject(this))
59#define interface_cond_broadcast(this)   object_cond_broadcast(InterfaceToIObject(this))
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(this)      object_lock_shared(this)
66#define object_unlock_peek(this)    object_unlock_shared(this)
67#define interface_lock_poke(this)   interface_lock_exclusive(this)
68#define interface_unlock_poke(this) interface_unlock_exclusive(this)
69#define interface_lock_peek(this)   interface_lock_shared(this)
70#define interface_unlock_peek(this) interface_unlock_shared(this)
71