Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/nto/bakJuly/Mutex.cpp

Go to the documentation of this file.
00001 #ifndef __J2K__PThread_Mutex_CPP__
00002 #define __J2K__PThread_Mutex_CPP__
00003 
00004 
00005 #include <j2k/nto/Mutex.hpp>
00006 
00007 // Uses:
00008 // pthread_mutex_t
00009 // pthread_mutex_attr_t
00010 
00011   // Default Constructor
00012   PThread_Mutex::PThread_Mutex() 
00013   {
00014     // Fill structure with 0
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   // Virtual Destructor
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     // Make sure the mutex is Unlock... 
00037 
00038     // WARNING:  Make sure that Mutex destroy
00039     // ========  don't destroy attribute, 
00040     //           so that there is no double destroy on attr.
00041 
00042     err = pthread_mutexattr_destroy( &attr );
00043     MC_OnError( err,  Error, "An error happened while deleting a mutex attribute." )
00044   }
00045 
00046   // Lock the mutex
00047   void PThread_Mutex::lock()
00048   {
00049 //    if ( state == 1 ) return;
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   // Unlock the mutex
00063   void PThread_Mutex::unlock()
00064   {
00065 //    if ( state == 0 ) return;
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 

Generated on Sun Oct 14 18:46:23 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001