| 1 |
#include "EventDistributor.hh" |
| 2 |
|
| 3 |
#include "EventListener.hh" |
| 4 |
#include "InputEventGenerator.hh" |
| 5 |
|
| 6 |
#include "Interpreter.hh" |
| 7 |
#include "RTScheduler.hh" |
| 8 |
#include "Reactor.hh" |
| 9 |
#include "Thread.hh" |
| 10 |
|
| 11 |
#include "stl.hh" |
| 12 |
|
| 13 |
#include <algorithm> |
| 14 |
#include <cassert> |
| 15 |
|
| 16 |
namespace openmsx { |
| 17 |
|
| 18 |
EventDistributor::EventDistributor(Reactor& reactor_) |
| 19 |
: reactor(reactor_) |
| 20 |
{ |
| 21 |
} |
| 22 |
|
| 23 |
void EventDistributor::registerEventListener( |
| 24 |
EventType type, EventListener& listener, Priority priority) |
| 25 |
{ |
| 26 |
std::scoped_lock lock(mutex); |
| 27 |
auto& priorityMap = listeners[size_t(type)]; |
| 28 |
|
| 29 |
assert(!contains(priorityMap, &listener, &Entry::listener)); |
| 30 |
|
| 31 |
auto it = std::ranges::upper_bound(priorityMap, priority, {}, &Entry::priority); |
| 32 |
priorityMap.emplace(it, Entry{.priority = priority, .listener = &listener}); |
| 33 |
} |
| 34 |
|
| 35 |
void EventDistributor::unregisterEventListener( |
| 36 |
EventType type, EventListener& listener) |
| 37 |
{ |
| 38 |
std::scoped_lock lock(mutex); |
| 39 |
auto& priorityMap = listeners[size_t(type)]; |
| 40 |
priorityMap.erase(rfind_unguarded(priorityMap, &listener, &Entry::listener)); |
| 41 |
} |
| 42 |
|
| 43 |
void EventDistributor::distributeEvent(Event&& event) |
| 44 |
{ |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
std::unique_lock lock(mutex); |
| 52 |
if (!listeners[size_t(getType(event))].empty()) { |
| 53 |
scheduledEvents.push_back(std::move(event)); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
lock.unlock(); |
| 60 |
reactor.enterMainLoop(); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
bool EventDistributor::isRegistered(EventType type, EventListener* listener) const |
| 65 |
{ |
| 66 |
return contains(listeners[size_t(type)], listener, &Entry::listener); |
| 67 |
} |
| 68 |
|
| 69 |
void EventDistributor::deliverEvents(std::optional<int> timeoutMs) |
| 70 |
{ |
| 71 |
static PriorityMap priorityMapCopy; |
| 72 |
static EventQueue eventsCopy; |
| 73 |
|
| 74 |
assert(Thread::isMainThread()); |
| 75 |
|
| 76 |
reactor.getInputEventGenerator().poll(timeoutMs); |
| 77 |
reactor.getInterpreter().poll(); |
| 78 |
reactor.getRTScheduler().execute(); |
| 79 |
|
| 80 |
std::unique_lock lock(mutex); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
while (!scheduledEvents.empty()) { |
| 89 |
assert(eventsCopy.empty()); |
| 90 |
swap(eventsCopy, scheduledEvents); |
| 91 |
for (const auto& event : eventsCopy) { |
| 92 |
auto type = getType(event); |
| 93 |
priorityMapCopy = listeners[size_t(type)]; |
| 94 |
lock.unlock(); |
| 95 |
auto allowPriority = Priority::LOWEST; |
| 96 |
for (const auto& e : priorityMapCopy) { |
| 97 |
|
| 98 |
|
| 99 |
if (!isRegistered(type, e.listener)) continue; |
| 100 |
|
| 101 |
if (e.priority > allowPriority) break; |
| 102 |
|
| 103 |
|
| 104 |
if (e.listener->signalEvent(event)) { |
| 105 |
allowPriority = e.priority; |
| 106 |
} |
| 107 |
} |
| 108 |
lock.lock(); |
| 109 |
} |
| 110 |
eventsCopy.clear(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
} |