26 std::lock_guard<std::mutex> lock(mutex);
27 auto& priorityMap = listeners[type];
32 priorityMap.insert(it, {priority, &listener});
38 std::lock_guard<std::mutex> lock(mutex);
39 auto& priorityMap = listeners[type];
41 [&](
auto& v) {
return v.second == &listener; }));
53 std::unique_lock<std::mutex> lock(mutex);
54 if (!listeners[event->getType()].empty()) {
55 scheduledEvents.push_back(event);
61 condition.notify_all();
74 static PriorityMap priorityMapCopy;
75 static EventQueue eventsCopy;
83 std::unique_lock<std::mutex> lock(mutex);
91 while (!scheduledEvents.empty()) {
92 assert(eventsCopy.empty());
93 swap(eventsCopy, scheduledEvents);
94 for (
auto& event : eventsCopy) {
95 auto type =
event->getType();
96 priorityMapCopy = listeners[type];
98 int blockPriority = Priority::LOWEST;
99 for (
const auto& [priority, listener] : priorityMapCopy) {
102 if (!isRegistered(type, listener))
continue;
104 if (priority >= blockPriority)
break;
107 assert(block > priority);
108 blockPriority = block;
119 std::chrono::microseconds duration(us);
120 std::unique_lock<std::mutex> lock(cvMutex);
121 return condition.wait_for(lock, duration) == std::cv_status::timeout;
void unregisterEventListener(EventType type, EventListener &listener)
Unregisters a previously registered event listener.
EventDistributor(Reactor &reactor)
std::shared_ptr< const Event > EventPtr
bool sleep(unsigned us)
Sleep for the specified amount of time, but return early when (another thread) called the distributeE...
void registerEventListener(EventType type, EventListener &listener, Priority priority=OTHER)
Registers a given object to receive certain events.
void deliverEvents()
This actually delivers the events.
Priority
Priorities from high to low, higher priority listeners can block events for lower priority listeners.
void distributeEvent(const EventPtr &event)
Schedule the given event for delivery.
virtual int signalEvent(const std::shared_ptr< const Event > &event) noexcept=0
This method gets called when an event you are subscribed to occurs.
void execute()
Execute all expired RTSchedulables.
Contains the main loop of openMSX.
RTScheduler & getRTScheduler()
InputEventGenerator & getInputEventGenerator()
Interpreter & getInterpreter()
bool isMainThread()
Returns true when called from the main thread.
This file implemented 3 utility functions:
auto upper_bound(ForwardRange &&range, const T &value)
constexpr auto values(Map &&map)
constexpr bool contains(ITER first, ITER last, const VAL &val)
Check if a range contains a given value, using linear search.
constexpr auto rfind_if_unguarded(RANGE &range, PRED pred)