2013-12-31 12:10:24 +01:00
|
|
|
#include "stdafx.h"
|
|
|
|
|
#include "Emu/SysCalls/SysCalls.h"
|
|
|
|
|
#include "Emu/SysCalls/SC_FUNC.h"
|
2014-01-12 11:27:59 +01:00
|
|
|
#include <mutex>
|
2013-12-31 12:10:24 +01:00
|
|
|
|
|
|
|
|
void cellSync_init();
|
2014-01-12 11:27:59 +01:00
|
|
|
void cellSync_unload();
|
|
|
|
|
Module cellSync("cellSync", cellSync_init, nullptr, cellSync_unload);
|
|
|
|
|
std::mutex g_SyncMutex;
|
2013-12-31 12:10:24 +01:00
|
|
|
|
|
|
|
|
// Return Codes
|
|
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
CELL_SYNC_ERROR_AGAIN = 0x80410101,
|
|
|
|
|
CELL_SYNC_ERROR_INVAL = 0x80410102,
|
|
|
|
|
CELL_SYNC_ERROR_NOMEM = 0x80410104,
|
|
|
|
|
CELL_SYNC_ERROR_DEADLK = 0x80410108,
|
|
|
|
|
CELL_SYNC_ERROR_PERM = 0x80410109,
|
|
|
|
|
CELL_SYNC_ERROR_BUSY = 0x8041010A,
|
|
|
|
|
CELL_SYNC_ERROR_STAT = 0x8041010F,
|
|
|
|
|
CELL_SYNC_ERROR_ALIGN = 0x80410110,
|
|
|
|
|
CELL_SYNC_ERROR_NULL_POINTER = 0x80410111,
|
|
|
|
|
CELL_SYNC_ERROR_NOT_SUPPORTED_THREAD = 0x80410112,
|
|
|
|
|
CELL_SYNC_ERROR_NO_NOTIFIER = 0x80410113,
|
|
|
|
|
CELL_SYNC_ERROR_NO_SPU_CONTEXT_STORAGE = 0x80410114,
|
|
|
|
|
};
|
|
|
|
|
|
2014-01-10 02:30:59 +01:00
|
|
|
struct CellSyncMutex {
|
|
|
|
|
be_t<u16> m_freed;
|
|
|
|
|
be_t<u16> m_order;
|
|
|
|
|
/*
|
2014-01-12 11:27:59 +01:00
|
|
|
(???) Initialize: set zeros
|
2014-01-10 02:30:59 +01:00
|
|
|
(???) Lock: increase m_order and wait until m_freed == old m_order
|
|
|
|
|
(???) Unlock: increase m_freed
|
|
|
|
|
(???) TryLock: ?????
|
|
|
|
|
*/
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int cellSyncMutexInitialize(mem_ptr_t<CellSyncMutex> mutex)
|
2013-12-31 12:10:24 +01:00
|
|
|
{
|
2014-01-07 21:27:34 +01:00
|
|
|
cellSync.Log("cellSyncMutexInitialize(mutex=0x%x)", mutex.GetAddr());
|
2014-01-10 02:30:59 +01:00
|
|
|
|
|
|
|
|
if (!mutex.IsGood())
|
2013-12-31 12:10:24 +01:00
|
|
|
{
|
|
|
|
|
return CELL_SYNC_ERROR_NULL_POINTER;
|
|
|
|
|
}
|
2014-01-10 02:30:59 +01:00
|
|
|
if (mutex.GetAddr() % 4)
|
2013-12-31 12:10:24 +01:00
|
|
|
{
|
|
|
|
|
return CELL_SYNC_ERROR_ALIGN;
|
|
|
|
|
}
|
2014-01-12 11:27:59 +01:00
|
|
|
|
|
|
|
|
{ /* global mutex */
|
|
|
|
|
std::lock_guard<std::mutex> lock(g_SyncMutex); //???
|
2014-01-10 02:30:59 +01:00
|
|
|
mutex->m_freed = 0;
|
|
|
|
|
mutex->m_order = 0;
|
2014-01-12 11:27:59 +01:00
|
|
|
return CELL_OK;
|
2014-01-10 02:30:59 +01:00
|
|
|
}
|
2013-12-31 12:10:24 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-10 02:30:59 +01:00
|
|
|
int cellSyncMutexLock(mem_ptr_t<CellSyncMutex> mutex)
|
2013-12-31 12:10:24 +01:00
|
|
|
{
|
2014-01-12 11:27:59 +01:00
|
|
|
cellSync.Log("cellSyncMutexLock(mutex=0x%x)", mutex.GetAddr());
|
|
|
|
|
|
2014-01-10 02:30:59 +01:00
|
|
|
if (!mutex.IsGood())
|
2013-12-31 12:10:24 +01:00
|
|
|
{
|
|
|
|
|
return CELL_SYNC_ERROR_NULL_POINTER;
|
|
|
|
|
}
|
2014-01-10 02:30:59 +01:00
|
|
|
if (mutex.GetAddr() % 4)
|
2013-12-31 12:10:24 +01:00
|
|
|
{
|
|
|
|
|
return CELL_SYNC_ERROR_ALIGN;
|
|
|
|
|
}
|
2014-01-10 02:30:59 +01:00
|
|
|
|
|
|
|
|
be_t<u16> old_order;
|
2014-01-12 11:27:59 +01:00
|
|
|
{ /* global mutex */
|
|
|
|
|
std::lock_guard<std::mutex> lock(g_SyncMutex);
|
2014-01-10 02:30:59 +01:00
|
|
|
old_order = mutex->m_order;
|
|
|
|
|
mutex->m_order = mutex->m_order + 1;
|
|
|
|
|
}
|
2014-01-12 11:27:59 +01:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-31 12:10:24 +01:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-10 02:30:59 +01:00
|
|
|
int cellSyncMutexTryLock(mem_ptr_t<CellSyncMutex> mutex)
|
2013-12-31 12:10:24 +01:00
|
|
|
{
|
2014-01-12 11:27:59 +01:00
|
|
|
cellSync.Log("cellSyncMutexTryLock(mutex=0x%x)", mutex.GetAddr());
|
|
|
|
|
|
2014-01-10 02:30:59 +01:00
|
|
|
if (!mutex.IsGood())
|
2013-12-31 12:10:24 +01:00
|
|
|
{
|
|
|
|
|
return CELL_SYNC_ERROR_NULL_POINTER;
|
|
|
|
|
}
|
2014-01-10 02:30:59 +01:00
|
|
|
if (mutex.GetAddr() % 4)
|
2013-12-31 12:10:24 +01:00
|
|
|
{
|
|
|
|
|
return CELL_SYNC_ERROR_ALIGN;
|
|
|
|
|
}
|
2014-01-12 11:27:59 +01:00
|
|
|
{ /* 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;
|
2013-12-31 12:10:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-10 02:30:59 +01:00
|
|
|
int cellSyncMutexUnlock(mem_ptr_t<CellSyncMutex> mutex)
|
2013-12-31 12:10:24 +01:00
|
|
|
{
|
2014-01-12 11:27:59 +01:00
|
|
|
cellSync.Log("cellSyncMutexUnlock(mutex=0x%x)", mutex.GetAddr());
|
|
|
|
|
|
2014-01-10 02:30:59 +01:00
|
|
|
if (!mutex.IsGood())
|
2013-12-31 12:10:24 +01:00
|
|
|
{
|
|
|
|
|
return CELL_SYNC_ERROR_NULL_POINTER;
|
|
|
|
|
}
|
2014-01-10 02:30:59 +01:00
|
|
|
if (mutex.GetAddr() % 4)
|
2013-12-31 12:10:24 +01:00
|
|
|
{
|
|
|
|
|
return CELL_SYNC_ERROR_ALIGN;
|
|
|
|
|
}
|
2014-01-10 02:30:59 +01:00
|
|
|
|
2014-01-12 11:27:59 +01:00
|
|
|
{ /* global mutex */
|
|
|
|
|
std::lock_guard<std::mutex> lock(g_SyncMutex);
|
|
|
|
|
mutex->m_freed = mutex->m_freed + 1;
|
|
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
2013-12-31 12:10:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void cellSync_init()
|
|
|
|
|
{
|
|
|
|
|
cellSync.AddFunc(0xa9072dee, cellSyncMutexInitialize);
|
|
|
|
|
cellSync.AddFunc(0x1bb675c2, cellSyncMutexLock);
|
|
|
|
|
cellSync.AddFunc(0xd06918c4, cellSyncMutexTryLock);
|
|
|
|
|
cellSync.AddFunc(0x91f2b7b0, cellSyncMutexUnlock);
|
2014-01-12 11:27:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void cellSync_unload()
|
|
|
|
|
{
|
|
|
|
|
g_SyncMutex.unlock();
|
2013-12-31 12:10:24 +01:00
|
|
|
}
|