- events tested and implemented multithreading support through mpscq
This commit is contained in:
@@ -19,14 +19,41 @@
|
||||
#include <fennec/containers/dynarray.h>
|
||||
#include <fennec/containers/set.h>
|
||||
#include <fennec/core/event.h>
|
||||
#include <fennec/threading/lock_guard.h>
|
||||
#include <fennec/threading/mpscq.h>
|
||||
#include <fennec/threading/mutex.h>
|
||||
|
||||
#define FENNEC_EVENT_QUEUE_SIZE 8192
|
||||
|
||||
namespace fennec
|
||||
{
|
||||
|
||||
static mutex lock;
|
||||
static dynarray<set<event_listener*>> listeners;
|
||||
static mpscq<event> queue(FENNEC_EVENT_QUEUE_SIZE);
|
||||
|
||||
void event::handle_events() {
|
||||
lock_guard guard(lock);
|
||||
|
||||
// if we don't copy into a temporary buffer, we could deadlock ourselves
|
||||
// if someone unwittingly calls dispatch() in an event listener.
|
||||
dynarray<unique_ptr<event>> dispatch(FENNEC_EVENT_QUEUE_SIZE);
|
||||
|
||||
// we query the size instead of empty just in case someone decides to run their own thread
|
||||
// and send events from that thread.
|
||||
size_t n = queue.size();
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
dispatch[i] = queue.pop();
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
_handle_event(dispatch[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void event::_add_listener(event_listener* listener, uint64_t type) {
|
||||
lock_guard guard(lock);
|
||||
|
||||
void event::add_listener(event_listener* listener, uint64_t type) {
|
||||
if (listeners.size() <= type) {
|
||||
listeners.resize(type + 1);
|
||||
}
|
||||
@@ -34,9 +61,29 @@ void event::add_listener(event_listener* listener, uint64_t type) {
|
||||
}
|
||||
|
||||
void event::remove_listener(event_listener* listener) {
|
||||
lock_guard guard(lock);
|
||||
|
||||
for (auto& it : listeners) {
|
||||
it.erase(listener);
|
||||
}
|
||||
}
|
||||
|
||||
void event::_handle_event(unique_ptr<event>& event) {
|
||||
auto type = event->get_type().id();
|
||||
for (auto& listener : listeners[type]) {
|
||||
listener->handle_event(event.get());
|
||||
}
|
||||
}
|
||||
|
||||
void event::_dispatch(unique_ptr<event>&& event) {
|
||||
// queue is taking ownership of the resource
|
||||
queue.emplace(event.release());
|
||||
}
|
||||
|
||||
void event::_dispatch_immediate(unique_ptr<event>&& event) {
|
||||
lock_guard guard(lock);
|
||||
|
||||
_handle_event(event);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user