event_queue_t, event_port_t rewritten

This commit is contained in:
Nekotekina 2015-03-04 07:42:04 +03:00
parent 78c37ff8b6
commit ad38e9f0fe
22 changed files with 386 additions and 584 deletions

View file

@ -14,66 +14,68 @@ SysCallBase sys_event("sys_event");
u32 event_queue_create(u32 protocol, s32 type, u64 name_u64, u64 event_queue_key, s32 size)
{
std::shared_ptr<EventQueue> eq(new EventQueue(protocol, type, name_u64, event_queue_key, size));
std::shared_ptr<event_queue_t> queue(new event_queue_t(protocol, type, name_u64, event_queue_key, size));
if (event_queue_key && !Emu.GetEventManager().RegisterKey(eq, event_queue_key))
{
return 0;
}
Emu.GetEventManager().RegisterKey(queue, event_queue_key);
const u32 id = sys_event.GetNewId(eq, TYPE_EVENT_QUEUE);
eq->sq.set_full_name(fmt::Format("EventQueue(%d)", id));
sys_event.Warning("*** event_queue created [%s] (protocol=0x%x, type=0x%x, key=0x%llx, size=0x%x): id = %d",
std::string((const char*)&name_u64, 8).c_str(), protocol, type, event_queue_key, size, id);
return id;
return sys_event.GetNewId(queue, TYPE_EVENT_QUEUE);
}
s32 sys_event_queue_create(vm::ptr<u32> equeue_id, vm::ptr<sys_event_queue_attr> attr, u64 event_queue_key, s32 size)
{
sys_event.Warning("sys_event_queue_create(equeue_id_addr=0x%x, attr_addr=0x%x, event_queue_key=0x%llx, size=%d)",
equeue_id.addr(), attr.addr(), event_queue_key, size);
sys_event.Warning("sys_event_queue_create(equeue_id=*0x%x, attr=*0x%x, event_queue_key=0x%llx, size=%d)", equeue_id, attr, event_queue_key, size);
if(size <= 0 || size > 127)
if (size <= 0 || size > 127)
{
return CELL_EINVAL;
}
switch (attr->protocol.data())
const u32 protocol = attr->protocol;
switch (protocol)
{
case se32(SYS_SYNC_PRIORITY): break;
case se32(SYS_SYNC_RETRY): sys_event.Error("Invalid protocol (SYS_SYNC_RETRY)"); return CELL_EINVAL;
case se32(SYS_SYNC_PRIORITY_INHERIT): sys_event.Error("Invalid protocol (SYS_SYNC_PRIORITY_INHERIT)"); return CELL_EINVAL;
case se32(SYS_SYNC_FIFO): break;
default: sys_event.Error("Unknown protocol (0x%x)", attr->protocol); return CELL_EINVAL;
case SYS_SYNC_PRIORITY: break;
case SYS_SYNC_RETRY: sys_event.Error("Invalid protocol (SYS_SYNC_RETRY)"); return CELL_EINVAL;
case SYS_SYNC_PRIORITY_INHERIT: sys_event.Error("Invalid protocol (SYS_SYNC_PRIORITY_INHERIT)"); return CELL_EINVAL;
case SYS_SYNC_FIFO: break;
default: sys_event.Error("Unknown protocol (0x%x)", protocol); return CELL_EINVAL;
}
switch (attr->type.data())
const u32 type = attr->type;
switch (type)
{
case se32(SYS_PPU_QUEUE): break;
case se32(SYS_SPU_QUEUE): break;
default: sys_event.Error("Unknown event queue type (0x%x)", attr->type); return CELL_EINVAL;
case SYS_PPU_QUEUE: break;
case SYS_SPU_QUEUE: break;
default: sys_event.Error("Unknown event queue type (0x%x)", type); return CELL_EINVAL;
}
if (event_queue_key && Emu.GetEventManager().CheckKey(event_queue_key))
LV2_LOCK;
if (Emu.GetEventManager().CheckKey(event_queue_key))
{
return CELL_EEXIST;
}
if (u32 id = event_queue_create(attr->protocol, attr->type, attr->name_u64, event_queue_key, size))
std::shared_ptr<event_queue_t> queue(new event_queue_t(protocol, type, attr->name_u64, event_queue_key, size));
if (!Emu.GetEventManager().RegisterKey(queue, event_queue_key))
{
*equeue_id = id;
return CELL_OK;
return CELL_EAGAIN;
}
return CELL_EAGAIN;
*equeue_id = sys_event.GetNewId(queue, TYPE_EVENT_QUEUE);
return CELL_OK;
}
s32 sys_event_queue_destroy(u32 equeue_id, int mode)
s32 sys_event_queue_destroy(u32 equeue_id, s32 mode)
{
sys_event.Todo("sys_event_queue_destroy(equeue_id=%d, mode=0x%x)", equeue_id, mode);
sys_event.Warning("sys_event_queue_destroy(equeue_id=%d, mode=%d)", equeue_id, mode);
std::shared_ptr<EventQueue> eq;
if (!Emu.GetIdManager().GetIDData(equeue_id, eq))
LV2_LOCK;
std::shared_ptr<event_queue_t> queue;
if (!Emu.GetIdManager().GetIDData(equeue_id, queue))
{
return CELL_ESRCH;
}
@ -83,203 +85,145 @@ s32 sys_event_queue_destroy(u32 equeue_id, int mode)
return CELL_EINVAL;
}
//u32 tid = GetCurrentPPUThread().GetId();
//eq->sq.m_mutex.lock();
//eq->owner.lock(tid);
// check if some threads are waiting for an event
//if (!mode && eq->sq.list.size())
//{
// eq->owner.unlock(tid);
// eq->sq.m_mutex.unlock();
// return CELL_EBUSY;
//}
//eq->owner.unlock(tid, ~0);
//eq->sq.m_mutex.unlock();
//while (eq->sq.list.size())
//{
// std::this_thread::sleep_for(std::chrono::milliseconds(1)); // hack
// if (Emu.IsStopped())
// {
// sys_event.Warning("sys_event_queue_destroy(equeue=%d) aborted", equeue_id);
// break;
// }
//}
if (!mode && queue->waiters)
{
return CELL_EBUSY;
}
else
{
// set special value for waiters
queue->waiters.exchange(-1);
}
Emu.GetEventManager().UnregisterKey(eq->key);
eq->ports.clear();
Emu.GetEventManager().UnregisterKey(queue->key);
Emu.GetIdManager().RemoveID(equeue_id);
return CELL_OK;
}
s32 sys_event_queue_tryreceive(u32 equeue_id, vm::ptr<sys_event_data> event_array, s32 size, vm::ptr<u32> number)
s32 sys_event_queue_tryreceive(u32 equeue_id, vm::ptr<sys_event_t> event_array, s32 size, vm::ptr<u32> number)
{
sys_event.Todo("sys_event_queue_tryreceive(equeue_id=%d, event_array_addr=0x%x, size=%d, number_addr=0x%x)",
equeue_id, event_array.addr(), size, number.addr());
sys_event.Warning("sys_event_queue_tryreceive(equeue_id=%d, event_array=*0x%x, size=%d, number=*0x%x)", equeue_id, event_array, size, number);
std::shared_ptr<EventQueue> eq;
if (!Emu.GetIdManager().GetIDData(equeue_id, eq))
LV2_LOCK;
std::shared_ptr<event_queue_t> queue;
if (!Emu.GetIdManager().GetIDData(equeue_id, queue))
{
return CELL_ESRCH;
}
if (eq->type != SYS_PPU_QUEUE)
if (queue->type != SYS_PPU_QUEUE || size < 0) // ???
{
return CELL_EINVAL;
}
if (size == 0)
s32 count = 0;
while (count < size && queue->events.size())
{
*number = 0;
return CELL_OK;
auto& event = queue->events.front();
event_array[count++] = { be_t<u64>::make(event.source), be_t<u64>::make(event.data1), be_t<u64>::make(event.data2), be_t<u64>::make(event.data3) };
queue->events.pop_front();
}
//u32 tid = GetCurrentPPUThread().GetId();
//eq->sq.m_mutex.lock();
//eq->owner.lock(tid);
//if (eq->sq.list.size())
//{
// *number = 0;
// eq->owner.unlock(tid);
// eq->sq.m_mutex.unlock();
// return CELL_OK;
//}
*number = eq->events.pop_all(event_array.get_ptr(), size);
//eq->owner.unlock(tid);
//eq->sq.m_mutex.unlock();
*number = count;
return CELL_OK;
}
s32 sys_event_queue_receive(u32 equeue_id, vm::ptr<sys_event_data> dummy_event, u64 timeout)
s32 sys_event_queue_receive(PPUThread& CPU, u32 equeue_id, vm::ptr<sys_event_t> dummy_event, u64 timeout)
{
// dummy_event argument is ignored, data returned in registers
sys_event.Log("sys_event_queue_receive(equeue_id=%d, dummy_event_addr=0x%x, timeout=%lld)",
equeue_id, dummy_event.addr(), timeout);
sys_event.Log("sys_event_queue_receive(equeue_id=%d, event=*0x%x, timeout=0x%llx)", equeue_id, dummy_event, timeout);
const u64 start_time = get_system_time();
std::shared_ptr<EventQueue> eq;
if (!Emu.GetIdManager().GetIDData(equeue_id, eq))
LV2_LOCK;
std::shared_ptr<event_queue_t> queue;
if (!Emu.GetIdManager().GetIDData(equeue_id, queue))
{
return CELL_ESRCH;
}
if (eq->type != SYS_PPU_QUEUE)
if (queue->type != SYS_PPU_QUEUE)
{
return CELL_EINVAL;
}
const u32 tid = GetCurrentPPUThread().GetId();
// protocol is ignored in current implementation
queue->waiters++;
eq->sq.push(tid, eq->protocol); // add thread to sleep queue
while (true)
while (queue->events.empty())
{
const u32 old_owner = eq->owner.compare_and_swap(0, tid);
const s32 res = old_owner ? (old_owner == tid ? 1 : 2) : 0;
switch (res)
if (queue->waiters < 0)
{
case 0:
{
const u32 next = eq->events.count() ? eq->sq.signal(eq->protocol) : 0;
if (next != tid)
{
if (!eq->owner.compare_and_swap_test(tid, next))
{
assert(!"sys_event_queue_receive() failed (I)");
}
break;
}
// fallthrough
}
case 1:
{
sys_event_data event;
eq->events.pop(event);
if (!eq->owner.compare_and_swap_test(tid, 0))
{
assert(!"sys_event_queue_receive() failed (II)");
}
sys_event.Log(" *** event received: source=0x%llx, d1=0x%llx, d2=0x%llx, d3=0x%llx",
(u64)event.source, (u64)event.data1, (u64)event.data2, (u64)event.data3);
/* passing event data in registers */
PPUThread& t = GetCurrentPPUThread();
t.GPR[4] = event.source;
t.GPR[5] = event.data1;
t.GPR[6] = event.data2;
t.GPR[7] = event.data3;
if (!eq->sq.invalidate(tid, eq->protocol) && !eq->sq.pop(tid, eq->protocol))
{
assert(!"sys_event_queue_receive() failed (receiving)");
}
return CELL_OK;
}
}
if (!~old_owner)
{
if (!eq->sq.invalidate(tid, eq->protocol))
{
assert(!"sys_event_queue_receive() failed (cancelling)");
}
queue->waiters--;
return CELL_ECANCELED;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // hack
if (timeout && get_system_time() - start_time > timeout)
{
if (!eq->sq.invalidate(tid, eq->protocol))
{
assert(!"sys_event_queue_receive() failed (timeout)");
}
queue->waiters--;
return CELL_ETIMEDOUT;
}
if (Emu.IsStopped())
{
sys_event.Warning("sys_event_queue_receive(equeue=%d) aborted", equeue_id);
sys_event.Warning("sys_event_queue_receive(equeue_id=%d) aborted", equeue_id);
return CELL_OK;
}
queue->cv.wait_for(lv2_lock, std::chrono::milliseconds(1));
}
// event data is returned in registers, second arg is not used
auto& event = queue->events.front();
CPU.GPR[4] = event.source;
CPU.GPR[5] = event.data1;
CPU.GPR[6] = event.data2;
CPU.GPR[7] = event.data3;
queue->events.pop_front();
queue->waiters--;
return CELL_OK;
}
s32 sys_event_queue_drain(u32 equeue_id)
{
sys_event.Log("sys_event_queue_drain(equeue_id=%d)", equeue_id);
std::shared_ptr<EventQueue> eq;
if (!Emu.GetIdManager().GetIDData(equeue_id, eq))
LV2_LOCK;
std::shared_ptr<event_queue_t> queue;
if (!Emu.GetIdManager().GetIDData(equeue_id, queue))
{
return CELL_ESRCH;
}
eq->events.clear();
queue->events = {};
return CELL_OK;
}
u32 event_port_create(u64 name)
{
std::shared_ptr<EventPort> eport(new EventPort());
u32 id = sys_event.GetNewId(eport, TYPE_EVENT_PORT);
std::shared_ptr<event_port_t> eport(new event_port_t());
const u32 id = sys_event.GetNewId(eport, TYPE_EVENT_PORT);
eport->name = name ? name : ((u64)process_getpid() << 32) | (u64)id;
sys_event.Warning("*** sys_event_port created: id = %d, name=0x%llx", id, eport->name);
return id;
}
s32 sys_event_port_create(vm::ptr<u32> eport_id, s32 port_type, u64 name)
{
sys_event.Warning("sys_event_port_create(eport_id_addr=0x%x, port_type=0x%x, name=0x%llx)",
eport_id.addr(), port_type, name);
sys_event.Warning("sys_event_port_create(eport_id=*0x%x, port_type=%d, name=0x%llx)", eport_id, port_type, name);
if (port_type != SYS_EVENT_PORT_LOCAL)
{
sys_event.Error("sys_event_port_create: invalid port_type(0x%x)", port_type);
sys_event.Error("sys_event_port_create(): invalid port_type (%d)", port_type);
return CELL_EINVAL;
}
LV2_LOCK;
*eport_id = event_port_create(name);
return CELL_OK;
}
@ -288,24 +232,19 @@ s32 sys_event_port_destroy(u32 eport_id)
{
sys_event.Warning("sys_event_port_destroy(eport_id=%d)", eport_id);
std::shared_ptr<EventPort> eport;
if (!Emu.GetIdManager().GetIDData(eport_id, eport))
LV2_LOCK;
std::shared_ptr<event_port_t> port;
if (!Emu.GetIdManager().GetIDData(eport_id, port))
{
return CELL_ESRCH;
}
if (!eport->m_mutex.try_lock())
if (!port->queue.expired())
{
return CELL_EISCONN;
}
if (eport->eq)
{
eport->m_mutex.unlock();
return CELL_EISCONN;
}
eport->m_mutex.unlock();
Emu.GetIdManager().RemoveID(eport_id);
return CELL_OK;
}
@ -314,37 +253,23 @@ s32 sys_event_port_connect_local(u32 eport_id, u32 equeue_id)
{
sys_event.Warning("sys_event_port_connect_local(eport_id=%d, equeue_id=%d)", eport_id, equeue_id);
std::shared_ptr<EventPort> eport;
if (!Emu.GetIdManager().GetIDData(eport_id, eport))
LV2_LOCK;
std::shared_ptr<event_port_t> port;
std::shared_ptr<event_queue_t> queue;
if (!Emu.GetIdManager().GetIDData(eport_id, port) || !Emu.GetIdManager().GetIDData(equeue_id, queue))
{
return CELL_ESRCH;
}
if (!eport->m_mutex.try_lock())
// CELL_EINVAL is never returned (I have no idea if SYS_EVENT_PORT_LOCAL is the only possible type)
if (!port->queue.expired())
{
return CELL_EISCONN;
}
if (eport->eq)
{
eport->m_mutex.unlock();
return CELL_EISCONN;
}
std::shared_ptr<EventQueue> equeue;
if (!Emu.GetIdManager().GetIDData(equeue_id, equeue))
{
sys_event.Error("sys_event_port_connect_local: event_queue(%d) not found!", equeue_id);
eport->m_mutex.unlock();
return CELL_ESRCH;
}
else
{
equeue->ports.add(eport);
}
eport->eq = equeue;
eport->m_mutex.unlock();
port->queue = queue;
return CELL_OK;
}
@ -352,51 +277,58 @@ s32 sys_event_port_disconnect(u32 eport_id)
{
sys_event.Warning("sys_event_port_disconnect(eport_id=%d)", eport_id);
std::shared_ptr<EventPort> eport;
if (!Emu.GetIdManager().GetIDData(eport_id, eport))
LV2_LOCK;
std::shared_ptr<event_port_t> port;
if (!Emu.GetIdManager().GetIDData(eport_id, port))
{
return CELL_ESRCH;
}
if (!eport->eq)
std::shared_ptr<event_queue_t> queue = port->queue.lock();
if (!queue)
{
return CELL_ENOTCONN;
}
if (!eport->m_mutex.try_lock())
{
return CELL_EBUSY;
}
//for (auto& event : queue->events)
//{
// if (event.source == port->name)
// {
// return CELL_EBUSY; // not sure about it
// }
//}
eport->eq->ports.remove(eport);
eport->eq = nullptr;
eport->m_mutex.unlock();
port->queue.reset();
return CELL_OK;
}
s32 sys_event_port_send(u32 eport_id, u64 data1, u64 data2, u64 data3)
{
sys_event.Log("sys_event_port_send(eport_id=%d, data1=0x%llx, data2=0x%llx, data3=0x%llx)",
eport_id, data1, data2, data3);
sys_event.Log("sys_event_port_send(eport_id=%d, data1=0x%llx, data2=0x%llx, data3=0x%llx)", eport_id, data1, data2, data3);
std::shared_ptr<EventPort> eport;
if (!Emu.GetIdManager().GetIDData(eport_id, eport))
LV2_LOCK;
std::shared_ptr<event_port_t> port;
if (!Emu.GetIdManager().GetIDData(eport_id, port))
{
return CELL_ESRCH;
}
std::lock_guard<std::mutex> lock(eport->m_mutex);
std::shared_ptr<event_queue_t> queue = port->queue.lock();
std::shared_ptr<EventQueue> eq = eport->eq;
if (!eq)
if (!queue)
{
return CELL_ENOTCONN;
}
if (!eq->events.push(eport->name, data1, data2, data3))
if (queue->events.size() >= queue->size)
{
return CELL_EBUSY;
}
queue->events.emplace_back(port->name, data1, data2, data3);
queue->cv.notify_one();
return CELL_OK;
}