xref: /haiku/headers/private/shared/locks.h (revision 37fedaf8494b34aad811abcc49e79aa32943f880)
1 /*
2  * Copyright 2009, Michael Lotz, mmlr@mlotz.ch.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _LOCKS_H_
6 #define _LOCKS_H_
7 
8 #include <OS.h>
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 typedef struct mutex {
15 	const char*	name;
16 	int32		lock;
17 	uint32		flags;
18 } mutex;
19 
20 #define MUTEX_FLAG_CLONE_NAME		0x1
21 #define MUTEX_FLAG_ADAPTIVE			0x2
22 #define MUTEX_INITIALIZER(name)		{ name, 0, 0 }
23 
24 void		mutex_init(mutex *lock, const char *name);
25 void		mutex_init_etc(mutex *lock, const char *name, uint32 flags);
26 void		mutex_destroy(mutex *lock);
27 status_t	mutex_lock(mutex *lock);
28 void		mutex_unlock(mutex *lock);
29 
30 
31 typedef struct rw_lock {
32 	mutex					lock;
33 	struct rw_lock_waiter *	waiters;
34 	struct rw_lock_waiter *	last_waiter;
35 	thread_id				holder;
36 	int32					reader_count;
37 	int32					writer_count;
38 	int32					owner_count;
39 } rw_lock;
40 
41 #define RW_LOCK_FLAG_CLONE_NAME			MUTEX_FLAG_CLONE_NAME
42 #define RW_LOCK_INITIALIZER(name)		{ MUTEX_INITIALIZER(name), NULL, \
43 											NULL, -1, 0, 0, 0 }
44 
45 void		rw_lock_init(rw_lock *lock, const char *name);
46 void		rw_lock_init_etc(rw_lock *lock, const char *name, uint32 flags);
47 void		rw_lock_destroy(rw_lock *lock);
48 status_t	rw_lock_read_lock(rw_lock *lock);
49 status_t	rw_lock_read_unlock(rw_lock *lock);
50 status_t	rw_lock_write_lock(rw_lock *lock);
51 status_t	rw_lock_write_unlock(rw_lock *lock);
52 
53 
54 typedef struct recursive_lock {
55 	mutex		lock;
56 	thread_id	holder;
57 	int32		recursion;
58 } recursive_lock;
59 
60 #define RECURSIVE_LOCK_FLAG_CLONE_NAME		MUTEX_FLAG_CLONE_NAME
61 #define RECURSIVE_LOCK_INITIALIZER(name)	{ MUTEX_INITIALIZER(name), -1, 0 }
62 
63 void		recursive_lock_init(recursive_lock *lock, const char *name);
64 void		recursive_lock_init_etc(recursive_lock *lock, const char *name,
65 				uint32 flags);
66 void		recursive_lock_destroy(recursive_lock *lock);
67 status_t	recursive_lock_lock(recursive_lock *lock);
68 void		recursive_lock_unlock(recursive_lock *lock);
69 int32		recursive_lock_get_recursion(recursive_lock *lock);
70 
71 
72 #define		INIT_ONCE_UNINITIALIZED	-1
73 #define		INIT_ONCE_INITIALIZED	-4
74 
75 status_t	__init_once(int32* control, status_t (*initRoutine)(void*),
76 				void* data);
77 
78 #ifdef __cplusplus
79 } // extern "C"
80 
81 
82 #include <AutoLocker.h>
83 
84 class MutexLocking {
85 public:
86 	inline bool Lock(struct mutex *lock)
87 	{
88 		return mutex_lock(lock) == B_OK;
89 	}
90 
91 	inline void Unlock(struct mutex *lock)
92 	{
93 		mutex_unlock(lock);
94 	}
95 };
96 
97 
98 class RWLockReadLocking {
99 public:
100 	inline bool Lock(struct rw_lock *lock)
101 	{
102 		return rw_lock_read_lock(lock) == B_OK;
103 	}
104 
105 	inline void Unlock(struct rw_lock *lock)
106 	{
107 		rw_lock_read_unlock(lock);
108 	}
109 };
110 
111 
112 class RWLockWriteLocking {
113 public:
114 	inline bool Lock(struct rw_lock *lock)
115 	{
116 		return rw_lock_write_lock(lock) == B_OK;
117 	}
118 
119 	inline void Unlock(struct rw_lock *lock)
120 	{
121 		rw_lock_write_unlock(lock);
122 	}
123 };
124 
125 
126 typedef AutoLocker<mutex, MutexLocking> MutexLocker;
127 typedef AutoLocker<rw_lock, RWLockReadLocking> ReadLocker;
128 typedef AutoLocker<rw_lock, RWLockWriteLocking> WriteLocker;
129 
130 #endif // __cplusplus
131 
132 #endif // _LOCKS_H_
133