rpcs3/Utilities/SMutex.h

180 lines
3.1 KiB
C
Raw Normal View History

2014-02-05 11:55:32 +00:00
#pragma once
#include "BEType.h"
2014-07-10 20:54:12 +00:00
#include "Emu/System.h"
2014-02-05 11:55:32 +00:00
extern void SM_Sleep();
2014-02-26 11:27:06 +00:00
extern size_t SM_GetCurrentThreadId();
2014-02-05 11:55:32 +00:00
extern u32 SM_GetCurrentCPUThreadId();
extern be_t<u32> SM_GetCurrentCPUThreadIdBE();
enum SMutexResult
{
SMR_OK = 0, // succeeded (lock, trylock, unlock)
SMR_FAILED, // failed (trylock, unlock)
SMR_DEADLOCK, // mutex reached deadlock (lock, trylock)
SMR_SIGNAL = SMR_DEADLOCK, // unlock can be used for signaling specific thread
SMR_PERMITTED, // not owner of the mutex (unlock)
SMR_ABORT, // emulator has been stopped (lock, trylock, unlock)
SMR_DESTROYED, // mutex has been destroyed (lock, trylock, unlock)
SMR_TIMEOUT, // timed out (lock)
};
template
<
typename T,
2014-07-18 16:55:26 +00:00
const u64 free_value = 0,
const u64 dead_value = 0xffffffffffffffffull,
void (*wait)() = SM_Sleep
2014-02-05 11:55:32 +00:00
>
class SMutexBase
{
2014-03-01 23:50:47 +00:00
static_assert(sizeof(T) == sizeof(std::atomic<T>), "Invalid SMutexBase type");
2014-02-05 11:55:32 +00:00
std::atomic<T> owner;
public:
2014-07-18 16:55:26 +00:00
static const T GetFreeValue()
2014-02-05 11:55:32 +00:00
{
2014-07-18 16:55:26 +00:00
static const u64 value = free_value;
return (const T&)value;
2014-02-05 11:55:32 +00:00
}
2014-07-18 16:55:26 +00:00
static const T GetDeadValue()
{
2014-07-18 16:55:26 +00:00
static const u64 value = dead_value;
return (const T&)value;
}
2014-07-18 16:55:26 +00:00
void initialize()
2014-02-05 11:55:32 +00:00
{
2014-07-18 16:55:26 +00:00
owner = GetFreeValue();
2014-02-05 11:55:32 +00:00
}
2014-07-18 17:30:22 +00:00
SMutexBase()
{
initialize();
}
2014-07-18 16:55:26 +00:00
void finalize()
{
2014-07-18 16:55:26 +00:00
owner = GetDeadValue();
}
2014-07-18 16:55:26 +00:00
__forceinline T GetOwner() const
{
2014-07-18 16:55:26 +00:00
return (T&)owner;
}
2014-02-05 11:55:32 +00:00
SMutexResult trylock(T tid)
{
if (Emu.IsStopped())
{
return SMR_ABORT;
}
2014-07-18 16:55:26 +00:00
T old = GetFreeValue();
2014-02-05 11:55:32 +00:00
if (!owner.compare_exchange_strong(old, tid))
{
if (old == tid)
{
return SMR_DEADLOCK;
}
2014-07-18 16:55:26 +00:00
if (old == GetDeadValue())
2014-02-05 11:55:32 +00:00
{
return SMR_DESTROYED;
}
return SMR_FAILED;
}
return SMR_OK;
}
2014-07-18 16:55:26 +00:00
SMutexResult unlock(T tid, T to = GetFreeValue())
2014-02-05 11:55:32 +00:00
{
if (Emu.IsStopped())
{
return SMR_ABORT;
}
2014-02-05 11:55:32 +00:00
T old = tid;
if (!owner.compare_exchange_strong(old, to))
{
2014-07-18 16:55:26 +00:00
if (old == GetFreeValue())
2014-02-05 11:55:32 +00:00
{
return SMR_FAILED;
}
2014-07-18 16:55:26 +00:00
if (old == GetDeadValue())
2014-02-05 11:55:32 +00:00
{
return SMR_DESTROYED;
}
return SMR_PERMITTED;
}
return SMR_OK;
}
SMutexResult lock(T tid, u64 timeout = 0)
{
u64 counter = 0;
while (true)
{
switch (SMutexResult res = trylock(tid))
{
case SMR_FAILED: break;
default: return res;
}
2014-07-10 20:54:12 +00:00
if (wait != nullptr) wait();
2014-02-05 11:55:32 +00:00
if (timeout && counter++ > timeout)
{
return SMR_TIMEOUT;
}
}
}
};
template<typename T, typename Tid, Tid (get_tid)()>
2014-02-05 11:55:32 +00:00
class SMutexLockerBase
{
T& sm;
public:
const Tid tid;
2014-02-05 11:55:32 +00:00
__forceinline SMutexLockerBase(T& _sm)
2014-02-05 11:55:32 +00:00
: sm(_sm)
, tid(get_tid())
{
if (!tid)
{
if (!Emu.IsStopped())
{
LOG_ERROR(HLE, "SMutexLockerBase: thread id == 0");
Emu.Pause();
}
return;
}
2014-02-05 11:55:32 +00:00
sm.lock(tid);
}
__forceinline ~SMutexLockerBase()
2014-02-05 11:55:32 +00:00
{
if (tid) sm.unlock(tid);
2014-02-05 11:55:32 +00:00
}
};
2014-02-26 11:27:06 +00:00
typedef SMutexBase<size_t>
2014-02-05 11:55:32 +00:00
SMutexGeneral;
typedef SMutexBase<u32>
SMutex;
typedef SMutexBase<be_t<u32>>
SMutexBE;
typedef SMutexLockerBase<SMutexGeneral, size_t, SM_GetCurrentThreadId>
2014-02-05 11:55:32 +00:00
SMutexGeneralLocker;
typedef SMutexLockerBase<SMutex, u32, SM_GetCurrentCPUThreadId>
2014-02-05 11:55:32 +00:00
SMutexLocker;
typedef SMutexLockerBase<SMutexBE, be_t<u32>, SM_GetCurrentCPUThreadIdBE>
SMutexBELocker;