Alternative mutex
This commit is contained in:
@@ -8,11 +8,76 @@
|
||||
*/
|
||||
|
||||
#include "xenia/base/mutex.h"
|
||||
#if XE_PLATFORM_WIN32 == 1
|
||||
#include "xenia/base/platform_win.h"
|
||||
#endif
|
||||
|
||||
|
||||
namespace xe {
|
||||
#if XE_PLATFORM_WIN32 == 1 &&XE_ENABLE_FAST_WIN32_MUTEX == 1
|
||||
//default spincount for entercriticalsection is insane on windows, 0x20007D0i64 (33556432 times!!)
|
||||
//when a lock is highly contended performance degrades sharply on some processors
|
||||
#define XE_CRIT_SPINCOUNT 128
|
||||
/*
|
||||
chrispy: todo, if a thread exits before releasing the global mutex we need to
|
||||
check this and release the mutex one way to do this is by using FlsAlloc and
|
||||
PFLS_CALLBACK_FUNCTION, which gets called with the fiber local data when a
|
||||
thread exits
|
||||
*/
|
||||
thread_local unsigned global_mutex_depth = 0;
|
||||
static CRITICAL_SECTION* global_critical_section(xe_global_mutex* mutex) {
|
||||
return reinterpret_cast<CRITICAL_SECTION*>(mutex);
|
||||
}
|
||||
|
||||
xe_global_mutex::xe_global_mutex() {
|
||||
InitializeCriticalSectionAndSpinCount(global_critical_section(this),
|
||||
XE_CRIT_SPINCOUNT);
|
||||
}
|
||||
xe_global_mutex ::~xe_global_mutex() {
|
||||
DeleteCriticalSection(global_critical_section(this));
|
||||
}
|
||||
void xe_global_mutex::lock() {
|
||||
if (global_mutex_depth) {
|
||||
} else {
|
||||
EnterCriticalSection(global_critical_section(this));
|
||||
}
|
||||
global_mutex_depth++;
|
||||
}
|
||||
void xe_global_mutex::unlock() {
|
||||
if (--global_mutex_depth == 0) {
|
||||
LeaveCriticalSection(global_critical_section(this));
|
||||
}
|
||||
}
|
||||
bool xe_global_mutex::try_lock() {
|
||||
if (global_mutex_depth) {
|
||||
++global_mutex_depth;
|
||||
return true;
|
||||
} else {
|
||||
BOOL success = TryEnterCriticalSection(global_critical_section(this));
|
||||
if (success) {
|
||||
++global_mutex_depth;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
||||
CRITICAL_SECTION* fast_crit(xe_fast_mutex* mutex) {
|
||||
return reinterpret_cast<CRITICAL_SECTION*>(mutex);
|
||||
}
|
||||
xe_fast_mutex::xe_fast_mutex() {
|
||||
InitializeCriticalSectionAndSpinCount(fast_crit(this), XE_CRIT_SPINCOUNT);
|
||||
}
|
||||
xe_fast_mutex::~xe_fast_mutex() { DeleteCriticalSection(fast_crit(this)); }
|
||||
|
||||
void xe_fast_mutex::lock() { EnterCriticalSection(fast_crit(this)); }
|
||||
void xe_fast_mutex::unlock() { LeaveCriticalSection(fast_crit(this)); }
|
||||
bool xe_fast_mutex::try_lock() {
|
||||
return TryEnterCriticalSection(fast_crit(this));
|
||||
}
|
||||
#endif
|
||||
// chrispy: moved this out of body of function to eliminate the initialization
|
||||
// guards
|
||||
static std::recursive_mutex global_mutex;
|
||||
std::recursive_mutex& global_critical_region::mutex() { return global_mutex; }
|
||||
static global_mutex_type global_mutex;
|
||||
global_mutex_type& global_critical_region::mutex() { return global_mutex; }
|
||||
|
||||
} // namespace xe
|
||||
|
||||
@@ -9,11 +9,50 @@
|
||||
|
||||
#ifndef XENIA_BASE_MUTEX_H_
|
||||
#define XENIA_BASE_MUTEX_H_
|
||||
|
||||
#include <mutex>
|
||||
#include "platform.h"
|
||||
|
||||
//#define XE_ENABLE_FAST_WIN32_MUTEX 1
|
||||
namespace xe {
|
||||
|
||||
#if XE_PLATFORM_WIN32 == 1 && XE_ENABLE_FAST_WIN32_MUTEX==1
|
||||
/*
|
||||
must conform to
|
||||
BasicLockable:https://en.cppreference.com/w/cpp/named_req/BasicLockable as
|
||||
well as Lockable: https://en.cppreference.com/w/cpp/named_req/Lockable
|
||||
|
||||
this emulates a recursive mutex, except with far less overhead
|
||||
*/
|
||||
class alignas(64) xe_global_mutex {
|
||||
char detail[64];
|
||||
|
||||
public:
|
||||
xe_global_mutex();
|
||||
~xe_global_mutex();
|
||||
|
||||
void lock();
|
||||
void unlock();
|
||||
bool try_lock();
|
||||
};
|
||||
using global_mutex_type = xe_global_mutex;
|
||||
|
||||
class alignas(64) xe_fast_mutex {
|
||||
char detail[64];
|
||||
|
||||
public:
|
||||
xe_fast_mutex();
|
||||
~xe_fast_mutex();
|
||||
|
||||
void lock();
|
||||
void unlock();
|
||||
bool try_lock();
|
||||
};
|
||||
using xe_mutex = xe_fast_mutex;
|
||||
#else
|
||||
using global_mutex_type = std::recursive_mutex;
|
||||
using xe_mutex = std::mutex;
|
||||
#endif
|
||||
using global_unique_lock_type = std::unique_lock<global_mutex_type>;
|
||||
// The global critical region mutex singleton.
|
||||
// This must guard any operation that may suspend threads or be sensitive to
|
||||
// being suspended such as global table locks and such.
|
||||
@@ -54,30 +93,30 @@ namespace xe {
|
||||
// };
|
||||
class global_critical_region {
|
||||
public:
|
||||
static std::recursive_mutex& mutex();
|
||||
static global_mutex_type& mutex();
|
||||
|
||||
// Acquires a lock on the global critical section.
|
||||
// Use this when keeping an instance is not possible. Otherwise, prefer
|
||||
// to keep an instance of global_critical_region near the members requiring
|
||||
// it to keep things readable.
|
||||
static std::unique_lock<std::recursive_mutex> AcquireDirect() {
|
||||
return std::unique_lock<std::recursive_mutex>(mutex());
|
||||
static global_unique_lock_type AcquireDirect() {
|
||||
return global_unique_lock_type(mutex());
|
||||
}
|
||||
|
||||
// Acquires a lock on the global critical section.
|
||||
inline std::unique_lock<std::recursive_mutex> Acquire() {
|
||||
return std::unique_lock<std::recursive_mutex>(mutex());
|
||||
inline global_unique_lock_type Acquire() {
|
||||
return global_unique_lock_type(mutex());
|
||||
}
|
||||
|
||||
// Acquires a deferred lock on the global critical section.
|
||||
inline std::unique_lock<std::recursive_mutex> AcquireDeferred() {
|
||||
return std::unique_lock<std::recursive_mutex>(mutex(), std::defer_lock);
|
||||
inline global_unique_lock_type AcquireDeferred() {
|
||||
return global_unique_lock_type(mutex(), std::defer_lock);
|
||||
}
|
||||
|
||||
// Tries to acquire a lock on the glboal critical section.
|
||||
// Check owns_lock() to see if the lock was successfully acquired.
|
||||
inline std::unique_lock<std::recursive_mutex> TryAcquire() {
|
||||
return std::unique_lock<std::recursive_mutex>(mutex(), std::try_to_lock);
|
||||
inline global_unique_lock_type TryAcquire() {
|
||||
return global_unique_lock_type(mutex(), std::try_to_lock);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ using WaitItem = TimerQueueWaitItem;
|
||||
edit: actually had to change it back, when i was testing it only worked because i fixed disruptorplus' code to compile (it gives wrong args to condition_variable::wait_until) but now builds
|
||||
|
||||
*/
|
||||
using WaitStrat = dp::spin_wait_strategy; //dp::blocking_wait_strategy;
|
||||
using WaitStrat = dp::blocking_wait_strategy;
|
||||
|
||||
class TimerQueue {
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user