2019-10-14 23:36:48 +03:00
|
|
|
|
#include "stdafx.h"
|
2018-09-29 01:12:00 +03:00
|
|
|
|
#include "sys_cond.h"
|
|
|
|
|
|
|
2015-03-07 01:58:42 +03:00
|
|
|
|
#include "Emu/IdManager.h"
|
2017-07-24 18:59:48 +03:00
|
|
|
|
#include "Emu/IPC.h"
|
2014-08-23 18:51:51 +04:00
|
|
|
|
|
2016-04-14 01:23:53 +03:00
|
|
|
|
#include "Emu/Cell/ErrorCodes.h"
|
2014-08-23 18:51:51 +04:00
|
|
|
|
#include "Emu/Cell/PPUThread.h"
|
2013-06-30 11:46:29 +03:00
|
|
|
|
|
2018-08-25 15:39:00 +03:00
|
|
|
|
LOG_CHANNEL(sys_cond);
|
2013-06-30 11:46:29 +03:00
|
|
|
|
|
2017-07-24 18:59:48 +03:00
|
|
|
|
template<> DECLARE(ipc_manager<lv2_cond, u64>::g_ipc) {};
|
|
|
|
|
|
|
2019-06-09 01:38:01 +03:00
|
|
|
|
error_code sys_cond_create(ppu_thread& ppu, vm::ptr<u32> cond_id, u32 mutex_id, vm::ptr<sys_cond_attribute_t> attr)
|
2013-06-30 11:46:29 +03:00
|
|
|
|
{
|
2020-06-05 12:36:28 +03:00
|
|
|
|
ppu.state += cpu_flag::wait;
|
2019-06-09 01:38:01 +03:00
|
|
|
|
|
2016-01-13 00:57:16 +03:00
|
|
|
|
sys_cond.warning("sys_cond_create(cond_id=*0x%x, mutex_id=0x%x, attr=*0x%x)", cond_id, mutex_id, attr);
|
2014-09-20 04:08:12 +04:00
|
|
|
|
|
2017-02-02 20:47:25 +03:00
|
|
|
|
auto mutex = idm::get<lv2_obj, lv2_mutex>(mutex_id);
|
|
|
|
|
|
|
|
|
|
|
|
if (!mutex)
|
2014-02-13 20:59:13 +04:00
|
|
|
|
{
|
2017-02-02 20:47:25 +03:00
|
|
|
|
return CELL_ESRCH;
|
2014-02-13 20:59:13 +04:00
|
|
|
|
}
|
2013-06-30 11:46:29 +03:00
|
|
|
|
|
2020-04-02 22:18:23 +03:00
|
|
|
|
const auto _attr = *attr;
|
|
|
|
|
|
|
|
|
|
|
|
if (auto error = lv2_obj::create<lv2_cond>(_attr.pshared, _attr.ipc_key, _attr.flags, [&]
|
2017-07-24 18:59:48 +03:00
|
|
|
|
{
|
|
|
|
|
|
return std::make_shared<lv2_cond>(
|
2020-04-02 22:18:23 +03:00
|
|
|
|
_attr.pshared,
|
|
|
|
|
|
_attr.flags,
|
|
|
|
|
|
_attr.ipc_key,
|
|
|
|
|
|
_attr.name_u64,
|
2020-06-09 18:35:14 +03:00
|
|
|
|
mutex_id,
|
2017-07-24 18:59:48 +03:00
|
|
|
|
std::move(mutex));
|
|
|
|
|
|
}))
|
2017-02-02 20:47:25 +03:00
|
|
|
|
{
|
2017-07-24 18:59:48 +03:00
|
|
|
|
return error;
|
2017-02-02 20:47:25 +03:00
|
|
|
|
}
|
2013-06-30 11:46:29 +03:00
|
|
|
|
|
2017-07-24 18:59:48 +03:00
|
|
|
|
*cond_id = idm::last_id();
|
|
|
|
|
|
return CELL_OK;
|
2013-06-30 11:46:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-09 01:38:01 +03:00
|
|
|
|
error_code sys_cond_destroy(ppu_thread& ppu, u32 cond_id)
|
2013-06-30 11:46:29 +03:00
|
|
|
|
{
|
2020-06-05 12:36:28 +03:00
|
|
|
|
ppu.state += cpu_flag::wait;
|
2019-06-09 01:38:01 +03:00
|
|
|
|
|
2016-01-13 00:57:16 +03:00
|
|
|
|
sys_cond.warning("sys_cond_destroy(cond_id=0x%x)", cond_id);
|
2013-06-30 11:46:29 +03:00
|
|
|
|
|
2017-02-02 20:47:25 +03:00
|
|
|
|
const auto cond = idm::withdraw<lv2_obj, lv2_cond>(cond_id, [&](lv2_cond& cond) -> CellError
|
|
|
|
|
|
{
|
2019-10-03 23:05:34 +03:00
|
|
|
|
std::lock_guard lock(cond.mutex->mutex);
|
|
|
|
|
|
|
2017-02-02 20:47:25 +03:00
|
|
|
|
if (cond.waiters)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CELL_EBUSY;
|
|
|
|
|
|
}
|
2015-03-07 01:10:04 +03:00
|
|
|
|
|
2020-06-04 06:37:25 +03:00
|
|
|
|
cond.mutex->obj_count.atomic_op([](typename lv2_mutex::count_info& info){ info.cond_count--; });
|
2017-02-02 20:47:25 +03:00
|
|
|
|
return {};
|
|
|
|
|
|
});
|
2015-03-07 01:10:04 +03:00
|
|
|
|
|
2015-04-12 04:36:25 +03:00
|
|
|
|
if (!cond)
|
2014-02-13 20:59:13 +04:00
|
|
|
|
{
|
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-02 20:47:25 +03:00
|
|
|
|
if (cond.ret)
|
2014-02-13 20:59:13 +04:00
|
|
|
|
{
|
2017-02-02 20:47:25 +03:00
|
|
|
|
return cond.ret;
|
2014-02-13 20:59:13 +04:00
|
|
|
|
}
|
2013-06-30 11:46:29 +03:00
|
|
|
|
|
|
|
|
|
|
return CELL_OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-06 21:36:46 +03:00
|
|
|
|
error_code sys_cond_signal(ppu_thread& ppu, u32 cond_id)
|
2013-06-30 11:46:29 +03:00
|
|
|
|
{
|
2020-06-05 12:36:28 +03:00
|
|
|
|
ppu.state += cpu_flag::wait;
|
2019-06-09 01:38:01 +03:00
|
|
|
|
|
2016-01-13 00:57:16 +03:00
|
|
|
|
sys_cond.trace("sys_cond_signal(cond_id=0x%x)", cond_id);
|
2013-06-30 11:46:29 +03:00
|
|
|
|
|
2019-08-28 01:22:31 +03:00
|
|
|
|
const auto cond = idm::check<lv2_obj, lv2_cond>(cond_id, [](lv2_cond& cond)
|
2017-02-02 20:47:25 +03:00
|
|
|
|
{
|
|
|
|
|
|
if (cond.waiters)
|
|
|
|
|
|
{
|
2018-09-03 22:28:33 +03:00
|
|
|
|
std::lock_guard lock(cond.mutex->mutex);
|
2017-02-02 20:47:25 +03:00
|
|
|
|
|
|
|
|
|
|
if (const auto cpu = cond.schedule<ppu_thread>(cond.sq, cond.mutex->protocol))
|
|
|
|
|
|
{
|
2019-10-03 23:05:34 +03:00
|
|
|
|
// TODO: Is EBUSY returned after reqeueing, on sys_cond_destroy?
|
|
|
|
|
|
cond.waiters--;
|
|
|
|
|
|
|
|
|
|
|
|
if (cond.mutex->try_own(*cpu, cpu->id))
|
|
|
|
|
|
{
|
|
|
|
|
|
cond.awake(cpu);
|
|
|
|
|
|
}
|
2017-02-02 20:47:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2015-03-07 01:10:04 +03:00
|
|
|
|
|
2015-04-12 04:36:25 +03:00
|
|
|
|
if (!cond)
|
2014-02-14 15:40:41 +04:00
|
|
|
|
{
|
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-02-26 14:35:30 +04:00
|
|
|
|
return CELL_OK;
|
2013-06-30 11:46:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-06 21:36:46 +03:00
|
|
|
|
error_code sys_cond_signal_all(ppu_thread& ppu, u32 cond_id)
|
2013-06-30 11:46:29 +03:00
|
|
|
|
{
|
2020-06-05 12:36:28 +03:00
|
|
|
|
ppu.state += cpu_flag::wait;
|
2019-06-09 01:38:01 +03:00
|
|
|
|
|
2016-01-13 00:57:16 +03:00
|
|
|
|
sys_cond.trace("sys_cond_signal_all(cond_id=0x%x)", cond_id);
|
2013-06-30 11:46:29 +03:00
|
|
|
|
|
2017-02-02 20:47:25 +03:00
|
|
|
|
const auto cond = idm::check<lv2_obj, lv2_cond>(cond_id, [](lv2_cond& cond)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (cond.waiters)
|
|
|
|
|
|
{
|
2018-09-03 22:28:33 +03:00
|
|
|
|
std::lock_guard lock(cond.mutex->mutex);
|
2017-02-02 20:47:25 +03:00
|
|
|
|
|
2019-10-03 23:05:34 +03:00
|
|
|
|
cpu_thread* result = nullptr;
|
|
|
|
|
|
cond.waiters -= ::size32(cond.sq);
|
|
|
|
|
|
|
2020-05-02 11:01:19 +03:00
|
|
|
|
while (const auto cpu = cond.schedule<ppu_thread>(cond.sq, SYS_SYNC_PRIORITY))
|
2017-02-02 20:47:25 +03:00
|
|
|
|
{
|
2019-10-03 23:05:34 +03:00
|
|
|
|
if (cond.mutex->try_own(*cpu, cpu->id))
|
|
|
|
|
|
{
|
|
|
|
|
|
verify(HERE), !std::exchange(result, cpu);
|
|
|
|
|
|
}
|
2017-02-02 20:47:25 +03:00
|
|
|
|
}
|
2015-03-07 01:10:04 +03:00
|
|
|
|
|
2019-10-03 23:05:34 +03:00
|
|
|
|
if (result)
|
2019-08-28 01:22:31 +03:00
|
|
|
|
{
|
2019-10-03 23:05:34 +03:00
|
|
|
|
lv2_obj::awake(result);
|
2019-08-28 01:22:31 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-02-02 20:47:25 +03:00
|
|
|
|
});
|
2015-03-07 01:10:04 +03:00
|
|
|
|
|
2015-04-12 04:36:25 +03:00
|
|
|
|
if (!cond)
|
2014-02-14 15:40:41 +04:00
|
|
|
|
{
|
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
|
}
|
2013-06-30 11:46:29 +03:00
|
|
|
|
|
|
|
|
|
|
return CELL_OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-06 21:36:46 +03:00
|
|
|
|
error_code sys_cond_signal_to(ppu_thread& ppu, u32 cond_id, u32 thread_id)
|
2013-06-30 11:46:29 +03:00
|
|
|
|
{
|
2020-06-05 12:36:28 +03:00
|
|
|
|
ppu.state += cpu_flag::wait;
|
2019-06-09 01:38:01 +03:00
|
|
|
|
|
2016-01-13 00:57:16 +03:00
|
|
|
|
sys_cond.trace("sys_cond_signal_to(cond_id=0x%x, thread_id=0x%x)", cond_id, thread_id);
|
2015-03-07 01:10:04 +03:00
|
|
|
|
|
2019-08-28 01:22:31 +03:00
|
|
|
|
const auto cond = idm::check<lv2_obj, lv2_cond>(cond_id, [&](lv2_cond& cond) -> int
|
2017-02-02 20:47:25 +03:00
|
|
|
|
{
|
2020-03-23 13:22:54 +02:00
|
|
|
|
if (const auto cpu = idm::check_unlocked<named_thread<ppu_thread>>(thread_id);
|
|
|
|
|
|
!cpu || cpu->joiner == ppu_join_status::exited)
|
2019-05-03 10:15:31 +03:00
|
|
|
|
{
|
2019-08-28 01:22:31 +03:00
|
|
|
|
return -1;
|
2019-05-03 10:15:31 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-02 20:47:25 +03:00
|
|
|
|
if (cond.waiters)
|
|
|
|
|
|
{
|
2018-09-03 22:28:33 +03:00
|
|
|
|
std::lock_guard lock(cond.mutex->mutex);
|
2017-02-02 20:47:25 +03:00
|
|
|
|
|
|
|
|
|
|
for (auto cpu : cond.sq)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (cpu->id == thread_id)
|
|
|
|
|
|
{
|
2019-08-27 15:01:08 +03:00
|
|
|
|
verify(HERE), cond.unqueue(cond.sq, cpu);
|
2019-10-03 23:05:34 +03:00
|
|
|
|
|
|
|
|
|
|
cond.waiters--;
|
|
|
|
|
|
|
|
|
|
|
|
if (cond.mutex->try_own(*cpu, cpu->id))
|
|
|
|
|
|
{
|
|
|
|
|
|
cond.awake(cpu);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-08-28 01:22:31 +03:00
|
|
|
|
return 1;
|
2017-02-02 20:47:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-08-28 01:22:31 +03:00
|
|
|
|
return 0;
|
2017-02-02 20:47:25 +03:00
|
|
|
|
});
|
2014-02-14 15:40:41 +04:00
|
|
|
|
|
2019-08-28 01:22:31 +03:00
|
|
|
|
if (!cond || cond.ret == -1)
|
2014-02-14 15:40:41 +04:00
|
|
|
|
{
|
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
|
}
|
2013-06-30 11:46:29 +03:00
|
|
|
|
|
2019-08-28 01:22:31 +03:00
|
|
|
|
if (!cond.ret)
|
2014-02-26 14:35:30 +04:00
|
|
|
|
{
|
2017-02-02 20:47:25 +03:00
|
|
|
|
return not_an_error(CELL_EPERM);
|
2014-02-26 14:35:30 +04:00
|
|
|
|
}
|
2015-03-07 01:10:04 +03:00
|
|
|
|
|
2015-07-26 14:21:25 +03:00
|
|
|
|
return CELL_OK;
|
2014-02-13 20:59:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-02 20:47:25 +03:00
|
|
|
|
error_code sys_cond_wait(ppu_thread& ppu, u32 cond_id, u64 timeout)
|
2014-02-13 20:59:13 +04:00
|
|
|
|
{
|
2020-06-05 12:36:28 +03:00
|
|
|
|
ppu.state += cpu_flag::wait;
|
2019-06-09 01:38:01 +03:00
|
|
|
|
|
2016-01-13 00:57:16 +03:00
|
|
|
|
sys_cond.trace("sys_cond_wait(cond_id=0x%x, timeout=%lld)", cond_id, timeout);
|
2014-02-14 15:40:41 +04:00
|
|
|
|
|
2020-06-04 13:27:25 +03:00
|
|
|
|
// Further function result
|
|
|
|
|
|
ppu.gpr[3] = CELL_OK;
|
|
|
|
|
|
|
|
|
|
|
|
const auto cond = idm::get<lv2_obj, lv2_cond>(cond_id, [&](lv2_cond& cond) -> s64
|
2017-02-02 20:47:25 +03:00
|
|
|
|
{
|
2020-06-04 13:27:25 +03:00
|
|
|
|
if (cond.mutex->owner >> 1 != ppu.id)
|
2019-10-03 23:05:34 +03:00
|
|
|
|
{
|
2020-06-04 13:27:25 +03:00
|
|
|
|
return -1;
|
2019-10-03 23:05:34 +03:00
|
|
|
|
}
|
2015-03-07 01:10:04 +03:00
|
|
|
|
|
2020-06-04 13:27:25 +03:00
|
|
|
|
std::lock_guard lock(cond.mutex->mutex);
|
|
|
|
|
|
|
|
|
|
|
|
// Register waiter
|
|
|
|
|
|
cond.sq.emplace_back(&ppu);
|
|
|
|
|
|
cond.waiters++;
|
|
|
|
|
|
|
|
|
|
|
|
// Unlock the mutex
|
|
|
|
|
|
const auto count = cond.mutex->lock_count.exchange(0);
|
|
|
|
|
|
|
|
|
|
|
|
if (auto cpu = cond.mutex->reown<ppu_thread>())
|
|
|
|
|
|
{
|
|
|
|
|
|
cond.mutex->append(cpu);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Sleep current thread and schedule mutex waiter
|
|
|
|
|
|
cond.sleep(ppu, timeout);
|
|
|
|
|
|
|
2017-02-02 20:47:25 +03:00
|
|
|
|
// Save the recursive value
|
2020-06-04 13:27:25 +03:00
|
|
|
|
return count;
|
2017-02-02 20:47:25 +03:00
|
|
|
|
});
|
2015-03-07 01:10:04 +03:00
|
|
|
|
|
2015-04-12 04:36:25 +03:00
|
|
|
|
if (!cond)
|
2014-02-14 15:40:41 +04:00
|
|
|
|
{
|
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-04 13:27:25 +03:00
|
|
|
|
if (cond.ret < 0)
|
2014-02-14 15:40:41 +04:00
|
|
|
|
{
|
2014-02-26 14:35:30 +04:00
|
|
|
|
return CELL_EPERM;
|
2014-02-14 15:40:41 +04:00
|
|
|
|
}
|
2015-07-08 01:33:24 +03:00
|
|
|
|
|
2016-08-09 17:14:41 +03:00
|
|
|
|
while (!ppu.state.test_and_reset(cpu_flag::signal))
|
2015-07-19 04:56:33 +03:00
|
|
|
|
{
|
2018-10-11 01:17:19 +03:00
|
|
|
|
if (ppu.is_stopped())
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-02 20:47:25 +03:00
|
|
|
|
if (timeout)
|
2015-07-08 01:33:24 +03:00
|
|
|
|
{
|
2019-07-14 06:55:11 +03:00
|
|
|
|
if (lv2_obj::wait_timeout(timeout, &ppu))
|
2015-07-19 04:56:33 +03:00
|
|
|
|
{
|
2019-10-03 23:05:34 +03:00
|
|
|
|
// Wait for rescheduling
|
|
|
|
|
|
if (ppu.check_state())
|
|
|
|
|
|
{
|
2019-10-05 06:05:55 +03:00
|
|
|
|
continue;
|
2019-10-03 23:05:34 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-03 22:28:33 +03:00
|
|
|
|
std::lock_guard lock(cond->mutex->mutex);
|
2018-02-09 17:49:37 +03:00
|
|
|
|
|
2017-02-02 20:47:25 +03:00
|
|
|
|
// Try to cancel the waiting
|
|
|
|
|
|
if (cond->unqueue(cond->sq, &ppu))
|
2015-07-08 01:33:24 +03:00
|
|
|
|
{
|
2019-10-03 23:05:34 +03:00
|
|
|
|
// TODO: Is EBUSY returned after reqeueing, on sys_cond_destroy?
|
|
|
|
|
|
cond->waiters--;
|
|
|
|
|
|
|
2017-02-02 20:47:25 +03:00
|
|
|
|
ppu.gpr[3] = CELL_ETIMEDOUT;
|
2019-10-03 23:05:34 +03:00
|
|
|
|
|
|
|
|
|
|
// Own or requeue
|
2019-10-14 23:36:48 +03:00
|
|
|
|
if (cond->mutex->try_own(ppu, ppu.id))
|
2019-10-03 23:05:34 +03:00
|
|
|
|
{
|
2019-10-14 23:36:48 +03:00
|
|
|
|
break;
|
2019-10-03 23:05:34 +03:00
|
|
|
|
}
|
2015-07-08 01:33:24 +03:00
|
|
|
|
}
|
2019-10-14 23:36:48 +03:00
|
|
|
|
else if (cond->mutex->owner >> 1 == ppu.id)
|
|
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2015-07-19 04:56:33 +03:00
|
|
|
|
|
2019-10-14 23:36:48 +03:00
|
|
|
|
cond->mutex->sleep(ppu);
|
|
|
|
|
|
timeout = 0;
|
|
|
|
|
|
continue;
|
2015-04-12 04:36:25 +03:00
|
|
|
|
}
|
2015-07-19 04:56:33 +03:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2017-02-02 20:47:25 +03:00
|
|
|
|
thread_ctrl::wait();
|
2014-02-26 14:35:30 +04:00
|
|
|
|
}
|
2015-01-02 02:41:29 +03:00
|
|
|
|
}
|
2015-03-07 01:10:04 +03:00
|
|
|
|
|
2017-02-02 20:47:25 +03:00
|
|
|
|
// Verify ownership
|
|
|
|
|
|
verify(HERE), cond->mutex->owner >> 1 == ppu.id;
|
2015-07-08 01:33:24 +03:00
|
|
|
|
|
2017-02-02 20:47:25 +03:00
|
|
|
|
// Restore the recursive value
|
2020-08-17 17:22:35 +03:00
|
|
|
|
cond->mutex->lock_count.release(static_cast<u32>(cond.ret));
|
2015-07-08 01:33:24 +03:00
|
|
|
|
|
2017-02-22 13:10:55 +03:00
|
|
|
|
return not_an_error(ppu.gpr[3]);
|
2014-06-17 17:44:03 +02:00
|
|
|
|
}
|