Make UEFI boot-platform build again
[haiku.git] / headers / private / shared / locks.h
blobefea5907e5b7864f4aa3eaf002649b76fb02d6a0
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_
8 #include <OS.h>
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
14 typedef struct mutex {
15 const char* name;
16 int32 lock;
17 uint32 flags;
18 } mutex;
20 #define MUTEX_FLAG_CLONE_NAME 0x1
21 #define MUTEX_FLAG_ADAPTIVE 0x2
22 #define MUTEX_INITIALIZER(name) { name, 0, 0 }
24 #define mutex_init(lock, name) __mutex_init(lock, name)
25 #define mutex_init_etc(lock, name, flags) __mutex_init_etc(lock, name, flags)
26 #define mutex_destroy(lock) __mutex_destroy(lock)
27 #define mutex_lock(lock) __mutex_lock(lock)
28 #define mutex_unlock(lock) __mutex_unlock(lock)
30 void __mutex_init(mutex *lock, const char *name);
31 void __mutex_init_etc(mutex *lock, const char *name, uint32 flags);
32 void __mutex_destroy(mutex *lock);
33 status_t __mutex_lock(mutex *lock);
34 void __mutex_unlock(mutex *lock);
37 typedef struct rw_lock {
38 mutex lock;
39 struct rw_lock_waiter * waiters;
40 struct rw_lock_waiter * last_waiter;
41 thread_id holder;
42 int32 reader_count;
43 int32 writer_count;
44 int32 owner_count;
45 } rw_lock;
47 #define RW_LOCK_FLAG_CLONE_NAME MUTEX_FLAG_CLONE_NAME
48 #define RW_LOCK_INITIALIZER(name) { MUTEX_INITIALIZER(name), NULL, \
49 NULL, -1, 0, 0, 0 }
51 #define rw_lock_init(lock, name) __rw_lock_init(lock, name)
52 #define rw_lock_init_etc(lock, name, flags) \
53 __rw_lock_init_etc(lock, name, flags)
54 #define rw_lock_destroy(lock) __rw_lock_destroy(lock)
55 #define rw_lock_read_lock(lock) __rw_lock_read_lock(lock)
56 #define rw_lock_read_unlock(lock) __rw_lock_read_unlock(lock)
57 #define rw_lock_write_lock(lock) __rw_lock_write_lock(lock)
58 #define rw_lock_write_unlock(lock) __rw_lock_write_unlock(lock)
60 void __rw_lock_init(rw_lock *lock, const char *name);
61 void __rw_lock_init_etc(rw_lock *lock, const char *name, uint32 flags);
62 void __rw_lock_destroy(rw_lock *lock);
63 status_t __rw_lock_read_lock(rw_lock *lock);
64 status_t __rw_lock_read_unlock(rw_lock *lock);
65 status_t __rw_lock_write_lock(rw_lock *lock);
66 status_t __rw_lock_write_unlock(rw_lock *lock);
69 typedef struct recursive_lock {
70 mutex lock;
71 thread_id holder;
72 int32 recursion;
73 } recursive_lock;
75 #define RECURSIVE_LOCK_FLAG_CLONE_NAME MUTEX_FLAG_CLONE_NAME
76 #define RECURSIVE_LOCK_INITIALIZER(name) { MUTEX_INITIALIZER(name), -1, 0 }
78 #define recursive_lock_init(lock, name) __recursive_lock_init(lock, name)
79 #define recursive_lock_init_etc(lock, name, flags) \
80 __recursive_lock_init_etc(lock, name, flags)
81 #define recursive_lock_destroy(lock) __recursive_lock_destroy(lock)
82 #define recursive_lock_lock(lock) __recursive_lock_lock(lock)
83 #define recursive_lock_unlock(lock) __recursive_lock_unlock(lock)
84 #define recursive_lock_get_recursion(lock) __recursive_lock_get_recursion(lock)
86 void __recursive_lock_init(recursive_lock *lock, const char *name);
87 void __recursive_lock_init_etc(recursive_lock *lock, const char *name,
88 uint32 flags);
89 void __recursive_lock_destroy(recursive_lock *lock);
90 status_t __recursive_lock_lock(recursive_lock *lock);
91 void __recursive_lock_unlock(recursive_lock *lock);
92 int32 __recursive_lock_get_recursion(recursive_lock *lock);
95 #define INIT_ONCE_UNINITIALIZED -1
96 #define INIT_ONCE_INITIALIZED -4
98 status_t __init_once(int32* control, status_t (*initRoutine)(void*),
99 void* data);
101 #ifdef __cplusplus
102 } // extern "C"
105 #include <AutoLocker.h>
107 class MutexLocking {
108 public:
109 inline bool Lock(struct mutex *lock)
111 return mutex_lock(lock) == B_OK;
114 inline void Unlock(struct mutex *lock)
116 mutex_unlock(lock);
121 class RWLockReadLocking {
122 public:
123 inline bool Lock(struct rw_lock *lock)
125 return rw_lock_read_lock(lock) == B_OK;
128 inline void Unlock(struct rw_lock *lock)
130 rw_lock_read_unlock(lock);
135 class RWLockWriteLocking {
136 public:
137 inline bool Lock(struct rw_lock *lock)
139 return rw_lock_write_lock(lock) == B_OK;
142 inline void Unlock(struct rw_lock *lock)
144 rw_lock_write_unlock(lock);
149 typedef AutoLocker<mutex, MutexLocking> MutexLocker;
150 typedef AutoLocker<rw_lock, RWLockReadLocking> ReadLocker;
151 typedef AutoLocker<rw_lock, RWLockWriteLocking> WriteLocker;
153 #endif // __cplusplus
155 #endif // _LOCKS_H_