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

PThread_Mutex Class Reference

#include <Mutex.hpp>

Inheritance diagram for PThread_Mutex::

RecursiveMutex List of all members.

Public Methods

 PThread_Mutex ()
virtual ~PThread_Mutex ()
void lock ()
void unlock ()
__inline void tryLock ()
__inline int getPriorityCeiling ()
__inline int setPriorityCeiling (int new_ceiling)
__inline int getType ()
__inline int getProtocol ()

Private Methods

__inline PThread_Mutex (const PThread_Mutex &src)
__inline const PThread_Mutex & operator= (const PThread_Mutex &src)

Private Attributes

int mutex_type
int protocol
int prioceiling
int state
pid_t owner
pthread_mutexattr_t attr
pthread_mutex_t mutex

Constructor & Destructor Documentation

PThread_Mutex::PThread_Mutex  
 

Definition at line 12 of file Mutex.cpp.

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   }

PThread_Mutex::~PThread_Mutex   [virtual]
 

Definition at line 28 of file Mutex.cpp.

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   }

PThread_Mutex::PThread_Mutex const PThread_Mutex &    src [inline, private]
 

Definition at line 9 of file Mutex.inl.

00010   {
00011     memcpy( &attr,        &src.attr,        sizeof( pthread_mutexattr_t ) );
00012     memcpy( &mutex,       &src.mutex,       sizeof( pthread_mutex_t     ) );
00013     state = 0;
00014   }


Member Function Documentation

int PThread_Mutex::getPriorityCeiling   [inline]
 

Definition at line 34 of file Mutex.inl.

00035   {
00036     int ceiling = 0;
00037     int err = pthread_mutex_getprioceiling( &mutex, &ceiling );
00038     MC_OnError( err, Error, "An error happened while retrieving the priority ceiling of a mutex." )
00039 
00040     return ceiling;
00041   }

int PThread_Mutex::getProtocol   [inline]
 

Definition at line 66 of file Mutex.inl.

00067   {
00068     int err = pthread_mutexattr_getprotocol( &attr, &protocol );
00069     MC_OnError( err, Error, "while getting the mutex attribute protocol.")
00070   
00071     return protocol;
00072   }

int PThread_Mutex::getType   [inline]
 

Definition at line 55 of file Mutex.inl.

00056   {
00057     int mtype = 0;
00058 
00059     int err = pthread_mutexattr_gettype( &attr, &mtype );
00060     MC_OnError( err, Error, "An error happened while getting the mutex type." )
00061 
00062     return mtype;
00063   }

void PThread_Mutex::lock  
 

Reimplemented in RecursiveMutex.

Definition at line 47 of file Mutex.cpp.

Referenced by Semaphore::P(), Semaphore::V(), Lock::get(), Semaphore::get(), RecursiveMutex::lock(), RWLock::read(), RWLock::releaseRead(), RWLock::releaseWrite(), Fred::run(), and RWLock::write().

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   }

const PThread_Mutex & PThread_Mutex::operator= const PThread_Mutex &    src [inline, private]
 

Definition at line 17 of file Mutex.inl.

00018   {
00019     memcpy( &attr,        &src.attr,        sizeof( pthread_mutexattr_t ) );
00020     memcpy( &mutex,       &src.mutex,       sizeof( pthread_mutex_t     ) );
00021     state = 0;
00022 
00023     return *this;
00024   }

int PThread_Mutex::setPriorityCeiling int    new_ceiling [inline]
 

Definition at line 44 of file Mutex.inl.

00045   {
00046     int old_ceiling = 0;
00047 
00048     int err = pthread_mutex_setprioceiling( &mutex, new_ceiling, &old_ceiling );
00049     MC_OnError( err, Error, "An error happened while setting the priority ceiling of a mutex." )
00050 
00051     return old_ceiling;
00052   }

void PThread_Mutex::tryLock   [inline]
 

Definition at line 27 of file Mutex.inl.

00028   {
00029     int err = pthread_mutex_trylock( &mutex );
00030     MC_OnError( err, Error, "An error happened while trying to lock a mutex." )
00031   }

void PThread_Mutex::unlock  
 

Reimplemented in RecursiveMutex.

Definition at line 63 of file Mutex.cpp.

Referenced by Semaphore::P(), Semaphore::V(), Semaphore::get(), if(), RWLock::read(), Lock::release(), RWLock::releaseRead(), RWLock::releaseWrite(), Fred::run(), and RWLock::write().

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   }


Member Data Documentation

pthread_mutexattr_t PThread_Mutex::attr [private]
 

Definition at line 64 of file Mutex.hpp.

Referenced by PThread_Mutex(), and operator=().

pthread_mutex_t PThread_Mutex::mutex [private]
 

Definition at line 65 of file Mutex.hpp.

Referenced by PThread_Mutex(), and operator=().

int PThread_Mutex::mutex_type [private]
 

Definition at line 58 of file Mutex.hpp.

pid_t PThread_Mutex::owner [private]
 

Reimplemented in RecursiveMutex.

Definition at line 62 of file Mutex.hpp.

int PThread_Mutex::prioceiling [private]
 

Definition at line 60 of file Mutex.hpp.

int PThread_Mutex::protocol [private]
 

Definition at line 59 of file Mutex.hpp.

int PThread_Mutex::state [private]
 

Definition at line 61 of file Mutex.hpp.


The documentation for this class was generated from the following files:
Generated on Sun Oct 14 18:49:32 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001