From e379905942a8f18da4b0c627484704aa86fbb35f Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 1 May 2010 09:10:52 +0000 Subject: [PATCH] Added mutex guards around logging --- mk/windoze/Glest.suo | Bin 235008 -> 238080 bytes source/glest_game/main/main.cpp | 28 ++++++++++-------- source/glest_game/main/program.cpp | 18 ++++++----- .../include/platform/common/base_thread.h | 2 +- source/shared_lib/include/util/util.h | 8 ++++- .../sources/platform/common/base_thread.cpp | 5 ++-- source/shared_lib/sources/util/util.cpp | 25 ++++++++++++---- 7 files changed, 56 insertions(+), 30 deletions(-) diff --git a/mk/windoze/Glest.suo b/mk/windoze/Glest.suo index e16002a82ea8262b49975eba563d0cb1dbefdd50..a7338754bdda14489b2feee2fde3238d630703ce 100755 GIT binary patch delta 6518 zcma)A3s_XwwLWX_0fzVR5D^i_5fLB2fW#o-F`y>k0|C+aAo36iDk>m8VuB2YB;FVm z*(u95jTx+;{hAs}9XrRSiFY)qr9L7txxKkfW5G}xBPv>BT5}_J?GgK>;f%fe`~I18 z_TFdhz1Mp$>!ASa`vD!*x`3`b`b_UDr-3Wf8pIaeX^(H{$qR$LV3Kg^b@t63XLaKJ zK(na#Iq#`38uW1u+UtGKb59tc7I1%dePF(r;bZoAf#%#5{|KC|_opX00WEu3tmn_^ zt+7pPG30uK=f8{?=q=J)+%@Ei_jHM(K4_P3nF2t))B^Fd1$-n-!V~#yxZM(b%#$tg zu-^^e3M}FW_AL>o;=ISvv;smfW;O7TXh}{-IQR49;2Za@4@gRSg7+Xr+28v6MIvCQ z0iRjSgWH-wZ)dsp^blx=!nkk|nplYmFEO8}YGs8|rmR*9y~PlaFRV9R`KN2K&)E z2N%ZVFk#*39A2%gRLYg76}gA?NJjam7Ngk-IbLbXI=LaNt%N6qwcigjrg4+K zd9%aUW4V&56pKAoA)@<>c)PW^dAz7>%odK-v(N@SQbZo|6VWT1hu70d>Re~67(A6P z@#sf*;%#UWZyuW5`fZdw>hL8H!G()N{96~W=Ep2q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2ZAu#?&gg`K=pg~!)lJLF`+6@HD) zNiSyL)7)<-Xg#NncM)T~k=l=Afo!jhl}mOdsW&|$3*?&*#8T`6;euMZ===k+y~IL~ z7K;niK2_L#C0rw97omk*%Y}uG3>TxQ-OlBz=QaB^iIMBCRw`>mJN^1K&OINl#%Y@ED$vy?SIXZo&DwccRSO3^}|Ab4cQ{^1mC zz#P@KSny{o@60iD_BH+}v|&~Zc{Yez9m(({w*hVCR=MG93`I4H8KF;H&(i1<@F0zt zfDSTk65q=O6H1z^MTDGg8fRkS2wmETK&SMWvj6AJ5SjH+vua)@b|F>O35(3;8a#HJ zNT8bK;t_IW3VYXMD6V1G>**Fsf%%(-ZY_Cai)d0%BSyv0v)e>goXKE7Zqk0bw2yi# zK<=1#k6h4qpjMK*!+G0JAVh#eb~w7Ls>j79V8u$PCQf9~#wT=>B`>;6u0ePMxgt5$ R@T_Tais?RH=eg>3{~MI$=~@5) diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index f202a39b0..cbd5b39e6 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 f2b242b2b..d4c18c400 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 fb0daa2bd..9e8c41cef 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 556302643..93d8e7067 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 15d93dc8a..82e2ae974 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 55e45bfe1..5bcd957f3 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 {