Skip to content

Commit 846fa30

Browse files
committed
Refactor Lock: solve mutex in CLR problem.
1 parent 15d57b5 commit 846fa30

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

src/xrCore/Threading/Lock.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
#include "stdafx.h"
22
#include "Lock.hpp"
3+
#include <mutex>
4+
5+
struct LockImpl
6+
{
7+
std::recursive_mutex mutex;
8+
};
9+
10+
Lock::~Lock()
11+
{
12+
delete impl;
13+
}
314

415
#ifdef CONFIG_PROFILE_LOCKS
516
static add_profile_portion_callback add_profile_portion = 0;
@@ -28,6 +39,8 @@ struct profiler
2839
}
2940
};
3041

42+
Lock::Lock(const char* id) : impl(new LockImpl), lockCounter(0), id(id) {}
43+
3144
void Lock::Enter()
3245
{
3346
#if 0 // def DEBUG
@@ -39,8 +52,30 @@ void Lock::Enter()
3952
mutex.lock();
4053
isLocked = true;
4154
}
55+
#else
56+
Lock::Lock() : impl(new LockImpl), lockCounter(0) {}
57+
58+
void Lock::Enter()
59+
{
60+
impl->mutex.lock();
61+
lockCounter++;
62+
}
4263
#endif // CONFIG_PROFILE_LOCKS
4364

65+
bool Lock::TryEnter()
66+
{
67+
bool locked = impl->mutex.try_lock();
68+
if (locked)
69+
lockCounter++;
70+
return locked;
71+
}
72+
73+
void Lock::Leave()
74+
{
75+
impl->mutex.unlock();
76+
lockCounter--;
77+
}
78+
4479
#ifdef DEBUG
4580
extern void OutputDebugStackTrace(const char* header);
4681
#endif

src/xrCore/Threading/Lock.hpp

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#pragma once
2-
#include <mutex>
32
#include <atomic>
43

4+
#include "Common/Noncopyable.hpp"
5+
56
#ifdef CONFIG_PROFILE_LOCKS
67
typedef void (*add_profile_portion_callback)(LPCSTR id, const u64& time);
78
void XRCORE_API set_add_profile_portion(add_profile_portion_callback callback);
@@ -14,45 +15,30 @@ void XRCORE_API set_add_profile_portion(add_profile_portion_callback callback);
1415
#define MUTEX_PROFILE_ID(a) STRINGIZER(CONCATENIZE(MUTEX_PROFILE_PREFIX_ID, a))
1516
#endif // CONFIG_PROFILE_LOCKS
1617

17-
class XRCORE_API Lock
18+
class XRCORE_API Lock : Noncopyable
1819
{
20+
struct LockImpl* impl;
1921
public:
2022
#ifdef CONFIG_PROFILE_LOCKS
21-
Lock(const char* id) : lockCounter(0), id(id) {}
23+
Lock(const char* id);
2224
#else
23-
Lock() : lockCounter(0) {}
25+
Lock();
2426
#endif
25-
26-
Lock(const Lock&) = delete;
27-
Lock operator=(const Lock&) = delete;
27+
~Lock();
2828

2929
#ifdef CONFIG_PROFILE_LOCKS
3030
void Enter();
3131
#else
32-
void Enter()
33-
{
34-
mutex.lock();
35-
lockCounter++;
36-
}
32+
void Enter();
3733
#endif
3834

39-
bool TryEnter()
40-
{
41-
bool locked = mutex.try_lock();
42-
if (locked)
43-
lockCounter++;
44-
return locked;
45-
}
35+
bool TryEnter();
4636

47-
void Leave()
48-
{
49-
mutex.unlock();
50-
lockCounter--;
51-
}
37+
void Leave();
5238

5339
bool IsLocked() const { return !!lockCounter; }
40+
5441
private:
55-
std::recursive_mutex mutex;
5642
std::atomic_int lockCounter;
5743
#ifdef CONFIG_PROFILE_LOCKS
5844
const char* id;

0 commit comments

Comments
 (0)