00001 #ifndef __J2K__PThread_Mutex_CPP__
00002 #define __J2K__PThread_Mutex_CPP__
00003
00004
00005 #include <j2k/nto/Mutex.hpp>
00006
00007
00008
00009
00010
00011
00012 PThread_Mutex::PThread_Mutex()
00013 {
00014
00015 memset( &attr, 0, sizeof( pthread_mutexattr_t ) );
00016 memset( &mutex, 0, sizeof( pthread_mutex_t ) );
00017
00018 int err = pthread_mutexattr_init( &attr );
00019 MC_OnError( err, Error, "An unknown error happened while initializing a mutex attribute." )
00020
00021 err = pthread_mutex_init( &mutex, &attr );
00022 MC_OnError( err, Error, "An error happened while initializing a mutex." )
00023
00024 state = 0;
00025 }
00026
00027
00028 PThread_Mutex::~PThread_Mutex()
00029 {
00030 while( state > 0 ) { printf("DELETE" ); fflush( stdout ); exit( 1 ); }
00031
00032 int err = pthread_mutex_destroy( &mutex );
00033 MC_OnError( err, Error, "An error happened while deleting a mutex." )
00034
00035
00036
00037
00038
00039
00040
00041
00042 err = pthread_mutexattr_destroy( &attr );
00043 MC_OnError( err, Error, "An error happened while deleting a mutex attribute." )
00044 }
00045
00046
00047 void PThread_Mutex::lock()
00048 {
00049
00050
00051 if ( pthread_self() != owner ) {
00052 sched_yield();
00053 }
00054
00055 state = 1;
00056 register int err = pthread_mutex_lock( &mutex );
00057 MC_OnError( err, Error, "An error happened while locking a mutex." )
00058
00059 owner = pthread_self();
00060 }
00061
00062
00063 void PThread_Mutex::unlock()
00064 {
00065
00066
00067 if ( pthread_self() != owner ) {
00068 sched_yield();
00069 return;
00070 }
00071 register int err = pthread_mutex_unlock( &mutex );
00072
00073 if ( err == EPERM ) {
00074 printf( "Unlock. Permission denied. Not owner." );
00075 } else {
00076 MC_OnError( err, Error, "An error happened while unlocking a mutex." )
00077 }
00078 state = 0;
00079 }
00080
00081 #endif
00082