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#include "Configuration.h"
18
19/** \file locks.h Mutual exclusion and condition variables */
20
21#ifdef USE_DEBUG
22extern void object_lock_exclusive_(IObject *thiz, const char *file, int line);
23extern void object_unlock_exclusive_(IObject *thiz, const char *file, int line);
24extern void object_unlock_exclusive_attributes_(IObject *thiz, unsigned attr,
25    const char *file, int line);
26extern void object_cond_wait_(IObject *thiz, const char *file, int line);
27#else
28extern void object_lock_exclusive(IObject *thiz);
29extern void object_unlock_exclusive(IObject *thiz);
30extern void object_unlock_exclusive_attributes(IObject *thiz, unsigned attr);
31extern void object_cond_wait(IObject *thiz);
32#endif
33extern void object_cond_signal(IObject *thiz);
34extern void object_cond_broadcast(IObject *thiz);
35
36#ifdef USE_DEBUG
37#define object_lock_exclusive(thiz) object_lock_exclusive_((thiz), __FILE__, __LINE__)
38#define object_unlock_exclusive(thiz) object_unlock_exclusive_((thiz), __FILE__, __LINE__)
39#define object_unlock_exclusive_attributes(thiz, attr) \
40    object_unlock_exclusive_attributes_((thiz), (attr), __FILE__, __LINE__)
41#define object_cond_wait(thiz) object_cond_wait_((thiz), __FILE__, __LINE__)
42#endif
43
44// Currently shared locks are implemented as exclusive, but don't count on it
45
46#define object_lock_shared(thiz)   object_lock_exclusive(thiz)
47#define object_unlock_shared(thiz) object_unlock_exclusive(thiz)
48
49// Currently interface locks are actually on whole object, but don't count on it.
50// These operations are undefined on IObject, as it lacks an mThis.
51// If you have an IObject, then use the object_ functions instead.
52
53#define interface_lock_exclusive(thiz)   object_lock_exclusive(InterfaceToIObject(thiz))
54#define interface_unlock_exclusive(thiz) object_unlock_exclusive(InterfaceToIObject(thiz))
55#define interface_unlock_exclusive_attributes(thiz, attr) \
56    object_unlock_exclusive_attributes(InterfaceToIObject(thiz), (attr))
57#define interface_lock_shared(thiz)      object_lock_shared(InterfaceToIObject(thiz))
58#define interface_unlock_shared(thiz)    object_unlock_shared(InterfaceToIObject(thiz))
59#define interface_cond_wait(thiz)        object_cond_wait(InterfaceToIObject(thiz))
60#define interface_cond_signal(thiz)      object_cond_signal(InterfaceToIObject(thiz))
61#define interface_cond_broadcast(thiz)   object_cond_broadcast(InterfaceToIObject(thiz))
62
63// Peek and poke are an optimization for small atomic fields that don't "matter".
64// Don't use for struct, as struct copy might not be atomic.
65// On uniprocessor they can be no-ops, on SMP they could be memory barriers but locks are easier.
66
67#define object_lock_peek(thiz)      object_lock_shared(thiz)
68#define object_unlock_peek(thiz)    object_unlock_shared(thiz)
69#define interface_lock_poke(thiz)   interface_lock_exclusive(thiz)
70#define interface_unlock_poke(thiz) interface_unlock_exclusive(thiz)
71#define interface_lock_peek(thiz)   interface_lock_shared(thiz)
72#define interface_unlock_peek(thiz) interface_unlock_shared(thiz)
73