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