Skip to content

Commit

Permalink
xrCore/Threading/Lock: Made inline and reimplemented via std::mutex.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaffeine committed Nov 10, 2015
1 parent cc3644d commit 1c13715
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 38 deletions.
30 changes: 6 additions & 24 deletions src/xrCore/Threading/Lock.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "stdafx.h"
#include "Lock.hpp"
#include <windows.h>

#ifdef CONFIG_PROFILE_LOCKS
static add_profile_portion_callback add_profile_portion = 0;
Expand Down Expand Up @@ -32,37 +31,20 @@ struct profiler
(*add_profile_portion)(m_timer_id, time - m_time);
}
};
#endif // CONFIG_PROFILE_LOCKS

#ifdef CONFIG_PROFILE_LOCKS
Lock::Lock(const char *id) : id(id)
#else
Lock::Lock()
#endif
{ InitializeCriticalSection(&cs); }

Lock::~Lock() { DeleteCriticalSection(&cs); }

#ifdef DEBUG
extern void OutputDebugStackTrace(const char *header);
#endif

void Lock::Enter()
{
#ifdef CONFIG_PROFILE_LOCKS
# if 0//def DEBUG
static bool show_call_stack = false;
if (show_call_stack)
OutputDebugStackTrace("----------------------------------------------------");
# endif // DEBUG
profiler temp(id);
#endif // CONFIG_PROFILE_LOCKS
EnterCriticalSection(&cs);
mutex.lock();
isLocked = true;
}
#endif // CONFIG_PROFILE_LOCKS

bool Lock::TryEnter() { return !!TryEnterCriticalSection(&cs); }

void Lock::Leave() { LeaveCriticalSection(&cs); }

bool Lock::IsLocked() const
{ return cs.RecursionCount>0 && (DWORD)cs.OwningThread==GetCurrentThreadId(); }
#ifdef DEBUG
extern void OutputDebugStackTrace(const char *header);
#endif
45 changes: 31 additions & 14 deletions src/xrCore/Threading/Lock.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#pragma once
#include "xrCore/xrCore.h"

#ifdef CONFIG_PROFILE_LOCKS
#include <mutex>
#include <atomic>

#ifdef CONFIG_PROFILE_LOCKS.
typedef void(*add_profile_portion_callback) (LPCSTR id, const u64& time);
void XRCORE_API set_add_profile_portion(add_profile_portion_callback callback);

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

class XRCORE_API Lock
{
private:
friend class AutoLock;
CRITICAL_SECTION cs;
#ifdef CONFIG_PROFILE_LOCKS
const char *id;
#endif

public:
#ifdef CONFIG_PROFILE_LOCKS
Lock(const char *id);
Lock(const char *id) : isLocked(false), id(id) { }
#else
Lock();
Lock() : isLocked(false) { }
#endif
~Lock();

Lock(const Lock &) = delete;
Lock operator=(const Lock &) = delete;

#ifdef CONFIG_PROFILE_LOCKS
void Enter();
bool TryEnter();
void Leave();
bool IsLocked() const;
#else
void Enter() { return mutex.lock(); isLocked = true; }
#endif

bool TryEnter()
{
bool locked = mutex.try_lock();
if (locked)
{
isLocked = true;
}
return locked;
}

void Leave() { return mutex.unlock(); isLocked = false; }

bool IsLocked() const { return isLocked; }

private:
std::mutex mutex;
std::atomic_bool isLocked;
#ifdef CONFIG_PROFILE_LOCKS
const char *id;
#endif
};

0 comments on commit 1c13715

Please sign in to comment.