openMSX
EventDistributor.hh
Go to the documentation of this file.
1 #ifndef EVENTDISTRIBUTOR_HH
2 #define EVENTDISTRIBUTOR_HH
3 
4 #include "Event.hh"
5 #include <condition_variable>
6 #include <mutex>
7 #include <vector>
8 
9 namespace openmsx {
10 
11 class Reactor;
12 class EventListener;
13 
15 {
16 public:
20  enum Priority {
24  MSX,
25  LOWEST, // should only be used internally in EventDistributor
26  };
27 
28  explicit EventDistributor(Reactor& reactor);
29 
37  void registerEventListener(EventType type, EventListener& listener,
38  Priority priority = OTHER);
39 
45  void unregisterEventListener(EventType type, EventListener& listener);
46 
51  void distributeEvent(Event&& event);
52 
57  void deliverEvents();
58 
65  bool sleep(unsigned us);
66 
67 private:
68  [[nodiscard]] bool isRegistered(EventType type, EventListener* listener) const;
69 
70 private:
71  Reactor& reactor;
72 
73  struct Entry {
74  Priority priority;
75  EventListener* listener;
76  };
77  using PriorityMap = std::vector<Entry>; // sorted on priority
78  PriorityMap listeners[size_t(EventType::NUM_EVENT_TYPES)];
79  using EventQueue = std::vector<Event>;
80  EventQueue scheduledEvents;
81  std::mutex mutex; // lock datastructures
82  std::mutex cvMutex; // lock condition_variable
83  std::condition_variable condition;
84 };
85 
86 } // namespace openmsx
87 
88 #endif
void unregisterEventListener(EventType type, EventListener &listener)
Unregisters a previously registered event listener.
void distributeEvent(Event &&event)
Schedule the given event for delivery.
EventDistributor(Reactor &reactor)
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.
Contains the main loop of openMSX.
Definition: Reactor.hh:68
This file implemented 3 utility functions:
Definition: Autofire.cc:9
EventType
Definition: Event.hh:507