Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

mutex.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 #include "mutex.h"
00003 #include <string.h>
00004 #include <logger.h>
00005 
00006 #include <stdio.h>
00007 
00008 //---------------------------------------------------------------------------
00009 
00010 #pragma package(smart_init)
00011 //=============================================================================
00012 TMutex::TMutex( const MUTEX_NAME p_name )
00013 {
00014    if ( p_name == NULL )
00015    {
00016       throw worm_exception("TMutex(): no mutex name supplied");
00017    }
00018    strcpy( Name, p_name );
00019 
00020 #if defined(_WINNT) || defined(_Windows) //-------------------------------------------
00021 
00022    if ( (MutexHandle = CreateMutex( NULL, FALSE, Name )) == NULL )
00023    {
00024       worm_exception _expt( "TMutex::TMutex(): Error creating mutex handle <" );
00025                      _expt += Name;
00026                      _expt += ">";
00027       throw _expt;
00028    }
00029 
00030 #elif defined(_SOLARIS) //-------------------------------------------
00031 
00032 /*
00033    // Some of the code needed if this was to be a truly
00034    // inter-process mutex
00035 
00036    // kludge together an id from the character values of the Name
00037    //
00038    //
00039    key_t _memkey = 2091; // fako base value, hopefully above system ids
00040 
00041    for ( int _c = 0, _csz = strlen(p_name) ; _c < _csz ; _c++ )
00042    {
00043       _memkey += ( (2 ^ _c) * p_name[_c] );
00044    }
00045 
00046    if ( (ShMemoryId = shmget( _memkey, sizeof(mutex_t), 0 )) == -1 )
00047    {
00048       // memory region does not yet exist, this is the owner
00049    }
00050 */
00051 
00052    void * _dummy   = NULL;
00053    int    _retcode;
00054 
00055    if ( (_retcode = mutex_init( &MutexHandle, USYNC_THREAD, _dummy )) != 0 )
00056    {
00057       worm_exception _expt( "TMutex(): Error " );
00058                      _expt += (int)_retcode;
00059                      _expt += " returned from mutex_init()";
00060       throw _expt;
00061    }
00062 
00063 #else            //-------------------------------------------
00064 #error TMutex::TMutex(): Not yet implemented for this O/S
00065 #endif           //-------------------------------------------
00066 }
00067 //=============================================================================
00068 TMutex::~TMutex()
00069 {
00070 #if defined(_WINNT) || defined(_Windows) //-------------------------------------------
00071    CloseHandle( MutexHandle );
00072 #elif defined(_SOLARIS) //-------------------------------------------
00073    int   _retcode;
00074    if ( (_retcode = mutex_destroy( &MutexHandle )) != 0 )
00075    {
00076       fprintf( stderr,
00077              , "~TMutex(): Error from mutex_destroy: %d\n",
00078              , _retcode
00079              );
00080    }
00081 #else            //-------------------------------------------
00082 #error TMutex::~TMutex(): Not yet implemented for this O/S
00083 #endif           //-------------------------------------------
00084 }
00085 //=============================================================================
00086 void TMutex::RequestLock()
00087 {
00088 #if defined(_WINNT) || defined(_Windows) //-------------------------------------------
00089    if ( WaitForSingleObject( MutexHandle, INFINITE ) == WAIT_FAILED ) // waits forever
00090    {
00091       worm_exception _expt( "TMutex()::RequestLock(" );
00092                      _expt += Name;
00093                      _expt += "): WaitForSingleObject() failed";
00094       throw _expt;
00095    }
00096 #elif defined(_SOLARIS) //-------------------------------------------
00097    int   _retcode;
00098 
00099    if ( (_retcode = mutex_lock( &MutexHandle )) != 0 )
00100    {
00101       worm_exception _expt( "TMutex()::RequestLock() Error " );
00102                      _expt += (int)_retcode;
00103                      _expt += " returned from mutex_lock()";
00104       throw _expt;
00105    }
00106 #else            //-------------------------------------------
00107 #error TMutex::RequestLock(): Not yet implemented for this O/S
00108 #endif           //-------------------------------------------
00109 }
00110 //=============================================================================
00111 void TMutex::ReleaseLock()
00112 {
00113 #if defined(_WINNT) || defined(_Windows) //-------------------------------------------
00114    if ( ReleaseMutex( MutexHandle ) == 0 )
00115    {
00116       worm_exception _expt( "TMutex()::ReleaseLock() Error releasing mutex <" );
00117                      _expt += Name;
00118                      _expt += ">";
00119       throw _expt;
00120    }
00121 #elif defined(_SOLARIS) //-------------------------------------------
00122    int   _retcode;
00123 
00124    if ( (_retcode = mutex_unlock( &MutexHandle )) != 0 )
00125    {
00126       worm_exception _expt( "TMutex()::ReleaseLock() Error " );
00127                      _expt += (int)_retcode;
00128                      _expt += " returned from mutex_unlock()";
00129       throw _expt;
00130    }
00131 #else            //-------------------------------------------
00132 #error TMutex::ReleaseLock(): Not yet implemented for this O/S
00133 #endif           //-------------------------------------------
00134 }
00135 //=============================================================================
00136 

Generated on Tue May 6 09:16:06 2003 for Earthworm Libs by doxygen1.3-rc3