00001 #include "Platform.h" 00002 #include "Declarations.h" 00003 #include "LockMechanism.h" 00004 #include <assert.h> 00005 00006 /* 00007 CLockMechanism - SW (Nov 2006 to Mar 2007) 00008 00009 Implementation of a platform independent mutex object in order to synchronize multiple thread 00010 or multiple task access. 00011 00012 Used inside ResponseWaitQueue, PacketQueue, MessageBuffers 00013 */ 00014 00015 CLockMechanism::CLockMechanism() : m_hMutex(NULL) 00016 { 00017 m_hMutex = platformCreateMutex(); 00018 m_uiTimeoutMsec = 1000; // now: 1 second 00019 // on windows, this is infinitely repeated until success or clear error 00020 // (!= timeout) message; see platformWaitForMutex (windows version) 00021 00022 } 00023 00024 CLockMechanism::~CLockMechanism() 00025 { 00026 if (m_hMutex != NULL) platformDeleteMutex( m_hMutex ); 00027 } 00028 00029 00030 void CLockMechanism::setTimeoutMsec( UINT32 timeout ) 00031 { 00032 m_uiTimeoutMsec = timeout; 00033 } 00034 00035 UINT32 CLockMechanism::getTimeoutMsec() 00036 { 00037 return m_uiTimeoutMsec; 00038 } 00039 00042 bool CLockMechanism::lock () 00043 { 00044 if ( m_hMutex == NULL ) return false; 00045 00046 bool ret = platformWaitForMutex(m_hMutex, m_uiTimeoutMsec); 00047 00048 _DEBUG_(if (ret == false) printf("ERROR! LockMechanism::lock returned %d\n", ret)); 00049 assert( ret == true ); 00050 00051 return ret; 00052 } 00053 00054 void CLockMechanism::unlock () 00055 { 00056 if (m_hMutex != NULL) 00057 { 00058 bool ret = true; 00059 00060 ret = platformSignalMutex(m_hMutex); 00061 00062 _DEBUG_(if (ret == false) printf("ERROR! LockMechanism::unlock (signal mutex) returned %d\n", ret)); 00063 00064 assert( ret == true ); 00065 00066 } 00067 } 00068