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

275 lines
5 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-08-19 23:14:10 +02:00
namespace vm { using namespace ps3; }
2017-05-13 20:30:37 +02:00
logs::channel sys_semaphore("sys_semaphore");
extern u64 get_system_time();
2017-01-31 00:09:55 +01:00
error_code 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;
2017-02-06 19:36:46 +01:00
if (protocol == SYS_SYNC_PRIORITY_INHERIT)
sys_semaphore.todo("sys_semaphore_create(): SYS_SYNC_PRIORITY_INHERIT");
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
}
2017-01-31 00:09:55 +01:00
if (const u32 id = idm::make<lv2_obj, lv2_sema>(protocol, attr->name_u64, max_val, initial_val))
{
*sem_id = id;
return CELL_OK;
}
2015-03-08 04:37:07 +01:00
2017-01-31 00:09:55 +01:00
return CELL_EAGAIN;
}
2017-01-31 00:09:55 +01:00
error_code sys_semaphore_destroy(u32 sem_id)
{
sys_semaphore.warning("sys_semaphore_destroy(sem_id=0x%x)", sem_id);
2017-01-31 00:09:55 +01:00
const auto sem = idm::withdraw<lv2_obj, lv2_sema>(sem_id, [](lv2_sema& sema) -> CellError
{
if (sema.val < 0)
{
return CELL_EBUSY;
}
2015-03-08 04:37:07 +01:00
2017-01-31 00:09:55 +01:00
return {};
});
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 (sem.ret)
2014-06-21 16:24:27 +02:00
{
return sem.ret;
2014-06-21 16:24:27 +02:00
}
return CELL_OK;
}
2017-01-31 00:09:55 +01:00
error_code 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);
2017-01-31 00:09:55 +01:00
const auto sem = idm::get<lv2_obj, lv2_sema>(sem_id, [&](lv2_sema& sema)
{
const s32 val = sema.val;
if (val > 0)
{
if (sema.val.compare_and_swap_test(val, val - 1))
{
return true;
}
}
semaphore_lock lock(sema.mutex);
2015-03-08 04:37:07 +01:00
2017-01-31 00:09:55 +01:00
if (sema.val-- <= 0)
{
sema.sq.emplace_back(&ppu);
sema.sleep(ppu, timeout);
2017-01-31 00:09:55 +01:00
return false;
}
return true;
});
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 (sem.ret)
2015-07-21 13:55:29 +02:00
{
return CELL_OK;
}
2017-02-06 19:36:46 +01:00
ppu.gpr[3] = CELL_OK;
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-21 13:55:29 +02:00
if (timeout)
2014-06-21 16:24:27 +02:00
{
const u64 passed = get_system_time() - ppu.start_time;
2014-06-21 16:24:27 +02:00
2015-07-21 13:55:29 +02:00
if (passed >= timeout)
{
2017-01-31 00:09:55 +01:00
semaphore_lock lock(sem->mutex);
const s32 val = sem->val.fetch_op([](s32& val)
{
if (val < 0)
{
val++;
}
});
if (val >= 0)
{
timeout = 0;
continue;
}
verify(HERE), sem->unqueue(sem->sq, &ppu);
2017-02-06 19:36:46 +01:00
ppu.gpr[3] = CELL_ETIMEDOUT;
break;
2015-07-21 13:55:29 +02:00
}
2017-01-31 00:09:55 +01:00
thread_ctrl::wait_for(timeout - passed);
2015-07-21 13:55:29 +02:00
}
else
{
2017-01-31 00:09:55 +01:00
thread_ctrl::wait();
2015-07-21 13:55:29 +02:00
}
}
2015-03-08 04:37:07 +01:00
2017-02-06 19:36:46 +01:00
return not_an_error(ppu.gpr[3]);
}
2017-01-31 00:09:55 +01:00
error_code sys_semaphore_trywait(u32 sem_id)
{
sys_semaphore.trace("sys_semaphore_trywait(sem_id=0x%x)", sem_id);
2017-01-31 00:09:55 +01:00
const auto sem = idm::check<lv2_obj, lv2_sema>(sem_id, [&](lv2_sema& sema)
{
const s32 val = sema.val;
if (val > 0)
{
if (sema.val.compare_and_swap_test(val, val - 1))
{
return true;
}
}
2015-03-08 04:37:07 +01:00
2017-01-31 00:09:55 +01:00
return false;
});
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 (!sem.ret)
2014-06-21 16:24:27 +02:00
{
2017-01-31 00:09:55 +01:00
return not_an_error(CELL_EBUSY);
2014-06-21 16:24:27 +02:00
}
2015-03-08 04:37:07 +01:00
return CELL_OK;
}
2017-02-06 19:36:46 +01:00
error_code sys_semaphore_post(ppu_thread& ppu, 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
2017-01-31 00:09:55 +01:00
if (count < 0)
{
return CELL_EINVAL;
}
const auto sem = idm::get<lv2_obj, lv2_sema>(sem_id, [&](lv2_sema& sema)
{
const s32 val = sema.val;
if (val >= 0 && count <= sema.max - val)
{
if (sema.val.compare_and_swap_test(val, val + count))
{
return true;
}
}
2017-01-31 00:09:55 +01:00
return false;
});
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 (sem.ret)
2014-06-21 16:24:27 +02:00
{
2017-01-31 00:09:55 +01:00
return CELL_OK;
2014-06-21 16:24:27 +02:00
}
2017-01-31 00:09:55 +01:00
else
{
2017-01-31 00:09:55 +01:00
semaphore_lock lock(sem->mutex);
2014-06-21 16:24:27 +02:00
2017-01-31 00:09:55 +01:00
const s32 val = sem->val.fetch_op([=](s32& val)
{
if (val + s64{count} <= sem->max)
{
val += count;
}
});
2015-07-21 13:55:29 +02:00
2017-01-31 00:09:55 +01:00
if (val + s64{count} > sem->max)
{
return not_an_error(CELL_EBUSY);
}
2015-07-21 13:55:29 +02:00
2017-01-31 00:09:55 +01:00
// Wake threads
for (s32 i = std::min<s32>(-std::min<s32>(val, 0), count); i > 0; i--)
{
2017-03-11 00:14:48 +01:00
sem->awake(*verify(HERE, sem->schedule<ppu_thread>(sem->sq, sem->protocol)));
2017-01-31 00:09:55 +01:00
}
}
2015-07-21 13:55:29 +02:00
return CELL_OK;
}
2017-01-31 00:09:55 +01:00
error_code 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
2014-12-24 00:38:13 +01:00
if (!count)
{
return CELL_EFAULT;
}
2017-01-31 00:09:55 +01:00
if (!idm::check<lv2_obj, lv2_sema>(sem_id, [=](lv2_sema& sema)
{
*count = std::max<s32>(0, sema.val);
}))
2014-06-21 16:24:27 +02:00
{
return CELL_ESRCH;
}
return CELL_OK;
}