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