2 * Copyright 2009, Michael Lotz, mmlr@mlotz.ch.
3 * Distributed under the terms of the MIT License.
14 typedef struct mutex
{
20 #define MUTEX_FLAG_CLONE_NAME 0x1
21 #define MUTEX_FLAG_ADAPTIVE 0x2
22 #define MUTEX_INITIALIZER(name) { name, 0, 0 }
24 #define mutex_init(lock, name) __mutex_init(lock, name)
25 #define mutex_init_etc(lock, name, flags) __mutex_init_etc(lock, name, flags)
26 #define mutex_destroy(lock) __mutex_destroy(lock)
27 #define mutex_lock(lock) __mutex_lock(lock)
28 #define mutex_unlock(lock) __mutex_unlock(lock)
30 void __mutex_init(mutex
*lock
, const char *name
);
31 void __mutex_init_etc(mutex
*lock
, const char *name
, uint32 flags
);
32 void __mutex_destroy(mutex
*lock
);
33 status_t
__mutex_lock(mutex
*lock
);
34 void __mutex_unlock(mutex
*lock
);
37 typedef struct rw_lock
{
39 struct rw_lock_waiter
* waiters
;
40 struct rw_lock_waiter
* last_waiter
;
47 #define RW_LOCK_FLAG_CLONE_NAME MUTEX_FLAG_CLONE_NAME
48 #define RW_LOCK_INITIALIZER(name) { MUTEX_INITIALIZER(name), NULL, \
51 #define rw_lock_init(lock, name) __rw_lock_init(lock, name)
52 #define rw_lock_init_etc(lock, name, flags) \
53 __rw_lock_init_etc(lock, name, flags)
54 #define rw_lock_destroy(lock) __rw_lock_destroy(lock)
55 #define rw_lock_read_lock(lock) __rw_lock_read_lock(lock)
56 #define rw_lock_read_unlock(lock) __rw_lock_read_unlock(lock)
57 #define rw_lock_write_lock(lock) __rw_lock_write_lock(lock)
58 #define rw_lock_write_unlock(lock) __rw_lock_write_unlock(lock)
60 void __rw_lock_init(rw_lock
*lock
, const char *name
);
61 void __rw_lock_init_etc(rw_lock
*lock
, const char *name
, uint32 flags
);
62 void __rw_lock_destroy(rw_lock
*lock
);
63 status_t
__rw_lock_read_lock(rw_lock
*lock
);
64 status_t
__rw_lock_read_unlock(rw_lock
*lock
);
65 status_t
__rw_lock_write_lock(rw_lock
*lock
);
66 status_t
__rw_lock_write_unlock(rw_lock
*lock
);
69 typedef struct recursive_lock
{
75 #define RECURSIVE_LOCK_FLAG_CLONE_NAME MUTEX_FLAG_CLONE_NAME
76 #define RECURSIVE_LOCK_INITIALIZER(name) { MUTEX_INITIALIZER(name), -1, 0 }
78 #define recursive_lock_init(lock, name) __recursive_lock_init(lock, name)
79 #define recursive_lock_init_etc(lock, name, flags) \
80 __recursive_lock_init_etc(lock, name, flags)
81 #define recursive_lock_destroy(lock) __recursive_lock_destroy(lock)
82 #define recursive_lock_lock(lock) __recursive_lock_lock(lock)
83 #define recursive_lock_unlock(lock) __recursive_lock_unlock(lock)
84 #define recursive_lock_get_recursion(lock) __recursive_lock_get_recursion(lock)
86 void __recursive_lock_init(recursive_lock
*lock
, const char *name
);
87 void __recursive_lock_init_etc(recursive_lock
*lock
, const char *name
,
89 void __recursive_lock_destroy(recursive_lock
*lock
);
90 status_t
__recursive_lock_lock(recursive_lock
*lock
);
91 void __recursive_lock_unlock(recursive_lock
*lock
);
92 int32
__recursive_lock_get_recursion(recursive_lock
*lock
);
95 #define INIT_ONCE_UNINITIALIZED -1
96 #define INIT_ONCE_INITIALIZED -4
98 status_t
__init_once(int32
* control
, status_t (*initRoutine
)(void*),
105 #include <AutoLocker.h>
109 inline bool Lock(struct mutex
*lock
)
111 return mutex_lock(lock
) == B_OK
;
114 inline void Unlock(struct mutex
*lock
)
121 class RWLockReadLocking
{
123 inline bool Lock(struct rw_lock
*lock
)
125 return rw_lock_read_lock(lock
) == B_OK
;
128 inline void Unlock(struct rw_lock
*lock
)
130 rw_lock_read_unlock(lock
);
135 class RWLockWriteLocking
{
137 inline bool Lock(struct rw_lock
*lock
)
139 return rw_lock_write_lock(lock
) == B_OK
;
142 inline void Unlock(struct rw_lock
*lock
)
144 rw_lock_write_unlock(lock
);
149 typedef AutoLocker
<mutex
, MutexLocking
> MutexLocker
;
150 typedef AutoLocker
<rw_lock
, RWLockReadLocking
> ReadLocker
;
151 typedef AutoLocker
<rw_lock
, RWLockWriteLocking
> WriteLocker
;
153 #endif // __cplusplus