File tree Expand file tree Collapse file tree 2 files changed +37
-38
lines changed Expand file tree Collapse file tree 2 files changed +37
-38
lines changed Original file line number Diff line number Diff line change 1
1
#include " stdafx.h"
2
2
#include " Lock.hpp"
3
- #include < windows.h>
4
3
5
4
#ifdef CONFIG_PROFILE_LOCKS
6
5
static add_profile_portion_callback add_profile_portion = 0 ;
@@ -32,37 +31,20 @@ struct profiler
32
31
(*add_profile_portion)(m_timer_id, time - m_time);
33
32
}
34
33
};
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
49
34
50
35
void Lock::Enter ()
51
36
{
52
- #ifdef CONFIG_PROFILE_LOCKS
53
37
# if 0// def DEBUG
54
38
static bool show_call_stack = false ;
55
39
if (show_call_stack)
56
40
OutputDebugStackTrace (" ----------------------------------------------------" );
57
41
# endif // DEBUG
58
42
profiler temp (id);
59
- # endif // CONFIG_PROFILE_LOCKS
60
- EnterCriticalSection (&cs) ;
43
+ mutex. lock ();
44
+ isLocked = true ;
61
45
}
46
+ #endif // CONFIG_PROFILE_LOCKS
62
47
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
Original file line number Diff line number Diff line change 1
1
#pragma once
2
2
#include " xrCore/xrCore.h"
3
3
4
- #ifdef CONFIG_PROFILE_LOCKS
4
+ #include < mutex>
5
+ #include < atomic>
6
+
7
+ #ifdef CONFIG_PROFILE_LOCKS.
5
8
typedef void (*add_profile_portion_callback) (LPCSTR id, const u64 & time);
6
9
void XRCORE_API set_add_profile_portion (add_profile_portion_callback callback);
7
10
@@ -15,26 +18,40 @@ void XRCORE_API set_add_profile_portion(add_profile_portion_callback callback);
15
18
16
19
class XRCORE_API Lock
17
20
{
18
- private:
19
- friend class AutoLock ;
20
- CRITICAL_SECTION cs;
21
- #ifdef CONFIG_PROFILE_LOCKS
22
- const char *id;
23
- #endif
24
-
25
21
public:
26
22
#ifdef CONFIG_PROFILE_LOCKS
27
- Lock (const char *id);
23
+ Lock (const char *id) : isLocked( false ), id(id) { }
28
24
#else
29
- Lock ();
25
+ Lock () : isLocked( false ) { }
30
26
#endif
31
- ~Lock ();
32
27
33
28
Lock (const Lock &) = delete ;
34
29
Lock operator =(const Lock &) = delete ;
35
30
31
+ #ifdef CONFIG_PROFILE_LOCKS
36
32
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
40
57
};
You can’t perform that action at this time.
0 commit comments