- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Lang/Mutex.cpp

    00001 // Mutex.cpp
    00002
    00003 #define PREFIX   "Mutex." << className << " : "
    00004 #define TRACE(x) { LOG4CPLUS_TRACE( logger, PREFIX << x ); }
    00005 #define DEBUG(x) { LOG4CPLUS_DEBUG( logger, PREFIX << x ); }
    00006 #define INFO(x)  { LOG4CPLUS_INFO ( logger, PREFIX << x ); }
    00007 #define ERROR(x) { LOG4CPLUS_ERROR( logger, std::endl << " *** " << PREFIX << x << " ***" << std::endl ); exit(9); }
    00008
    00009 #include "Mutex.h"
    00010
    00011 #include <errno.h>
    00012
    00013 using Mers::Utils::Lang::Mutex;
    00014 using Mers::Utils::Lang::String;
    00015 using log4cplus::Logger;
    00016
    00017 Mutex::Mutex( String _className, bool recursive ) :
    00018         className( _className ),
    00019         logger( Logger::getInstance( log4cplus::tstring( "Mers.Utils.Lang.Mutex." + _className ) ) )
    00020 {
    00021         InitializeMutex( recursive );
    00022 }
    00023
    00024 Mutex::~Mutex( void )
    00025 {
    00026         ReleaseMutex();
    00027 }
    00028
    00029 bool Mutex::InitializeMutex( bool recursive )
    00030 {
    00031   pthread_mutexattr_t *pAttr = new pthread_mutexattr_t;
    00032   int err = pthread_mutexattr_init( pAttr );
    00033   if( err ) {
    00034                 ERROR( "InitializeMutex() : Failed to initialize mutex attribute structure : " << strerror( err ) );
    00035                 return false;
    00036   }
    00037         if( recursive ) {
    00038           err = pthread_mutexattr_settype( pAttr, PTHREAD_MUTEX_RECURSIVE ); // recursively lock/unlock
    00039         }
    00040         else {
    00041           err = pthread_mutexattr_settype( pAttr, 0 );
    00042         }
    00043   if( err ) {
    00044                 ERROR( "InitializeMutex() : Failed to set mutex attribute structure : " << strerror( err ) );
    00045                 return false;
    00046   }
    00047   err = pthread_mutex_init( &mutex, pAttr );
    00048   if( err ) {
    00049                 ERROR( "InitializeMutex() : Failed to initialize mutex : " << strerror( err ) );
    00050                 return false;
    00051   }
    00052   return true;
    00053   err = pthread_mutexattr_destroy( pAttr );
    00054   if( err ) {
    00055                 ERROR( "InitializeMutex() : Failed to destroy mutex attribute structure : " << strerror( err ) );
    00056                 return false;
    00057   }
    00058         delete pAttr;
    00059   return true;
    00060 }
    00061
    00062 bool Mutex::ReleaseMutex( void )
    00063 {
    00064   int err = pthread_mutex_destroy( &mutex );
    00065   if( err ) {
    00066                 ERROR( "ReleaseMutex() : Failed to release mutex : " << strerror( err ) );
    00067                 return false;
    00068         }
    00069   return true;
    00070 }
    00071
    00072 bool Mutex::LockMutex( String tag )
    00073 {
    00074         if( tag != "" ) {
    00075                 tag += " : ";
    00076         }
    00077         TRACE( tag << "Waiting to lock mutex ... " );
    00078   int err = pthread_mutex_lock( &mutex );
    00079         if( err ) {
    00080                 ERROR( tag << "Failed to lock mutex : " << strerror( err ) );
    00081                 return false;
    00082   }
    00083         TRACE( "locked" );
    00084         return true;
    00085 }
    00086
    00087 bool Mutex::UnlockMutex( String tag )
    00088 {
    00089         if( tag != "" ) {
    00090                 tag += " : ";
    00091         }
    00092   int err = pthread_mutex_unlock( &mutex );
    00093   if( err ) {
    00094     ERROR( tag << "Failed to unlock mutex : " << strerror( err ) );
    00095                 return false;
    00096         }
    00097         TRACE( tag << "Unlocked mutex" );
    00098         return true;
    00099 }