2013-06-30 10:46:29 +02: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-12-22 01:56:04 +01:00
|
|
|
#include "sys_mutex.h"
|
2014-06-25 00:38:34 +02:00
|
|
|
#include "sys_cond.h"
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2016-05-13 15:55:34 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
logs::channel sys_cond("sys_cond", logs::level::notice);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-07-06 01:21:15 +02:00
|
|
|
extern u64 get_system_time();
|
|
|
|
|
|
2016-04-14 00:23:53 +02:00
|
|
|
void lv2_cond_t::notify(lv2_lock_t, cpu_thread* thread)
|
2015-07-19 03:56:33 +02:00
|
|
|
{
|
|
|
|
|
if (mutex->owner)
|
|
|
|
|
{
|
|
|
|
|
// add thread to the mutex sleep queue if cannot lock immediately
|
|
|
|
|
mutex->sq.emplace_back(thread);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-07-27 23:43:22 +02:00
|
|
|
mutex->owner = idm::get<ppu_thread>(thread->id);
|
2016-08-15 02:11:49 +02:00
|
|
|
thread->set_signal();
|
2015-07-19 03:56:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-22 00:27:58 +02:00
|
|
|
s32 sys_cond_create(vm::ptr<u32> cond_id, u32 mutex_id, vm::ptr<sys_cond_attribute_t> attr)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2016-01-12 22:57:16 +01: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 02:08:12 +02:00
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto mutex = idm::get<lv2_mutex_t>(mutex_id);
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!mutex)
|
2014-02-13 17:59:13 +01:00
|
|
|
{
|
2015-03-06 23:10:04 +01:00
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-15 18:23:17 +02:00
|
|
|
if (attr->pshared != SYS_SYNC_NOT_PROCESS_SHARED || attr->ipc_key || attr->flags)
|
2015-03-06 23:10:04 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_cond.error("sys_cond_create(): unknown attributes (pshared=0x%x, ipc_key=0x%llx, flags=0x%x)", attr->pshared, attr->ipc_key, attr->flags);
|
2014-02-13 17:59:13 +01:00
|
|
|
return CELL_EINVAL;
|
|
|
|
|
}
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
if (!++mutex->cond_count)
|
2014-02-13 17:59:13 +01:00
|
|
|
{
|
2016-08-08 18:01:06 +02:00
|
|
|
fmt::throw_exception("Unexpected cond_count" HERE);
|
2014-02-13 17:59:13 +01:00
|
|
|
}
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
*cond_id = idm::make<lv2_cond_t>(mutex, attr->name_u64);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
|
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-25 00:38:34 +02:00
|
|
|
s32 sys_cond_destroy(u32 cond_id)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_cond.warning("sys_cond_destroy(cond_id=0x%x)", cond_id);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto cond = idm::get<lv2_cond_t>(cond_id);
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!cond)
|
2014-02-13 17:59:13 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-21 13:55:29 +02:00
|
|
|
if (!cond->sq.empty())
|
2014-02-13 17:59:13 +01:00
|
|
|
{
|
|
|
|
|
return CELL_EBUSY;
|
|
|
|
|
}
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
if (!cond->mutex->cond_count--)
|
|
|
|
|
{
|
2016-08-08 18:01:06 +02:00
|
|
|
fmt::throw_exception("Unexpected cond_count" HERE);
|
2015-03-06 23:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
idm::remove<lv2_cond_t>(cond_id);
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-25 00:38:34 +02:00
|
|
|
s32 sys_cond_signal(u32 cond_id)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_cond.trace("sys_cond_signal(cond_id=0x%x)", cond_id);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto cond = idm::get<lv2_cond_t>(cond_id);
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!cond)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-19 03:56:33 +02:00
|
|
|
// signal one waiting thread; protocol is ignored in current implementation
|
|
|
|
|
if (!cond->sq.empty())
|
2015-03-06 23:10:04 +01:00
|
|
|
{
|
2015-07-26 13:21:25 +02:00
|
|
|
cond->notify(lv2_lock, cond->sq.front());
|
2015-07-19 03:56:33 +02:00
|
|
|
cond->sq.pop_front();
|
2015-03-06 23:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
2014-02-26 11:35:30 +01:00
|
|
|
return CELL_OK;
|
2013-06-30 10:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
2014-06-25 00:38:34 +02:00
|
|
|
s32 sys_cond_signal_all(u32 cond_id)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_cond.trace("sys_cond_signal_all(cond_id=0x%x)", cond_id);
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto cond = idm::get<lv2_cond_t>(cond_id);
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!cond)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-07-19 03:56:33 +02:00
|
|
|
// signal all waiting threads; protocol is ignored in current implementation
|
2015-07-26 13:21:25 +02:00
|
|
|
for (auto& thread : cond->sq)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
2015-07-26 13:21:25 +02:00
|
|
|
cond->notify(lv2_lock, thread);
|
2014-02-14 12:40:41 +01:00
|
|
|
}
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2015-07-19 03:56:33 +02:00
|
|
|
cond->sq.clear();
|
|
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
return CELL_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-25 00:38:34 +02:00
|
|
|
s32 sys_cond_signal_to(u32 cond_id, u32 thread_id)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_cond.trace("sys_cond_signal_to(cond_id=0x%x, thread_id=0x%x)", cond_id, thread_id);
|
2015-03-06 23:10:04 +01:00
|
|
|
|
|
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto cond = idm::get<lv2_cond_t>(cond_id);
|
2014-02-14 12:40:41 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!cond)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
2013-06-30 10:46:29 +02:00
|
|
|
|
2016-04-14 00:23:53 +02:00
|
|
|
const auto found = std::find_if(cond->sq.begin(), cond->sq.end(), [=](cpu_thread* thread)
|
2015-07-26 13:21:25 +02:00
|
|
|
{
|
2016-04-14 00:23:53 +02:00
|
|
|
return thread->id == thread_id;
|
2015-07-26 13:21:25 +02:00
|
|
|
});
|
2015-07-08 00:33:24 +02:00
|
|
|
|
2015-07-26 13:21:25 +02:00
|
|
|
// TODO: check if CELL_ESRCH is returned if thread_id is invalid
|
|
|
|
|
if (found == cond->sq.end())
|
2014-02-26 11:35:30 +01:00
|
|
|
{
|
2015-07-26 13:21:25 +02:00
|
|
|
return CELL_EPERM;
|
2014-02-26 11:35:30 +01:00
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2015-07-26 13:21:25 +02:00
|
|
|
// signal specified thread
|
|
|
|
|
cond->notify(lv2_lock, *found);
|
|
|
|
|
cond->sq.erase(found);
|
|
|
|
|
|
|
|
|
|
return CELL_OK;
|
2014-02-13 17:59:13 +01:00
|
|
|
}
|
|
|
|
|
|
2016-07-27 23:43:22 +02:00
|
|
|
s32 sys_cond_wait(ppu_thread& ppu, u32 cond_id, u64 timeout)
|
2014-02-13 17:59:13 +01:00
|
|
|
{
|
2016-01-12 22:57:16 +01:00
|
|
|
sys_cond.trace("sys_cond_wait(cond_id=0x%x, timeout=%lld)", cond_id, timeout);
|
2014-02-14 12:40:41 +01:00
|
|
|
|
2015-01-02 00:41:29 +01:00
|
|
|
const u64 start_time = get_system_time();
|
|
|
|
|
|
2015-03-06 23:10:04 +01:00
|
|
|
LV2_LOCK;
|
|
|
|
|
|
2015-08-05 17:30:32 +02:00
|
|
|
const auto cond = idm::get<lv2_cond_t>(cond_id);
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2015-04-12 03:36:25 +02:00
|
|
|
if (!cond)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
|
|
|
|
return CELL_ESRCH;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
// check current ownership
|
|
|
|
|
if (cond->mutex->owner.get() != &ppu)
|
2014-02-14 12:40:41 +01:00
|
|
|
{
|
2014-02-26 11:35:30 +01:00
|
|
|
return CELL_EPERM;
|
2014-02-14 12:40:41 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-17 18:27:12 +02:00
|
|
|
// save the recursive value
|
2015-03-06 23:10:04 +01:00
|
|
|
const u32 recursive_value = cond->mutex->recursive_count.exchange(0);
|
2014-02-13 17:59:13 +01:00
|
|
|
|
2015-07-17 18:27:12 +02:00
|
|
|
// unlock the mutex
|
|
|
|
|
cond->mutex->unlock(lv2_lock);
|
2015-04-12 03:36:25 +02:00
|
|
|
|
2015-07-19 03:56:33 +02:00
|
|
|
// add waiter; protocol is ignored in current implementation
|
2016-04-14 00:23:53 +02:00
|
|
|
sleep_entry<cpu_thread> waiter(cond->sq, ppu);
|
2015-07-08 00:33:24 +02:00
|
|
|
|
2015-07-21 13:55:29 +02:00
|
|
|
// potential mutex waiter (not added immediately)
|
2016-04-14 00:23:53 +02:00
|
|
|
sleep_entry<cpu_thread> mutex_waiter(cond->mutex->sq, ppu, defer_sleep);
|
2015-07-19 03:56:33 +02:00
|
|
|
|
2016-08-09 16:14:41 +02:00
|
|
|
while (!ppu.state.test_and_reset(cpu_flag::signal))
|
2015-07-19 03:56:33 +02:00
|
|
|
{
|
2015-07-19 13:36:32 +02:00
|
|
|
CHECK_EMU_STATUS;
|
|
|
|
|
|
2015-07-19 03:56:33 +02:00
|
|
|
// timeout is ignored if waiting on the cond var is already dropped
|
|
|
|
|
if (timeout && waiter)
|
2015-07-08 00:33:24 +02:00
|
|
|
{
|
2015-07-19 03:56:33 +02:00
|
|
|
const u64 passed = get_system_time() - start_time;
|
2015-07-08 00:33:24 +02:00
|
|
|
|
2015-07-19 03:56:33 +02:00
|
|
|
if (passed >= timeout)
|
|
|
|
|
{
|
|
|
|
|
// try to reown mutex and exit if timed out
|
|
|
|
|
if (!cond->mutex->owner)
|
2015-07-08 00:33:24 +02:00
|
|
|
{
|
2016-07-27 23:43:22 +02:00
|
|
|
cond->mutex->owner = idm::get<ppu_thread>(ppu.id);
|
2015-07-08 00:33:24 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2015-07-19 03:56:33 +02:00
|
|
|
|
|
|
|
|
// drop condition variable and start waiting on the mutex queue
|
|
|
|
|
mutex_waiter.enter();
|
|
|
|
|
waiter.leave();
|
|
|
|
|
continue;
|
2015-04-12 03:36:25 +02:00
|
|
|
}
|
2015-03-10 15:42:08 +01:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
get_current_thread_cv().wait_for(lv2_lock, std::chrono::microseconds(timeout - passed));
|
2015-07-19 03:56:33 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-04-25 12:49:12 +02:00
|
|
|
get_current_thread_cv().wait(lv2_lock);
|
2014-02-26 11:35:30 +01:00
|
|
|
}
|
2015-01-02 00:41:29 +01:00
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2015-07-19 03:56:33 +02:00
|
|
|
// mutex owner is restored after notification or unlocking
|
2015-07-08 00:33:24 +02:00
|
|
|
if (cond->mutex->owner.get() != &ppu)
|
|
|
|
|
{
|
2016-08-08 18:01:06 +02:00
|
|
|
fmt::throw_exception("Unexpected mutex owner" HERE);
|
2015-07-08 00:33:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// restore the recursive value
|
2015-03-06 23:10:04 +01:00
|
|
|
cond->mutex->recursive_count = recursive_value;
|
2015-07-08 00:33:24 +02:00
|
|
|
|
2015-07-19 03:56:33 +02:00
|
|
|
// check timeout (unclear)
|
2015-07-08 00:33:24 +02:00
|
|
|
if (timeout && get_system_time() - start_time > timeout)
|
|
|
|
|
{
|
|
|
|
|
return CELL_ETIMEDOUT;
|
|
|
|
|
}
|
2015-03-06 23:10:04 +01:00
|
|
|
|
2014-03-19 01:32:23 +01:00
|
|
|
return CELL_OK;
|
2014-06-17 17:44:03 +02:00
|
|
|
}
|