diff --git a/src/profiler.cpp b/src/profiler.cpp index fba9d559..263b07bd 100644 --- a/src/profiler.cpp +++ b/src/profiler.cpp @@ -198,6 +198,8 @@ profilerFuncMap::profilerFuncMap(void) { //printf("profilerFuncMap Constructor: %p\n", this); pMgr.addThreadProfiler(this); + + _map_it = _map.begin(); } //------------------------------------------------------------------------- profilerFuncMap::~profilerFuncMap(void) @@ -205,11 +207,15 @@ profilerFuncMap::~profilerFuncMap(void) //printf("profilerFuncMap Destructor: %p\n", this); pMgr.removeThreadProfiler(this); - for (auto it = _map.begin(); it != _map.end(); it++) { - delete it->second; + autoScopedLock aLock(_mapMtx); + + for (auto it = _map.begin(); it != _map.end(); it++) + { + delete it->second; + } + _map.clear(); } - _map.clear(); } //------------------------------------------------------------------------- void profilerFuncMap::pushStack(funcProfileRecord *rec) @@ -228,6 +234,7 @@ funcProfileRecord *profilerFuncMap::findRecord(const char *fileNameStringLiteral const char *commentStringLiteral, bool create) { + autoScopedLock aLock(_mapMtx); char lineString[64]; funcProfileRecord *rec = nullptr; @@ -255,8 +262,45 @@ funcProfileRecord *profilerFuncMap::findRecord(const char *fileNameStringLiteral return rec; } //------------------------------------------------------------------------- +funcProfileRecord *profilerFuncMap::iterateBegin(void) +{ + autoScopedLock aLock(_mapMtx); + funcProfileRecord *rec = nullptr; + + _map_it = _map.begin(); + + if (_map_it != _map.end()) + { + rec = _map_it->second; + } + return rec; +} +//------------------------------------------------------------------------- +funcProfileRecord *profilerFuncMap::iterateNext(void) +{ + autoScopedLock aLock(_mapMtx); + funcProfileRecord *rec = nullptr; + + if (_map_it != _map.end()) + { + _map_it++; + } + if (_map_it != _map.end()) + { + rec = _map_it->second; + } + return rec; +} +//------------------------------------------------------------------------- //----- profilerManager class //------------------------------------------------------------------------- +profilerManager* profilerManager::instance = nullptr; + +profilerManager* profilerManager::getInstance(void) +{ + return instance; +} +//------------------------------------------------------------------------- profilerManager::profilerManager(void) { calibrateTSC(); @@ -266,6 +310,11 @@ profilerManager::profilerManager(void) { pLog = stdout; } + + if (instance == nullptr) + { + instance = this; + } } profilerManager::~profilerManager(void) @@ -280,6 +329,10 @@ profilerManager::~profilerManager(void) { fclose(pLog); pLog = nullptr; } + if (instance == this) + { + instance = nullptr; + } } int profilerManager::addThreadProfiler( profilerFuncMap *m ) diff --git a/src/profiler.h b/src/profiler.h index 50dca879..ceef4b15 100644 --- a/src/profiler.h +++ b/src/profiler.h @@ -273,10 +273,15 @@ namespace FCEU const char *commentStringLiteral, bool create = false); + funcProfileRecord *iterateBegin(void); + funcProfileRecord *iterateNext(void); + void pushStack(funcProfileRecord *rec); void popStack(funcProfileRecord *rec); private: + mutex _mapMtx; std::map _map; + std::map::iterator _map_it; std::vector stack; }; @@ -291,10 +296,13 @@ namespace FCEU int removeThreadProfiler( profilerFuncMap *m, bool shouldDestroy = false ); static FILE *pLog; + + static profilerManager *getInstance(); private: mutex threadListMtx; std::list threadList; + static profilerManager *instance; }; }