IBR-DTNSuite
0.8
|
00001 /* 00002 * RWMutex.cpp 00003 * 00004 * Created on: 27.03.2012 00005 * Author: morgenro 00006 */ 00007 00008 #include "ibrcommon/thread/RWMutex.h" 00009 #include <errno.h> 00010 00011 namespace ibrcommon 00012 { 00013 RWMutex::RWMutex() 00014 { 00015 pthread_rwlock_init(&_rwlock, NULL); 00016 } 00017 00018 RWMutex::~RWMutex() 00019 { 00020 pthread_rwlock_destroy(&_rwlock); 00021 } 00022 00023 void RWMutex::trylock(LockState state) throw (MutexException) 00024 { 00025 int ret = 0; 00026 00027 switch (state) { 00028 case LOCK_READONLY: 00029 ret = pthread_rwlock_tryrdlock(&_rwlock); 00030 break; 00031 00032 case LOCK_READWRITE: 00033 ret = pthread_rwlock_trywrlock(&_rwlock); 00034 break; 00035 } 00036 00037 switch (ret) 00038 { 00039 case 0: 00040 break; 00041 00042 case EBUSY: 00043 throw MutexException("The mutex could not be acquired because it was already locked."); 00044 break; 00045 } 00046 } 00047 00048 void RWMutex::enter(LockState state) throw (MutexException) 00049 { 00050 switch (state) { 00051 case LOCK_READONLY: 00052 pthread_rwlock_rdlock(&_rwlock); 00053 break; 00054 00055 case LOCK_READWRITE: 00056 pthread_rwlock_wrlock(&_rwlock); 00057 break; 00058 } 00059 } 00060 00061 void RWMutex::leave() throw (MutexException) 00062 { 00063 pthread_rwlock_unlock(&_rwlock); 00064 } 00065 } /* namespace ibrcommon */