1 /* 2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _USERLAND_FS_RECURSIVE_LOCK_H 6 #define _USERLAND_FS_RECURSIVE_LOCK_H 7 8 #include <lock.h> // private/kernel 9 10 11 namespace UserlandFSUtil { 12 13 14 class RecursiveLock { 15 public: 16 RecursiveLock(bool benaphore_style = true) 17 { 18 recursive_lock_init(&fLock, "anonymous locker"); 19 } 20 21 RecursiveLock(const char *name, bool benaphore_style = true) 22 { 23 recursive_lock_init_etc(&fLock, name, MUTEX_FLAG_CLONE_NAME); 24 } 25 ~RecursiveLock()26 ~RecursiveLock() 27 { 28 recursive_lock_destroy(&fLock); 29 } 30 InitCheck()31 status_t InitCheck() const 32 { 33 return B_OK; 34 } 35 Lock(void)36 bool Lock(void) 37 { 38 return recursive_lock_lock(&fLock) == B_OK; 39 } 40 Unlock(void)41 void Unlock(void) 42 { 43 recursive_lock_unlock(&fLock); 44 } 45 LockingThread(void)46 thread_id LockingThread(void) const 47 { 48 return RECURSIVE_LOCK_HOLDER(&fLock); 49 } 50 IsLocked(void)51 bool IsLocked(void) const 52 { 53 return RECURSIVE_LOCK_HOLDER(&fLock) == find_thread(NULL); 54 } 55 CountLocks(void)56 int32 CountLocks(void) const 57 { 58 int32 count = recursive_lock_get_recursion( 59 const_cast<recursive_lock*>(&fLock)); 60 return count >= 0 ? count : 0; 61 } 62 63 private: 64 recursive_lock fLock; 65 }; 66 67 68 }; // namespace UserlandFSUtil 69 70 using UserlandFSUtil::RecursiveLock; 71 72 #endif // _USERLAND_FS_RECURSIVE_LOCK_H 73