Protect profiler map with a mutex

This commit is contained in:
Chris Cannam
2021-03-11 16:14:40 +00:00
parent 7705c6409e
commit 09e5bd29bf

View File

@@ -23,6 +23,8 @@
#include "Profiler.h" #include "Profiler.h"
#include "system/Thread.h"
#include <algorithm> #include <algorithm>
#include <set> #include <set>
#include <string> #include <string>
@@ -45,9 +47,13 @@ Profiler::m_profiles;
Profiler::WorstCallMap Profiler::WorstCallMap
Profiler::m_worstCalls; Profiler::m_worstCalls;
static Mutex profileMutex;
void void
Profiler::add(const char *id, float ms) Profiler::add(const char *id, float ms)
{ {
profileMutex.lock();
ProfileMap::iterator pmi = m_profiles.find(id); ProfileMap::iterator pmi = m_profiles.find(id);
if (pmi != m_profiles.end()) { if (pmi != m_profiles.end()) {
++pmi->second.first; ++pmi->second.first;
@@ -62,6 +68,8 @@ Profiler::add(const char *id, float ms)
} else { } else {
m_worstCalls[id] = ms; m_worstCalls[id] = ms;
} }
profileMutex.unlock();
} }
void void
@@ -74,6 +82,8 @@ Profiler::dump()
std::string std::string
Profiler::getReport() Profiler::getReport()
{ {
profileMutex.lock();
static const int buflen = 256; static const int buflen = 256;
char buffer[buflen]; char buffer[buflen];
std::string report; std::string report;
@@ -167,6 +177,8 @@ Profiler::getReport()
report += buffer; report += buffer;
} }
profileMutex.unlock();
return report; return report;
} }