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 65 extern void fssh_rw_lock_init(fssh_rw_lock* lock, const char* name); 66 // name is *not* cloned nor freed in rw_lock_destroy() 67 extern void fssh_rw_lock_init_etc(fssh_rw_lock* lock, const char* name, uint32_t flags); 68 extern void fssh_rw_lock_destroy(fssh_rw_lock* lock); 69 extern fssh_status_t fssh_rw_lock_read_lock(fssh_rw_lock* lock); 70 extern fssh_status_t fssh_rw_lock_read_unlock(fssh_rw_lock* lock); 71 extern fssh_status_t fssh_rw_lock_write_lock(fssh_rw_lock* lock); 72 extern fssh_status_t fssh_rw_lock_write_unlock(fssh_rw_lock* lock); 73 74 extern void fssh_mutex_init(fssh_mutex* lock, const char* name); 75 // name is *not* cloned nor freed in mutex_destroy() 76 extern void fssh_mutex_init_etc(fssh_mutex* lock, const char* name, uint32_t flags); 77 extern void fssh_mutex_destroy(fssh_mutex* lock); 78 extern fssh_status_t fssh_mutex_lock(fssh_mutex* lock); 79 extern fssh_status_t fssh_mutex_trylock(fssh_mutex* lock); 80 extern void fssh_mutex_unlock(fssh_mutex* lock); 81 extern void fssh_mutex_transfer_lock(fssh_mutex* lock, fssh_thread_id thread); 82 83 #ifdef __cplusplus 84 } 85 86 namespace FSShell { 87 88 // MutexLocking 89 class MutexLocking { 90 public: 91 inline bool Lock(fssh_mutex *lockable) 92 { 93 return fssh_mutex_lock(lockable) == FSSH_B_OK; 94 } 95 96 inline void Unlock(fssh_mutex *lockable) 97 { 98 fssh_mutex_unlock(lockable); 99 } 100 }; 101 102 // MutexLocker 103 typedef AutoLocker<fssh_mutex, MutexLocking> MutexLocker; 104 105 // RecursiveLockLocking 106 class RecursiveLockLocking { 107 public: 108 inline bool Lock(fssh_recursive_lock *lockable) 109 { 110 return fssh_recursive_lock_lock(lockable) == FSSH_B_OK; 111 } 112 113 inline void Unlock(fssh_recursive_lock *lockable) 114 { 115 fssh_recursive_lock_unlock(lockable); 116 } 117 }; 118 119 // RecursiveLocker 120 typedef AutoLocker<fssh_recursive_lock, RecursiveLockLocking> RecursiveLocker; 121 122 class ReadWriteLockReadLocking { 123 public: 124 inline bool Lock(fssh_rw_lock *lockable) 125 { 126 return fssh_rw_lock_read_lock(lockable) == FSSH_B_OK; 127 } 128 129 inline void Unlock(fssh_rw_lock *lockable) 130 { 131 fssh_rw_lock_read_unlock(lockable); 132 } 133 }; 134 135 class ReadWriteLockWriteLocking { 136 public: 137 inline bool Lock(fssh_rw_lock *lockable) 138 { 139 return fssh_rw_lock_write_lock(lockable) == FSSH_B_OK; 140 } 141 142 inline void Unlock(fssh_rw_lock *lockable) 143 { 144 fssh_rw_lock_write_unlock(lockable); 145 } 146 }; 147 148 typedef AutoLocker<fssh_rw_lock, ReadWriteLockReadLocking> ReadLocker; 149 typedef AutoLocker<fssh_rw_lock, ReadWriteLockWriteLocking> WriteLocker; 150 151 } // namespace FSShell 152 153 using FSShell::AutoLocker; 154 using FSShell::MutexLocker; 155 using FSShell::RecursiveLocker; 156 using FSShell::ReadLocker; 157 using FSShell::WriteLocker; 158 159 #endif // __cplusplus 160 161 #endif /* _FSSH_KERNEL_LOCK_H */ 162