2020-12-05 13:08:24 +01:00
|
|
|
#include "stdafx.h"
|
2018-09-29 00:12:00 +02:00
|
|
|
#include "sys_mutex.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-08-23 16:51:51 +02:00
|
|
|
#include "Emu/Cell/PPUThread.h"
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2018-08-25 14:39:00 +02:00
|
|
|
LOG_CHANNEL(sys_mutex);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2019-06-09 01:03:24 +02:00
|
|
|
error_code sys_mutex_create(ppu_thread& ppu, vm::ptr<u32> mutex_id, vm::ptr<sys_mutex_attribute_t> attr)
|
2014-08-22 16:21:55 +02:00
|
|
|
{
|
2020-06-05 11:36:28 +02:00
|
|
|
ppu.state += cpu_flag::wait;
|
2019-06-09 01:03:24 +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
|
|
|
}
|
|
|
|
|
|
2020-04-02 21:18:23 +02:00
|
|
|
const auto _attr = *attr;
|
|
|
|
|
|
|
|
|
|
switch (_attr.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;
|
2017-02-06 19:36:46 +01:00
|
|
|
case SYS_SYNC_PRIORITY_INHERIT:
|
2019-08-17 20:28:22 +02:00
|
|
|
sys_mutex.warning("sys_mutex_create(): SYS_SYNC_PRIORITY_INHERIT");
|
2017-02-06 19:36:46 +01:00
|
|
|
break;
|
2015-07-19 03:56:33 +02:00
|
|
|
default:
|
|
|
|
|
{
|
2020-04-02 21:18:23 +02:00
|
|
|
sys_mutex.error("sys_mutex_create(): unknown protocol (0x%x)", _attr.protocol);
|
2015-07-19 03:56:33 +02:00
|
|
|
return CELL_EINVAL;
|
|
|
|
|
}
|
2014-02-14 12:40:41 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-02 21:18:23 +02:00
|
|
|
switch (_attr.recursive)
|
2017-02-02 18:47:25 +01:00
|
|
|
{
|
|
|
|
|
case SYS_SYNC_RECURSIVE: break;
|
2018-02-09 15:49:37 +01:00
|
|
|
case SYS_SYNC_NOT_RECURSIVE: break;
|
2017-02-02 18:47:25 +01:00
|
|
|
default:
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
2020-04-02 21:18:23 +02:00
|
|
|
sys_mutex.error("sys_mutex_create(): unknown recursive (0x%x)", _attr.recursive);
|
2017-02-02 18:47:25 +01:00
|
|
|
return CELL_EINVAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2020-04-02 21:18:23 +02:00
|
|
|
if (_attr.adaptive != SYS_SYNC_NOT_ADAPTIVE)
|
2017-02-02 18:47:25 +01:00
|
|
|
{
|
2020-04-02 21:18:23 +02:00
|
|
|
sys_mutex.todo("sys_mutex_create(): unexpected adaptive (0x%x)", _attr.adaptive);
|
2014-02-14 12:40:41 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-02 21:18:23 +02:00
|
|
|
if (auto error = lv2_obj::create<lv2_mutex>(_attr.pshared, _attr.ipc_key, _attr.flags, [&]()
|
2017-07-24 17:59:48 +02:00
|
|
|
{
|
|
|
|
|
return std::make_shared<lv2_mutex>(
|
2020-04-02 21:18:23 +02:00
|
|
|
_attr.protocol,
|
|
|
|
|
_attr.recursive,
|
|
|
|
|
_attr.adaptive,
|
|
|
|
|
_attr.ipc_key,
|
|
|
|
|
_attr.name_u64);
|
2017-07-24 17:59:48 +02:00
|
|
|
}))
|
2017-02-02 18:47:25 +01:00
|
|
|
{
|
2017-07-24 17:59:48 +02:00
|
|
|
return error;
|
2017-02-02 18:47:25 +01:00
|
|
|
}
|
2014-12-28 14:15:22 +01:00
|
|
|
|
2017-07-24 17:59:48 +02:00
|
|
|
*mutex_id = idm::last_id();
|
|
|
|
|
return CELL_OK;
|
2013-06-30 10:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-09 01:03:24 +02:00
|
|
|
error_code sys_mutex_destroy(ppu_thread& ppu, u32 mutex_id)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2020-06-05 11:36:28 +02:00
|
|
|
ppu.state += cpu_flag::wait;
|
2019-06-09 01:03:24 +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
|
|
|
|
2017-02-02 18:47:25 +01:00
|
|
|
const auto mutex = idm::withdraw<lv2_obj, lv2_mutex>(mutex_id, [](lv2_mutex& mutex) -> CellError
|
|
|
|
|
{
|
2018-12-30 15:39:40 +01:00
|
|
|
std::lock_guard lock(mutex.mutex);
|
|
|
|
|
|
2017-02-02 18:47:25 +01:00
|
|
|
if (mutex.owner || mutex.lock_count)
|
|
|
|
|
{
|
|
|
|
|
return CELL_EBUSY;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 08:58:30 +02:00
|
|
|
if (mutex.cond_count)
|
2017-02-02 18:47:25 +01:00
|
|
|
{
|
|
|
|
|
return CELL_EPERM;
|
|
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2021-05-07 12:08:16 +02:00
|
|
|
lv2_obj::on_id_destroy(mutex, mutex.key);
|
2017-02-02 18:47:25 +01:00
|
|
|
return {};
|
|
|
|
|
});
|
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
|
|
|
|
2017-02-02 18:47:25 +01:00
|
|
|
if (mutex.ret)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
2017-02-02 18:47:25 +01:00
|
|
|
return mutex.ret;
|
2014-02-14 12:40:41 +01:00
|
|
|
}
|
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 18:47:25 +01:00
|
|
|
error_code sys_mutex_lock(ppu_thread& ppu, u32 mutex_id, u64 timeout)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2020-06-05 11:36:28 +02:00
|
|
|
ppu.state += cpu_flag::wait;
|
2019-06-09 01:03:24 +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
|
|
|
|
2017-02-02 18:47:25 +01:00
|
|
|
const auto mutex = idm::get<lv2_obj, lv2_mutex>(mutex_id, [&](lv2_mutex& mutex)
|
|
|
|
|
{
|
2017-02-06 19:36:46 +01:00
|
|
|
CellError result = mutex.try_lock(ppu.id);
|
|
|
|
|
|
|
|
|
|
if (result == CELL_EBUSY)
|
|
|
|
|
{
|
2018-09-03 21:28:33 +02:00
|
|
|
std::lock_guard lock(mutex.mutex);
|
2017-02-06 19:36:46 +01:00
|
|
|
|
|
|
|
|
if (mutex.try_own(ppu, ppu.id))
|
|
|
|
|
{
|
|
|
|
|
result = {};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-02-22 11:10:55 +01:00
|
|
|
mutex.sleep(ppu, timeout);
|
2017-02-06 19:36:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2017-02-02 18:47:25 +01:00
|
|
|
});
|
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
|
|
|
|
2017-02-02 18:47:25 +01:00
|
|
|
if (mutex.ret)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
2017-02-02 18:47:25 +01:00
|
|
|
if (mutex.ret != CELL_EBUSY)
|
2014-02-03 14:12:25 +01:00
|
|
|
{
|
2017-02-02 18:47:25 +01:00
|
|
|
return mutex.ret;
|
2014-02-14 12:40:41 +01:00
|
|
|
}
|
2014-12-23 00:31:11 +01:00
|
|
|
}
|
2017-02-02 18:47:25 +01:00
|
|
|
else
|
2015-07-08 00:33:24 +02:00
|
|
|
{
|
|
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
ppu.gpr[3] = CELL_OK;
|
2015-07-08 00:33:24 +02:00
|
|
|
|
2021-02-13 15:50:07 +01:00
|
|
|
while (auto state = ppu.state.fetch_sub(cpu_flag::signal))
|
2014-02-20 03:16:17 +01:00
|
|
|
{
|
2021-02-13 15:50:07 +01:00
|
|
|
if (is_stopped(state) || state & cpu_flag::signal)
|
2018-10-11 00:17:19 +02:00
|
|
|
{
|
2021-02-13 15:50:07 +01:00
|
|
|
break;
|
2018-10-11 00:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
if (timeout)
|
2014-12-23 00:31:11 +01:00
|
|
|
{
|
2019-07-14 05:55:11 +02:00
|
|
|
if (lv2_obj::wait_timeout(timeout, &ppu))
|
2015-07-08 00:33:24 +02:00
|
|
|
{
|
2020-03-28 04:16:59 +01:00
|
|
|
// Wait for rescheduling
|
|
|
|
|
if (ppu.check_state())
|
|
|
|
|
{
|
2021-02-13 15:50:07 +01:00
|
|
|
return {};
|
2020-03-28 04:16:59 +01:00
|
|
|
}
|
|
|
|
|
|
2018-09-03 21:28:33 +02:00
|
|
|
std::lock_guard lock(mutex->mutex);
|
2017-02-02 18:47:25 +01:00
|
|
|
|
|
|
|
|
if (!mutex->unqueue(mutex->sq, &ppu))
|
|
|
|
|
{
|
2020-03-28 04:16:59 +01:00
|
|
|
break;
|
2017-02-02 18:47:25 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
ppu.gpr[3] = CELL_ETIMEDOUT;
|
|
|
|
|
break;
|
2015-07-08 00:33:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-02-13 15:50:07 +01:00
|
|
|
thread_ctrl::wait_on(ppu.state, state);
|
2015-07-08 00:33:24 +02:00
|
|
|
}
|
2015-01-02 00:41:29 +01:00
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
return not_an_error(ppu.gpr[3]);
|
2013-06-30 10:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
2017-02-02 18:47:25 +01:00
|
|
|
error_code sys_mutex_trylock(ppu_thread& ppu, u32 mutex_id)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2020-06-05 11:36:28 +02:00
|
|
|
ppu.state += cpu_flag::wait;
|
2019-06-09 01:03:24 +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
|
|
|
|
2017-02-02 18:47:25 +01:00
|
|
|
const auto mutex = idm::check<lv2_obj, lv2_mutex>(mutex_id, [&](lv2_mutex& mutex)
|
|
|
|
|
{
|
|
|
|
|
return mutex.try_lock(ppu.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
|
|
|
|
2017-02-02 18:47:25 +01:00
|
|
|
if (mutex.ret)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
2017-02-02 18:47:25 +01:00
|
|
|
if (mutex.ret == CELL_EBUSY)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
2017-02-02 18:47:25 +01:00
|
|
|
return not_an_error(CELL_EBUSY);
|
2014-02-14 12:40:41 +01:00
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2017-02-02 18:47:25 +01:00
|
|
|
return mutex.ret;
|
2014-02-14 12:40:41 +01:00
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
|
|
|
|
return CELL_OK;
|
2013-06-30 10:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
2017-02-02 18:47:25 +01:00
|
|
|
error_code sys_mutex_unlock(ppu_thread& ppu, u32 mutex_id)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2020-06-05 11:36:28 +02:00
|
|
|
ppu.state += cpu_flag::wait;
|
2019-06-09 01:03:24 +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
|
|
|
|
2017-02-02 18:47:25 +01:00
|
|
|
const auto mutex = idm::check<lv2_obj, lv2_mutex>(mutex_id, [&](lv2_mutex& mutex)
|
|
|
|
|
{
|
|
|
|
|
return mutex.try_unlock(ppu.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
|
|
|
|
2017-02-02 18:47:25 +01:00
|
|
|
if (mutex.ret == CELL_EBUSY)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
2018-09-03 21:28:33 +02:00
|
|
|
std::lock_guard lock(mutex->mutex);
|
2014-12-23 00:31:11 +01:00
|
|
|
|
2017-02-22 11:10:55 +01:00
|
|
|
if (auto cpu = mutex->reown<ppu_thread>())
|
|
|
|
|
{
|
2019-04-25 16:27:50 +02:00
|
|
|
mutex->awake(cpu);
|
2017-02-22 11:10:55 +01:00
|
|
|
}
|
2014-12-23 00:31:11 +01:00
|
|
|
}
|
2017-02-02 18:47:25 +01:00
|
|
|
else if (mutex.ret)
|
2014-12-23 00:31:11 +01:00
|
|
|
{
|
2017-02-02 18:47:25 +01:00
|
|
|
return mutex.ret;
|
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
|
|
|
}
|