diff --git a/source/glest_game/network/connection_slot.cpp b/source/glest_game/network/connection_slot.cpp index 0e284ce7..c8f99080 100644 --- a/source/glest_game/network/connection_slot.cpp +++ b/source/glest_game/network/connection_slot.cpp @@ -56,9 +56,9 @@ void ConnectionSlotThread::signalUpdate(ConnectionSlotEvent *event) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); if(event != NULL) { - triggerIdMutex.p(); + MutexSafeWrapper safeMutex(&triggerIdMutex); this->event = event; - triggerIdMutex.v(); + safeMutex.ReleaseLock(); } semTaskSignalled.signal(); @@ -69,9 +69,9 @@ void ConnectionSlotThread::setTaskCompleted(ConnectionSlotEvent *event) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); if(event != NULL) { - triggerIdMutex.p(); + MutexSafeWrapper safeMutex(&triggerIdMutex); event->eventCompleted = true; - triggerIdMutex.v(); + safeMutex.ReleaseLock(); } SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); @@ -80,9 +80,9 @@ void ConnectionSlotThread::setTaskCompleted(ConnectionSlotEvent *event) { bool ConnectionSlotThread::isSignalCompleted() { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); - triggerIdMutex.p(); - bool result = this->event->eventCompleted; - triggerIdMutex.v(); + MutexSafeWrapper safeMutex(&triggerIdMutex); + bool result = (this->event != NULL ? this->event->eventCompleted : true); + safeMutex.ReleaseLock(); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); diff --git a/source/glest_game/network/server_interface.cpp b/source/glest_game/network/server_interface.cpp index eb5dc9dd..605e3972 100644 --- a/source/glest_game/network/server_interface.cpp +++ b/source/glest_game/network/server_interface.cpp @@ -645,8 +645,6 @@ bool ServerInterface::launchGame(const GameSettings* gameSettings){ SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); - //serverSynchAccessor.p(); - for(int i= 0; iisConnected() == false); - serverSynchAccessor.v(); if(isSlotOpen == true) { ++openSlotCount; } } - serverSynchAccessor.p(); + MutexSafeWrapper safeMutex(&serverSynchAccessor); serverSocket.listen(openSlotCount); - serverSynchAccessor.v(); + safeMutex.ReleaseLock(); } int ServerInterface::getOpenSlotCount() { int openSlotCount= 0; for(int i= 0; iisConnected() == false); - serverSynchAccessor.v(); if(isSlotOpen == true) { ++openSlotCount; diff --git a/source/shared_lib/include/platform/common/cache_manager.h b/source/shared_lib/include/platform/common/cache_manager.h index b91a8823..e5dce03e 100644 --- a/source/shared_lib/include/platform/common/cache_manager.h +++ b/source/shared_lib/include/platform/common/cache_manager.h @@ -44,25 +44,23 @@ protected: if(accessor == cacheItemSet) { if(value == NULL) { try { - mutexCache.p(); + MutexSafeWrapper safeMutex(&mutexCache); if(itemCache.find(cacheKey) != itemCache.end()) { itemCache.erase(cacheKey); } - mutexCache.v(); + safeMutex.ReleaseLock(); } catch(const std::exception &ex) { - mutexCache.v(); throw runtime_error(ex.what()); } } try { - mutexCache.p(); + MutexSafeWrapper safeMutex(&mutexCache); itemCache[cacheKey] = *value; - mutexCache.v(); + safeMutex.ReleaseLock(); } catch(const std::exception &ex) { - mutexCache.v(); throw runtime_error(ex.what()); } } diff --git a/source/shared_lib/include/platform/sdl/thread.h b/source/shared_lib/include/platform/sdl/thread.h index a4f31757..27eedd04 100644 --- a/source/shared_lib/include/platform/sdl/thread.h +++ b/source/shared_lib/include/platform/sdl/thread.h @@ -51,7 +51,7 @@ private: // class Mutex // ===================================================== -class Mutex{ +class Mutex { private: SDL_mutex* mutex; @@ -62,6 +62,28 @@ public: void v(); }; +class MutexSafeWrapper { +protected: + Mutex *mutex; +public: + + MutexSafeWrapper(Mutex *mutex) { + this->mutex = mutex; + if(this->mutex != NULL) { + this->mutex->p(); + } + } + ~MutexSafeWrapper() { + ReleaseLock(); + } + void ReleaseLock() { + if(this->mutex != NULL) { + this->mutex->v(); + this->mutex = NULL; + } + } +}; + // ===================================================== // class Semaphore // ===================================================== diff --git a/source/shared_lib/sources/platform/common/base_thread.cpp b/source/shared_lib/sources/platform/common/base_thread.cpp index 1b4616bf..4d9c92c4 100644 --- a/source/shared_lib/sources/platform/common/base_thread.cpp +++ b/source/shared_lib/sources/platform/common/base_thread.cpp @@ -45,9 +45,9 @@ void BaseThread::signalQuit() { void BaseThread::setQuitStatus(bool value) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - mutexQuit.p(); + MutexSafeWrapper safeMutex(&mutexQuit); quit = value; - mutexQuit.v(); + safeMutex.ReleaseLock(); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } @@ -56,9 +56,9 @@ bool BaseThread::getQuitStatus() { //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); bool retval = false; - mutexQuit.p(); + MutexSafeWrapper safeMutex(&mutexQuit); retval = quit; - mutexQuit.v(); + safeMutex.ReleaseLock(); //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -69,9 +69,9 @@ bool BaseThread::getRunningStatus() { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); bool retval = false; - mutexRunning.p(); + MutexSafeWrapper safeMutex(&mutexRunning); retval = running; - mutexRunning.v(); + safeMutex.ReleaseLock(); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] running = %d\n",__FILE__,__FUNCTION__,__LINE__,retval); @@ -81,9 +81,9 @@ bool BaseThread::getRunningStatus() { void BaseThread::setRunningStatus(bool value) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] value = %d\n",__FILE__,__FUNCTION__,__LINE__,value); - mutexRunning.p(); + MutexSafeWrapper safeMutex(&mutexRunning); running = value; - mutexRunning.v(); + safeMutex.ReleaseLock(); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] running = %d\n",__FILE__,__FUNCTION__,__LINE__,value); } diff --git a/source/shared_lib/sources/platform/common/simple_threads.cpp b/source/shared_lib/sources/platform/common/simple_threads.cpp index adadd2c7..730fe3ab 100644 --- a/source/shared_lib/sources/platform/common/simple_threads.cpp +++ b/source/shared_lib/sources/platform/common/simple_threads.cpp @@ -135,9 +135,9 @@ void SimpleTaskThread::execute() { void SimpleTaskThread::setTaskSignalled(bool value) { //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - mutexTaskSignaller.p(); + MutexSafeWrapper safeMutex(&mutexTaskSignaller); taskSignalled = value; - mutexTaskSignaller.v(); + safeMutex.ReleaseLock(); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } @@ -146,9 +146,9 @@ bool SimpleTaskThread::getTaskSignalled() { //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); bool retval = false; - mutexTaskSignaller.p(); + MutexSafeWrapper safeMutex(&mutexTaskSignaller); retval = taskSignalled; - mutexTaskSignaller.v(); + safeMutex.ReleaseLock(); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);