1 #ifndef CARMA_UTIL_PTHREAD_RW_LOCK_H
2 #define CARMA_UTIL_PTHREAD_RW_LOCK_H
7 #include "carma/util/posixErrors.h"
14 class PthreadRWLockAttr;
18 void exclusiveLock( ::pthread_rwlock_t & );
19 void exclusiveLock( PthreadRWLock & );
21 bool tryExclusiveLock( ::pthread_rwlock_t & );
22 bool tryExclusiveLock( PthreadRWLock & );
24 void exclusiveUnlock( ::pthread_rwlock_t & );
25 void exclusiveUnlock( PthreadRWLock & );
27 void sharedLock( ::pthread_rwlock_t & );
28 void sharedLock( PthreadRWLock & );
30 bool trySharedLock( ::pthread_rwlock_t & );
31 bool trySharedLock( PthreadRWLock & );
33 void sharedUnlock( ::pthread_rwlock_t & );
34 void sharedUnlock( PthreadRWLock & );
73 explicit PthreadRWLock( const ::pthread_rwlockattr_t & attr );
152 ::pthread_rwlock_t rwlock_;
161 carma::util::exclusiveLock( ::pthread_rwlock_t & l )
163 failIfPosixError( ::pthread_rwlock_wrlock( &l ),
164 "::pthread_rwlock_wrlock error" );
169 carma::util::tryExclusiveLock( ::pthread_rwlock_t & l )
173 const int posixRetVal = ::pthread_rwlock_trywrlock( &l );
175 switch ( posixRetVal ) {
185 throwPosixError( posixRetVal,
"::pthread_rwlock_trywrlock error" );
193 carma::util::exclusiveUnlock( ::pthread_rwlock_t & l )
195 failIfPosixError( ::pthread_rwlock_unlock( &l ),
196 "::pthread_rwlock_unlock error" );
201 carma::util::sharedLock( ::pthread_rwlock_t & l )
203 failIfPosixError( ::pthread_rwlock_rdlock( &l ),
204 "::pthread_rwlock_rdlock error" );
209 carma::util::trySharedLock( ::pthread_rwlock_t & l )
213 const int posixRetVal = ::pthread_rwlock_tryrdlock( &l );
215 switch ( posixRetVal ) {
225 throwPosixError( posixRetVal,
"::pthread_rwlock_tryrdlock error" );
233 carma::util::sharedUnlock( ::pthread_rwlock_t & l )
235 failIfPosixError( ::pthread_rwlock_unlock( &l ),
236 "::pthread_rwlock_unlock error" );
243 exclusiveLock( rwlock_ );
250 return tryExclusiveLock( rwlock_ );
257 exclusiveUnlock( rwlock_ );
264 sharedLock( rwlock_ );
271 return trySharedLock( rwlock_ );
278 sharedUnlock( rwlock_ );
282 inline const ::pthread_rwlock_t &
289 inline ::pthread_rwlock_t &
304 carma::util::tryExclusiveLock( PthreadRWLock & l )
306 return l.TryExclusiveLock();
311 carma::util::exclusiveUnlock( PthreadRWLock & l )
318 carma::util::sharedLock( PthreadRWLock & l )
325 carma::util::trySharedLock( PthreadRWLock & l )
327 return l.TrySharedLock();
332 carma::util::sharedUnlock( PthreadRWLock & l )
A simple wrapper class that makes use of ::pthread_rwlock_t easier in a C++ world.
void SharedLock()
Obtain an shared lock on the instance for the caller's thread.
void ExclusiveLock()
Obtain an exclusive lock on the instance for the caller's thread.
virtual ~PthreadRWLock()
Destruct a read/write lock.
void SharedUnlock()
Release a shared lock on the instance for the caller's thread.
bool TryExclusiveLock()
Try to obtain an exclusive lock on the instance for the caller's thread.
const ::pthread_rwlock_t & InternalPthreadRWLock() const
Obtain a reference to the internal ::pthread_rwlock_t.
bool TrySharedLock()
Try to obtain a shared lock on the instance for the caller's thread.
PthreadRWLock()
Construct a read/write lock with the CARMA defaults.
A simple wrapper class that makes use of ::pthread_rwlockattr_t easier in a C++ world.
void ExclusiveUnlock()
Release an exclusive lock on the instance for the caller's thread.