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_INITIALIZER(name) { name, 0, 0 } 22 23 void mutex_init(mutex *lock, const char *name); 24 void mutex_init_etc(mutex *lock, const char *name, uint32 flags); 25 void mutex_destroy(mutex *lock); 26 status_t mutex_lock(mutex *lock); 27 void mutex_unlock(mutex *lock); 28 29 30 typedef struct rw_lock { 31 mutex lock; 32 struct rw_lock_waiter * waiters; 33 struct rw_lock_waiter * last_waiter; 34 thread_id holder; 35 int32 reader_count; 36 int32 writer_count; 37 int32 owner_count; 38 } rw_lock; 39 40 #define RW_LOCK_FLAG_CLONE_NAME MUTEX_FLAG_CLONE_NAME 41 #define RW_LOCK_INITIALIZER(name) { MUTEX_INITIALIZER(name), NULL, \ 42 NULL, -1, 0, 0, 0 } 43 44 void rw_lock_init(rw_lock *lock, const char *name); 45 void rw_lock_init_etc(rw_lock *lock, const char *name, uint32 flags); 46 void rw_lock_destroy(rw_lock *lock); 47 status_t rw_lock_read_lock(rw_lock *lock); 48 status_t rw_lock_read_unlock(rw_lock *lock); 49 status_t rw_lock_write_lock(rw_lock *lock); 50 status_t rw_lock_write_unlock(rw_lock *lock); 51 52 53 typedef struct recursive_lock { 54 mutex lock; 55 thread_id holder; 56 int32 recursion; 57 } recursive_lock; 58 59 #define RECURSIVE_LOCK_FLAG_CLONE_NAME MUTEX_FLAG_CLONE_NAME 60 #define RECURSIVE_LOCK_INITIALIZER(name) { MUTEX_INITIALIZER(name), -1, 0 } 61 62 void recursive_lock_init(recursive_lock *lock, const char *name); 63 void recursive_lock_init_etc(recursive_lock *lock, const char *name, 64 uint32 flags); 65 void recursive_lock_destroy(recursive_lock *lock); 66 status_t recursive_lock_lock(recursive_lock *lock); 67 void recursive_lock_unlock(recursive_lock *lock); 68 int32 recursive_lock_get_recursion(recursive_lock *lock); 69 70 71 #define INIT_ONCE_UNINITIALIZED -1 72 #define INIT_ONCE_INITIALIZED -4 73 74 status_t __init_once(vint32* control, status_t (*initRoutine)(void*), 75 void* data); 76 77 #ifdef __cplusplus 78 } // extern "C" 79 80 81 #include <AutoLocker.h> 82 83 class MutexLocking { 84 public: 85 inline bool Lock(struct mutex *lock) 86 { 87 return mutex_lock(lock) == B_OK; 88 } 89 90 inline void Unlock(struct mutex *lock) 91 { 92 mutex_unlock(lock); 93 } 94 }; 95 96 97 class RWLockReadLocking { 98 public: 99 inline bool Lock(struct rw_lock *lock) 100 { 101 return rw_lock_read_lock(lock) == B_OK; 102 } 103 104 inline void Unlock(struct rw_lock *lock) 105 { 106 rw_lock_read_unlock(lock); 107 } 108 }; 109 110 111 class RWLockWriteLocking { 112 public: 113 inline bool Lock(struct rw_lock *lock) 114 { 115 return rw_lock_write_lock(lock) == B_OK; 116 } 117 118 inline void Unlock(struct rw_lock *lock) 119 { 120 rw_lock_write_unlock(lock); 121 } 122 }; 123 124 125 typedef AutoLocker<mutex, MutexLocking> MutexLocker; 126 typedef AutoLocker<rw_lock, RWLockReadLocking> ReadLocker; 127 typedef AutoLocker<rw_lock, RWLockWriteLocking> WriteLocker; 128 129 #endif // __cplusplus 130 131 #endif // _LOCKS_H_ 132