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 _FSSH_KERNEL_LOCK_H 10 #define _FSSH_KERNEL_LOCK_H 11 12 13 #include <fssh_auto_locker.h> 14 #include <fssh_errors.h> 15 #include <fssh_os.h> 16 17 18 typedef struct fssh_mutex { 19 fssh_sem_id sem; 20 fssh_thread_id holder; 21 } fssh_mutex; 22 23 #define FSSH_MUTEX_FLAG_CLONE_NAME 0x1 24 25 26 typedef struct fssh_recursive_lock { 27 fssh_sem_id sem; 28 fssh_thread_id holder; 29 int recursion; 30 } fssh_recursive_lock; 31 32 33 typedef struct fssh_rw_lock { 34 fssh_sem_id sem; 35 fssh_thread_id holder; 36 int32_t count; 37 } fssh_rw_lock; 38 39 #define FSSH_RW_LOCK_FLAG_CLONE_NAME 0x1 40 41 #define FSSH_ASSERT_LOCKED_RECURSIVE(r) 42 #define FSSH_ASSERT_LOCKED_MUTEX(m) 43 #define FSSH_ASSERT_WRITE_LOCKED_RW_LOCK(l) 44 #define FSSH_ASSERT_READ_LOCKED_RW_LOCK(l) 45 46 // static initializers 47 #define FSSH_MUTEX_INITIALIZER(name) { name, NULL, 0, 0 } 48 #define FSSH_RECURSIVE_LOCK_INITIALIZER(name) { FSSH_MUTEX_INITIALIZER(name), -1, 0 } 49 #define FSSH_RW_LOCK_INITIALIZER(name) { name, NULL, -1, 0, 0, 0 } 50 51 #ifdef __cplusplus 52 extern "C" { 53 #endif 54 55 extern void fssh_recursive_lock_init(fssh_recursive_lock *lock, const char *name); 56 // name is *not* cloned nor freed in recursive_lock_destroy() 57 extern void fssh_recursive_lock_init_etc(fssh_recursive_lock *lock, const char *name, 58 uint32_t flags); 59 extern void fssh_recursive_lock_destroy(fssh_recursive_lock *lock); 60 extern fssh_status_t fssh_recursive_lock_lock(fssh_recursive_lock *lock); 61 extern fssh_status_t fssh_recursive_lock_trylock(fssh_recursive_lock *lock); 62 extern void fssh_recursive_lock_unlock(fssh_recursive_lock *lock); 63 extern int32_t fssh_recursive_lock_get_recursion(fssh_recursive_lock *lock); 64 extern void fssh_recursive_lock_transfer_lock(fssh_recursive_lock *lock, fssh_thread_id thread); 65 66 extern void fssh_rw_lock_init(fssh_rw_lock* lock, const char* name); 67 // name is *not* cloned nor freed in rw_lock_destroy() 68 extern void fssh_rw_lock_init_etc(fssh_rw_lock* lock, const char* name, uint32_t flags); 69 extern void fssh_rw_lock_destroy(fssh_rw_lock* lock); 70 extern fssh_status_t fssh_rw_lock_read_lock(fssh_rw_lock* lock); 71 extern fssh_status_t fssh_rw_lock_read_unlock(fssh_rw_lock* lock); 72 extern fssh_status_t fssh_rw_lock_write_lock(fssh_rw_lock* lock); 73 extern fssh_status_t fssh_rw_lock_write_unlock(fssh_rw_lock* lock); 74 75 extern void fssh_mutex_init(fssh_mutex* lock, const char* name); 76 // name is *not* cloned nor freed in mutex_destroy() 77 extern void fssh_mutex_init_etc(fssh_mutex* lock, const char* name, uint32_t flags); 78 extern void fssh_mutex_destroy(fssh_mutex* lock); 79 extern fssh_status_t fssh_mutex_lock(fssh_mutex* lock); 80 extern fssh_status_t fssh_mutex_trylock(fssh_mutex* lock); 81 extern void fssh_mutex_unlock(fssh_mutex* lock); 82 extern void fssh_mutex_transfer_lock(fssh_mutex* lock, fssh_thread_id thread); 83 84 #ifdef __cplusplus 85 } 86 87 namespace FSShell { 88 89 // MutexLocking 90 class MutexLocking { 91 public: 92 inline bool Lock(fssh_mutex *lockable) 93 { 94 return fssh_mutex_lock(lockable) == FSSH_B_OK; 95 } 96 97 inline void Unlock(fssh_mutex *lockable) 98 { 99 fssh_mutex_unlock(lockable); 100 } 101 }; 102 103 // MutexLocker 104 typedef AutoLocker<fssh_mutex, MutexLocking> MutexLocker; 105 106 // RecursiveLockLocking 107 class RecursiveLockLocking { 108 public: 109 inline bool Lock(fssh_recursive_lock *lockable) 110 { 111 return fssh_recursive_lock_lock(lockable) == FSSH_B_OK; 112 } 113 114 inline void Unlock(fssh_recursive_lock *lockable) 115 { 116 fssh_recursive_lock_unlock(lockable); 117 } 118 }; 119 120 // RecursiveLocker 121 typedef AutoLocker<fssh_recursive_lock, RecursiveLockLocking> RecursiveLocker; 122 123 class ReadWriteLockReadLocking { 124 public: 125 inline bool Lock(fssh_rw_lock *lockable) 126 { 127 return fssh_rw_lock_read_lock(lockable) == FSSH_B_OK; 128 } 129 130 inline void Unlock(fssh_rw_lock *lockable) 131 { 132 fssh_rw_lock_read_unlock(lockable); 133 } 134 }; 135 136 class ReadWriteLockWriteLocking { 137 public: 138 inline bool Lock(fssh_rw_lock *lockable) 139 { 140 return fssh_rw_lock_write_lock(lockable) == FSSH_B_OK; 141 } 142 143 inline void Unlock(fssh_rw_lock *lockable) 144 { 145 fssh_rw_lock_write_unlock(lockable); 146 } 147 }; 148 149 typedef AutoLocker<fssh_rw_lock, ReadWriteLockReadLocking> ReadLocker; 150 typedef AutoLocker<fssh_rw_lock, ReadWriteLockWriteLocking> WriteLocker; 151 152 } // namespace FSShell 153 154 using FSShell::AutoLocker; 155 using FSShell::MutexLocker; 156 using FSShell::RecursiveLocker; 157 using FSShell::ReadLocker; 158 using FSShell::WriteLocker; 159 160 #endif // __cplusplus 161 162 #endif /* _FSSH_KERNEL_LOCK_H */ 163