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 int32 benaphore; 16 sem_id semaphore; 17 } mutex; 18 19 status_t mutex_init(mutex *lock, const char *name); 20 void mutex_destroy(mutex *lock); 21 status_t mutex_lock(mutex *lock); 22 void mutex_unlock(mutex *lock); 23 24 25 typedef struct lazy_mutex { 26 int32 benaphore; 27 sem_id semaphore; 28 const char* name; 29 } lazy_mutex; 30 31 status_t lazy_mutex_init(lazy_mutex *lock, const char *name); 32 // name will not be cloned and must rename valid 33 void lazy_mutex_destroy(lazy_mutex *lock); 34 status_t lazy_mutex_lock(lazy_mutex *lock); 35 void lazy_mutex_unlock(lazy_mutex *lock); 36 37 38 typedef struct rw_lock { 39 const char * name; 40 mutex lock; 41 struct rw_lock_waiter * waiters; 42 struct rw_lock_waiter * last_waiter; 43 thread_id holder; 44 int32 reader_count; 45 int32 writer_count; 46 int32 owner_count; 47 } rw_lock; 48 49 status_t rw_lock_init(rw_lock *lock, const char *name); 50 void rw_lock_destroy(rw_lock *lock); 51 status_t rw_lock_read_lock(rw_lock *lock); 52 status_t rw_lock_read_unlock(rw_lock *lock); 53 status_t rw_lock_write_lock(rw_lock *lock); 54 status_t rw_lock_write_unlock(rw_lock *lock); 55 56 57 typedef struct recursive_lock { 58 mutex lock; 59 thread_id holder; 60 int recursion; 61 } recursive_lock; 62 63 status_t recursive_lock_init(recursive_lock *lock, const char *name); 64 void recursive_lock_destroy(recursive_lock *lock); 65 status_t recursive_lock_lock(recursive_lock *lock); 66 void recursive_lock_unlock(recursive_lock *lock); 67 int32 recursive_lock_get_recursion(recursive_lock *lock); 68 69 70 typedef struct lazy_recursive_lock { 71 lazy_mutex lock; 72 thread_id holder; 73 int recursion; 74 } lazy_recursive_lock; 75 76 status_t lazy_recursive_lock_init(lazy_recursive_lock *lock, 77 const char *name); 78 // name will not be cloned and must rename valid 79 void lazy_recursive_lock_destroy(lazy_recursive_lock *lock); 80 status_t lazy_recursive_lock_lock(lazy_recursive_lock *lock); 81 void lazy_recursive_lock_unlock(lazy_recursive_lock *lock); 82 int32 lazy_recursive_lock_get_recursion(lazy_recursive_lock *lock); 83 84 85 #define INIT_ONCE_UNINITIALIZED -1 86 #define INIT_ONCE_INITIALIZED -4 87 88 status_t __init_once(vint32* control, status_t (*initRoutine)(void*), 89 void* data); 90 91 #ifdef __cplusplus 92 } // extern "C" 93 94 95 #include <AutoLocker.h> 96 97 class MutexLocking { 98 public: 99 inline bool Lock(struct mutex *lock) 100 { 101 return mutex_lock(lock) == B_OK; 102 } 103 104 inline void Unlock(struct mutex *lock) 105 { 106 mutex_unlock(lock); 107 } 108 }; 109 110 111 class RWLockReadLocking { 112 public: 113 inline bool Lock(struct rw_lock *lock) 114 { 115 return rw_lock_read_lock(lock) == B_OK; 116 } 117 118 inline void Unlock(struct rw_lock *lock) 119 { 120 rw_lock_read_unlock(lock); 121 } 122 }; 123 124 125 class RWLockWriteLocking { 126 public: 127 inline bool Lock(struct rw_lock *lock) 128 { 129 return rw_lock_write_lock(lock) == B_OK; 130 } 131 132 inline void Unlock(struct rw_lock *lock) 133 { 134 rw_lock_write_unlock(lock); 135 } 136 }; 137 138 139 typedef AutoLocker<mutex, MutexLocking> MutexLocker; 140 typedef AutoLocker<rw_lock, RWLockReadLocking> ReadLocker; 141 typedef AutoLocker<rw_lock, RWLockWriteLocking> WriteLocker; 142 143 #endif // __cplusplus 144 145 #endif // _LOCKS_H_ 146