diff --git a/mk/windoze/Glest.suo b/mk/windoze/Glest.suo index e16002a8..a7338754 100755 Binary files a/mk/windoze/Glest.suo and b/mk/windoze/Glest.suo differ diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index f202a39b..cbd5b39e 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -30,6 +30,7 @@ #include "ImageReaders.h" #include "renderer.h" #include "simple_threads.h" +#include #include "leak_dumper.h" @@ -264,6 +265,11 @@ void MainWindow::setProgram(Program *program) { SystemFlags debugger; int glestMain(int argc, char** argv){ + +#ifdef SL_LEAK_DUMP + AllocRegistry memoryLeaks = AllocRegistry::getInstance(); +#endif + #ifdef STREFLOP_H streflop_init(); printf("%s, STREFLOP enabled.\n",getNetworkVersionString().c_str()); @@ -280,9 +286,8 @@ int glestMain(int argc, char** argv){ ExceptionHandler exceptionHandler; exceptionHandler.install( getCrashDumpFileName() ); - FileCRCPreCacheThread preCacheThread; - try{ + std::auto_ptr preCacheThread; Config &config = Config::getInstance(); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -344,8 +349,9 @@ int glestMain(int argc, char** argv){ if(config.getBool("AllowGameDataSynchCheck","false") == true) { vector techDataPaths = config.getPathListForType(ptTechs); - preCacheThread.setTechDataPaths(techDataPaths); - preCacheThread.start(); + preCacheThread.reset(new FileCRCPreCacheThread()); + preCacheThread->setTechDataPaths(techDataPaths); + preCacheThread->start(); } // test @@ -357,35 +363,31 @@ int glestMain(int argc, char** argv){ while(Window::handleEvent()){ program->loop(); } + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } catch(const exception &e){ - preCacheThread.shutdownAndWait(); - //exceptionMessage(e); ExceptionHandler::handleRuntimeError(e.what()); } catch(const char *e){ - //exceptionMessage(e); - preCacheThread.shutdownAndWait(); ExceptionHandler::handleRuntimeError(e); } catch(const string &ex){ - //exceptionMessage(e); - preCacheThread.shutdownAndWait(); ExceptionHandler::handleRuntimeError(ex.c_str()); } catch(...){ - //exceptionMessage(e); - preCacheThread.shutdownAndWait(); ExceptionHandler::handleRuntimeError("Unknown error!"); } - preCacheThread.shutdownAndWait(); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //SoundRenderer &soundRenderer= SoundRenderer::getInstance(); //soundRenderer.stopAllSounds(); delete mainWindow; + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //SystemFlags::Close(); return 0; diff --git a/source/glest_game/main/program.cpp b/source/glest_game/main/program.cpp index f2b242b2..d4c18c40 100644 --- a/source/glest_game/main/program.cpp +++ b/source/glest_game/main/program.cpp @@ -120,7 +120,6 @@ void Program::ShowMessageProgramState::update() { Program::Program() { programState= NULL; singleton = this; - soundThreadManager = NULL; } @@ -165,7 +164,7 @@ Program::~Program(){ singleton = NULL; BaseThread::shutdownAndWait(soundThreadManager); - delete soundThreadManager; + delete soundThreadManager; soundThreadManager = NULL; } @@ -205,7 +204,9 @@ void Program::loop(){ //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); - //!!!SoundRenderer::getInstance().update(); + if(soundThreadManager == NULL) { + SoundRenderer::getInstance().update(); + } //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -363,10 +364,13 @@ void Program::init(WindowGl *window, bool initSound, bool toggleFullScreen){ SoundRenderer &soundRenderer= SoundRenderer::getInstance(); soundRenderer.init(window); - BaseThread::shutdownAndWait(soundThreadManager); - delete soundThreadManager; - soundThreadManager = new SimpleTaskThread(&SoundRenderer::getInstance(),0,50); - soundThreadManager->start(); + // Run sound streaming in a background thread if enabled + if(config.getBool("ThreadedSoundStream","true") == true) { + BaseThread::shutdownAndWait(soundThreadManager); + delete soundThreadManager; + soundThreadManager = new SimpleTaskThread(&SoundRenderer::getInstance(),0,50); + soundThreadManager->start(); + } } NetworkInterface::setAllowGameDataSynchCheck(Config::getInstance().getBool("AllowGameDataSynchCheck","0")); diff --git a/source/shared_lib/include/platform/common/base_thread.h b/source/shared_lib/include/platform/common/base_thread.h index fb0daa2b..9e8c41ce 100644 --- a/source/shared_lib/include/platform/common/base_thread.h +++ b/source/shared_lib/include/platform/common/base_thread.h @@ -42,7 +42,7 @@ public: void signalQuit(); bool getQuitStatus(); bool getRunningStatus(); - static void shutdownAndWait(BaseThread *pThread); + static void shutdownAndWait(BaseThread *ppThread); void shutdownAndWait(); }; diff --git a/source/shared_lib/include/util/util.h b/source/shared_lib/include/util/util.h index 55630264..93d8e706 100644 --- a/source/shared_lib/include/util/util.h +++ b/source/shared_lib/include/util/util.h @@ -15,8 +15,10 @@ #include #include #include +#include "thread.h" using std::string; +using namespace Shared::Platform; namespace Shared{ namespace Util{ @@ -35,7 +37,7 @@ public: { protected: DebugType debugType; - + public: SystemFlagsType() { this->debugType = debugSystem; @@ -43,6 +45,7 @@ public: this->fileStream = NULL; this->debugLogFileName = ""; this->fileStreamOwner = false; + this->mutex = NULL; } SystemFlagsType(DebugType debugType) { this->debugType = debugType; @@ -50,6 +53,7 @@ public: this->fileStream = NULL; this->debugLogFileName = ""; this->fileStreamOwner = false; + this->mutex = NULL; } SystemFlagsType(DebugType debugType,bool enabled, std::ofstream *fileStream,std::string debugLogFileName) { @@ -58,12 +62,14 @@ public: this->fileStream = fileStream; this->debugLogFileName = debugLogFileName; this->fileStreamOwner = false; + this->mutex = mutex; } bool enabled; std::ofstream *fileStream; std::string debugLogFileName; bool fileStreamOwner; + Mutex *mutex; }; protected: diff --git a/source/shared_lib/sources/platform/common/base_thread.cpp b/source/shared_lib/sources/platform/common/base_thread.cpp index 15d93dc8..82e2ae97 100644 --- a/source/shared_lib/sources/platform/common/base_thread.cpp +++ b/source/shared_lib/sources/platform/common/base_thread.cpp @@ -19,8 +19,7 @@ using namespace Shared::Util; namespace Shared { namespace PlatformCommon { -BaseThread::BaseThread() { - +BaseThread::BaseThread() : Thread() { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); setQuitStatus(false); @@ -88,7 +87,7 @@ void BaseThread::setRunningStatus(bool value) { } void BaseThread::shutdownAndWait(BaseThread *pThread) { - if(pThread != NULL) { + if(pThread != NULL && pThread->getRunningStatus() == true) { pThread->signalQuit(); for( time_t elapsed = time(NULL); difftime(time(NULL),elapsed) <= 10; ) { if(pThread->getRunningStatus() == false) { diff --git a/source/shared_lib/sources/util/util.cpp b/source/shared_lib/sources/util/util.cpp index 55e45bfe..5bcd957f 100644 --- a/source/shared_lib/sources/util/util.cpp +++ b/source/shared_lib/sources/util/util.cpp @@ -72,20 +72,23 @@ SystemFlags::~SystemFlags() { } void SystemFlags::Close() { - printf("Closing logfile\n"); + printf("START Closing logfiles\n"); for(std::map::iterator iterMap = SystemFlags::debugLogFileList.begin(); iterMap != SystemFlags::debugLogFileList.end(); iterMap++) { SystemFlags::SystemFlagsType ¤tDebugLog = iterMap->second; - if( currentDebugLog.fileStream != NULL && - currentDebugLog.fileStream->is_open() == true) { - currentDebugLog.fileStream->close(); - } + if(currentDebugLog.fileStreamOwner == true) { + if( currentDebugLog.fileStream != NULL && + currentDebugLog.fileStream->is_open() == true) { + currentDebugLog.fileStream->close(); + } delete currentDebugLog.fileStream; + delete currentDebugLog.mutex; } currentDebugLog.fileStream = NULL; currentDebugLog.fileStreamOwner = false; + currentDebugLog.mutex = NULL; } if(SystemFlags::lockFile != -1) { @@ -101,6 +104,8 @@ void SystemFlags::Close() { SystemFlags::lockfilename = ""; } } + + printf("END Closing logfiles\n"); } void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) { @@ -138,6 +143,7 @@ void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) { currentDebugLog2.fileStream != NULL) { currentDebugLog.fileStream = currentDebugLog2.fileStream; currentDebugLog.fileStreamOwner = false; + currentDebugLog.mutex = currentDebugLog2.mutex; break; } } @@ -183,12 +189,17 @@ void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) { currentDebugLog.fileStream = new std::ofstream(); currentDebugLog.fileStream->open(debugLog.c_str(), ios_base::out | ios_base::trunc); currentDebugLog.fileStreamOwner = true; + currentDebugLog.mutex = new Mutex(); } printf("Opening logfile [%s] type = %d, currentDebugLog.fileStreamOwner = %d\n",debugLog.c_str(),type, currentDebugLog.fileStreamOwner); + currentDebugLog.mutex->p(); + (*currentDebugLog.fileStream) << "Starting Mega-Glest logging for type: " << type << "\n"; (*currentDebugLog.fileStream).flush(); + + currentDebugLog.mutex->v(); } //printf("Logfile is open [%s]\n",SystemFlags::debugLogFile); @@ -197,8 +208,12 @@ void SystemFlags::OutputDebug(DebugType type, const char *fmt, ...) { assert(currentDebugLog.fileStream != NULL); + currentDebugLog.mutex->p(); + (*currentDebugLog.fileStream) << "[" << szBuf2 << "] " << szBuf; (*currentDebugLog.fileStream).flush(); + + currentDebugLog.mutex->v(); } // output to console else {