Skip to content

Commit

Permalink
xrCore/Threading/Lock.cpp: use CRITICAL_SECTION for Windows because i…
Browse files Browse the repository at this point in the history
…t's faster
  • Loading branch information
Xottab-DUTY committed Aug 8, 2018
1 parent 489cbd6 commit f773297
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/xrCore/Threading/Lock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@

struct LockImpl
{
#ifdef WINDOWS
CRITICAL_SECTION cs;

LockImpl() { InitializeCriticalSection(&cs); }
~LockImpl() { DeleteCriticalSection(&cs); }

ICF void Lock() { EnterCriticalSection(&cs); }
ICF void Unlock() { LeaveCriticalSection(&cs); }
ICF bool TryLock() { return !!TryEnterCriticalSection(&cs); }
#else
std::recursive_mutex mutex;
};

Lock::~Lock()
{
delete impl;
}
ICF void Lock() { mutex.lock(); }
ICF void Unlock() { mutex.unlock(); }
ICF bool TryLock() { return mutex.try_lock(); }
#endif
};

#ifdef CONFIG_PROFILE_LOCKS
static add_profile_portion_callback add_profile_portion = 0;
Expand Down Expand Up @@ -55,25 +65,27 @@ void Lock::Enter()
#else
Lock::Lock() : impl(new LockImpl), lockCounter(0) {}

Lock::~Lock() { delete impl; }

void Lock::Enter()
{
impl->mutex.lock();
impl->Lock();
lockCounter++;
}
#endif // CONFIG_PROFILE_LOCKS

bool Lock::TryEnter()
{
bool locked = impl->mutex.try_lock();
const bool locked = impl->TryLock();
if (locked)
lockCounter++;
++lockCounter;
return locked;
}

void Lock::Leave()
{
impl->mutex.unlock();
lockCounter--;
impl->Unlock();
--lockCounter;
}

#ifdef DEBUG
Expand Down

0 comments on commit f773297

Please sign in to comment.