CellSync review (mutexes, SPU).

File descriptors temporarily detached from ID manager.
Some logs disabled.
This commit is contained in:
Nekotekina 2014-01-12 14:27:59 +04:00
parent 4e8cd72e59
commit 970b834f2e
6 changed files with 138 additions and 87 deletions

View file

@ -1,10 +1,12 @@
#include "stdafx.h"
#include "Emu/SysCalls/SysCalls.h"
#include "Emu/SysCalls/SC_FUNC.h"
#include <mutex>
void cellSync_init();
Module cellSync("cellSync", cellSync_init);
wxMutex g_SyncMutex;
void cellSync_unload();
Module cellSync("cellSync", cellSync_init, nullptr, cellSync_unload);
std::mutex g_SyncMutex;
// Return Codes
enum
@ -27,6 +29,7 @@ struct CellSyncMutex {
be_t<u16> m_freed;
be_t<u16> m_order;
/*
(???) Initialize: set zeros
(???) Lock: increase m_order and wait until m_freed == old m_order
(???) Unlock: increase m_freed
(???) TryLock: ?????
@ -45,16 +48,19 @@ int cellSyncMutexInitialize(mem_ptr_t<CellSyncMutex> mutex)
{
return CELL_SYNC_ERROR_ALIGN;
}
{
wxMutexLocker lock(g_SyncMutex);
{ /* global mutex */
std::lock_guard<std::mutex> lock(g_SyncMutex); //???
mutex->m_freed = 0;
mutex->m_order = 0;
return CELL_OK;
}
return CELL_OK;
}
int cellSyncMutexLock(mem_ptr_t<CellSyncMutex> mutex)
{
cellSync.Log("cellSyncMutexLock(mutex=0x%x)", mutex.GetAddr());
if (!mutex.IsGood())
{
return CELL_SYNC_ERROR_NULL_POINTER;
@ -65,18 +71,31 @@ int cellSyncMutexLock(mem_ptr_t<CellSyncMutex> mutex)
}
be_t<u16> old_order;
{
wxMutexLocker lock(g_SyncMutex);
cellSync.Log("cellSyncMutexLock(mutex=0x%x[order=%d,freed=%d])", mutex.GetAddr(), (u16)mutex->m_order, (u16)mutex->m_freed);
{ /* global mutex */
std::lock_guard<std::mutex> lock(g_SyncMutex);
old_order = mutex->m_order;
mutex->m_order = mutex->m_order + 1;
}
while (old_order != mutex->m_freed) Sleep(1);
int counter = 0;
while (old_order != mutex->m_freed)
{
Sleep(1);
if (++counter >= 5000)
{
Emu.Pause();
cellSync.Error("cellSyncMutexLock(mutex=0x%x, old_order=%d, order=%d, freed=%d): TIMEOUT",
mutex.GetAddr(), (u16)old_order, (u16)mutex->m_order, (u16)mutex->m_freed);
break;
}
}
return CELL_OK;
}
int cellSyncMutexTryLock(mem_ptr_t<CellSyncMutex> mutex)
{
cellSync.Log("cellSyncMutexTryLock(mutex=0x%x)", mutex.GetAddr());
if (!mutex.IsGood())
{
return CELL_SYNC_ERROR_NULL_POINTER;
@ -85,19 +104,21 @@ int cellSyncMutexTryLock(mem_ptr_t<CellSyncMutex> mutex)
{
return CELL_SYNC_ERROR_ALIGN;
}
wxMutexLocker lock(g_SyncMutex);
cellSync.Log("cellSyncMutexTryLock(mutex=0x%x[order=%d,freed=%d])", mutex.GetAddr(), (u16)mutex->m_order, (u16)mutex->m_freed);
if (mutex->m_order != mutex->m_freed)
{
return CELL_SYNC_ERROR_BUSY;
{ /* global mutex */
std::lock_guard<std::mutex> lock(g_SyncMutex);
if (mutex->m_order != mutex->m_freed)
{
return CELL_SYNC_ERROR_BUSY;
}
mutex->m_order = mutex->m_order + 1;
return CELL_OK;
}
mutex->m_order = mutex->m_order + 1;
return CELL_OK;
}
int cellSyncMutexUnlock(mem_ptr_t<CellSyncMutex> mutex)
{
cellSync.Log("cellSyncMutexUnlock(mutex=0x%x)", mutex.GetAddr());
if (!mutex.IsGood())
{
return CELL_SYNC_ERROR_NULL_POINTER;
@ -107,10 +128,11 @@ int cellSyncMutexUnlock(mem_ptr_t<CellSyncMutex> mutex)
return CELL_SYNC_ERROR_ALIGN;
}
wxMutexLocker lock(g_SyncMutex);
cellSync.Log("cellSyncMutexUnlock(mutex=0x%x[order=%d,freed=%d])", mutex.GetAddr(), (u16)mutex->m_order, (u16)mutex->m_freed);
mutex->m_freed = mutex->m_freed + 1;
return CELL_OK;
{ /* global mutex */
std::lock_guard<std::mutex> lock(g_SyncMutex);
mutex->m_freed = mutex->m_freed + 1;
return CELL_OK;
}
}
void cellSync_init()
@ -119,4 +141,9 @@ void cellSync_init()
cellSync.AddFunc(0x1bb675c2, cellSyncMutexLock);
cellSync.AddFunc(0xd06918c4, cellSyncMutexTryLock);
cellSync.AddFunc(0x91f2b7b0, cellSyncMutexUnlock);
}
void cellSync_unload()
{
g_SyncMutex.unlock();
}