Skip to content

Commit 1c13715

Browse files
committed
xrCore/Threading/Lock: Made inline and reimplemented via std::mutex.
1 parent cc3644d commit 1c13715

File tree

2 files changed

+37
-38
lines changed

2 files changed

+37
-38
lines changed

src/xrCore/Threading/Lock.cpp

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "stdafx.h"
22
#include "Lock.hpp"
3-
#include <windows.h>
43

54
#ifdef CONFIG_PROFILE_LOCKS
65
static add_profile_portion_callback add_profile_portion = 0;
@@ -32,37 +31,20 @@ struct profiler
3231
(*add_profile_portion)(m_timer_id, time - m_time);
3332
}
3433
};
35-
#endif // CONFIG_PROFILE_LOCKS
36-
37-
#ifdef CONFIG_PROFILE_LOCKS
38-
Lock::Lock(const char *id) : id(id)
39-
#else
40-
Lock::Lock()
41-
#endif
42-
{ InitializeCriticalSection(&cs); }
43-
44-
Lock::~Lock() { DeleteCriticalSection(&cs); }
45-
46-
#ifdef DEBUG
47-
extern void OutputDebugStackTrace(const char *header);
48-
#endif
4934

5035
void Lock::Enter()
5136
{
52-
#ifdef CONFIG_PROFILE_LOCKS
5337
# if 0//def DEBUG
5438
static bool show_call_stack = false;
5539
if (show_call_stack)
5640
OutputDebugStackTrace("----------------------------------------------------");
5741
# endif // DEBUG
5842
profiler temp(id);
59-
#endif // CONFIG_PROFILE_LOCKS
60-
EnterCriticalSection(&cs);
43+
mutex.lock();
44+
isLocked = true;
6145
}
46+
#endif // CONFIG_PROFILE_LOCKS
6247

63-
bool Lock::TryEnter() { return !!TryEnterCriticalSection(&cs); }
64-
65-
void Lock::Leave() { LeaveCriticalSection(&cs); }
66-
67-
bool Lock::IsLocked() const
68-
{ return cs.RecursionCount>0 && (DWORD)cs.OwningThread==GetCurrentThreadId(); }
48+
#ifdef DEBUG
49+
extern void OutputDebugStackTrace(const char *header);
50+
#endif

src/xrCore/Threading/Lock.hpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#pragma once
22
#include "xrCore/xrCore.h"
33

4-
#ifdef CONFIG_PROFILE_LOCKS
4+
#include <mutex>
5+
#include <atomic>
6+
7+
#ifdef CONFIG_PROFILE_LOCKS.
58
typedef void(*add_profile_portion_callback) (LPCSTR id, const u64& time);
69
void XRCORE_API set_add_profile_portion(add_profile_portion_callback callback);
710

@@ -15,26 +18,40 @@ void XRCORE_API set_add_profile_portion(add_profile_portion_callback callback);
1518

1619
class XRCORE_API Lock
1720
{
18-
private:
19-
friend class AutoLock;
20-
CRITICAL_SECTION cs;
21-
#ifdef CONFIG_PROFILE_LOCKS
22-
const char *id;
23-
#endif
24-
2521
public:
2622
#ifdef CONFIG_PROFILE_LOCKS
27-
Lock(const char *id);
23+
Lock(const char *id) : isLocked(false), id(id) { }
2824
#else
29-
Lock();
25+
Lock() : isLocked(false) { }
3026
#endif
31-
~Lock();
3227

3328
Lock(const Lock &) = delete;
3429
Lock operator=(const Lock &) = delete;
3530

31+
#ifdef CONFIG_PROFILE_LOCKS
3632
void Enter();
37-
bool TryEnter();
38-
void Leave();
39-
bool IsLocked() const;
33+
#else
34+
void Enter() { return mutex.lock(); isLocked = true; }
35+
#endif
36+
37+
bool TryEnter()
38+
{
39+
bool locked = mutex.try_lock();
40+
if (locked)
41+
{
42+
isLocked = true;
43+
}
44+
return locked;
45+
}
46+
47+
void Leave() { return mutex.unlock(); isLocked = false; }
48+
49+
bool IsLocked() const { return isLocked; }
50+
51+
private:
52+
std::mutex mutex;
53+
std::atomic_bool isLocked;
54+
#ifdef CONFIG_PROFILE_LOCKS
55+
const char *id;
56+
#endif
4057
};

0 commit comments

Comments
 (0)