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