1589f1a91SAxel Dörfler /* 2589f1a91SAxel Dörfler * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de. 3589f1a91SAxel Dörfler * Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de. 4589f1a91SAxel Dörfler * Distributed under the terms of the MIT License. 5589f1a91SAxel Dörfler * 6589f1a91SAxel Dörfler * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved. 7589f1a91SAxel Dörfler * Distributed under the terms of the NewOS License. 8589f1a91SAxel Dörfler */ 9589f1a91SAxel Dörfler #ifndef _FSSH_KERNEL_LOCK_H 10589f1a91SAxel Dörfler #define _FSSH_KERNEL_LOCK_H 11589f1a91SAxel Dörfler 12589f1a91SAxel Dörfler 13589f1a91SAxel Dörfler #include <fssh_auto_locker.h> 14589f1a91SAxel Dörfler #include <fssh_errors.h> 15589f1a91SAxel Dörfler #include <fssh_os.h> 16589f1a91SAxel Dörfler 17589f1a91SAxel Dörfler 18589f1a91SAxel Dörfler typedef struct fssh_mutex { 19589f1a91SAxel Dörfler fssh_sem_id sem; 20589f1a91SAxel Dörfler fssh_thread_id holder; 21589f1a91SAxel Dörfler } fssh_mutex; 22589f1a91SAxel Dörfler 23589f1a91SAxel Dörfler #define FSSH_MUTEX_FLAG_CLONE_NAME 0x1 24589f1a91SAxel Dörfler 25589f1a91SAxel Dörfler 26589f1a91SAxel Dörfler typedef struct fssh_recursive_lock { 27589f1a91SAxel Dörfler fssh_sem_id sem; 28589f1a91SAxel Dörfler fssh_thread_id holder; 29589f1a91SAxel Dörfler int recursion; 30589f1a91SAxel Dörfler } fssh_recursive_lock; 31589f1a91SAxel Dörfler 32589f1a91SAxel Dörfler 33589f1a91SAxel Dörfler typedef struct fssh_rw_lock { 34589f1a91SAxel Dörfler fssh_sem_id sem; 3578b32dd0SAxel Dörfler fssh_thread_id holder; 36589f1a91SAxel Dörfler int32_t count; 37589f1a91SAxel Dörfler } fssh_rw_lock; 38589f1a91SAxel Dörfler 39589f1a91SAxel Dörfler #define FSSH_RW_LOCK_FLAG_CLONE_NAME 0x1 40589f1a91SAxel Dörfler 41589f1a91SAxel Dörfler #define FSSH_ASSERT_LOCKED_RECURSIVE(r) 42589f1a91SAxel Dörfler #define FSSH_ASSERT_LOCKED_MUTEX(m) 4337de5a0dSAxel Dörfler #define FSSH_ASSERT_WRITE_LOCKED_RW_LOCK(l) 4437de5a0dSAxel Dörfler #define FSSH_ASSERT_READ_LOCKED_RW_LOCK(l) 45589f1a91SAxel Dörfler 46589f1a91SAxel Dörfler // static initializers 47589f1a91SAxel Dörfler #define FSSH_MUTEX_INITIALIZER(name) { name, NULL, 0, 0 } 48589f1a91SAxel Dörfler #define FSSH_RECURSIVE_LOCK_INITIALIZER(name) { FSSH_MUTEX_INITIALIZER(name), -1, 0 } 49589f1a91SAxel Dörfler #define FSSH_RW_LOCK_INITIALIZER(name) { name, NULL, -1, 0, 0, 0 } 50589f1a91SAxel Dörfler 51589f1a91SAxel Dörfler #ifdef __cplusplus 52589f1a91SAxel Dörfler extern "C" { 53589f1a91SAxel Dörfler #endif 54589f1a91SAxel Dörfler 55589f1a91SAxel Dörfler extern void fssh_recursive_lock_init(fssh_recursive_lock *lock, const char *name); 56589f1a91SAxel Dörfler // name is *not* cloned nor freed in recursive_lock_destroy() 57589f1a91SAxel Dörfler extern void fssh_recursive_lock_init_etc(fssh_recursive_lock *lock, const char *name, 58589f1a91SAxel Dörfler uint32_t flags); 59589f1a91SAxel Dörfler extern void fssh_recursive_lock_destroy(fssh_recursive_lock *lock); 60589f1a91SAxel Dörfler extern fssh_status_t fssh_recursive_lock_lock(fssh_recursive_lock *lock); 61af370c70SAxel Dörfler extern fssh_status_t fssh_recursive_lock_trylock(fssh_recursive_lock *lock); 62589f1a91SAxel Dörfler extern void fssh_recursive_lock_unlock(fssh_recursive_lock *lock); 63589f1a91SAxel Dörfler extern int32_t fssh_recursive_lock_get_recursion(fssh_recursive_lock *lock); 64*1674a53aSAxel Dörfler extern void fssh_recursive_lock_transfer_lock(fssh_recursive_lock *lock, fssh_thread_id thread); 65589f1a91SAxel Dörfler 66589f1a91SAxel Dörfler extern void fssh_rw_lock_init(fssh_rw_lock* lock, const char* name); 67589f1a91SAxel Dörfler // name is *not* cloned nor freed in rw_lock_destroy() 68589f1a91SAxel Dörfler extern void fssh_rw_lock_init_etc(fssh_rw_lock* lock, const char* name, uint32_t flags); 69589f1a91SAxel Dörfler extern void fssh_rw_lock_destroy(fssh_rw_lock* lock); 70589f1a91SAxel Dörfler extern fssh_status_t fssh_rw_lock_read_lock(fssh_rw_lock* lock); 71589f1a91SAxel Dörfler extern fssh_status_t fssh_rw_lock_read_unlock(fssh_rw_lock* lock); 72589f1a91SAxel Dörfler extern fssh_status_t fssh_rw_lock_write_lock(fssh_rw_lock* lock); 73589f1a91SAxel Dörfler extern fssh_status_t fssh_rw_lock_write_unlock(fssh_rw_lock* lock); 74589f1a91SAxel Dörfler 75589f1a91SAxel Dörfler extern void fssh_mutex_init(fssh_mutex* lock, const char* name); 76589f1a91SAxel Dörfler // name is *not* cloned nor freed in mutex_destroy() 77589f1a91SAxel Dörfler extern void fssh_mutex_init_etc(fssh_mutex* lock, const char* name, uint32_t flags); 78589f1a91SAxel Dörfler extern void fssh_mutex_destroy(fssh_mutex* lock); 79589f1a91SAxel Dörfler extern fssh_status_t fssh_mutex_lock(fssh_mutex* lock); 80589f1a91SAxel Dörfler extern fssh_status_t fssh_mutex_trylock(fssh_mutex* lock); 81589f1a91SAxel Dörfler extern void fssh_mutex_unlock(fssh_mutex* lock); 8278b32dd0SAxel Dörfler extern void fssh_mutex_transfer_lock(fssh_mutex* lock, fssh_thread_id thread); 83589f1a91SAxel Dörfler 84589f1a91SAxel Dörfler #ifdef __cplusplus 85589f1a91SAxel Dörfler } 86589f1a91SAxel Dörfler 87589f1a91SAxel Dörfler namespace FSShell { 88589f1a91SAxel Dörfler 89589f1a91SAxel Dörfler // MutexLocking 90589f1a91SAxel Dörfler class MutexLocking { 91589f1a91SAxel Dörfler public: Lock(fssh_mutex * lockable)92589f1a91SAxel Dörfler inline bool Lock(fssh_mutex *lockable) 93589f1a91SAxel Dörfler { 94589f1a91SAxel Dörfler return fssh_mutex_lock(lockable) == FSSH_B_OK; 95589f1a91SAxel Dörfler } 96589f1a91SAxel Dörfler Unlock(fssh_mutex * lockable)97589f1a91SAxel Dörfler inline void Unlock(fssh_mutex *lockable) 98589f1a91SAxel Dörfler { 99589f1a91SAxel Dörfler fssh_mutex_unlock(lockable); 100589f1a91SAxel Dörfler } 101589f1a91SAxel Dörfler }; 102589f1a91SAxel Dörfler 103589f1a91SAxel Dörfler // MutexLocker 104589f1a91SAxel Dörfler typedef AutoLocker<fssh_mutex, MutexLocking> MutexLocker; 105589f1a91SAxel Dörfler 106589f1a91SAxel Dörfler // RecursiveLockLocking 107589f1a91SAxel Dörfler class RecursiveLockLocking { 108589f1a91SAxel Dörfler public: Lock(fssh_recursive_lock * lockable)109589f1a91SAxel Dörfler inline bool Lock(fssh_recursive_lock *lockable) 110589f1a91SAxel Dörfler { 111589f1a91SAxel Dörfler return fssh_recursive_lock_lock(lockable) == FSSH_B_OK; 112589f1a91SAxel Dörfler } 113589f1a91SAxel Dörfler Unlock(fssh_recursive_lock * lockable)114589f1a91SAxel Dörfler inline void Unlock(fssh_recursive_lock *lockable) 115589f1a91SAxel Dörfler { 116589f1a91SAxel Dörfler fssh_recursive_lock_unlock(lockable); 117589f1a91SAxel Dörfler } 118589f1a91SAxel Dörfler }; 119589f1a91SAxel Dörfler 120589f1a91SAxel Dörfler // RecursiveLocker 121589f1a91SAxel Dörfler typedef AutoLocker<fssh_recursive_lock, RecursiveLockLocking> RecursiveLocker; 122589f1a91SAxel Dörfler 12306d5e1b3SAxel Dörfler class ReadWriteLockReadLocking { 12406d5e1b3SAxel Dörfler public: Lock(fssh_rw_lock * lockable)12506d5e1b3SAxel Dörfler inline bool Lock(fssh_rw_lock *lockable) 12606d5e1b3SAxel Dörfler { 12706d5e1b3SAxel Dörfler return fssh_rw_lock_read_lock(lockable) == FSSH_B_OK; 12806d5e1b3SAxel Dörfler } 12906d5e1b3SAxel Dörfler Unlock(fssh_rw_lock * lockable)13006d5e1b3SAxel Dörfler inline void Unlock(fssh_rw_lock *lockable) 13106d5e1b3SAxel Dörfler { 13206d5e1b3SAxel Dörfler fssh_rw_lock_read_unlock(lockable); 13306d5e1b3SAxel Dörfler } 13406d5e1b3SAxel Dörfler }; 13506d5e1b3SAxel Dörfler 13606d5e1b3SAxel Dörfler class ReadWriteLockWriteLocking { 13706d5e1b3SAxel Dörfler public: Lock(fssh_rw_lock * lockable)13806d5e1b3SAxel Dörfler inline bool Lock(fssh_rw_lock *lockable) 13906d5e1b3SAxel Dörfler { 14006d5e1b3SAxel Dörfler return fssh_rw_lock_write_lock(lockable) == FSSH_B_OK; 14106d5e1b3SAxel Dörfler } 14206d5e1b3SAxel Dörfler Unlock(fssh_rw_lock * lockable)14306d5e1b3SAxel Dörfler inline void Unlock(fssh_rw_lock *lockable) 14406d5e1b3SAxel Dörfler { 14506d5e1b3SAxel Dörfler fssh_rw_lock_write_unlock(lockable); 14606d5e1b3SAxel Dörfler } 14706d5e1b3SAxel Dörfler }; 14806d5e1b3SAxel Dörfler 14906d5e1b3SAxel Dörfler typedef AutoLocker<fssh_rw_lock, ReadWriteLockReadLocking> ReadLocker; 15006d5e1b3SAxel Dörfler typedef AutoLocker<fssh_rw_lock, ReadWriteLockWriteLocking> WriteLocker; 15106d5e1b3SAxel Dörfler 152589f1a91SAxel Dörfler } // namespace FSShell 153589f1a91SAxel Dörfler 154589f1a91SAxel Dörfler using FSShell::AutoLocker; 155589f1a91SAxel Dörfler using FSShell::MutexLocker; 156589f1a91SAxel Dörfler using FSShell::RecursiveLocker; 15706d5e1b3SAxel Dörfler using FSShell::ReadLocker; 15806d5e1b3SAxel Dörfler using FSShell::WriteLocker; 159589f1a91SAxel Dörfler 160589f1a91SAxel Dörfler #endif // __cplusplus 161589f1a91SAxel Dörfler 162589f1a91SAxel Dörfler #endif /* _FSSH_KERNEL_LOCK_H */ 163