From 9150701e70fe37c0c36924d8eea28314831a8441 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sun, 2 Jan 2011 04:16:24 +0000 Subject: [PATCH] - more thread guards for IRC Client --- .../include/platform/posix/ircclient.h | 3 ++- .../sources/platform/posix/ircclient.cpp | 23 +++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/source/shared_lib/include/platform/posix/ircclient.h b/source/shared_lib/include/platform/posix/ircclient.h index ba977c88..2baccd6a 100644 --- a/source/shared_lib/include/platform/posix/ircclient.h +++ b/source/shared_lib/include/platform/posix/ircclient.h @@ -90,7 +90,8 @@ public: std::vector & getCachedNickList() { return eventData; } void setCachedNickList(std::vector &list) { eventData = list; } - IRCCallbackInterface * getCallbackObj(); + Mutex * getMutexIRCCB() { return &mutexIRCCB; } + IRCCallbackInterface * getCallbackObj(bool lockObj=true); void setCallbackObj(IRCCallbackInterface *cb); }; diff --git a/source/shared_lib/sources/platform/posix/ircclient.cpp b/source/shared_lib/sources/platform/posix/ircclient.cpp index 7392b6ff..14a6418d 100644 --- a/source/shared_lib/sources/platform/posix/ircclient.cpp +++ b/source/shared_lib/sources/platform/posix/ircclient.cpp @@ -191,8 +191,10 @@ void event_channel(irc_session_t * session, const char * event, const char * ori IRCThread *ctx = (IRCThread *)irc_get_ctx(session); if(ctx != NULL) { - if(ctx->getCallbackObj() != NULL) { - ctx->getCallbackObj()->IRC_CallbackEvent(IRC_evt_chatText, nickbuf, params, count); + MutexSafeWrapper safeMutex(ctx->getMutexIRCCB()); + IRCCallbackInterface *cb = ctx->getCallbackObj(false); + if(cb != NULL) { + cb->IRC_CallbackEvent(IRC_evt_chatText, nickbuf, params, count); } } @@ -347,7 +349,7 @@ void IRCThread::signalQuit() { if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC: signalQuit [%p]\n",ircSession); if(ircSession != NULL) { - callbackObj=NULL; + setCallbackObj(NULL); if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC: Quitting Channel\n"); irc_cmd_quit(ircSession, "MG Bot is closing!"); BaseThread::signalQuit(); @@ -411,8 +413,11 @@ std::vector IRCThread::getNickList() { return nickList; } -IRCCallbackInterface * IRCThread::getCallbackObj() { - MutexSafeWrapper safeMutex(&mutexIRCCB); +IRCCallbackInterface * IRCThread::getCallbackObj(bool lockObj) { + MutexSafeWrapper safeMutex(NULL); + if(lockObj == true) { + safeMutex.setMutex(&mutexIRCCB); + } return callbackObj; } void IRCThread::setCallbackObj(IRCCallbackInterface *cb) { @@ -518,9 +523,13 @@ void IRCThread::execute() { // Delete ourself when the thread is done (no other actions can happen after this // such as the mutex which modifies the running status of this method - if(getCallbackObj() != NULL) { - getCallbackObj()->IRC_CallbackEvent(IRC_evt_exitThread, NULL, NULL, 0); + MutexSafeWrapper safeMutex(&mutexIRCCB); + IRCCallbackInterface *cb = getCallbackObj(false); + if(cb != NULL) { + cb->IRC_CallbackEvent(IRC_evt_exitThread, NULL, NULL, 0); } + safeMutex.ReleaseLock(); + delete this; }