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

339 lines
5.8 KiB
C++
Raw Normal View History

2020-12-05 13:08:24 +01:00
#include "stdafx.h"
#include "sys_semaphore.h"
2015-03-06 23:58:42 +01:00
#include "Emu/IdManager.h"
2017-07-24 17:59:48 +02:00
#include "Emu/IPC.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"
LOG_CHANNEL(sys_semaphore);
lv2_sema::lv2_sema(utils::serial& ar)
: protocol(ar)
, key(ar)
, name(ar)
, max(ar)
{
ar(val);
}
std::shared_ptr<void> lv2_sema::load(utils::serial& ar)
{
auto sema = std::make_shared<lv2_sema>(ar);
return lv2_obj::load(sema->key, sema);
}
void lv2_sema::save(utils::serial& ar)
{
USING_SERIALIZATION_VERSION(lv2_sync);
ar(protocol, key, name, max, std::max<s32>(+val, 0));
}
error_code sys_semaphore_create(ppu_thread& ppu, vm::ptr<u32> sem_id, vm::ptr<sys_semaphore_attribute_t> attr, s32 initial_val, s32 max_val)
{
ppu.state += cpu_flag::wait;
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;
}
const auto _attr = *attr;
const u32 protocol = _attr.protocol;
2015-03-08 04:37:07 +01:00
if (protocol != SYS_SYNC_FIFO && protocol != SYS_SYNC_PRIORITY)
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;
}
const u64 ipc_key = lv2_obj::get_key(_attr);
if (auto error = lv2_obj::create<lv2_sema>(_attr.pshared, ipc_key, _attr.flags, [&]
{
return std::make_shared<lv2_sema>(protocol, ipc_key, _attr.name_u64, max_val, initial_val);
2017-07-24 17:59:48 +02:00
}))
2017-01-31 00:09:55 +01:00
{
2017-07-24 17:59:48 +02:00
return error;
2017-01-31 00:09:55 +01:00
}
2015-03-08 04:37:07 +01:00
static_cast<void>(ppu.test_stopped());
2017-07-24 17:59:48 +02:00
*sem_id = idm::last_id();
return CELL_OK;
}
error_code sys_semaphore_destroy(ppu_thread& ppu, u32 sem_id)
{
ppu.state += cpu_flag::wait;
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
lv2_obj::on_id_destroy(sema, sema.key);
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;
}
2018-02-09 15:49:37 +01:00
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)
{
ppu.state += cpu_flag::wait;
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;
2018-02-09 15:49:37 +01:00
2017-01-31 00:09:55 +01:00
if (val > 0)
{
if (sema.val.compare_and_swap_test(val, val - 1))
{
return true;
}
}
lv2_obj::notify_all_t notify;
std::lock_guard 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, true);
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 (auto state = ppu.state.fetch_sub(cpu_flag::signal))
2014-06-21 16:24:27 +02:00
{
if (state & cpu_flag::signal)
{
break;
}
if (is_stopped(state))
{
std::lock_guard lock(sem->mutex);
if (std::find(sem->sq.begin(), sem->sq.end(), &ppu) == sem->sq.end())
{
break;
}
ppu.state += cpu_flag::again;
return {};
}
2015-07-21 13:55:29 +02:00
if (timeout)
2014-06-21 16:24:27 +02:00
{
2019-07-14 05:55:11 +02:00
if (lv2_obj::wait_timeout(timeout, &ppu))
2015-07-21 13:55:29 +02:00
{
// Wait for rescheduling
if (ppu.check_state())
{
continue;
}
std::lock_guard lock(sem->mutex);
2017-01-31 00:09:55 +01:00
2017-07-26 21:35:45 +02:00
if (!sem->unqueue(sem->sq, &ppu))
{
break;
2017-07-26 21:35:45 +02:00
}
ensure(0 > sem->val.fetch_op([](s32& val)
2017-01-31 00:09:55 +01:00
{
if (val < 0)
{
val++;
}
}));
2017-01-31 00:09:55 +01:00
2017-02-06 19:36:46 +01:00
ppu.gpr[3] = CELL_ETIMEDOUT;
break;
2015-07-21 13:55:29 +02:00
}
}
else
{
thread_ctrl::wait_on(ppu.state, state);
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]);
}
error_code sys_semaphore_trywait(ppu_thread& ppu, u32 sem_id)
{
ppu.state += cpu_flag::wait;
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)
{
return sema.val.try_dec(0);
2017-01-31 00:09:55 +01:00
});
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)
{
ppu.state += cpu_flag::wait;
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
const auto sem = idm::get<lv2_obj, lv2_sema>(sem_id, [&](lv2_sema& sema)
{
const s32 val = sema.val;
2018-12-26 15:28:26 +01:00
if (val >= 0 && count > 0 && count <= sema.max - val)
2017-01-31 00:09:55 +01:00
{
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;
}
2018-12-26 15:28:26 +01:00
if (count <= 0)
{
return CELL_EINVAL;
}
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
{
std::lock_guard lock(sem->mutex);
2014-06-21 16:24:27 +02:00
for (auto cpu : sem->sq)
{
if (static_cast<ppu_thread*>(cpu)->state & cpu_flag::again)
{
ppu.state += cpu_flag::again;
return {};
}
}
const auto [val, ok] = sem->val.fetch_op([&](s32& val)
2017-01-31 00:09:55 +01:00
{
if (count + 0u <= sem->max + 0u - val)
2017-01-31 00:09:55 +01:00
{
val += count;
return true;
2017-01-31 00:09:55 +01:00
}
return false;
2017-01-31 00:09:55 +01:00
});
2015-07-21 13:55:29 +02:00
if (!ok)
2017-01-31 00:09:55 +01:00
{
return not_an_error(CELL_EBUSY);
}
2015-07-21 13:55:29 +02:00
2017-01-31 00:09:55 +01:00
// Wake threads
const s32 to_awake = std::min<s32>(-std::min<s32>(val, 0), count);
for (s32 i = 0; i < to_awake; i++)
2017-01-31 00:09:55 +01:00
{
sem->append((ensure(sem->schedule<ppu_thread>(sem->sq, sem->protocol))));
2017-01-31 00:09:55 +01:00
}
if (to_awake > 0)
{
lv2_obj::awake_all();
}
2017-01-31 00:09:55 +01:00
}
2015-07-21 13:55:29 +02:00
return CELL_OK;
}
error_code sys_semaphore_get_value(ppu_thread& ppu, u32 sem_id, vm::ptr<s32> count)
{
ppu.state += cpu_flag::wait;
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
const auto sema = idm::check<lv2_obj, lv2_sema>(sem_id, [](lv2_sema& sema)
2017-01-31 00:09:55 +01:00
{
return std::max<s32>(0, sema.val);
});
if (!sema)
2014-06-21 16:24:27 +02:00
{
return CELL_ESRCH;
}
if (!count)
{
return CELL_EFAULT;
}
static_cast<void>(ppu.test_stopped());
*count = sema.ret;
return CELL_OK;
}