From a17fcdb5b4171addd5cf920785ea176845bd2351 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Fri, 17 May 2013 00:40:54 +0000 Subject: [PATCH] bugfixes for thread shutdown --- source/glest_game/main/main.cpp | 3 +++ .../shared_lib/include/platform/sdl/thread.h | 1 + .../sources/platform/sdl/thread.cpp | 23 ++++++++++++++++--- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index ba000cc9..b525ea78 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -209,6 +209,7 @@ static void cleanupProcessObjects() { ircClient->disconnect(); ircClient->signalQuit(); ircClient = NULL; + sleep(0); /* ircClient->setCallbackObj(NULL); @@ -288,6 +289,8 @@ static void cleanupProcessObjects() { } if(SystemFlags::VERBOSE_MODE_ENABLED) printf("end running threads = " MG_SIZE_T_SPECIFIER "\n",Thread::getThreadList().size()); + Thread::shutdownThreads(); + std::map &crcPlayerTextureCache = CacheManager::getCachedItem< std::map >(GameConstants::playerTextureCacheLookupKey); //deleteMapValues(crcPlayerTextureCache.begin(),crcPlayerTextureCache.end()); crcPlayerTextureCache.clear(); diff --git a/source/shared_lib/include/platform/sdl/thread.h b/source/shared_lib/include/platform/sdl/thread.h index a9cebb20..91a1da3c 100644 --- a/source/shared_lib/include/platform/sdl/thread.h +++ b/source/shared_lib/include/platform/sdl/thread.h @@ -65,6 +65,7 @@ private: protected: void addThreadToList(); void removeThreadFromList(); + void queueAutoCleanThread(); public: Thread(); diff --git a/source/shared_lib/sources/platform/sdl/thread.cpp b/source/shared_lib/sources/platform/sdl/thread.cpp index f6cbc7fa..612666a7 100644 --- a/source/shared_lib/sources/platform/sdl/thread.cpp +++ b/source/shared_lib/sources/platform/sdl/thread.cpp @@ -52,6 +52,7 @@ public: removeThreadFromList(); } virtual ~ThreadAutoCleanup() { + //printf("In ~ThreadAutoCleanup Line: %d\n",__LINE__); } virtual void execute() { RunningStatusSafeWrapper runningStatus(this); @@ -110,16 +111,22 @@ void Thread::shutdownThreads() { } safeMutex.ReleaseLock(); + //printf("In Thread::shutdownThreads Line: %d\n",__LINE__); if(cleanupThread.get() != 0) { + //printf("In Thread::shutdownThreads Line: %d\n",__LINE__); sleep(0); cleanupThread->signalQuit(); + //printf("In Thread::shutdownThreads Line: %d\n",__LINE__); time_t elapsed = time(NULL); for(;cleanupThread->getRunningStatus() == true && difftime((long int)time(NULL),elapsed) <= 5;) { sleep(100); } + //printf("In Thread::shutdownThreads Line: %d\n",__LINE__); + //sleep(100); cleanupThread.reset(0); + //printf("In Thread::shutdownThreads Line: %d\n",__LINE__); } } @@ -176,14 +183,24 @@ int Thread::beginExecution(void* data) { //printf("In Thread::execute Line: %d thread = %p\n",__LINE__,thread); - if(thread->deleteAfterExecute == true) { + thread->queueAutoCleanThread(); + + return 0; +} + +void Thread::queueAutoCleanThread() { + if(this->deleteAfterExecute == true) { + //printf("In Thread::shutdownThreads Line: %d\n",__LINE__); + if(cleanupThread.get() == NULL) { + //printf("In Thread::shutdownThreads Line: %d\n",__LINE__); cleanupThread.reset(new ThreadAutoCleanup()); cleanupThread->start(); + sleep(0); } - cleanupThread->addThread(thread); + cleanupThread->addThread(this); + //printf("In Thread::shutdownThreads Line: %d\n",__LINE__); } - return 0; } void Thread::kill() {