rpcsx/rpcs3/Emu/SysCalls/lv2/sys_mutex.cpp

261 lines
5.6 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "Emu/Memory/Memory.h"
#include "Emu/System.h"
#include "Emu/SysCalls/SysCalls.h"
2014-12-22 01:56:04 +01:00
#include "Emu/Memory/atomic_type.h"
2014-08-23 16:51:51 +02:00
2014-08-26 01:55:37 +02:00
#include "Emu/CPU/CPUThreadManager.h"
2014-08-23 16:51:51 +02:00
#include "Emu/Cell/PPUThread.h"
2014-12-23 00:31:11 +01:00
#include "sleep_queue_type.h"
#include "sys_time.h"
#include "sys_mutex.h"
2014-08-23 16:51:51 +02:00
SysCallBase sys_mutex("sys_mutex");
2014-08-22 16:21:55 +02:00
Mutex::~Mutex()
{
2014-12-23 00:31:11 +01:00
if (u32 tid = owner.read_sync())
2014-08-22 16:21:55 +02:00
{
2014-12-23 00:31:11 +01:00
sys_mutex.Notice("Mutex(%d) was owned by thread %d (recursive=%d)", id, tid, recursive);
2014-08-22 16:21:55 +02:00
}
2014-12-23 00:31:11 +01:00
if (!queue.m_mutex.try_lock()) return;
2014-08-22 16:21:55 +02:00
2014-12-23 00:31:11 +01:00
for (u32 i = 0; i < queue.list.size(); i++)
2014-08-22 16:21:55 +02:00
{
2014-12-23 00:31:11 +01:00
if (u32 owner = queue.list[i])
{
sys_mutex.Notice("Mutex(%d) was waited by thread %d", id, owner);
}
2014-08-22 16:21:55 +02:00
}
2014-12-23 00:31:11 +01:00
queue.m_mutex.unlock();
2014-08-22 16:21:55 +02:00
}
2014-12-23 00:31:11 +01:00
s32 sys_mutex_create(PPUThread& CPU, vm::ptr<u32> mutex_id, vm::ptr<sys_mutex_attribute> attr)
{
2014-09-02 03:05:13 +02:00
sys_mutex.Log("sys_mutex_create(mutex_id_addr=0x%x, attr_addr=0x%x)", mutex_id.addr(), attr.addr());
2014-09-20 02:08:12 +02:00
LV2_LOCK(0);
switch (attr->protocol.ToBE())
{
case se32(SYS_SYNC_FIFO): break;
case se32(SYS_SYNC_PRIORITY): break;
2014-08-23 16:51:51 +02:00
case se32(SYS_SYNC_PRIORITY_INHERIT): sys_mutex.Todo("sys_mutex_create(): SYS_SYNC_PRIORITY_INHERIT"); break;
case se32(SYS_SYNC_RETRY): sys_mutex.Error("sys_mutex_create(): SYS_SYNC_RETRY"); return CELL_EINVAL;
default: sys_mutex.Error("Unknown protocol attribute(0x%x)", (u32)attr->protocol); return CELL_EINVAL;
}
bool is_recursive;
switch (attr->recursive.ToBE())
{
case se32(SYS_SYNC_RECURSIVE): is_recursive = true; break;
case se32(SYS_SYNC_NOT_RECURSIVE): is_recursive = false; break;
2014-08-23 16:51:51 +02:00
default: sys_mutex.Error("Unknown recursive attribute(0x%x)", (u32)attr->recursive); return CELL_EINVAL;
}
if (attr->pshared.ToBE() != se32(0x200))
{
2014-08-23 16:51:51 +02:00
sys_mutex.Error("Unknown pshared attribute(0x%x)", (u32)attr->pshared);
return CELL_EINVAL;
}
2014-03-06 12:40:50 +01:00
Mutex* mutex = new Mutex((u32)attr->protocol, is_recursive, attr->name_u64);
2014-12-23 00:31:11 +01:00
const u32 id = sys_mutex.GetNewId(mutex, TYPE_MUTEX);
mutex->id.exchange(id);
2014-09-01 02:51:48 +02:00
*mutex_id = id;
2014-08-23 16:51:51 +02:00
sys_mutex.Warning("*** mutex created [%s] (protocol=0x%x, recursive=%s): id = %d",
2014-09-01 02:51:48 +02:00
std::string(attr->name, 8).c_str(), (u32) attr->protocol, (is_recursive ? "true" : "false"), id);
2014-09-13 22:40:12 +02:00
Emu.GetSyncPrimManager().AddSyncPrimData(TYPE_MUTEX, id, std::string(attr->name, 8));
2014-02-16 16:23:58 +01:00
// TODO: unlock mutex when owner thread does exit
return CELL_OK;
}
2014-12-23 00:31:11 +01:00
s32 sys_mutex_destroy(PPUThread& CPU, u32 mutex_id)
{
2014-08-23 16:51:51 +02:00
sys_mutex.Warning("sys_mutex_destroy(mutex_id=%d)", mutex_id);
2014-09-20 02:08:12 +02:00
LV2_LOCK(0);
Mutex* mutex;
if (!Emu.GetIdManager().GetIDData(mutex_id, mutex))
{
return CELL_ESRCH;
}
2014-03-19 01:32:23 +01:00
if (mutex->cond_count) // check if associated condition variable exists
{
return CELL_EPERM;
}
2014-12-23 00:31:11 +01:00
const u32 tid = CPU.GetId();
2014-12-23 00:31:11 +01:00
if (mutex->owner.compare_and_swap_test(0, tid)) // check if locked
{
return CELL_EBUSY;
}
2014-12-23 00:31:11 +01:00
if (!mutex->queue.finalize())
{
2014-12-23 00:31:11 +01:00
if (!mutex->owner.compare_and_swap_test(tid, 0))
{
assert(!"sys_mutex_destroy() failed (busy)");
}
return CELL_EBUSY;
}
2014-12-23 00:31:11 +01:00
if (!mutex->owner.compare_and_swap_test(tid, ~0))
{
assert(!"sys_mutex_destroy() failed");
}
Emu.GetIdManager().RemoveID(mutex_id);
2014-09-13 22:40:12 +02:00
Emu.GetSyncPrimManager().EraseSyncPrimData(TYPE_MUTEX, mutex_id);
return CELL_OK;
}
2014-12-23 00:31:11 +01:00
s32 sys_mutex_lock(PPUThread& CPU, u32 mutex_id, u64 timeout)
{
2014-08-23 16:51:51 +02:00
sys_mutex.Log("sys_mutex_lock(mutex_id=%d, timeout=%lld)", mutex_id, timeout);
Mutex* mutex;
if (!Emu.GetIdManager().GetIDData(mutex_id, mutex))
{
return CELL_ESRCH;
}
2014-12-23 00:31:11 +01:00
const u32 tid = CPU.GetId();
2014-12-23 00:31:11 +01:00
if (mutex->owner.read_sync() == tid)
{
if (mutex->is_recursive)
{
2014-12-23 00:31:11 +01:00
if (!++mutex->recursive)
{
return CELL_EKRESOURCE;
}
return CELL_OK;
}
else
{
return CELL_EDEADLK;
}
}
2014-12-23 00:31:11 +01:00
if (mutex->owner.compare_and_swap_test(0, tid))
{
mutex->recursive = 1;
CPU.owned_mutexes++;
return CELL_OK;
}
mutex->queue.push(tid, mutex->protocol);
const u64 time_start = get_system_time();
while (true)
{
2014-12-23 00:31:11 +01:00
auto old_owner = mutex->owner.compare_and_swap(0, tid);
if (!old_owner)
{
2014-12-23 00:31:11 +01:00
mutex->queue.invalidate(tid);
break;
}
2014-12-23 00:31:11 +01:00
if (old_owner == tid)
{
2014-12-23 00:31:11 +01:00
break;
}
2014-12-23 00:31:11 +01:00
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // hack
2014-12-23 00:31:11 +01:00
if (timeout && get_system_time() - time_start > timeout)
{
mutex->queue.invalidate(tid);
return CELL_ETIMEDOUT;
}
2014-12-23 00:31:11 +01:00
if (Emu.IsStopped())
{
sys_mutex.Warning("sys_mutex_lock(id=%d) aborted", mutex_id);
return CELL_OK;
}
}
2014-12-23 00:31:11 +01:00
mutex->recursive = 1;
CPU.owned_mutexes++;
return CELL_OK;
}
2014-12-23 00:31:11 +01:00
s32 sys_mutex_trylock(PPUThread& CPU, u32 mutex_id)
{
2014-08-23 16:51:51 +02:00
sys_mutex.Log("sys_mutex_trylock(mutex_id=%d)", mutex_id);
Mutex* mutex;
if (!Emu.GetIdManager().GetIDData(mutex_id, mutex))
{
return CELL_ESRCH;
}
2014-12-23 00:31:11 +01:00
const u32 tid = CPU.GetId();
2014-12-23 00:31:11 +01:00
if (mutex->owner.read_sync() == tid)
{
if (mutex->is_recursive)
{
2014-12-23 00:31:11 +01:00
if (!++mutex->recursive)
{
return CELL_EKRESOURCE;
}
return CELL_OK;
}
else
{
return CELL_EDEADLK;
}
}
2014-12-23 00:31:11 +01:00
if (!mutex->owner.compare_and_swap_test(0, tid))
{
2014-12-23 00:31:11 +01:00
return CELL_EBUSY;
}
2014-12-23 00:31:11 +01:00
mutex->recursive = 1;
CPU.owned_mutexes++;
return CELL_OK;
}
2014-12-23 00:31:11 +01:00
s32 sys_mutex_unlock(PPUThread& CPU, u32 mutex_id)
{
2014-08-23 16:51:51 +02:00
sys_mutex.Log("sys_mutex_unlock(mutex_id=%d)", mutex_id);
Mutex* mutex;
if (!Emu.GetIdManager().GetIDData(mutex_id, mutex))
{
return CELL_ESRCH;
}
2014-12-23 00:31:11 +01:00
const u32 tid = CPU.GetId();
2014-12-23 00:31:11 +01:00
if (mutex->owner.read_sync() != tid)
{
2014-12-23 00:31:11 +01:00
return CELL_EPERM;
}
if (!mutex->recursive || (mutex->recursive != 1 && !mutex->is_recursive))
{
sys_mutex.Error("sys_mutex_unlock(%d): wrong recursive value fixed (%d)", mutex_id, mutex->recursive);
mutex->recursive = 1;
}
if (!--mutex->recursive)
{
if (!mutex->owner.compare_and_swap_test(tid, mutex->queue.pop(mutex->protocol)))
{
2014-12-23 00:31:11 +01:00
assert(!"sys_mutex_unlock() failed");
}
2014-12-23 00:31:11 +01:00
CPU.owned_mutexes--;
}
2014-12-23 00:31:11 +01:00
return CELL_OK;
}