Skip to content

Commit a598ee0

Browse files
committed
Adds a synchronization to the stack tracing methods.
1 parent b7a8955 commit a598ee0

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

src/xrCore/Threading/ScopeLock.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include "Common/Noncopyable.hpp"
4+
#include "Threading/Lock.hpp"
5+
#include "xrDebug.h"
6+
7+
class ScopeLock: Noncopyable
8+
{
9+
public:
10+
ScopeLock(Lock* SyncObject);
11+
~ScopeLock();
12+
13+
private:
14+
Lock * m_SyncObject;
15+
};
16+
17+
ScopeLock::ScopeLock(Lock* SyncObject): m_SyncObject(SyncObject)
18+
{
19+
VERIFY(m_SyncObject);
20+
21+
m_SyncObject->Enter();
22+
}
23+
24+
ScopeLock::~ScopeLock()
25+
{
26+
m_SyncObject->Leave();
27+
}

src/xrCore/xrCore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@
354354
<ClInclude Include="SubAlloc.hpp" />
355355
<ClInclude Include="Text\MbHelpers.h" />
356356
<ClInclude Include="Threading\Event.hpp" />
357+
<ClInclude Include="Threading\ScopeLock.h" />
357358
<ClInclude Include="Threading\ThreadPool.hpp" />
358359
<ClInclude Include="Threading\Lock.hpp" />
359360
<ClInclude Include="vector.h" />

src/xrCore/xrCore.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,9 @@
668668
<ClInclude Include="xrMemory.h">
669669
<Filter>Memory</Filter>
670670
</ClInclude>
671+
<ClInclude Include="Threading\ScopeLock.h">
672+
<Filter>Threading</Filter>
673+
</ClInclude>
671674
</ItemGroup>
672675
<ItemGroup>
673676
<ResourceCompile Include="xrCore.rc">

src/xrCore/xrDebug.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#include "xrDebug.h"
55
#include "os_clipboard.h"
66
#include "Debug/dxerr.h"
7-
#include "xrCore/Threading/Lock.hpp"
7+
#include "Threading/ScopeLock.h"
8+
89
#pragma warning(push)
910
#pragma warning(disable : 4091) // 'typedef ': ignored on left of '' when no variable is declared
1011
#include "Debug/MiniDump.h"
@@ -76,7 +77,9 @@ xrDebug::CrashHandler xrDebug::OnCrash = nullptr;
7677
xrDebug::DialogHandler xrDebug::OnDialog = nullptr;
7778
string_path xrDebug::BugReportFile;
7879
bool xrDebug::ErrorAfterDialog = false;
80+
7981
bool xrDebug::m_SymEngineInitialized = false;
82+
Lock xrDebug::m_DbgHelpLock;
8083

8184
void xrDebug::SetBugReportFile(const char* fileName) { strcpy_s(BugReportFile, fileName); }
8285

@@ -187,6 +190,8 @@ void xrDebug::DeinitializeSymbolEngine(void)
187190

188191
xr_vector<xr_string> xrDebug::BuildStackTrace(PCONTEXT threadCtx, u16 maxFramesCount)
189192
{
193+
ScopeLock Lock(&m_DbgHelpLock);
194+
190195
SStringVec traceResult;
191196
STACKFRAME stackFrame = { 0 };
192197
xr_string frameStr;

src/xrCore/xrDebug.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "xrCore/_types.h"
33
#include "xrCommon/xr_string.h"
44
#include "xrCommon/xr_vector.h"
5+
#include "Threading/Lock.hpp"
6+
57
#include <string>
68

79
#pragma warning(push)
@@ -74,9 +76,6 @@ class XRCORE_API xrDebug
7476
const char* arg1 = nullptr, const char* arg2 = nullptr);
7577
static void DoExit(const std::string& message);
7678

77-
///
78-
/// Note: DbgHelp is singlethreaded, so you must synchronize calls to these functions
79-
///
8079
static void LogStackTrace(const char* header);
8180
static xr_vector<xr_string> BuildStackTrace(u16 maxFramesCount = MaxFramesCountDefault);
8281
private:
@@ -90,6 +89,7 @@ class XRCORE_API xrDebug
9089
/// Next members relates to stack tracing
9190
///
9291
static bool m_SymEngineInitialized;
92+
static Lock m_DbgHelpLock;
9393

9494
static xr_vector<xr_string> BuildStackTrace(PCONTEXT threadCtx, u16 maxFramesCount);
9595
static bool GetNextStackFrameString(LPSTACKFRAME stackFrame, PCONTEXT threadCtx, xr_string& frameStr);

0 commit comments

Comments
 (0)