text: //  -*-C++-*- Emacs
  /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   * File         : Mutex.cpp
   * Author       : Stuart MacNeill
   * Date Created : Friday June 10th 2005
   * Contents     : 
   ******************************************************************************
   * (C) COPYRIGHT AGILENT TECHNOLOGIES COMPANY 2005. ALL RIGHTS RESERVED.
   * NO PART OF THIS PROGRAM MAY BE PHOTOCOPIED REPRODUCED OR TRANSLATED
   * TO ANOTHER PROGRAM LANGUAGE WITHOUT THE PRIOR WRITTEN CONSENT OF
   * AGILENT TECHNOLOGIES COMPANY.
   *****************************************************************************
   * $Log: Mutex.cpp $
   * Revision 1 2011/03/09 07:56:56 +0800 bru55687 /MassTransit/MT1.00.100_Tip/DemoIntegration/martin
   *  Imported from FWmeb A.04.00
   * 
   */
  
  #include <string>
  #include <errno.h>
  #include "Mutex.h"
  
  using namespace std;
  using namespace Tsd;
  
  Mutex::Mutex( bool tRecursive )
  {
     pthread_mutexattr_t tAttr;
     
     pthread_mutexattr_init( &tAttr );
     
     // make it recursive if required
     if ( tRecursive )
     {
        pthread_mutexattr_settype( &tAttr, 
                                   PTHREAD_MUTEX_RECURSIVE );
     }
     
     // create the mutex
     pthread_mutex_init( &mMutex, &tAttr );
     
     // destroy the attr, we don't need it anymore
     pthread_mutexattr_destroy( &tAttr );
  }
  
  
  Mutex::~Mutex()
  {
     pthread_mutex_destroy( &mMutex );
  }
  
  
  void Mutex::acquire()
  {
     int tRet = pthread_mutex_lock( &mMutex );
     if ( tRet != 0 )
     {
        string tExp( "Error: Mutex::acquire failed to lock mutex" );
        throw( tExp );
     }
  }
  
  
  bool Mutex::tryAcquire()
  {
     int tRet = pthread_mutex_trylock( &mMutex );
     return ( (tRet != EBUSY) && 
              (tRet != EINVAL) );
  }
  
  
  void Mutex::release()
  {
     int tRet = pthread_mutex_unlock( &mMutex );
     if ( tRet != 0 )
     {
        string tExp( "Error: Mutex::release failed to unlock mutex" );
        throw( tExp );
     }
  }
  
  
  pthread_mutex_t *Mutex::getMutex()
  {
     return &mMutex;
  }
  
  