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_mutex.h"
00024 #include "LOW_thread_mutex_POSIX.h"
00025 #include "LOW_helper_msglog.h"
00026
00027
00028
00029
00030
00031
00032
00033
00034 LOW_thread_mutex_POSIX::LOW_thread_mutex_POSIX( mutexKind_t inMutexType) : LOW_thread_mutex( inMutexType)
00035 {
00036 if ( int errVal=pthread_mutexattr_init( &mutexAttr) != 0 )
00037 throw thread_mutex_error( errVal, "pthread_mutexattr_init() failed", __FILE__, __LINE__);
00038
00039 switch ( inMutexType ) {
00040
00041 case mutexKind_fast:
00042 if ( int errVal=pthread_mutexattr_settype( &mutexAttr, PTHREAD_MUTEX_ERRORCHECK_NP) != 0 )
00043 throw thread_mutex_error( errVal, "pthread_mutexattr_setkind_np() failed", __FILE__, __LINE__);
00044 break;
00045
00046 case mutexKind_recursive:
00047 if ( int errVal=pthread_mutexattr_settype( &mutexAttr, PTHREAD_MUTEX_RECURSIVE_NP) != 0 )
00048 throw thread_mutex_error( errVal, "pthread_mutexattr_setkind_np() failed", __FILE__, __LINE__);
00049 break;
00050
00051 case mutexKind_errorCheck:
00052 if ( int errVal=pthread_mutexattr_settype( &mutexAttr, PTHREAD_MUTEX_ERRORCHECK_NP) != 0 )
00053 throw thread_mutex_error( errVal, "pthread_mutexattr_setkind_np() failed", __FILE__, __LINE__);
00054 break;
00055
00056 default:
00057 throw thread_mutex_error( "unknown value of mutexKind_t", __FILE__, __LINE__);
00058 }
00059
00060 if ( int errVal=pthread_mutex_init( &theMutex, &mutexAttr) != 0 )
00061 throw thread_mutex_error( errVal, "pthread_mutex_init() failed", __FILE__, __LINE__);
00062 }
00063
00064
00065 LOW_thread_mutex_POSIX::~LOW_thread_mutex_POSIX()
00066 {
00067 pthread_mutexattr_destroy( &mutexAttr);
00068
00069 int errVal = pthread_mutex_destroy( &theMutex);
00070 if ( errVal == EBUSY )
00071 LOW_helper_msglog::printPerror( errVal, "~LOW_thread_mutex_POSIX: pthread_mutex_destroy() failed");
00072 }
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 void LOW_thread_mutex_POSIX::lock()
00083 {
00084 int errVal = pthread_mutex_lock( &theMutex);
00085
00086 if ( errVal == EDEADLK )
00087 throw thread_mutex_locked( errVal, "mutex already locked", __FILE__, __LINE__);
00088 else if ( errVal != 0 )
00089 throw thread_mutex_error( errVal, "pthread_mutex_lock() failed", __FILE__, __LINE__);
00090 }
00091
00092
00093 void LOW_thread_mutex_POSIX::tryLock()
00094 {
00095 int errVal = pthread_mutex_trylock( &theMutex);
00096
00097 if ( errVal == EDEADLK )
00098 throw thread_mutex_locked( errVal, "mutex already locked", __FILE__, __LINE__);
00099 else if ( errVal == EBUSY )
00100 throw thread_mutex_busy( errVal, "mutex already locked by another thread", __FILE__, __LINE__);
00101 else if ( errVal != 0 )
00102 throw thread_mutex_error( errVal, "pthread_mutex_lock() failed", __FILE__, __LINE__);
00103 }
00104
00105
00106 void LOW_thread_mutex_POSIX::unlock()
00107 {
00108 int errVal = pthread_mutex_unlock( &theMutex);
00109
00110 if ( errVal == EPERM )
00111 throw thread_mutex_busy( errVal, "mutex already locked by another thread", __FILE__, __LINE__);
00112 else if ( errVal != 0 )
00113 throw thread_mutex_error( errVal, "pthread_mutex_lock() failed", __FILE__, __LINE__);
00114 }