1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-22 02:32:36 +01:00

Small changes, some bugs fixed

This commit is contained in:
Nekotekina 2014-02-05 15:55:32 +04:00
parent aeca523a34
commit cfe154d916
13 changed files with 242 additions and 94 deletions

View File

@ -2,6 +2,10 @@
#include "Utilities/GNU.h"
#define se16(x) const_se_t<u16, x>::value
#define se32(x) const_se_t<u32, x>::value
#define se64(x) const_se_t<u64, x>::value
template<typename T, int size = sizeof(T)> struct se_t;
template<typename T> struct se_t<T, 1> { static __forceinline void func(T& dst, const T src) { (u8&)dst = (u8&)src; } };
template<typename T> struct se_t<T, 2> { static __forceinline void func(T& dst, const T src) { (u16&)dst = _byteswap_ushort((u16&)src); } };

26
Utilities/SMutex.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <stdafx.h>
#include <Utilities/SMutex.h>
__forceinline void SM_Sleep()
{
Sleep(1);
}
__forceinline DWORD SM_GetCurrentThreadId()
{
return GetCurrentThreadId();
}
__forceinline u32 SM_GetCurrentCPUThreadId()
{
if (CPUThread* t = GetCurrentCPUThread())
{
return t->GetId();
}
return 0;
}
__forceinline be_t<u32> SM_GetCurrentCPUThreadIdBE()
{
return SM_GetCurrentCPUThreadId();
}

151
Utilities/SMutex.h Normal file
View File

@ -0,0 +1,151 @@
#pragma once
#include <atomic>
extern void SM_Sleep();
extern DWORD SM_GetCurrentThreadId();
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,
u32 free_value = 0,
u32 dead_value = ~0,
void (wait)() = SM_Sleep
>
class SMutexBase
{
static_assert(sizeof(T) == 4, "Invalid SMutexBase typename");
std::atomic<T> owner;
public:
SMutexBase()
: owner((T)free_value)
{
}
~SMutexBase()
{
lock((T)dead_value);
owner = (T)dead_value;
}
__forceinline T GetOwner() const
{
return (T&)owner;
}
SMutexResult trylock(T tid)
{
T old = (T)free_value;
if (!owner.compare_exchange_strong(old, tid))
{
if (old == tid)
{
return SMR_DEADLOCK;
}
if (Emu.IsStopped())
{
return SMR_ABORT;
}
if (old == (T)dead_value)
{
return SMR_DESTROYED;
}
return SMR_FAILED;
}
return SMR_OK;
}
SMutexResult unlock(T tid, T to = (T)free_value)
{
T old = tid;
if (!owner.compare_exchange_strong(old, to))
{
if (old == (T)free_value)
{
return SMR_FAILED;
}
if (old == (T)dead_value)
{
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;
}
wait();
if (timeout && counter++ > timeout)
{
return SMR_TIMEOUT;
}
}
}
};
template<typename T, T (get_tid)()>
class SMutexLockerBase
{
typedef SMutexBase<T> T_SMutex;
T_SMutex& sm;
const T tid;
SMutexLockerBase(T_SMutex& _sm)
: sm(_sm)
, tid(get_tid())
{
if (!tid) throw "SMutexLockerBase: invalid thread id";
sm.lock(tid);
}
~SMutexLockerBase()
{
sm.unlock(tid);
}
};
typedef SMutexBase<DWORD>
SMutexGeneral;
typedef SMutexBase<u32>
SMutex;
typedef SMutexBase<be_t<u32>>
SMutexBE;
typedef SMutexLockerBase<DWORD, SM_GetCurrentThreadId>
SMutexGeneralLocker;
typedef SMutexLockerBase<u32, SM_GetCurrentCPUThreadId>
SMutexLocker;
typedef SMutexLockerBase<be_t<u32>, SM_GetCurrentCPUThreadIdBE>
SMutexBELocker;

View File

@ -78,7 +78,7 @@ void DynamicMemoryBlockBase<PT>::Delete()
template<typename PT>
bool DynamicMemoryBlockBase<PT>::AllocFixed(u64 addr, u32 size)
{
size = PAGE_4K(size); // align size
size = PAGE_4K(size + (addr & 4095)); // align size
addr &= ~4095; // align start address

View File

@ -7,7 +7,7 @@ extern gcmInfo gcm_info;
int cellGcmCallback(u32 context_addr, u32 count)
{
GSLockCurrent gslock(GS_LOCK_WAIT_FLUSH);
GSLockCurrent gslock(GS_LOCK_WAIT_FLUSH); // could stall on exit
CellGcmContextData& ctx = (CellGcmContextData&)Memory[context_addr];
CellGcmControl& ctrl = (CellGcmControl&)Memory[gcm_info.control_addr];

View File

@ -95,7 +95,7 @@ int sys_lwcond_wait(mem_ptr_t<sys_lwcond_t> lwcond, u64 timeout)
const u32 tid = GetCurrentPPUThread().GetId();
mem_ptr_t<sys_lwmutex_t> lwmutex((u32)lwcond->lwmutex_addr);
if ((u32)lwmutex->owner != tid) return CELL_EPERM; // caller must own this lwmutex
if ((u32)lwmutex->owner.GetOwner() != tid) return CELL_EPERM; // caller must own this lwmutex
lwc->begin_waiting(tid);
u32 counter = 0;

View File

@ -1,12 +1,11 @@
#include "stdafx.h"
#include "Emu/SysCalls/SysCalls.h"
#include "Emu/SysCalls/lv2/SC_Lwmutex.h"
#include "Utilities/SMutex.h"
#include <mutex>
SysCallBase sc_lwmutex("sys_lwmutex");
std::mutex g_lwmutex;
int sys_lwmutex_create(mem_ptr_t<sys_lwmutex_t> lwmutex, mem_ptr_t<sys_lwmutex_attribute_t> attr)
{
sc_lwmutex.Warning("sys_lwmutex_create(lwmutex_addr=0x%x, lwmutex_attr_addr=0x%x)",
@ -49,20 +48,9 @@ int sys_lwmutex_destroy(mem_ptr_t<sys_lwmutex_t> lwmutex)
if (!lwmutex->attribute) return CELL_EINVAL;
{ // global lock
std::lock_guard<std::mutex> lock(g_lwmutex);
if (!lwmutex->owner)
{
lwmutex->owner = ~0; // make it unable to lock
lwmutex->attribute = 0;
}
else
{
return CELL_EBUSY;
}
}
// try to make it unable to lock
if (lwmutex->owner.trylock(~0) != SMR_OK) return CELL_EBUSY;
lwmutex->attribute = 0;
return CELL_OK;
}
@ -74,35 +62,7 @@ int sys_lwmutex_lock(mem_ptr_t<sys_lwmutex_t> lwmutex, u64 timeout)
if (!lwmutex->attribute) return CELL_EINVAL;
const u32 tid = GetCurrentPPUThread().GetId();
int res = lwmutex->trylock(tid);
if (res != CELL_EBUSY) return res;
u32 counter = 0;
const u32 max_counter = timeout ? (timeout / 1000) : 20000;
do // waiting
{
if (Emu.IsStopped()) return CELL_ETIMEDOUT;
Sleep(1);
res = lwmutex->trylock(tid);
if (res != CELL_EBUSY) return res;
if (!lwmutex->attribute) return CELL_EINVAL;
if (counter++ > max_counter)
{
if (!timeout)
{
sc_lwmutex.Warning("sys_lwmutex_lock(lwmutex_addr=0x%x): TIMEOUT", lwmutex.GetAddr());
counter = 0;
}
else
{
return CELL_ETIMEDOUT;
}
}
} while (true);
return lwmutex->lock(GetCurrentPPUThread().GetId(), timeout ? ((timeout < 1000) ? 1 : (timeout / 1000)) : 0);
}
int sys_lwmutex_trylock(mem_ptr_t<sys_lwmutex_t> lwmutex)

View File

@ -1,4 +1,5 @@
#pragma once
#include <Utilities/SMutex.h>
// attr_protocol (waiting scheduling policy)
enum
@ -33,16 +34,14 @@ struct sys_lwmutex_attribute_t
char name[8];
};
extern std::mutex g_lwmutex;
struct sys_lwmutex_t
{
union // sys_lwmutex_variable_t
{
struct // sys_lwmutex_lock_info_t
{
/* volatile */ be_t<u32> owner;
/* volatile */ be_t<u32> waiter;
/* volatile */ SMutexBE owner;
/* volatile */ be_t<u32> waiter; // not used
};
struct
{
@ -56,75 +55,74 @@ struct sys_lwmutex_t
int trylock(u32 tid)
{
std::lock_guard<std::mutex> lock(g_lwmutex); // global lock
if ((u32)attribute & SYS_SYNC_RECURSIVE)
if (tid == (u32)owner.GetOwner()) // recursive or deadlock
{
if (tid == (u32)owner)
if (attribute & se32(SYS_SYNC_RECURSIVE))
{
recursive_count = (u32)recursive_count + 1;
if ((u32)recursive_count == 0xffffffff) return CELL_EKRESOURCE;
recursive_count += 1;
if (!recursive_count) return CELL_EKRESOURCE;
return CELL_OK;
}
}
else // recursive not allowed
{
if (tid == (u32)owner)
else
{
return CELL_EDEADLK;
}
}
if (!(u32)owner) // try lock
switch (owner.trylock(tid))
{
owner = tid;
recursive_count = 1;
return CELL_OK;
}
else
{
return CELL_EBUSY;
case SMR_OK: recursive_count = 1; return CELL_OK;
default: return CELL_EBUSY;
}
}
bool unlock(u32 tid)
{
std::lock_guard<std::mutex> lock(g_lwmutex); // global lock
if (tid != (u32)owner)
if (tid != (u32)owner.GetOwner())
{
return false;
}
else
{
recursive_count = (u32)recursive_count - 1;
if (!(u32)recursive_count)
recursive_count -= 1;
if (!recursive_count)
{
waiter = 0; // not used yet
owner = 0; // release
owner.unlock(tid);
}
return true;
}
}
int lock(u32 tid, u64 timeout)
{
switch (int res = trylock(tid))
{
case CELL_OK: return CELL_OK;
case CELL_EBUSY: break;
default: return res;
}
switch (owner.lock(tid, timeout))
{
case SMR_OK: recursive_count = 1; return CELL_OK;
case SMR_TIMEOUT: return CELL_ETIMEDOUT;
default: return CELL_EINVAL;
}
}
};
struct lwmutex_locker
class lwmutex_locker
{
private:
mem_ptr_t<sys_lwmutex_t> m_mutex;
u32 m_id;
public:
const int res;
lwmutex_locker(u32 lwmutex_addr, u32 tid)
lwmutex_locker(u32 lwmutex_addr, u32 tid, u64 timeout = 0)
: m_id(tid)
, m_mutex(lwmutex_addr)
, res(m_mutex->trylock(m_id))
{
if (int res = m_mutex->lock(m_id, timeout)) throw "lwmutex_locker: m_mutex->lock failed";
}
~lwmutex_locker()
{
if (res == CELL_OK) m_mutex->unlock(m_id);
m_mutex->unlock(m_id);
}
};

View File

@ -45,18 +45,18 @@ int sys_memory_container_destroy(u32 cid)
int sys_memory_allocate(u32 size, u32 flags, u32 alloc_addr_addr)
{
//0x30000100;
sc_mem.Warning("sys_memory_allocate(size=0x%x, flags=0x%x)", size, flags);
sc_mem.Log("sys_memory_allocate(size=0x%x, flags=0x%x)", size, flags);
u32 addr;
switch(flags)
{
case SYS_MEMORY_PAGE_SIZE_1M:
if(size & 0xfffff) return CELL_EALIGN;
addr = Memory.Alloc(size, 0x100000);
addr = Memory.Alloc(size, 1);
break;
case SYS_MEMORY_PAGE_SIZE_64K:
if(size & 0xffff) return CELL_EALIGN;
addr = Memory.Alloc(size, 0x10000);
addr = Memory.Alloc(size, 1);
break;
default: return CELL_EINVAL;

View File

@ -1,6 +1,7 @@
#include "stdafx.h"
#include "Emu/SysCalls/SysCalls.h"
#include "SC_Mutex.h"
#include "Utilities/SMutex.h"
SysCallBase sys_mtx("sys_mutex");

View File

@ -10,16 +10,16 @@ int sys_rwlock_create(mem32_t rw_lock_id, mem_ptr_t<sys_rwlock_attribute_t> attr
if (!rw_lock_id.IsGood() || !attr.IsGood()) return CELL_EFAULT;
switch ((u32)attr->attr_protocol)
switch (attr->attr_protocol)
{
case SYS_SYNC_PRIORITY: sys_rwlock.Warning("TODO: SYS_SYNC_PRIORITY attr"); break;
case SYS_SYNC_RETRY: sys_rwlock.Error("Invalid SYS_SYNC_RETRY attr"); break;
case SYS_SYNC_PRIORITY_INHERIT: sys_rwlock.Warning("TODO: SYS_SYNC_PRIORITY_INHERIT attr"); break;
case SYS_SYNC_FIFO: break;
case se32(SYS_SYNC_PRIORITY): sys_rwlock.Warning("TODO: SYS_SYNC_PRIORITY attr"); break;
case se32(SYS_SYNC_RETRY): sys_rwlock.Error("Invalid SYS_SYNC_RETRY attr"); break;
case se32(SYS_SYNC_PRIORITY_INHERIT): sys_rwlock.Warning("TODO: SYS_SYNC_PRIORITY_INHERIT attr"); break;
case se32(SYS_SYNC_FIFO): break;
default: return CELL_EINVAL;
}
if ((u32)attr->attr_pshared != 0x200)
if (attr->attr_pshared != se32(0x200))
{
sys_rwlock.Error("Invalid attr_pshared(0x%x)", (u32)attr->attr_pshared);
return CELL_EINVAL;

View File

@ -205,6 +205,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\Utilities\SMutex.cpp" />
<ClCompile Include="..\Utilities\Thread.cpp" />
<ClCompile Include="AppConnector.cpp" />
<ClCompile Include="Emu\Audio\AudioManager.cpp" />
@ -324,6 +325,7 @@
<ClInclude Include="..\Utilities\BEType.h" />
<ClInclude Include="..\Utilities\IdManager.h" />
<ClInclude Include="..\Utilities\MTProgressDialog.h" />
<ClInclude Include="..\Utilities\SMutex.h" />
<ClInclude Include="..\Utilities\Thread.h" />
<ClInclude Include="..\Utilities\Timer.h" />
<ClInclude Include="Emu\Audio\AudioManager.h" />

View File

@ -379,6 +379,9 @@
<ClCompile Include="Emu\FS\vfsLocalDir.cpp">
<Filter>Emu\FS</Filter>
</ClCompile>
<ClCompile Include="..\Utilities\SMutex.cpp">
<Filter>Utilities</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="rpcs3.rc" />
@ -558,5 +561,8 @@
<ClInclude Include="Emu\Audio\AudioManager.h">
<Filter>Include</Filter>
</ClInclude>
<ClInclude Include="..\Utilities\SMutex.h">
<Filter>Utilities</Filter>
</ClInclude>
</ItemGroup>
</Project>