rpcsx/rpcs3/Emu/Cell/lv2/sys_semaphore.cpp

209 lines
3.9 KiB
C++
Raw Normal View History

#include "stdafx.h"
#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-07-11 13:59:13 +02:00
#include "Emu/Cell/PPUThread.h"
2014-09-19 02:19:22 +02:00
#include "sys_semaphore.h"
2016-05-13 15:55:34 +02:00
logs::channel sys_semaphore("sys_semaphore", logs::level::notice);
extern u64 get_system_time();
2015-07-21 13:55:29 +02:00
s32 sys_semaphore_create(vm::ptr<u32> sem_id, vm::ptr<sys_semaphore_attribute_t> attr, s32 initial_val, s32 max_val)
{
sys_semaphore.warning("sys_semaphore_create(sem_id=*0x%x, attr=*0x%x, initial_val=%d, max_val=%d)", sem_id, attr, initial_val, max_val);
2015-07-21 13:55:29 +02:00
if (!sem_id || !attr)
2014-12-28 14:15:22 +01:00
{
return CELL_EFAULT;
}
2015-03-08 04:37:07 +01:00
if (max_val <= 0 || initial_val > max_val || initial_val < 0)
2014-12-28 14:15:22 +01:00
{
sys_semaphore.error("sys_semaphore_create(): invalid parameters (initial_val=%d, max_val=%d)", initial_val, max_val);
2014-06-21 16:24:27 +02:00
return CELL_EINVAL;
}
2015-03-08 04:37:07 +01:00
const u32 protocol = attr->protocol;
2015-07-21 13:55:29 +02:00
if (protocol != SYS_SYNC_FIFO && protocol != SYS_SYNC_PRIORITY && protocol != SYS_SYNC_PRIORITY_INHERIT)
2014-06-21 16:24:27 +02:00
{
sys_semaphore.error("sys_semaphore_create(): unknown protocol (0x%x)", protocol);
2015-07-21 13:55:29 +02:00
return CELL_EINVAL;
}
2015-09-15 18:23:17 +02:00
if (attr->pshared != SYS_SYNC_NOT_PROCESS_SHARED || attr->ipc_key || attr->flags)
{
sys_semaphore.error("sys_semaphore_create(): unknown attributes (pshared=0x%x, ipc_key=0x%x, flags=0x%x)", attr->pshared, attr->ipc_key, attr->flags);
return CELL_EINVAL;
2014-06-21 16:24:27 +02:00
}
*sem_id = idm::make<lv2_sema_t>(protocol, max_val, attr->name_u64, initial_val);
2015-03-08 04:37:07 +01:00
return CELL_OK;
}
2015-07-21 13:55:29 +02:00
s32 sys_semaphore_destroy(u32 sem_id)
{
sys_semaphore.warning("sys_semaphore_destroy(sem_id=0x%x)", sem_id);
2015-03-08 04:37:07 +01:00
LV2_LOCK;
const auto sem = idm::get<lv2_sema_t>(sem_id);
2015-03-11 16:30:50 +01:00
2015-07-21 13:55:29 +02:00
if (!sem)
2014-06-21 16:24:27 +02:00
{
return CELL_ESRCH;
}
2015-07-21 13:55:29 +02:00
if (sem->sq.size())
2014-06-21 16:24:27 +02:00
{
return CELL_EBUSY;
}
idm::remove<lv2_sema_t>(sem_id);
2015-03-08 04:37:07 +01:00
return CELL_OK;
}
s32 sys_semaphore_wait(ppu_thread& ppu, u32 sem_id, u64 timeout)
{
sys_semaphore.trace("sys_semaphore_wait(sem_id=0x%x, timeout=0x%llx)", sem_id, timeout);
const u64 start_time = get_system_time();
2015-03-08 04:37:07 +01:00
LV2_LOCK;
const auto sem = idm::get<lv2_sema_t>(sem_id);
2015-03-11 16:30:50 +01:00
2015-07-21 13:55:29 +02:00
if (!sem)
2014-06-21 16:24:27 +02:00
{
return CELL_ESRCH;
}
2015-07-21 13:55:29 +02:00
if (sem->value > 0)
{
sem->value--;
return CELL_OK;
}
2015-07-21 13:55:29 +02:00
// add waiter; protocol is ignored in current implementation
2016-04-14 00:23:53 +02:00
sleep_entry<cpu_thread> waiter(sem->sq, ppu);
2015-07-21 13:55:29 +02:00
while (!ppu.state.test_and_reset(cpu_flag::signal))
2014-06-21 16:24:27 +02:00
{
2015-07-04 01:22:24 +02:00
CHECK_EMU_STATUS;
2015-07-21 13:55:29 +02:00
if (timeout)
2014-06-21 16:24:27 +02:00
{
2015-07-21 13:55:29 +02:00
const u64 passed = get_system_time() - start_time;
2014-06-21 16:24:27 +02:00
2015-07-21 13:55:29 +02:00
if (passed >= timeout)
{
return CELL_ETIMEDOUT;
}
get_current_thread_cv().wait_for(lv2_lock, std::chrono::microseconds(timeout - passed));
2015-07-21 13:55:29 +02:00
}
else
{
get_current_thread_cv().wait(lv2_lock);
2015-07-21 13:55:29 +02:00
}
}
2015-03-08 04:37:07 +01:00
return CELL_OK;
}
2015-07-21 13:55:29 +02:00
s32 sys_semaphore_trywait(u32 sem_id)
{
sys_semaphore.trace("sys_semaphore_trywait(sem_id=0x%x)", sem_id);
2015-03-08 04:37:07 +01:00
LV2_LOCK;
const auto sem = idm::get<lv2_sema_t>(sem_id);
2015-03-11 16:30:50 +01:00
2015-07-21 13:55:29 +02:00
if (!sem)
2014-06-21 16:24:27 +02:00
{
return CELL_ESRCH;
}
2015-07-21 13:55:29 +02:00
if (sem->value <= 0 || sem->sq.size())
2014-06-21 16:24:27 +02:00
{
return CELL_EBUSY;
}
2015-03-08 04:37:07 +01:00
2015-07-21 13:55:29 +02:00
sem->value--;
2015-03-08 04:37:07 +01:00
return CELL_OK;
}
2015-07-21 13:55:29 +02:00
s32 sys_semaphore_post(u32 sem_id, s32 count)
{
sys_semaphore.trace("sys_semaphore_post(sem_id=0x%x, count=%d)", sem_id, count);
2015-03-08 04:37:07 +01:00
LV2_LOCK;
const auto sem = idm::get<lv2_sema_t>(sem_id);
2015-03-11 16:30:50 +01:00
2015-07-21 13:55:29 +02:00
if (!sem)
2014-06-21 16:24:27 +02:00
{
return CELL_ESRCH;
}
if (count < 0)
{
return CELL_EINVAL;
}
2015-07-21 13:55:29 +02:00
// get comparable values considering waiting threads
const u64 new_value = sem->value + count;
const u64 max_value = sem->max + sem->sq.size();
2015-03-11 16:30:50 +01:00
if (new_value > max_value)
{
return CELL_EBUSY;
2014-06-21 16:24:27 +02:00
}
2015-07-21 13:55:29 +02:00
// wakeup as much threads as possible
while (count && !sem->sq.empty())
2015-03-11 16:30:50 +01:00
{
2015-07-21 13:55:29 +02:00
count--;
2016-08-15 02:11:49 +02:00
const auto thread = sem->sq.front();
thread->set_signal();
2015-07-21 13:55:29 +02:00
sem->sq.pop_front();
2015-03-11 16:30:50 +01:00
}
2015-07-21 13:55:29 +02:00
// add the rest to the value
sem->value += count;
return CELL_OK;
}
2015-07-21 13:55:29 +02:00
s32 sys_semaphore_get_value(u32 sem_id, vm::ptr<s32> count)
{
sys_semaphore.trace("sys_semaphore_get_value(sem_id=0x%x, count=*0x%x)", sem_id, count);
2015-07-21 13:55:29 +02:00
LV2_LOCK;
2014-12-24 00:38:13 +01:00
if (!count)
{
return CELL_EFAULT;
}
const auto sem = idm::get<lv2_sema_t>(sem_id);
2015-03-11 16:30:50 +01:00
2015-07-21 13:55:29 +02:00
if (!sem)
2014-06-21 16:24:27 +02:00
{
return CELL_ESRCH;
}
2015-07-21 13:55:29 +02:00
*count = sem->value;
2015-03-08 04:37:07 +01:00
return CELL_OK;
}