2012-11-15 00:39:56 +01:00
|
|
|
#include "stdafx.h"
|
2014-06-02 19:27:24 +02:00
|
|
|
#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-08-23 16:51:51 +02:00
|
|
|
#include "Emu/Cell/PPUThread.h"
|
2014-06-25 00:38:34 +02:00
|
|
|
#include "sys_lwmutex.h"
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2016-08-19 23:14:10 +02:00
|
|
|
namespace vm { using namespace ps3; }
|
|
|
|
|
|
2017-05-13 20:30:37 +02:00
|
|
|
logs::channel sys_lwmutex("sys_lwmutex");
|
2014-08-23 16:51:51 +02:00
|
|
|
|
2015-07-06 01:21:15 +02:00
|
|
|
extern u64 get_system_time();
|
|
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
error_code _sys_lwmutex_create(vm::ptr<u32> lwmutex_id, u32 protocol, vm::ptr<sys_lwmutex_t> control, u32 arg4, u64 name, u32 arg6)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_lwmutex.warning("_sys_lwmutex_create(lwmutex_id=*0x%x, protocol=0x%x, control=*0x%x, arg4=0x%x, name=0x%llx, arg6=0x%x)", lwmutex_id, protocol, control, arg4, name, arg6);
|
2015-03-09 02:30:34 +01:00
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
if (protocol == SYS_SYNC_RETRY)
|
|
|
|
|
sys_lwmutex.todo("_sys_lwmutex_create(): SYS_SYNC_RETRY");
|
|
|
|
|
|
2015-07-19 23:29:40 +02:00
|
|
|
if (protocol != SYS_SYNC_FIFO && protocol != SYS_SYNC_RETRY && protocol != SYS_SYNC_PRIORITY)
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_lwmutex.error("_sys_lwmutex_create(): unknown protocol (0x%x)", protocol);
|
2015-07-19 23:29:40 +02:00
|
|
|
return CELL_EINVAL;
|
2015-03-09 02:30:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (arg4 != 0x80000001 || arg6)
|
|
|
|
|
{
|
2016-08-08 18:01:06 +02:00
|
|
|
fmt::throw_exception("Unknown arguments (arg4=0x%x, arg6=0x%x)" HERE, arg4, arg6);
|
2015-03-09 02:30:34 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
if (const u32 id = idm::make<lv2_obj, lv2_lwmutex>(protocol, control, name))
|
|
|
|
|
{
|
|
|
|
|
*lwmutex_id = id;
|
|
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
2015-03-09 02:30:34 +01:00
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
return CELL_EAGAIN;
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
error_code _sys_lwmutex_destroy(u32 lwmutex_id)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_lwmutex.warning("_sys_lwmutex_destroy(lwmutex_id=0x%x)", lwmutex_id);
|
2015-03-09 02:30:34 +01:00
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
const auto mutex = idm::withdraw<lv2_obj, lv2_lwmutex>(lwmutex_id, [&](lv2_lwmutex& mutex) -> CellError
|
|
|
|
|
{
|
|
|
|
|
semaphore_lock lock(mutex.mutex);
|
|
|
|
|
|
|
|
|
|
if (!mutex.sq.empty())
|
|
|
|
|
{
|
|
|
|
|
return CELL_EBUSY;
|
|
|
|
|
}
|
2015-03-09 02:30:34 +01:00
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
return {};
|
|
|
|
|
});
|
2015-03-11 16:30:50 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!mutex)
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
if (mutex.ret)
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
2017-02-03 00:16:09 +01:00
|
|
|
return mutex.ret;
|
2015-03-09 02:30:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CELL_OK;
|
2014-02-12 20:03:14 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
error_code _sys_lwmutex_lock(ppu_thread& ppu, u32 lwmutex_id, u64 timeout)
|
2014-02-12 20:03:14 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_lwmutex.trace("_sys_lwmutex_lock(lwmutex_id=0x%x, timeout=0x%llx)", lwmutex_id, timeout);
|
2015-03-09 02:30:34 +01:00
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
const auto mutex = idm::get<lv2_obj, lv2_lwmutex>(lwmutex_id, [&](lv2_lwmutex& mutex)
|
|
|
|
|
{
|
|
|
|
|
if (u32 value = mutex.signaled)
|
|
|
|
|
{
|
|
|
|
|
if (mutex.signaled.compare_and_swap_test(value, value - 1))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
semaphore_lock lock(mutex.mutex);
|
|
|
|
|
|
|
|
|
|
if (u32 value = mutex.signaled)
|
|
|
|
|
{
|
|
|
|
|
if (mutex.signaled.compare_and_swap_test(value, value - 1))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-09 02:30:34 +01:00
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
mutex.sq.emplace_back(&ppu);
|
2017-02-22 11:10:55 +01:00
|
|
|
mutex.sleep(ppu, timeout);
|
2017-02-03 00:16:09 +01:00
|
|
|
return false;
|
|
|
|
|
});
|
2015-03-11 16:30:50 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!mutex)
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
if (mutex.ret)
|
2015-07-21 18:35:55 +02:00
|
|
|
{
|
|
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
ppu.gpr[3] = CELL_OK;
|
2015-07-21 18:35:55 +02:00
|
|
|
|
2016-08-09 16:14:41 +02:00
|
|
|
while (!ppu.state.test_and_reset(cpu_flag::signal))
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
2015-07-21 18:35:55 +02:00
|
|
|
if (timeout)
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
2017-02-22 11:10:55 +01:00
|
|
|
const u64 passed = get_system_time() - ppu.start_time;
|
2015-03-09 02:30:34 +01:00
|
|
|
|
2015-07-21 18:35:55 +02:00
|
|
|
if (passed >= timeout)
|
|
|
|
|
{
|
2017-02-03 00:16:09 +01:00
|
|
|
semaphore_lock lock(mutex->mutex);
|
|
|
|
|
|
|
|
|
|
if (!mutex->unqueue(mutex->sq, &ppu))
|
|
|
|
|
{
|
|
|
|
|
timeout = 0;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
ppu.gpr[3] = CELL_ETIMEDOUT;
|
|
|
|
|
break;
|
2015-07-21 18:35:55 +02:00
|
|
|
}
|
2015-03-09 02:30:34 +01:00
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
thread_ctrl::wait_for(timeout - passed);
|
2015-07-21 18:35:55 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-02-03 00:16:09 +01:00
|
|
|
thread_ctrl::wait();
|
2015-07-21 18:35:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-09 02:30:34 +01:00
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
return not_an_error(ppu.gpr[3]);
|
2014-02-12 20:03:14 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
error_code _sys_lwmutex_trylock(u32 lwmutex_id)
|
2014-02-12 20:03:14 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_lwmutex.trace("_sys_lwmutex_trylock(lwmutex_id=0x%x)", lwmutex_id);
|
2015-03-09 02:30:34 +01:00
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
const auto mutex = idm::check<lv2_obj, lv2_lwmutex>(lwmutex_id, [&](lv2_lwmutex& mutex)
|
|
|
|
|
{
|
|
|
|
|
if (u32 value = mutex.signaled)
|
|
|
|
|
{
|
|
|
|
|
if (mutex.signaled.compare_and_swap_test(value, value - 1))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-09 02:30:34 +01:00
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
return false;
|
|
|
|
|
});
|
2015-03-11 16:30:50 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!mutex)
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
if (!mutex.ret)
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
2017-02-03 00:16:09 +01:00
|
|
|
return not_an_error(CELL_EBUSY);
|
2015-03-09 02:30:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CELL_OK;
|
2014-02-12 20:03:14 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-06 19:36:46 +01:00
|
|
|
error_code _sys_lwmutex_unlock(ppu_thread& ppu, u32 lwmutex_id)
|
2014-02-12 20:03:14 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_lwmutex.trace("_sys_lwmutex_unlock(lwmutex_id=0x%x)", lwmutex_id);
|
2015-03-09 02:30:34 +01:00
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
const auto mutex = idm::check<lv2_obj, lv2_lwmutex>(lwmutex_id, [&](lv2_lwmutex& mutex) -> cpu_thread*
|
|
|
|
|
{
|
|
|
|
|
semaphore_lock lock(mutex.mutex);
|
2015-03-09 02:30:34 +01:00
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
if (const auto cpu = mutex.schedule<ppu_thread>(mutex.sq, mutex.protocol))
|
|
|
|
|
{
|
|
|
|
|
return cpu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mutex.signaled++;
|
|
|
|
|
return nullptr;
|
|
|
|
|
});
|
2015-03-11 16:30:50 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!mutex)
|
2015-03-09 02:30:34 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-03 00:16:09 +01:00
|
|
|
if (mutex.ret)
|
|
|
|
|
{
|
2017-02-06 19:36:46 +01:00
|
|
|
mutex->awake(*mutex.ret);
|
2017-02-03 00:16:09 +01:00
|
|
|
}
|
2015-03-09 02:30:34 +01:00
|
|
|
|
|
|
|
|
return CELL_OK;
|
2014-02-23 17:52:52 +01:00
|
|
|
}
|