- try to fix issue #104

This commit is contained in:
SoftCoder
2016-01-04 17:32:34 -08:00
parent 01eb7b70d6
commit e21b2506d6
2 changed files with 75 additions and 10 deletions

View File

@@ -143,6 +143,15 @@ public:
SDL_LockMutex(mutex); SDL_LockMutex(mutex);
refCount++; 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() { inline void v() {
refCount--; refCount--;
SDL_UnlockMutex(mutex); SDL_UnlockMutex(mutex);
@@ -180,6 +189,14 @@ public:
} }
Lock(); 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 { inline bool isValidMutex() const {
return(this->mutex != NULL); return(this->mutex != NULL);
} }
@@ -213,6 +230,39 @@ public:
#endif #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) { inline void ReleaseLock(bool keepMutex=false,bool deleteMutexOnRelease=false) {
if(this->mutex != NULL) { if(this->mutex != NULL) {
#ifdef DEBUG_MUTEXES #ifdef DEBUG_MUTEXES

View File

@@ -545,7 +545,9 @@ void IRCThread::disconnect() {
if(SystemFlags::VERBOSE_MODE_ENABLED || IRCThread::debugEnabled) printf ("===> IRC: Quitting Channel\n"); if(SystemFlags::VERBOSE_MODE_ENABLED || IRCThread::debugEnabled) printf ("===> IRC: Quitting Channel\n");
MutexSafeWrapper safeMutex1(&mutexIRCSession,string(__FILE__) + "_" + intToStr(__LINE__)); MutexSafeWrapper safeMutex1(&mutexIRCSession,string(__FILE__) + "_" + intToStr(__LINE__));
if(ircSession != NULL) {
irc_disconnect(ircSession); irc_disconnect(ircSession);
}
safeMutex1.ReleaseLock(); safeMutex1.ReleaseLock();
BaseThread::signalQuit(); BaseThread::signalQuit();
@@ -570,7 +572,9 @@ void IRCThread::signalQuit() {
if(SystemFlags::VERBOSE_MODE_ENABLED || IRCThread::debugEnabled) printf ("===> IRC: Quitting Channel\n"); if(SystemFlags::VERBOSE_MODE_ENABLED || IRCThread::debugEnabled) printf ("===> IRC: Quitting Channel\n");
MutexSafeWrapper safeMutex1(&mutexIRCSession,string(__FILE__) + "_" + intToStr(__LINE__)); MutexSafeWrapper safeMutex1(&mutexIRCSession,string(__FILE__) + "_" + intToStr(__LINE__));
if(ircSession != NULL) {
irc_cmd_quit(ircSession, "MG Bot is closing!"); irc_cmd_quit(ircSession, "MG Bot is closing!");
}
safeMutex1.ReleaseLock(); safeMutex1.ReleaseLock();
hasJoinedChannel = false; hasJoinedChannel = false;
} }
@@ -598,7 +602,10 @@ void IRCThread::SendIRCCmdMessage(string target, string msg) {
#if !defined(DISABLE_IRCCLIENT) #if !defined(DISABLE_IRCCLIENT)
MutexSafeWrapper safeMutex1(&mutexIRCSession,string(__FILE__) + "_" + intToStr(__LINE__)); 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(); 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); 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,21 +674,29 @@ bool IRCThread::isConnected(bool mutexLockRequired) {
bool ret = false; bool ret = false;
if(this->getQuitStatus() == false) { if(this->getQuitStatus() == false) {
MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__));
int lockStatus = 0;
if(mutexLockRequired == true) { if(mutexLockRequired == true) {
safeMutex.setMutex(&mutexIRCSession); lockStatus = safeMutex.setMutexAndTryLock(&mutexIRCSession);
} }
bool validSession = (ircSession != NULL); bool validSession = (lockStatus == SDL_MUTEX_TIMEDOUT || (lockStatus == 0 && ircSession != NULL));
if(mutexLockRequired == true && lockStatus == 0) {
safeMutex.ReleaseLock(); safeMutex.ReleaseLock();
}
if(validSession == true) { if(validSession == true) {
#if !defined(DISABLE_IRCCLIENT) #if !defined(DISABLE_IRCCLIENT)
MutexSafeWrapper safeMutex1(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); MutexSafeWrapper safeMutex1(NULL,string(__FILE__) + "_" + intToStr(__LINE__));
if(ircSession != NULL) {
lockStatus = 0;
if(mutexLockRequired == true) { if(mutexLockRequired == true) {
safeMutex1.setMutex(&mutexIRCSession); lockStatus = safeMutex1.setMutexAndTryLock(&mutexIRCSession);
} }
ret = (irc_is_connected(ircSession) != 0); ret = (lockStatus == SDL_MUTEX_TIMEDOUT || (lockStatus == 0 && irc_is_connected(ircSession) != 0));
}
if(mutexLockRequired == true && lockStatus == 0) {
safeMutex1.ReleaseLock(); safeMutex1.ReleaseLock();
} }
}
#endif #endif
} }