Skip to content

Commit f773297

Browse files
committed
xrCore/Threading/Lock.cpp: use CRITICAL_SECTION for Windows because it's faster
1 parent 489cbd6 commit f773297

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/xrCore/Threading/Lock.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44

55
struct LockImpl
66
{
7+
#ifdef WINDOWS
8+
CRITICAL_SECTION cs;
9+
10+
LockImpl() { InitializeCriticalSection(&cs); }
11+
~LockImpl() { DeleteCriticalSection(&cs); }
12+
13+
ICF void Lock() { EnterCriticalSection(&cs); }
14+
ICF void Unlock() { LeaveCriticalSection(&cs); }
15+
ICF bool TryLock() { return !!TryEnterCriticalSection(&cs); }
16+
#else
717
std::recursive_mutex mutex;
8-
};
918

10-
Lock::~Lock()
11-
{
12-
delete impl;
13-
}
19+
ICF void Lock() { mutex.lock(); }
20+
ICF void Unlock() { mutex.unlock(); }
21+
ICF bool TryLock() { return mutex.try_lock(); }
22+
#endif
23+
};
1424

1525
#ifdef CONFIG_PROFILE_LOCKS
1626
static add_profile_portion_callback add_profile_portion = 0;
@@ -55,25 +65,27 @@ void Lock::Enter()
5565
#else
5666
Lock::Lock() : impl(new LockImpl), lockCounter(0) {}
5767

68+
Lock::~Lock() { delete impl; }
69+
5870
void Lock::Enter()
5971
{
60-
impl->mutex.lock();
72+
impl->Lock();
6173
lockCounter++;
6274
}
6375
#endif // CONFIG_PROFILE_LOCKS
6476

6577
bool Lock::TryEnter()
6678
{
67-
bool locked = impl->mutex.try_lock();
79+
const bool locked = impl->TryLock();
6880
if (locked)
69-
lockCounter++;
81+
++lockCounter;
7082
return locked;
7183
}
7284

7385
void Lock::Leave()
7486
{
75-
impl->mutex.unlock();
76-
lockCounter--;
87+
impl->Unlock();
88+
--lockCounter;
7789
}
7890

7991
#ifdef DEBUG

0 commit comments

Comments
 (0)