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

C:/temp/src/j2k/Posix/Mutex.cpp

Go to the documentation of this file.
00001 // Started by Harry
00002 // Finished by Fred
00003 
00004 #ifndef __J2K__PThread_Mutex_CPP__
00005 #define __J2K__PThread_Mutex_CPP__
00006 
00007 #include <j2k/445/Posix/Mutex.hpp>
00008 
00009 // Uses:
00010 // pthread_mutex_t
00011 // pthread_mutex_attr_t
00012 // pthread_cond_t
00013 // pthread_cond_attr_t
00014 
00015 /***********************************************
00016   Description of pthread_mutex_attr_t arguments.
00017 
00018   Attribute         Default Value
00019   =========         =============
00020   detachstate       PTHREAD_CREATE_JOINABLE
00021   schedpolicy       PTHREAD_INHERIT_SCHED
00022   schedparam        Inherited from parent thread
00023   contentionscope   PTHREAD_SCOPE_SYSTEM
00024   stacksize         4096
00025   stackaddr         NULL
00026 */
00027 
00028   // Default Constructor
00029   inline PThread_Mutex::PThread_Mutex() 
00030   {
00031     // Fill structure with 0
00032     memset( &attr,        0, sizeof( pthread_mutexattr_t ) );
00033     memset( &mutex,       0, sizeof( pthread_mutex_t     ) );
00034 //    memset( &cond,        0, sizeof( pthread_cond_t ) );
00035 //    memset( &cond_attr,   0, sizeof( pthread_condattr_t  ) );
00036 
00037     int err = pthread_mutexattr_init( &attr );
00038     MC_OnError( err, Error, "An unknown error happened while initializing a mutex attribute." )
00039 
00040     err = pthread_mutex_init( &mutex, &attr );
00041     MC_OnError( err,  Error, "An error happened while initializing a mutex." )
00042 
00043 //    err = pthread_condattr_init( &cond_attr );
00044 //    MC_OnError( err,  Error, "An error happened while initializing the condition variable attribute" )
00045 //
00046 //    err = pthread_cond_init( &cond, &cond_attr );
00047 //    MC_OnError( err,  Error, "An error happened while initializing the condition variable." )
00048   }
00049 
00050   // Copy Constructor
00051   inline PThread_Mutex::PThread_Mutex( const PThread_Mutex& src ) {
00052     memcpy( &attr,        &src.attr,        sizeof( pthread_mutexattr_t ) );
00053     memcpy( &mutex,       &src.mutex,       sizeof( pthread_mutex_t     ) );
00054 //    memcpy( &cond,        &src.cond,        sizeof( pthread_cond_t      ) );
00055 //    memcpy( &cond_attr,   &src.cond_attr,   sizeof( pthread_condattr_t  ) );
00056   }
00057 
00058   // Assign Operator
00059   inline const PThread_Mutex& PThread_Mutex::operator=( const PThread_Mutex& src ) 
00060   {
00061     memcpy( &attr,        &src.attr,        sizeof( pthread_mutexattr_t ) );
00062     memcpy( &mutex,       &src.mutex,       sizeof( pthread_mutex_t     ) );
00063 //    memcpy( &cond,        &src.cond,        sizeof( pthread_cond_t      ) );
00064 //    memcpy( &cond_attr,   &src.cond_attr,   sizeof( pthread_condattr_t  ) );
00065     return *this;
00066   }
00067 
00068   // Virtual Destructor
00069   inline PThread_Mutex::~PThread_Mutex() 
00070   {
00071     int err = pthread_mutex_destroy( &mutex );
00072     MC_OnError( err, Error, "An error happened while deleting a mutex." )
00073 
00074  
00075     // Make sure the mutex is Unlock... 
00076 
00077     // WARNING:  Make sure that Mutex destroy
00078     // ========  don't destroy attribute, 
00079     //           so that there is no double destroy on attr.
00080 
00081     err = pthread_mutexattr_destroy( &attr );
00082     MC_OnError( err,  Error, "An error happened while deleting a mutex attribute." )
00083 
00084 //  err = pthread_cond_destroy( &cond );
00085 //  MC_OnError( err,  Error, "An error happened while deleting a condition variable.")
00086 //
00087 //  err = pthread_condattr_destroy( &cond_attr );
00088 //  MC_OnError( err,  Error, "An error happened while deleting a condition variable attribute." )
00089   }
00090 
00091   // Lock the mutex
00092   inline void PThread_Mutex::lock()
00093   {
00094     int err = pthread_mutex_lock( &mutex );
00095     MC_OnError( err, Error, "An error happened while locking a mutex." )
00096   }
00097 
00098   // Unlock the mutex
00099   inline void PThread_Mutex::unlock()
00100   {
00101     int err = pthread_mutex_unlock( &mutex );
00102     MC_OnError( err, Error, "An error happened while unlocking a mutex." )
00103   }
00104 
00105   // Try to lock a mutex
00106   inline void PThread_Mutex::tryLock()
00107   {
00108     int err = pthread_mutex_trylock( &mutex );
00109     MC_OnError( err, Error, "An error happened while trying to lock a mutex." )
00110   }
00111 
00112   // Get the maximum priority (ceiling value) for the mutex
00113   inline int PThread_Mutex::getPriorityCeiling()
00114   {
00115     int ceiling = 0;
00116     int err = pthread_mutex_getprioceiling( &mutex, &ceiling );
00117     MC_OnError( err, Error, "An error happened while retrieving the priority ceiling of a mutex." )
00118 
00119     return ceiling;
00120   }
00121 
00122   // Set the maximum priority (ceiling value) for the mutex  
00123   inline int PThread_Mutex::setPriorityCeiling( int new_ceiling )
00124   {
00125     int old_ceiling = 0;
00126 
00127     int err = pthread_mutex_setprioceiling( &mutex, new_ceiling, &old_ceiling );
00128     MC_OnError( err, Error, "An error happened while setting the priority ceiling of a mutex." )
00129 
00130     return old_ceiling;
00131   }
00132 
00133   // Get the mutex attribute type
00134   inline int PThread_Mutex::getType()
00135   {
00136     int mtype = 0;
00137 
00138     int err = pthread_mutexattr_gettype( &attr, &mtype );
00139     MC_OnError( err, Error, "An error happened while getting the mutex type." )
00140 
00141     return mtype;
00142   }
00143 
00144   // Get the mutex attribute protocol
00145   inline int PThread_Mutex::getProtocol()
00146   {
00147     int err = pthread_mutexattr_getprotocol( &attr, &protocol );
00148     MC_OnError( err, Error, "while getting the mutex attribute protocol.")
00149   
00150     return protocol;
00151   }
00152 
00153 #if 0
00154   // Wait for a mutex
00155   inline void PThread_Mutex::wait() 
00156   {
00157 //    pthread_cond_wait( &cond, &mutex );
00158   }
00159 
00160   // Send a notify signal for other process waiting for the mutex
00161   inline void PThread_Mutex::notify() 
00162   {
00163     pthread_cond_signal( &cond );
00164   }
00165 
00166   // Send a notifyAll broadcast signal for other process waiting for the mutex
00167   inline void PThread_Mutex::notifyAll() 
00168   {
00169 //    pthread_cond_broadcast( &cond );
00170   }
00171 #endif
00172 
00173 #endif
00174 

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