2012-11-15 00:39:56 +01:00
|
|
|
#include "stdafx.h"
|
2014-06-02 19:27:24 +02:00
|
|
|
#include "Emu/Memory/Memory.h"
|
|
|
|
|
#include "Emu/System.h"
|
2015-03-06 23:58:42 +01:00
|
|
|
#include "Emu/IdManager.h"
|
2012-11-15 00:39:56 +01:00
|
|
|
#include "Emu/SysCalls/SysCalls.h"
|
2014-08-23 16:51:51 +02:00
|
|
|
|
|
|
|
|
#include "Emu/Cell/PPUThread.h"
|
2015-03-02 22:09:20 +01:00
|
|
|
#include "sleep_queue.h"
|
2014-06-25 00:38:34 +02:00
|
|
|
#include "sys_lwmutex.h"
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2014-08-23 16:51:51 +02:00
|
|
|
SysCallBase sys_lwmutex("sys_lwmutex");
|
|
|
|
|
|
2015-07-06 01:21:15 +02:00
|
|
|
extern u64 get_system_time();
|
|
|
|
|
|
2015-03-08 22:56:45 +01:00
|
|
|
s32 _sys_lwmutex_create(vm::ptr<u32> lwmutex_id, u32 protocol, vm::ptr<sys_lwmutex_t> control, u32 arg4, u64 name, u32 arg6)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2015-03-09 02:30:34 +01:00
|
|
|
sys_lwmutex.Warning("_sys_lwmutex_create(lwmutex_id=*0x%x, protocol=0x%x, control=*0x%x, arg4=0x%x, name=0x%llx, arg6=0x%x)", lwmutex_id, protocol, control, arg4, name, arg6);
|
|
|
|
|
|
2015-07-19 23:29:40 +02:00
|
|
|
if (protocol != SYS_SYNC_FIFO && protocol != SYS_SYNC_RETRY && protocol != SYS_SYNC_PRIORITY)
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
2015-07-19 23:29:40 +02:00
|
|
|
sys_lwmutex.Error("_sys_lwmutex_create(): unknown protocol (0x%x)", protocol);
|
|
|
|
|
return CELL_EINVAL;
|
2015-03-09 02:30:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (arg4 != 0x80000001 || arg6)
|
|
|
|
|
{
|
2015-07-19 23:29:40 +02:00
|
|
|
throw EXCEPTION("Unknown arguments (arg4=0x%x, arg6=0x%x)", arg4, arg6);
|
2015-03-09 02:30:34 +01:00
|
|
|
}
|
|
|
|
|
|
2015-05-27 05:11:59 +02:00
|
|
|
*lwmutex_id = Emu.GetIdManager().make<lv2_lwmutex_t>(protocol, name);
|
2015-03-09 02:30:34 +01:00
|
|
|
|
|
|
|
|
return CELL_OK;
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2015-03-09 02:30:34 +01:00
|
|
|
s32 _sys_lwmutex_destroy(u32 lwmutex_id)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2015-04-14 04:00:31 +02:00
|
|
|
sys_lwmutex.Warning("_sys_lwmutex_destroy(lwmutex_id=0x%x)", lwmutex_id);
|
2015-03-09 02:30:34 +01:00
|
|
|
|
|
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-05-27 05:11:59 +02:00
|
|
|
const auto mutex = Emu.GetIdManager().get<lv2_lwmutex_t>(lwmutex_id);
|
2015-03-11 16:30:50 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!mutex)
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mutex->waiters)
|
|
|
|
|
{
|
|
|
|
|
return CELL_EBUSY;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-27 05:11:59 +02:00
|
|
|
Emu.GetIdManager().remove<lv2_lwmutex_t>(lwmutex_id);
|
2015-03-09 02:30:34 +01:00
|
|
|
|
|
|
|
|
return CELL_OK;
|
2014-02-12 20:03:14 +01:00
|
|
|
}
|
|
|
|
|
|
2015-03-09 02:30:34 +01:00
|
|
|
s32 _sys_lwmutex_lock(u32 lwmutex_id, u64 timeout)
|
2014-02-12 20:03:14 +01:00
|
|
|
{
|
2015-04-14 04:00:31 +02:00
|
|
|
sys_lwmutex.Log("_sys_lwmutex_lock(lwmutex_id=0x%x, timeout=0x%llx)", lwmutex_id, timeout);
|
2015-03-09 02:30:34 +01:00
|
|
|
|
|
|
|
|
const u64 start_time = get_system_time();
|
|
|
|
|
|
|
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-05-27 05:11:59 +02:00
|
|
|
const auto mutex = Emu.GetIdManager().get<lv2_lwmutex_t>(lwmutex_id);
|
2015-03-11 16:30:50 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!mutex)
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// protocol is ignored in current implementation
|
2015-03-11 16:30:50 +01:00
|
|
|
mutex->waiters++;
|
2015-03-09 02:30:34 +01:00
|
|
|
|
2015-03-09 20:56:55 +01:00
|
|
|
while (!mutex->signaled)
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
2015-07-04 01:22:24 +02:00
|
|
|
CHECK_EMU_STATUS;
|
|
|
|
|
|
2015-03-09 02:30:34 +01:00
|
|
|
if (timeout && get_system_time() - start_time > timeout)
|
|
|
|
|
{
|
2015-03-11 16:30:50 +01:00
|
|
|
mutex->waiters--;
|
2015-03-09 02:30:34 +01:00
|
|
|
return CELL_ETIMEDOUT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mutex->cv.wait_for(lv2_lock, std::chrono::milliseconds(1));
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-09 20:56:55 +01:00
|
|
|
mutex->signaled--;
|
2015-03-09 02:30:34 +01:00
|
|
|
|
2015-03-11 16:30:50 +01:00
|
|
|
mutex->waiters--;
|
2015-03-09 02:30:34 +01:00
|
|
|
|
|
|
|
|
return CELL_OK;
|
2014-02-12 20:03:14 +01:00
|
|
|
}
|
|
|
|
|
|
2015-03-09 02:30:34 +01:00
|
|
|
s32 _sys_lwmutex_trylock(u32 lwmutex_id)
|
2014-02-12 20:03:14 +01:00
|
|
|
{
|
2015-04-14 04:00:31 +02:00
|
|
|
sys_lwmutex.Log("_sys_lwmutex_trylock(lwmutex_id=0x%x)", lwmutex_id);
|
2015-03-09 02:30:34 +01:00
|
|
|
|
|
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-05-27 05:11:59 +02:00
|
|
|
const auto mutex = Emu.GetIdManager().get<lv2_lwmutex_t>(lwmutex_id);
|
2015-03-11 16:30:50 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!mutex)
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-09 20:56:55 +01:00
|
|
|
if (mutex->waiters || !mutex->signaled)
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
|
|
|
|
return CELL_EBUSY;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-09 20:56:55 +01:00
|
|
|
mutex->signaled--;
|
2015-03-09 02:30:34 +01:00
|
|
|
|
|
|
|
|
return CELL_OK;
|
2014-02-12 20:03:14 +01:00
|
|
|
}
|
|
|
|
|
|
2015-03-09 02:30:34 +01:00
|
|
|
s32 _sys_lwmutex_unlock(u32 lwmutex_id)
|
2014-02-12 20:03:14 +01:00
|
|
|
{
|
2015-04-14 04:00:31 +02:00
|
|
|
sys_lwmutex.Log("_sys_lwmutex_unlock(lwmutex_id=0x%x)", lwmutex_id);
|
2015-03-09 02:30:34 +01:00
|
|
|
|
|
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-05-27 05:11:59 +02:00
|
|
|
const auto mutex = Emu.GetIdManager().get<lv2_lwmutex_t>(lwmutex_id);
|
2015-03-11 16:30:50 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!mutex)
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 11:39:54 +01:00
|
|
|
if (mutex->signaled)
|
|
|
|
|
{
|
2015-07-10 16:45:16 +02:00
|
|
|
throw EXCEPTION("Already signaled (lwmutex_id=0x%x)", lwmutex_id);
|
2015-03-11 11:39:54 +01:00
|
|
|
}
|
|
|
|
|
|
2015-03-09 20:56:55 +01:00
|
|
|
mutex->signaled++;
|
2015-03-11 16:30:50 +01:00
|
|
|
|
|
|
|
|
if (mutex->waiters)
|
|
|
|
|
{
|
|
|
|
|
mutex->cv.notify_one();
|
|
|
|
|
}
|
2015-03-09 02:30:34 +01:00
|
|
|
|
|
|
|
|
return CELL_OK;
|
2014-02-23 17:52:52 +01:00
|
|
|
}
|