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 <memory>
7 #include <mutex>
8 #include <utility>
9 #include <vector>
10 
11 namespace openmsx {
12 
13 class Reactor;
14 class EventListener;
15 
17 {
18 public:
19  using EventPtr = std::shared_ptr<const Event>;
20 
24  enum Priority {
28  MSX,
29  };
30 
31  explicit EventDistributor(Reactor& reactor);
32 
40  void registerEventListener(EventType type, EventListener& listener,
41  Priority priority = OTHER);
42 
48  void unregisterEventListener(EventType type, EventListener& listener);
49 
54  void distributeEvent(const EventPtr& event);
55 
60  void deliverEvents();
61 
68  bool sleep(unsigned us);
69 
70 private:
71  bool isRegistered(EventType type, EventListener* listener) const;
72 
73  Reactor& reactor;
74 
75  using PriorityMap = std::vector<std::pair<Priority, EventListener*>>; // sorted on priority
76  PriorityMap listeners[NUM_EVENT_TYPES];
77  using EventQueue = std::vector<EventPtr>;
78  EventQueue scheduledEvents;
79  std::mutex mutex; // lock datastructures
80  std::mutex cvMutex; // lock condition_variable
81  std::condition_variable condition;
82 };
83 
84 } // namespace openmsx
85 
86 #endif
Contains the main loop of openMSX.
Definition: Reactor.hh:66
void registerEventListener(EventType type, EventListener &listener, Priority priority=OTHER)
Registers a given object to receive certain events.
void unregisterEventListener(EventType type, EventListener &listener)
Unregisters a previously registered event listener.
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.
bool sleep(unsigned us)
Sleep for the specified amount of time, but return early when (another thread) called the distributeE...
EventDistributor(Reactor &reactor)
std::shared_ptr< const Event > EventPtr
void deliverEvents()
This actually delivers the events.
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
EventType
Definition: Event.hh:10