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  LOWEST, // should only be used internally in EventDistributor
30  };
31 
32  explicit EventDistributor(Reactor& reactor);
33 
41  void registerEventListener(EventType type, EventListener& listener,
42  Priority priority = OTHER);
43 
49  void unregisterEventListener(EventType type, EventListener& listener);
50 
55  void distributeEvent(const EventPtr& event);
56 
61  void deliverEvents();
62 
69  bool sleep(unsigned us);
70 
71 private:
72  [[nodiscard]] bool isRegistered(EventType type, EventListener* listener) const;
73 
74 private:
75  Reactor& reactor;
76 
77  using PriorityMap = std::vector<std::pair<Priority, EventListener*>>; // sorted on priority
78  PriorityMap listeners[NUM_EVENT_TYPES];
79  using EventQueue = std::vector<EventPtr>;
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.
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.
Contains the main loop of openMSX.
Definition: Reactor.hh:67
This file implemented 3 utility functions:
Definition: Autofire.cc:9
EventType
Definition: Event.hh:11
@ NUM_EVENT_TYPES
Definition: Event.hh:77