2013-06-30 10:46:29 +02: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"
|
2014-08-23 16:51:51 +02:00
|
|
|
|
2016-04-14 00:23:53 +02:00
|
|
|
#include "Emu/Cell/ErrorCodes.h"
|
2014-08-23 16:51:51 +02:00
|
|
|
#include "Emu/Cell/PPUThread.h"
|
2014-06-25 00:38:34 +02:00
|
|
|
#include "sys_mutex.h"
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2016-05-13 15:55:34 +02:00
|
|
|
logs::channel sys_mutex("sys_mutex", logs::level::notice);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-07-06 01:21:15 +02:00
|
|
|
extern u64 get_system_time();
|
|
|
|
|
|
2016-04-14 00:23:53 +02:00
|
|
|
void lv2_mutex_t::unlock(lv2_lock_t)
|
2015-07-08 17:01:59 +02:00
|
|
|
{
|
|
|
|
|
owner.reset();
|
|
|
|
|
|
|
|
|
|
if (sq.size())
|
|
|
|
|
{
|
|
|
|
|
// pick new owner; protocol is ignored in current implementation
|
2016-07-27 23:43:22 +02:00
|
|
|
owner = idm::get<ppu_thread>(sq.front()->id);
|
2015-07-08 17:01:59 +02:00
|
|
|
|
2016-05-13 15:55:34 +02:00
|
|
|
VERIFY(!owner->state.test_and_set(cpu_state::signal));
|
2016-07-27 23:43:22 +02:00
|
|
|
owner->notify();
|
2015-07-08 17:01:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
s32 sys_mutex_create(vm::ptr<u32> mutex_id, vm::ptr<sys_mutex_attribute_t> attr)
|
2014-08-22 16:21:55 +02:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_mutex.warning("sys_mutex_create(mutex_id=*0x%x, attr=*0x%x)", mutex_id, attr);
|
2014-08-22 16:21:55 +02:00
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
if (!mutex_id || !attr)
|
2014-08-22 16:21:55 +02:00
|
|
|
{
|
2015-03-06 23:10:04 +01:00
|
|
|
return CELL_EFAULT;
|
2014-08-22 16:21:55 +02:00
|
|
|
}
|
|
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
const u32 protocol = attr->protocol;
|
2014-02-14 12:40:41 +01:00
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
switch (protocol)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
2015-03-06 23:10:04 +01:00
|
|
|
case SYS_SYNC_FIFO: break;
|
|
|
|
|
case SYS_SYNC_PRIORITY: break;
|
2015-03-08 03:32:41 +01:00
|
|
|
case SYS_SYNC_PRIORITY_INHERIT: break;
|
2015-07-19 03:56:33 +02:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_mutex.error("sys_mutex_create(): unknown protocol (0x%x)", protocol);
|
2015-07-19 03:56:33 +02:00
|
|
|
return CELL_EINVAL;
|
|
|
|
|
}
|
2014-02-14 12:40:41 +01:00
|
|
|
}
|
|
|
|
|
|
2015-06-24 18:25:37 +02:00
|
|
|
const bool recursive = attr->recursive == SYS_SYNC_RECURSIVE;
|
2014-02-14 12:40:41 +01:00
|
|
|
|
2015-09-15 18:23:17 +02:00
|
|
|
if ((!recursive && attr->recursive != SYS_SYNC_NOT_RECURSIVE) || attr->pshared != SYS_SYNC_NOT_PROCESS_SHARED || attr->adaptive != SYS_SYNC_NOT_ADAPTIVE || attr->ipc_key || attr->flags)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_mutex.error("sys_mutex_create(): unknown attributes (recursive=0x%x, pshared=0x%x, adaptive=0x%x, ipc_key=0x%llx, flags=0x%x)", attr->recursive, attr->pshared, attr->adaptive, attr->ipc_key, attr->flags);
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2014-02-14 12:40:41 +01:00
|
|
|
return CELL_EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
*mutex_id = idm::make<lv2_mutex_t>(recursive, protocol, attr->name_u64);
|
2014-12-28 14:15:22 +01:00
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
s32 sys_mutex_destroy(u32 mutex_id)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_mutex.warning("sys_mutex_destroy(mutex_id=0x%x)", mutex_id);
|
2014-02-14 12:40:41 +01:00
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto mutex = idm::get<lv2_mutex_t>(mutex_id);
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!mutex)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-07-12 13:52:55 +02:00
|
|
|
if (mutex->owner || mutex->sq.size())
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
|
|
|
|
return CELL_EBUSY;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
if (mutex->cond_count)
|
|
|
|
|
{
|
|
|
|
|
return CELL_EPERM;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
idm::remove<lv2_mutex_t>(mutex_id);
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-27 23:43:22 +02:00
|
|
|
s32 sys_mutex_lock(ppu_thread& ppu, u32 mutex_id, u64 timeout)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_mutex.trace("sys_mutex_lock(mutex_id=0x%x, timeout=0x%llx)", mutex_id, timeout);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-01-02 00:41:29 +01:00
|
|
|
const u64 start_time = get_system_time();
|
|
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto mutex = idm::get<lv2_mutex_t>(mutex_id);
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!mutex)
|
2014-02-03 14:12:25 +01:00
|
|
|
{
|
2014-02-14 12:40:41 +01:00
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
2014-02-03 14:12:25 +01:00
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
// check current ownership
|
|
|
|
|
if (mutex->owner.get() == &ppu)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
2015-03-06 23:10:04 +01:00
|
|
|
if (mutex->recursive)
|
2014-02-03 14:12:25 +01:00
|
|
|
{
|
2015-03-06 23:10:04 +01:00
|
|
|
if (mutex->recursive_count == 0xffffffffu)
|
2014-02-03 14:12:25 +01:00
|
|
|
{
|
2014-02-14 12:40:41 +01:00
|
|
|
return CELL_EKRESOURCE;
|
2014-02-03 14:12:25 +01:00
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2015-03-09 20:56:55 +01:00
|
|
|
mutex->recursive_count++;
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2014-02-14 12:40:41 +01:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
|
|
|
|
return CELL_EDEADLK;
|
2014-12-23 00:31:11 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
// lock immediately if not locked
|
|
|
|
|
if (!mutex->owner)
|
|
|
|
|
{
|
2016-07-27 23:43:22 +02:00
|
|
|
mutex->owner = idm::get<ppu_thread>(ppu.id);
|
2014-12-23 00:31:11 +01:00
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add waiter; protocol is ignored in current implementation
|
2016-04-14 00:23:53 +02:00
|
|
|
sleep_entry<cpu_thread> waiter(mutex->sq, ppu);
|
2015-07-08 00:33:24 +02:00
|
|
|
|
2016-04-14 00:23:53 +02:00
|
|
|
while (!ppu.state.test_and_reset(cpu_state::signal))
|
2014-02-20 03:16:17 +01:00
|
|
|
{
|
2015-07-04 01:22:24 +02:00
|
|
|
CHECK_EMU_STATUS;
|
|
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
if (timeout)
|
2014-12-23 00:31:11 +01:00
|
|
|
{
|
2015-07-08 00:33:24 +02:00
|
|
|
const u64 passed = get_system_time() - start_time;
|
2014-02-14 12:40:41 +01:00
|
|
|
|
2015-07-19 03:56:33 +02:00
|
|
|
if (passed >= timeout)
|
2015-07-08 00:33:24 +02:00
|
|
|
{
|
|
|
|
|
return CELL_ETIMEDOUT;
|
|
|
|
|
}
|
2015-07-19 03:56:33 +02:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
get_current_thread_cv().wait_for(lv2_lock, std::chrono::microseconds(timeout - passed));
|
2015-07-08 00:33:24 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-04-25 12:49:12 +02:00
|
|
|
get_current_thread_cv().wait(lv2_lock);
|
2015-07-08 00:33:24 +02:00
|
|
|
}
|
2015-01-02 00:41:29 +01:00
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
// new owner must be set when unlocked
|
|
|
|
|
if (mutex->owner.get() != &ppu)
|
|
|
|
|
{
|
2016-08-08 18:01:06 +02:00
|
|
|
fmt::throw_exception("Unexpected mutex owner" HERE);
|
2015-07-08 00:33:24 +02:00
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2014-12-23 00:31:11 +01:00
|
|
|
return CELL_OK;
|
2013-06-30 10:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
2016-07-27 23:43:22 +02:00
|
|
|
s32 sys_mutex_trylock(ppu_thread& ppu, u32 mutex_id)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_mutex.trace("sys_mutex_trylock(mutex_id=0x%x)", mutex_id);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto mutex = idm::get<lv2_mutex_t>(mutex_id);
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!mutex)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
// check current ownership
|
|
|
|
|
if (mutex->owner.get() == &ppu)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
2015-03-06 23:10:04 +01:00
|
|
|
if (mutex->recursive)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
2015-03-06 23:10:04 +01:00
|
|
|
if (mutex->recursive_count == 0xffffffffu)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
|
|
|
|
return CELL_EKRESOURCE;
|
|
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2015-03-09 20:56:55 +01:00
|
|
|
mutex->recursive_count++;
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2014-02-14 12:40:41 +01:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
|
|
|
|
return CELL_EDEADLK;
|
2014-02-14 12:40:41 +01:00
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
if (mutex->owner)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
2015-03-06 23:10:04 +01:00
|
|
|
return CELL_EBUSY;
|
2014-02-14 12:40:41 +01:00
|
|
|
}
|
2014-12-23 00:31:11 +01:00
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
// own the mutex if free
|
2016-07-27 23:43:22 +02:00
|
|
|
mutex->owner = idm::get<ppu_thread>(ppu.id);
|
2015-03-06 23:10:04 +01:00
|
|
|
|
|
|
|
|
return CELL_OK;
|
2013-06-30 10:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
2016-07-27 23:43:22 +02:00
|
|
|
s32 sys_mutex_unlock(ppu_thread& ppu, u32 mutex_id)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_mutex.trace("sys_mutex_unlock(mutex_id=0x%x)", mutex_id);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto mutex = idm::get<lv2_mutex_t>(mutex_id);
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!mutex)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
// check current ownership
|
|
|
|
|
if (mutex->owner.get() != &ppu)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
2014-12-23 00:31:11 +01:00
|
|
|
return CELL_EPERM;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-09 20:56:55 +01:00
|
|
|
if (mutex->recursive_count)
|
2014-12-23 00:31:11 +01:00
|
|
|
{
|
2015-03-09 20:56:55 +01:00
|
|
|
if (!mutex->recursive)
|
|
|
|
|
{
|
2016-08-08 18:01:06 +02:00
|
|
|
fmt::throw_exception("Unexpected recursive_count" HERE);
|
2015-03-09 20:56:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mutex->recursive_count--;
|
2014-12-23 00:31:11 +01:00
|
|
|
}
|
2015-03-09 20:56:55 +01:00
|
|
|
else
|
2014-12-23 00:31:11 +01:00
|
|
|
{
|
2015-07-08 17:01:59 +02:00
|
|
|
mutex->unlock(lv2_lock);
|
2014-02-14 12:40:41 +01:00
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2014-12-23 00:31:11 +01:00
|
|
|
return CELL_OK;
|
2013-06-30 10:46:29 +02:00
|
|
|
}
|