diff --git a/source/shared_lib/include/platform/sdl/thread.h b/source/shared_lib/include/platform/sdl/thread.h index 86ed3b648..5924aa9f9 100644 --- a/source/shared_lib/include/platform/sdl/thread.h +++ b/source/shared_lib/include/platform/sdl/thread.h @@ -143,6 +143,15 @@ public: SDL_LockMutex(mutex); refCount++; } + // Returns return 0, SDL_MUTEX_TIMEDOUT, or -1 on error; + // call SDL_GetError() for more information. + inline int TryLock(int millisecondsToWait=0) { + int result = SDL_TryLockMutex(mutex); + if(result == 0) { + refCount++; + } + return result; + } inline void v() { refCount--; SDL_UnlockMutex(mutex); @@ -180,6 +189,14 @@ public: } Lock(); } + inline int setMutexAndTryLock(Mutex *mutex,string ownerId="") { + this->mutex = mutex; + if(this->ownerId != ownerId) { + this->ownerId = ownerId; + } + return this->mutex->TryLock(); + } + inline bool isValidMutex() const { return(this->mutex != NULL); } @@ -213,6 +230,39 @@ public: #endif } } + + inline int TryLock(int millisecondsToWait=0) { + if(this->mutex != NULL) { + #ifdef DEBUG_MUTEXES + if(this->ownerId != "") { + printf("TryLocking Mutex [%s] refCount: %d\n",this->ownerId.c_str(),this->mutex->getRefCount()); + } + #endif + +#ifdef DEBUG_PERFORMANCE_MUTEXES + chrono.start(); +#endif + + int result = this->mutex->TryLock(millisecondsToWait); + if(result == 0 && this->mutex != NULL) { + this->mutex->setOwnerId(ownerId); + } + +#ifdef DEBUG_PERFORMANCE_MUTEXES + if(chrono.getMillis() > 5) printf("In [%s::%s Line: %d] MUTEX LOCK took msecs: %lld, this->mutex->getRefCount() = %d ownerId [%s]\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis(),this->mutex->getRefCount(),ownerId.c_str()); + chrono.start(); +#endif + + #ifdef DEBUG_MUTEXES + if(this->ownerId != "") { + printf("Locked Mutex [%s] refCount: %d\n",this->ownerId.c_str(),this->mutex->getRefCount()); + } + #endif + + return result; + } + } + inline void ReleaseLock(bool keepMutex=false,bool deleteMutexOnRelease=false) { if(this->mutex != NULL) { #ifdef DEBUG_MUTEXES diff --git a/source/shared_lib/sources/platform/posix/ircclient.cpp b/source/shared_lib/sources/platform/posix/ircclient.cpp index dbd1a983d..095bee948 100644 --- a/source/shared_lib/sources/platform/posix/ircclient.cpp +++ b/source/shared_lib/sources/platform/posix/ircclient.cpp @@ -545,7 +545,9 @@ void IRCThread::disconnect() { if(SystemFlags::VERBOSE_MODE_ENABLED || IRCThread::debugEnabled) printf ("===> IRC: Quitting Channel\n"); MutexSafeWrapper safeMutex1(&mutexIRCSession,string(__FILE__) + "_" + intToStr(__LINE__)); - irc_disconnect(ircSession); + if(ircSession != NULL) { + irc_disconnect(ircSession); + } safeMutex1.ReleaseLock(); BaseThread::signalQuit(); @@ -570,7 +572,9 @@ void IRCThread::signalQuit() { if(SystemFlags::VERBOSE_MODE_ENABLED || IRCThread::debugEnabled) printf ("===> IRC: Quitting Channel\n"); MutexSafeWrapper safeMutex1(&mutexIRCSession,string(__FILE__) + "_" + intToStr(__LINE__)); - irc_cmd_quit(ircSession, "MG Bot is closing!"); + if(ircSession != NULL) { + irc_cmd_quit(ircSession, "MG Bot is closing!"); + } safeMutex1.ReleaseLock(); hasJoinedChannel = false; } @@ -598,7 +602,10 @@ void IRCThread::SendIRCCmdMessage(string target, string msg) { #if !defined(DISABLE_IRCCLIENT) MutexSafeWrapper safeMutex1(&mutexIRCSession,string(__FILE__) + "_" + intToStr(__LINE__)); - int ret = irc_cmd_msg (ircSession, target.c_str(), msg.c_str()); + int ret = 0; + if(ircSession != NULL) { + ret = irc_cmd_msg (ircSession, target.c_str(), msg.c_str()); + } safeMutex1.ReleaseLock(); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] sending IRC command to [%s] cmd [%s] ret = %d\n",__FILE__,__FUNCTION__,__LINE__,target.c_str(),msg.c_str(),ret); @@ -667,20 +674,28 @@ bool IRCThread::isConnected(bool mutexLockRequired) { bool ret = false; if(this->getQuitStatus() == false) { MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); + int lockStatus = 0; if(mutexLockRequired == true) { - safeMutex.setMutex(&mutexIRCSession); + lockStatus = safeMutex.setMutexAndTryLock(&mutexIRCSession); + } + bool validSession = (lockStatus == SDL_MUTEX_TIMEDOUT || (lockStatus == 0 && ircSession != NULL)); + if(mutexLockRequired == true && lockStatus == 0) { + safeMutex.ReleaseLock(); } - bool validSession = (ircSession != NULL); - safeMutex.ReleaseLock(); if(validSession == true) { #if !defined(DISABLE_IRCCLIENT) MutexSafeWrapper safeMutex1(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); - if(mutexLockRequired == true) { - safeMutex1.setMutex(&mutexIRCSession); + if(ircSession != NULL) { + lockStatus = 0; + if(mutexLockRequired == true) { + lockStatus = safeMutex1.setMutexAndTryLock(&mutexIRCSession); + } + ret = (lockStatus == SDL_MUTEX_TIMEDOUT || (lockStatus == 0 && irc_is_connected(ircSession) != 0)); + } + if(mutexLockRequired == true && lockStatus == 0) { + safeMutex1.ReleaseLock(); } - ret = (irc_is_connected(ircSession) != 0); - safeMutex1.ReleaseLock(); } #endif }