1/*
2 * Copyright 2008-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2002-2009, Axel D��rfler, axeld@pinc-software.de.
4 * Distributed under the terms of the MIT License.
5 *
6 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
7 * Distributed under the terms of the NewOS License.
8 */
9#ifndef _KERNEL_LOCK_H
10#define _KERNEL_LOCK_H
11
12
13#include <OS.h>
14
15#include <arch/atomic.h>
16#include <debug.h>
17
18
19struct mutex_waiter;
20
21typedef struct mutex {
22	const char*				name;
23	struct mutex_waiter*	waiters;
24	spinlock				lock;
25#if KDEBUG
26	thread_id				holder;
27#else
28	int32					count;
29	uint16					ignore_unlock_count;
30#endif
31	uint8					flags;
32} mutex;
33
34#define MUTEX_FLAG_CLONE_NAME	0x1
35
36
37typedef struct recursive_lock {
38	mutex		lock;
39#if !KDEBUG
40	thread_id	holder;
41#endif
42	int			recursion;
43} recursive_lock;
44
45
46struct rw_lock_waiter;
47
48typedef struct rw_lock {
49	const char*				name;
50	struct rw_lock_waiter*	waiters;
51	spinlock				lock;
52	thread_id				holder;
53	int32					count;
54	int32					owner_count;
55	int16					active_readers;
56								// Only > 0 while a writer is waiting: number
57								// of active readers when the first waiting
58								// writer started waiting.
59	int16					pending_readers;
60								// Number of readers that have already
61								// incremented "count", but have not yet started
62								// to wait at the time the last writer unlocked.
63	uint32					flags;
64} rw_lock;
65
66#define RW_LOCK_WRITER_COUNT_BASE	0x10000
67
68#define RW_LOCK_FLAG_CLONE_NAME	0x1
69
70
71#if KDEBUG
72#	define KDEBUG_RW_LOCK_DEBUG 0
73		// Define to 1 if you want to use ASSERT_READ_LOCKED_RW_LOCK().
74		// The rw_lock will just behave like a recursive locker then.
75#	define ASSERT_LOCKED_RECURSIVE(r) \
76		{ ASSERT(find_thread(NULL) == (r)->lock.holder); }
77#	define ASSERT_LOCKED_MUTEX(m) { ASSERT(find_thread(NULL) == (m)->holder); }
78#	define ASSERT_WRITE_LOCKED_RW_LOCK(l) \
79		{ ASSERT(find_thread(NULL) == (l)->holder); }
80#	if KDEBUG_RW_LOCK_DEBUG
81#		define ASSERT_READ_LOCKED_RW_LOCK(l) \
82			{ ASSERT(find_thread(NULL) == (l)->holder); }
83#	else
84#		define ASSERT_READ_LOCKED_RW_LOCK(l) do {} while (false)
85#	endif
86#else
87#	define ASSERT_LOCKED_RECURSIVE(r)		do {} while (false)
88#	define ASSERT_LOCKED_MUTEX(m)			do {} while (false)
89#	define ASSERT_WRITE_LOCKED_RW_LOCK(m)	do {} while (false)
90#	define ASSERT_READ_LOCKED_RW_LOCK(l)	do {} while (false)
91#endif
92
93
94// static initializers
95#if KDEBUG
96#	define MUTEX_INITIALIZER(name) \
97	{ name, NULL, B_SPINLOCK_INITIALIZER, -1, 0 }
98#	define RECURSIVE_LOCK_INITIALIZER(name)	{ MUTEX_INITIALIZER(name), 0 }
99#else
100#	define MUTEX_INITIALIZER(name) \
101	{ name, NULL, B_SPINLOCK_INITIALIZER, 0, 0, 0 }
102#	define RECURSIVE_LOCK_INITIALIZER(name)	{ MUTEX_INITIALIZER(name), -1, 0 }
103#endif
104
105#define RW_LOCK_INITIALIZER(name) \
106	{ name, NULL, B_SPINLOCK_INITIALIZER, -1, 0, 0, 0, 0, 0 }
107
108
109#if KDEBUG
110#	define RECURSIVE_LOCK_HOLDER(recursiveLock)	((recursiveLock)->lock.holder)
111#else
112#	define RECURSIVE_LOCK_HOLDER(recursiveLock)	((recursiveLock)->holder)
113#endif
114
115
116#ifdef __cplusplus
117extern "C" {
118#endif
119
120extern void	recursive_lock_init(recursive_lock *lock, const char *name);
121	// name is *not* cloned nor freed in recursive_lock_destroy()
122extern void recursive_lock_init_etc(recursive_lock *lock, const char *name,
123	uint32 flags);
124extern void recursive_lock_destroy(recursive_lock *lock);
125extern status_t recursive_lock_lock(recursive_lock *lock);
126extern status_t recursive_lock_trylock(recursive_lock *lock);
127extern void recursive_lock_unlock(recursive_lock *lock);
128extern int32 recursive_lock_get_recursion(recursive_lock *lock);
129
130extern void rw_lock_init(rw_lock* lock, const char* name);
131	// name is *not* cloned nor freed in rw_lock_destroy()
132extern void rw_lock_init_etc(rw_lock* lock, const char* name, uint32 flags);
133extern void rw_lock_destroy(rw_lock* lock);
134extern status_t rw_lock_write_lock(rw_lock* lock);
135
136extern void mutex_init(mutex* lock, const char* name);
137	// name is *not* cloned nor freed in mutex_destroy()
138extern void mutex_init_etc(mutex* lock, const char* name, uint32 flags);
139extern void mutex_destroy(mutex* lock);
140extern void mutex_transfer_lock(mutex* lock, thread_id thread);
141extern status_t mutex_switch_lock(mutex* from, mutex* to);
142	// Unlocks "from" and locks "to" such that unlocking and starting to wait
143	// for the lock is atomically. I.e. if "from" guards the object "to" belongs
144	// to, the operation is safe as long as "from" is held while destroying
145	// "to".
146extern status_t mutex_switch_from_read_lock(rw_lock* from, mutex* to);
147	// Like mutex_switch_lock(), just for a switching from a read-locked
148	// rw_lock.
149
150
151// implementation private:
152
153extern status_t _rw_lock_read_lock(rw_lock* lock);
154extern status_t _rw_lock_read_lock_with_timeout(rw_lock* lock,
155	uint32 timeoutFlags, bigtime_t timeout);
156extern void _rw_lock_read_unlock(rw_lock* lock);
157extern void _rw_lock_write_unlock(rw_lock* lock);
158
159extern status_t _mutex_lock(mutex* lock, void* locker);
160extern void _mutex_unlock(mutex* lock);
161extern status_t _mutex_trylock(mutex* lock);
162extern status_t _mutex_lock_with_timeout(mutex* lock, uint32 timeoutFlags,
163	bigtime_t timeout);
164
165
166static inline status_t
167rw_lock_read_lock(rw_lock* lock)
168{
169#if KDEBUG_RW_LOCK_DEBUG
170	return rw_lock_write_lock(lock);
171#else
172	int32 oldCount = atomic_add(&lock->count, 1);
173	if (oldCount >= RW_LOCK_WRITER_COUNT_BASE)
174		return _rw_lock_read_lock(lock);
175	return B_OK;
176#endif
177}
178
179
180static inline status_t
181rw_lock_read_lock_with_timeout(rw_lock* lock, uint32 timeoutFlags,
182	bigtime_t timeout)
183{
184#if KDEBUG_RW_LOCK_DEBUG
185	return mutex_lock_with_timeout(lock, timeoutFlags, timeout);
186#else
187	int32 oldCount = atomic_add(&lock->count, 1);
188	if (oldCount >= RW_LOCK_WRITER_COUNT_BASE)
189		return _rw_lock_read_lock_with_timeout(lock, timeoutFlags, timeout);
190	return B_OK;
191#endif
192}
193
194
195static inline void
196rw_lock_read_unlock(rw_lock* lock)
197{
198#if KDEBUG_RW_LOCK_DEBUG
199	rw_lock_write_unlock(lock);
200#else
201	int32 oldCount = atomic_add(&lock->count, -1);
202	if (oldCount >= RW_LOCK_WRITER_COUNT_BASE)
203		_rw_lock_read_unlock(lock);
204#endif
205}
206
207
208static inline void
209rw_lock_write_unlock(rw_lock* lock)
210{
211	_rw_lock_write_unlock(lock);
212}
213
214
215static inline status_t
216mutex_lock(mutex* lock)
217{
218#if KDEBUG
219	return _mutex_lock(lock, NULL);
220#else
221	if (atomic_add(&lock->count, -1) < 0)
222		return _mutex_lock(lock, NULL);
223	return B_OK;
224#endif
225}
226
227
228static inline status_t
229mutex_trylock(mutex* lock)
230{
231#if KDEBUG
232	return _mutex_trylock(lock);
233#else
234	if (atomic_test_and_set(&lock->count, -1, 0) != 0)
235		return B_WOULD_BLOCK;
236	return B_OK;
237#endif
238}
239
240
241static inline status_t
242mutex_lock_with_timeout(mutex* lock, uint32 timeoutFlags, bigtime_t timeout)
243{
244#if KDEBUG
245	return _mutex_lock_with_timeout(lock, timeoutFlags, timeout);
246#else
247	if (atomic_add(&lock->count, -1) < 0)
248		return _mutex_lock_with_timeout(lock, timeoutFlags, timeout);
249	return B_OK;
250#endif
251}
252
253
254static inline void
255mutex_unlock(mutex* lock)
256{
257#if !KDEBUG
258	if (atomic_add(&lock->count, 1) < -1)
259#endif
260		_mutex_unlock(lock);
261}
262
263
264static inline void
265recursive_lock_transfer_lock(recursive_lock* lock, thread_id thread)
266{
267	if (lock->recursion != 1)
268		panic("invalid recursion level for lock transfer!");
269
270#if KDEBUG
271	mutex_transfer_lock(&lock->lock, thread);
272#else
273	lock->holder = thread;
274#endif
275}
276
277
278extern void lock_debug_init();
279
280#ifdef __cplusplus
281}
282#endif
283
284#endif	/* _KERNEL_LOCK_H */
285