xref: /haiku/headers/private/kernel/lock.h (revision 8df6a8dbf579280f55b61d725e470dee5d504e83)
1 /*
2  * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2002-2008, 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 #ifdef KDEBUG
22 	thread_id				holder;
23 #else
24 	int32					count;
25 #endif
26 	uint8					flags;
27 } mutex;
28 
29 #define MUTEX_FLAG_CLONE_NAME	0x1
30 
31 
32 typedef struct recursive_lock {
33 	mutex		lock;
34 #ifndef KDEBUG
35 	thread_id	holder;
36 #endif
37 	int			recursion;
38 } recursive_lock;
39 
40 
41 struct rw_lock_waiter;
42 
43 typedef struct rw_lock {
44 	const char*				name;
45 	struct rw_lock_waiter*	waiters;
46 	thread_id				holder;
47 	int32					reader_count;
48 	int32					writer_count;
49 	int32					owner_count;
50 	uint32					flags;
51 } rw_lock;
52 
53 #define RW_LOCK_FLAG_CLONE_NAME	0x1
54 
55 
56 #if KDEBUG
57 #	define KDEBUG_RW_LOCK_DEBUG 0
58 		// Define to 1 if you want to use ASSERT_READ_LOCKED_RW_LOCK().
59 		// The rw_lock will just behave like a recursive locker then.
60 #	define ASSERT_LOCKED_RECURSIVE(r) \
61 		{ ASSERT(find_thread(NULL) == (r)->lock.holder); }
62 #	define ASSERT_LOCKED_MUTEX(m) { ASSERT(find_thread(NULL) == (m)->holder); }
63 #	define ASSERT_WRITE_LOCKED_RW_LOCK(l) \
64 		{ ASSERT(find_thread(NULL) == (l)->holder); }
65 #	if KDEBUG_RW_LOCK_DEBUG
66 #		define ASSERT_READ_LOCKED_RW_LOCK(l) \
67 			{ ASSERT(find_thread(NULL) == (l)->holder); }
68 #	else
69 #		define ASSERT_READ_LOCKED_RW_LOCK(l) do {} while (false)
70 #	endif
71 #else
72 #	define ASSERT_LOCKED_RECURSIVE(r)		do {} while (false)
73 #	define ASSERT_LOCKED_MUTEX(m)			do {} while (false)
74 #	define ASSERT_WRITE_LOCKED_RW_LOCK(m)	do {} while (false)
75 #	define ASSERT_READ_LOCKED_RW_LOCK(l)	do {} while (false)
76 #endif
77 
78 
79 // static initializers
80 #ifdef KDEBUG
81 #	define MUTEX_INITIALIZER(name)			{ name, NULL, -1, 0 }
82 #	define RECURSIVE_LOCK_INITIALIZER(name)	{ MUTEX_INITIALIZER(name), 0 }
83 #else
84 #	define MUTEX_INITIALIZER(name)			{ name, NULL, 0, 0 }
85 #	define RECURSIVE_LOCK_INITIALIZER(name)	{ MUTEX_INITIALIZER(name), -1, 0 }
86 #endif
87 
88 #define RW_LOCK_INITIALIZER(name)			{ name, NULL, -1, 0, 0, 0 }
89 
90 
91 #ifdef __cplusplus
92 extern "C" {
93 #endif
94 
95 extern void	recursive_lock_init(recursive_lock *lock, const char *name);
96 	// name is *not* cloned nor freed in recursive_lock_destroy()
97 extern void recursive_lock_init_etc(recursive_lock *lock, const char *name,
98 	uint32 flags);
99 extern void recursive_lock_destroy(recursive_lock *lock);
100 extern status_t recursive_lock_lock(recursive_lock *lock);
101 extern status_t recursive_lock_trylock(recursive_lock *lock);
102 extern void recursive_lock_unlock(recursive_lock *lock);
103 extern int32 recursive_lock_get_recursion(recursive_lock *lock);
104 
105 extern void rw_lock_init(rw_lock* lock, const char* name);
106 	// name is *not* cloned nor freed in rw_lock_destroy()
107 extern void rw_lock_init_etc(rw_lock* lock, const char* name, uint32 flags);
108 extern void rw_lock_destroy(rw_lock* lock);
109 extern status_t rw_lock_read_lock(rw_lock* lock);
110 extern status_t rw_lock_read_unlock(rw_lock* lock);
111 extern status_t rw_lock_write_lock(rw_lock* lock);
112 extern status_t rw_lock_write_unlock(rw_lock* lock);
113 
114 extern void mutex_init(mutex* lock, const char* name);
115 	// name is *not* cloned nor freed in mutex_destroy()
116 extern void mutex_init_etc(mutex* lock, const char* name, uint32 flags);
117 extern void mutex_destroy(mutex* lock);
118 extern status_t mutex_switch_lock(mutex* from, mutex* to);
119 	// Unlocks "from" and locks "to" such that unlocking and starting to wait
120 	// for the lock is atomically. I.e. if "from" guards the object "to" belongs
121 	// to, the operation is safe as long as "from" is held while destroying
122 	// "to".
123 
124 // implementation private:
125 extern status_t _mutex_lock(mutex* lock, bool threadsLocked);
126 extern void _mutex_unlock(mutex* lock, bool threadsLocked);
127 extern status_t _mutex_trylock(mutex* lock);
128 
129 
130 static inline status_t
131 mutex_lock(mutex* lock)
132 {
133 #ifdef KDEBUG
134 	return _mutex_lock(lock, false);
135 #else
136 	if (atomic_add(&lock->count, -1) < 0)
137 		return _mutex_lock(lock, false);
138 	return B_OK;
139 #endif
140 }
141 
142 
143 static inline status_t
144 mutex_lock_threads_locked(mutex* lock)
145 {
146 #ifdef KDEBUG
147 	return _mutex_lock(lock, true);
148 #else
149 	if (atomic_add(&lock->count, -1) < 0)
150 		return _mutex_lock(lock, true);
151 	return B_OK;
152 #endif
153 }
154 
155 
156 static inline status_t
157 mutex_trylock(mutex* lock)
158 {
159 #ifdef KDEBUG
160 	return _mutex_trylock(lock);
161 #else
162 	if (atomic_test_and_set(&lock->count, -1, 0) != 0)
163 		return B_WOULD_BLOCK;
164 	return B_OK;
165 #endif
166 }
167 
168 
169 static inline void
170 mutex_unlock(mutex* lock)
171 {
172 #if !defined(KDEBUG)
173 	if (atomic_add(&lock->count, 1) < -1)
174 #endif
175 		_mutex_unlock(lock, false);
176 }
177 
178 
179 static inline void
180 mutex_transfer_lock(mutex* lock, thread_id thread)
181 {
182 #ifdef KDEBUG
183 	lock->holder = thread;
184 #endif
185 }
186 
187 
188 extern void lock_debug_init();
189 
190 #ifdef __cplusplus
191 }
192 #endif
193 
194 #endif	/* _KERNEL_LOCK_H */
195