Go to the documentation of this file.00001 #ifndef __MUTEX_ABSTRACTION_CPP__
00002 #define __MUTEX_ABSTRACTION_CPP__
00003
00004 #include <stdio.h>
00005
00006 #include <system/mutex_abstraction.h>
00007 #include <utilities/exception.h>
00008 #include <utilities/nwindows.h>
00009 #include <utilities/testing_utilities.h>
00010
00011 #ifdef NIX_PLATFORM
00012 #include <pthread.h>
00013 #endif
00014
00015 #ifdef ENABLE_AUTOTESTING
00016 #include <system/thread_abstraction.h>
00017 #endif
00018
00019 namespace nitro
00020 {
00021 MutexAbstraction::MutexAbstraction( void )
00022 {
00023 try
00024 {
00025 Mutex = NULL;
00026 }
00027 catch( nitro::exception e )
00028 {
00029 throw( nitro::exception( std::string( "MutexAbstraction::MutexAbstraction( void )::" ) + e.what() , e.code() ) );
00030 }
00031 catch( ... )
00032 {
00033 throw( nitro::exception( std::string( "MutexAbstraction::MutexAbstraction( void )::An error occured" ) , 1 ) );
00034 }
00035 }
00036
00037 MutexAbstraction::~MutexAbstraction()
00038 {
00039 try
00040 {
00041 Release();
00042 }
00043 catch( ... )
00044 {
00045 }
00046 }
00047
00048 void MutexAbstraction::Release( void )
00049 {
00050 try
00051 {
00052 if( Mutex )
00053 {
00054 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00055 CloseHandle( * ( ( HANDLE * )Mutex ) );
00056
00057 delete ( HANDLE * )Mutex;
00058 #endif
00059
00060 #ifdef NIX_PLATFORM
00061 pthread_mutex_destroy( ( pthread_mutex_t * )Mutex );
00062
00063 delete ( pthread_mutex_t * )Mutex;
00064 #endif
00065 Mutex = NULL;
00066 }
00067 }
00068 catch( nitro::exception e )
00069 {
00070 throw( nitro::exception( std::string( "MutexAbstraction::Release( void )::" ) + e.what() , e.code() ) );
00071 }
00072 catch( ... )
00073 {
00074 throw( nitro::exception( std::string( "MutexAbstraction::Release( void )::An error occured" ) , 1 ) );
00075 }
00076 }
00077
00078 void MutexAbstraction::CreateMutex( void )
00079 {
00080 try
00081 {
00082 Release();
00083
00084 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00085 Mutex = ( void * )new HANDLE;
00086
00087 char MutexName[ 1024 ];
00088 memset( MutexName , 0 , 1024 );
00089 #if defined( WIN32_PLATFORM )
00090 sprintf( MutexName , "%I64d" , ( __int64 )this );
00091 #endif
00092
00093 #if defined( MINGW_PLATFORM )
00094 sprintf( MutexName , "%ld" , ( __int32 )this );
00095 #endif
00096 * ( ( HANDLE * )Mutex ) = ::CreateMutexA( NULL , false , MutexName );
00097 if( * ( ( HANDLE * )Mutex ) == NULL )
00098 {
00099 throw( nitro::exception( "Mutex was not initialized" , 1 ) );
00100 }
00101 #endif
00102
00103 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00104 Mutex = ( void * )new pthread_mutex_t;
00105
00106 if( pthread_mutex_init( ( pthread_mutex_t * )Mutex , NULL ) != 0 )
00107 {
00108 throw( nitro::exception( "Mutex was not initialized" , 1 ) );
00109 }
00110 #endif
00111 }
00112 catch( nitro::exception e )
00113 {
00114 throw( nitro::exception( std::string( "MutexAbstraction::CreateMutex( void )::" ) + e.what() , e.code() ) );
00115 }
00116 catch( ... )
00117 {
00118 throw( nitro::exception( std::string( "MutexAbstraction::CreateMutex( void )::An error occured" ) , 1 ) );
00119 }
00120 }
00121
00122 void MutexAbstraction::Lock( void )
00123 {
00124 try
00125 {
00126 if( Mutex )
00127 {
00128 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00129 WaitForSingleObject( * ( ( HANDLE * )Mutex ) , INFINITE );
00130 #endif
00131
00132 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00133 std::cout<<"9"<<std::endl;
00134 pthread_mutex_lock( ( pthread_mutex_t * )Mutex );
00135 std::cout<<"10"<<std::endl;
00136 #endif
00137 }
00138 else
00139 {
00140 std::cout<<"8"<<std::endl;
00141 throw( nitro::exception( "Mutex was not initialized" , 1 ) );
00142 }
00143 }
00144 catch( nitro::exception e )
00145 {
00146 throw( nitro::exception( std::string( "MutexAbstraction::Lock( void )::" ) + e.what() , e.code() ) );
00147 }
00148 catch( ... )
00149 {
00150 throw( nitro::exception( std::string( "MutexAbstraction::Lock( void )::An error occured" ) , 1 ) );
00151 }
00152 }
00153
00154 void MutexAbstraction::UnLock( void )
00155 {
00156 try
00157 {
00158 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM )
00159 ReleaseMutex( * ( ( HANDLE * )Mutex ) );
00160 #endif
00161
00162 #if defined( NIX_PLATFORM ) || defined( CYGWIN_PLATFORM )
00163 pthread_mutex_unlock( ( pthread_mutex_t * )Mutex );
00164 #endif
00165 }
00166 catch( nitro::exception e )
00167 {
00168 throw( nitro::exception( std::string( "MutexAbstraction::UnLock( void )::" ) + e.what() , e.code() ) );
00169 }
00170 catch( ... )
00171 {
00172 throw( nitro::exception( std::string( "MutexAbstraction::UnLock( void )::An error occured" ) , 1 ) );
00173 }
00174 }
00175
00176 MutexAbstraction::MutexAbstraction( const MutexAbstraction & m )
00177 {
00178 }
00179
00180 MutexAbstraction MutexAbstraction::operator=( const MutexAbstraction & m )
00181 {
00182 return( *this );
00183 }
00184
00185
00186
00187
00188 BEGIN_TESTING_SECTION()
00189
00190 ENABLE_CLASS_TESTING( MutexAbstraction )
00191
00192 #ifdef ENABLE_AUTOTESTING
00193
00194 int Result( 0 );
00195
00196 void * tfunc( void * )
00197 {
00198 try
00199 {
00200 std::cout<<"6"<<std::endl;
00201 MutexAbstractionGetter( "default" ).Lock();
00202 std::cout<<"7"<<std::endl;
00203 Result = 1;
00204
00205 return( NULL );
00206 }
00207 catch( ... )
00208 {
00209 std::cout<<"An exception was caught"<<std::endl;
00210 }
00211
00212 return( NULL );
00213 }
00214
00215 NITRO_EXPORTING int tstGetResult( void )
00216 {
00217 return( Result );
00218 }
00219
00220 NITRO_EXPORTING void CreateBlockedThread()
00221 {
00222 try
00223 {
00224 std::cout<<"1"<<std::endl;
00225 MutexAbstractionGetter( "default" ).Lock();
00226 std::cout<<"2"<<std::endl;
00227 ThreadAbstraction t;
00228 std::cout<<"3"<<std::endl;
00229 t.CreateThread( tfunc , NULL );
00230 std::cout<<"4"<<std::endl;
00231 }
00232 catch( nitro::exception e )
00233 {
00234 std::cout<<"5"<<std::endl;
00235 std::cout<<e.what()<<std::endl;
00236 }
00237 catch( ... )
00238 {
00239 std::cout<<"241"<<std::endl;
00240 }
00241 }
00242
00243 #endif
00244
00245 CLASS_MEMBER_FUNCTION_TESTING_0( MutexAbstraction , CreateMutex , tstCreateMutex , void , NO_RET )
00246 CLASS_MEMBER_FUNCTION_TESTING_0( MutexAbstraction , Release , tstRelease , void , NO_RET )
00247 CLASS_MEMBER_FUNCTION_TESTING_0( MutexAbstraction , Lock , tstLock , void , NO_RET )
00248 CLASS_MEMBER_FUNCTION_TESTING_0( MutexAbstraction , UnLock , tstUnLock , void , NO_RET )
00249
00250 END_TESTING_SECTION()
00251 }
00252
00253 #endif