2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
5 #ifndef _RW_LOCK_MANAGER_H
6 #define _RW_LOCK_MANAGER_H
10 #include <util/DoublyLinkedList.h>
24 struct Waiter
: DoublyLinkedListLinkImpl
<Waiter
> {
27 thread(find_thread(NULL
)),
39 typedef DoublyLinkedList
<Waiter
> WaiterList
;
41 friend class RWLockManager
;
56 status_t
Init() { return fLock
.InitCheck(); }
58 bool Lock() { return fLock
.Lock(); }
59 void Unlock() { return fLock
.Unlock(); }
61 bool ReadLock(RWLockable
* lockable
);
62 bool TryReadLock(RWLockable
* lockable
);
63 status_t
ReadLockWithTimeout(RWLockable
* lockable
,
65 void ReadUnlock(RWLockable
* lockable
);
67 bool WriteLock(RWLockable
* lockable
);
68 bool TryWriteLock(RWLockable
* lockable
);
69 status_t
WriteLockWithTimeout(RWLockable
* lockable
,
71 void WriteUnlock(RWLockable
* lockable
);
73 inline bool GenericLock(bool write
, RWLockable
* lockable
);
74 inline bool TryGenericLock(bool write
,
75 RWLockable
* lockable
);
76 inline status_t
GenericLockWithTimeout(bool write
,
77 RWLockable
* lockable
, bigtime_t timeout
);
78 inline void GenericUnlock(bool write
, RWLockable
* lockable
);
81 status_t
_Wait(RWLockable
* lockable
, bool writer
,
83 void _Unblock(RWLockable
* lockable
);
91 RWLockManager::GenericLock(bool write
, RWLockable
* lockable
)
93 return write
? WriteLock(lockable
) : ReadLock(lockable
);
98 RWLockManager::TryGenericLock(bool write
, RWLockable
* lockable
)
100 return write
? TryWriteLock(lockable
) : TryReadLock(lockable
);
105 RWLockManager::GenericLockWithTimeout(bool write
, RWLockable
* lockable
,
109 ? WriteLockWithTimeout(lockable
, timeout
)
110 : ReadLockWithTimeout(lockable
, timeout
);
115 RWLockManager::GenericUnlock(bool write
, RWLockable
* lockable
)
118 WriteUnlock(lockable
);
120 ReadUnlock(lockable
);
124 } // namespace BPrivate
127 using BPrivate::RWLockable
;
128 using BPrivate::RWLockManager
;
131 #endif // _RW_LOCK_MANAGER_H