vpr::MutexPosix Class Reference

Mutex wrapper for POSIX-compliant systems using pthreads mutex variables for the implementation. More...

#include <vpr/Sync/Mutex.h>

List of all members.

Public Member Functions

 MutexPosix ()
 Constructor for vpr::MutexPosix class.
 ~MutexPosix ()
 Destructor for vpr::MutexPosix class.
vpr::ReturnStatus acquire ()
 Locks this mutex.
vpr::ReturnStatus acquireRead ()
 Acquires a read lock on this mutex.
vpr::ReturnStatus acquireWrite ()
 Acquires a write lock on this mutex.
vpr::ReturnStatus tryAcquire ()
 Tries to acquire a lock on this mutex variable (does not block).
vpr::ReturnStatus tryAcquireRead ()
 Tries to acquire a read lock on this mutex (does not block).
vpr::ReturnStatus tryAcquireWrite ()
 Tries to acquire a write lock on this mutex (does not block).
vpr::ReturnStatus release ()
 Releases this mutex.
int test () const
 Tests the current lock status.

Protected Member Functions

void operator= (const MutexPosix &)
 MutexPosix (const MutexPosix &)

Protected Attributes

pthread_mutex_t mMutex
 Mutex variable for the class.

Friends

class CondVarPosix


Detailed Description

Mutex wrapper for POSIX-compliant systems using pthreads mutex variables for the implementation.

This is typedef'd to vpr::Mutex.

Definition at line 70 of file MutexPosix.h.


Constructor & Destructor Documentation

vpr::MutexPosix::MutexPosix (  ) 

Constructor for vpr::MutexPosix class.

Postcondition:
The mutex variable is initialized for use. It must be initialized before any other member functions can do anything with it.

Definition at line 49 of file MutexPosix.cpp.

References mMutex.

00050 {
00051    // Initialize the mutex.
00052 #ifndef _DEBUG
00053    pthread_mutex_init(&mMutex, NULL);
00054 #else
00055 #ifdef VPR_OS_Linux
00056    // If Linux and debug, then use error checking mutex
00057    pthread_mutexattr_t mutex_attr;
00058    pthread_mutexattr_init(&mutex_attr);
00059    pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK_NP);
00060    pthread_mutex_init(&mMutex, &mutex_attr);
00061    pthread_mutexattr_destroy(&mutex_attr);
00062 #else
00063    pthread_mutex_init(&mMutex, NULL);
00064 #endif
00065 #endif
00066 }

vpr::MutexPosix::~MutexPosix (  )  [inline]

Destructor for vpr::MutexPosix class.

Precondition:
The mutex variable should be unlocked before being destroyed, but if it is not, this routine will unlock it and then destroy it.
Postcondition:
The mutex variable is destroyed and unlocked if necessary.

Definition at line 90 of file MutexPosix.h.

References mMutex.

00091    {
00092       // Destroy the mutex.
00093       if ( pthread_mutex_destroy(&mMutex) == -1 )
00094       {
00095          pthread_mutex_unlock(&mMutex);
00096          pthread_mutex_destroy(&mMutex);
00097       }
00098    }

vpr::MutexPosix::MutexPosix ( const MutexPosix  )  [inline, protected]

Definition at line 277 of file MutexPosix.h.

00277 {;}


Member Function Documentation

vpr::ReturnStatus vpr::MutexPosix::acquire (  )  [inline]

Locks this mutex.

Postcondition:
A lock on the mutex variable is acquired by the caller. If a lock has already been acquired by another process/thread, the caller blocks until the mutex has been freed.
Returns:
vpr::ReturnStatus::Succeed is returned if the lock is acquired.

vpr::ReturnStatus::Fail is returned if an error occurred.

Definition at line 110 of file MutexPosix.h.

References vpr::ReturnStatus::Fail, and mMutex.

Referenced by vpr::SocketStreamImplSIM::accept(), vpr::CondVarPosix::acquire(), acquireRead(), acquireWrite(), vpr::SocketStreamImplSIM::addConnector(), vpr::sim::SocketManager::connect(), vpr::Debug::getStream(), vpr::ThreadPool::getThread(), vpr::sim::SocketManager::isListening(), vpr::sim::SocketManager::listen(), vpr::operator<<(), vpr::SocketImplSIM::read_i(), vpr::ThreadPool::threadLoop(), vpr::ThreadPool::threadSleep(), vpr::ThreadPool::wait(), and vpr::CondVarGeneric::wait().

00111    {
00112       int retval = pthread_mutex_lock(&mMutex);
00113 
00114       // Locking succeeded.
00115       if ( retval == 0 )
00116       {
00117          return vpr::ReturnStatus();
00118       }
00119 #ifdef _DEBUG
00120       // This thread tried to lock the mutex twice and a deadlock condition
00121       // was reported.
00122       else if ( retval == EDEADLK )
00123       {
00124          perror("Tried to lock mutex twice");
00125          assert(false && "Mutex deadlock detected");
00126          return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00127       }
00128 #endif
00129       // Some other error occurred.
00130       else
00131       {
00132          return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00133       }
00134    }

vpr::ReturnStatus vpr::MutexPosix::acquireRead (  )  [inline]

Acquires a read lock on this mutex.

Postcondition:
A lock on the mutex variable is acquired by the caller. If a lock has already been acquired by another process/thread, the caller blocks until the mutex has been freed.
Returns:
vpr::ReturnStatus::Succeed is returned if the lock is acquired.

vpr::ReturnStatus::Fail is returned if an error occurred.

Note:
No special read lock has been defined for now.

Definition at line 148 of file MutexPosix.h.

References acquire().

00149    {
00150       return this->acquire();
00151    }

vpr::ReturnStatus vpr::MutexPosix::acquireWrite (  )  [inline]

Acquires a write lock on this mutex.

Postcondition:
A lock on the mutex variable is acquired by the caller. If a lock has already been acquired by another process/thread, the caller blocks until the mutex has been freed.
Returns:
vpr::ReturnStatus::Succeed is returned if the lock is acquired.

vpr::ReturnStatus::Fail is returned if an error occurred.

Note:
No special write lock has been defined for now.

Definition at line 165 of file MutexPosix.h.

References acquire().

00166    {
00167       return this->acquire();
00168    }

vpr::ReturnStatus vpr::MutexPosix::tryAcquire (  )  [inline]

Tries to acquire a lock on this mutex variable (does not block).

Postcondition:
A lock on the mutex variable is acquired by the caller. If a lock has already been acquired by another process/thread, the caller returns does not wait for it to be unlocked.
Returns:
vpr::ReturnStatus::Succeed is returned if the lock is acquired.

vpr::ReturnStatus::Fail is returned if the mutex is already locked.

Definition at line 181 of file MutexPosix.h.

References vpr::ReturnStatus::Fail, and mMutex.

Referenced by vpr::CondVarPosix::tryAcquire(), tryAcquireRead(), and tryAcquireWrite().

00182    {
00183       if ( pthread_mutex_trylock(&mMutex) == 0 )
00184       {
00185          return vpr::ReturnStatus();
00186       }
00187       else
00188       {
00189          return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00190       }
00191    }

vpr::ReturnStatus vpr::MutexPosix::tryAcquireRead (  )  [inline]

Tries to acquire a read lock on this mutex (does not block).

Postcondition:
A lock on the mutex variable is acquired by the caller. If a lock has already been acquired by another process/thread, the caller returns does not wait for it to be unlocked.
Returns:
vpr::ReturnStatus::Succeed is returned if the read lock is acquired.

vpr::ReturnStatus::Fail is returned if the mutex is already locked.

Definition at line 205 of file MutexPosix.h.

References tryAcquire().

00206    {
00207       return this->tryAcquire();
00208    }

vpr::ReturnStatus vpr::MutexPosix::tryAcquireWrite (  )  [inline]

Tries to acquire a write lock on this mutex (does not block).

Postcondition:
A lock on the mutex variable is acquired by the caller. If a lock has already been acquired by another process/thread, the caller returns does not wait for it to be unlocked.
Returns:
vpr::ReturnStatus::Succeed is returned if the write lock is acquired.

vpr::ReturnStatus::Fail is returned if the mutex is already locked.

Definition at line 222 of file MutexPosix.h.

References tryAcquire().

00223    {
00224       return this->tryAcquire();
00225    }

vpr::ReturnStatus vpr::MutexPosix::release (  )  [inline]

Releases this mutex.

Precondition:
The mutex variable must be locked.
Postcondition:
The mutex variable is unlocked.
Returns:
vpr::ReturnStatus::Succeed is returned if the lock is released successfully.

vpr::ReturnStatus::Fail is returned otherwise.

Definition at line 237 of file MutexPosix.h.

References vpr::ReturnStatus::Fail, and mMutex.

Referenced by vpr::SocketStreamImplSIM::accept(), vpr::SocketStreamImplSIM::addConnector(), vpr::sim::SocketManager::connect(), vpr::ThreadPool::getThread(), vpr::sim::SocketManager::isListening(), vpr::sim::SocketManager::listen(), vpr::operator<<(), vpr::SocketImplSIM::read_i(), vpr::CondVarPosix::release(), vpr::CondVarPosix::setMutex(), vpr::CondVarGeneric::setMutex(), vpr::ThreadPool::threadLoop(), vpr::ThreadPool::ThreadPool(), vpr::ThreadPool::threadSleep(), vpr::ThreadPool::wait(), and vpr::CondVarGeneric::wait().

00238    {
00239       int retval = pthread_mutex_unlock(&mMutex);
00240 
00241       if (0 == retval )
00242       {
00243          return vpr::ReturnStatus();
00244       }
00245       else if(EPERM == retval)
00246       {
00247          perror("Tried to release a mutex we do not own");
00248          assert(false && "Mutex release by non-owning thread.");
00249          return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00250       }
00251       else
00252       {
00253          return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00254       }
00255    }

int vpr::MutexPosix::test (  )  const

Tests the current lock status.

Postcondition:
The state of the mutex variable is returned.
Returns:
0 is returned if this mutex is not currently locked.

1 is returned if this mutex is locked.

Definition at line 69 of file MutexPosix.cpp.

References mMutex.

Referenced by vpr::ThreadManager::addThread(), vpr::CondVarPosix::broadcast(), vpr::ThreadManager::removeThread(), vpr::CondVarPosix::test(), vpr::CondVarGeneric::test(), vpr::CondVarPosix::wait(), and vpr::CondVarGeneric::wait().

00070 {
00071    int ret_val;
00072 
00073    ret_val = pthread_mutex_trylock(const_cast<pthread_mutex_t*>(&mMutex));
00074 
00075    // If the return value from pthread_mutex_trylock() is 0, then this
00076    // process now has a lock on mutex.  Therefore, no other process could
00077    // have held a lock on it, so unlock the mutex and return 0.
00078    if ( ret_val == 0 )
00079    {
00080       pthread_mutex_unlock(const_cast<pthread_mutex_t*>(&mMutex));
00081       ret_val = 0;
00082    }
00083    // The mutex is currently locked by some thread if ret_val is non-zero.
00084    else
00085    {
00086       ret_val = 1;
00087    }
00088 
00089    return ret_val;
00090 }

void vpr::MutexPosix::operator= ( const MutexPosix  )  [inline, protected]

Definition at line 276 of file MutexPosix.h.

00276 {;}


Friends And Related Function Documentation

friend class CondVarPosix [friend]

Definition at line 270 of file MutexPosix.h.


Member Data Documentation

pthread_mutex_t vpr::MutexPosix::mMutex [protected]

Mutex variable for the class.

Definition at line 273 of file MutexPosix.h.

Referenced by acquire(), MutexPosix(), release(), test(), tryAcquire(), vpr::CondVarPosix::wait(), and ~MutexPosix().


The documentation for this class was generated from the following files:
Generated on Thu Jan 4 10:55:36 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1