00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <errno.h>
00020 #include <pthread.h>
00021
00022
00023 #include "LOW_thread_rwlock.h"
00024 #include "LOW_thread_rwlock_POSIX.h"
00025 #include "LOW_helper_msglog.h"
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 LOW_thread_rwlock_POSIX::LOW_thread_rwlock_POSIX()
00036 {
00037 if ( int errVal=pthread_rwlock_init( &theRwlock, NULL) < 0 )
00038 throw thread_rwlock_error( errVal, "pthread_rwlock_init() failed", __FILE__, __LINE__);
00039 }
00040
00041
00042 LOW_thread_rwlock_POSIX::~LOW_thread_rwlock_POSIX()
00043 {
00044 int errVal = pthread_rwlock_destroy( &theRwlock);
00045 if ( errVal == EBUSY )
00046 LOW_helper_msglog::printPerror( errVal, "~LOW_thread_rwlock_POSIX: pthread_rwlock_destroy() failed");
00047 }
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 void LOW_thread_rwlock_POSIX::lockRead()
00058 {
00059 int errVal = pthread_rwlock_rdlock( &theRwlock);
00060
00061 if ( errVal == EDEADLK )
00062 throw thread_rwlock_locked( errVal, "current thread already owns rwlock for writing", __FILE__, __LINE__);
00063 else if ( errVal != 0 )
00064 throw thread_rwlock_error( errVal, "pthread_rwlock_rdlock() failed", __FILE__, __LINE__);
00065 }
00066
00067
00068 void LOW_thread_rwlock_POSIX::tryLockRead()
00069 {
00070 int errVal = pthread_rwlock_tryrdlock( &theRwlock);
00071
00072 if ( errVal == EDEADLK )
00073 throw thread_rwlock_locked( errVal, "current thread already owns rwlock for writing", __FILE__, __LINE__);
00074 else if ( errVal == EBUSY )
00075 throw thread_rwlock_busy( errVal, "already locked for writing by another thread", __FILE__, __LINE__);
00076 else if ( errVal != 0 )
00077 throw thread_rwlock_error( errVal, "pthread_rwlock_tryrdlock() failed", __FILE__, __LINE__);
00078 }
00079
00080
00081 void LOW_thread_rwlock_POSIX::lockWrite()
00082 {
00083 int errVal = pthread_rwlock_wrlock( &theRwlock);
00084
00085 if ( errVal == EDEADLK )
00086 throw thread_rwlock_locked( errVal, "current thread already owns rwlock for reading or writing", __FILE__, __LINE__);
00087 else if ( errVal != 0 )
00088 throw thread_rwlock_error( errVal, "pthread_rwlock_wrlock() failed", __FILE__, __LINE__);
00089 }
00090
00091
00092 void LOW_thread_rwlock_POSIX::tryLockWrite()
00093 {
00094 int errVal = pthread_rwlock_trywrlock( &theRwlock);
00095
00096 if ( errVal == EDEADLK )
00097 throw thread_rwlock_locked( errVal, "current thread already owns rwlock for reading or writing", __FILE__, __LINE__);
00098 else if ( errVal == EBUSY )
00099 throw thread_rwlock_busy( errVal, "already locked for reading or writing", __FILE__, __LINE__);
00100 else if ( errVal != 0 )
00101 throw thread_rwlock_error( errVal, "pthread_rwlock_trywrlock() failed", __FILE__, __LINE__);
00102 }
00103
00104
00105 void LOW_thread_rwlock_POSIX::unlock()
00106 {
00107 int errVal = pthread_rwlock_unlock( &theRwlock);
00108
00109 if ( errVal == EPERM )
00110 throw thread_rwlock_busy( errVal, "Calling thread does not hold a lock", __FILE__, __LINE__);
00111 else if ( errVal != 0 )
00112 throw thread_rwlock_error( errVal, "pthread_rwlock_unlock() failed", __FILE__, __LINE__);
00113 }