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
openmsx::EventDistributor::deliverEvents
void deliverEvents()
This actually delivers the events.
Definition: EventDistributor.cc:72
openmsx::EventDistributor::EventDistributor
EventDistributor(Reactor &reactor)
Definition: EventDistributor.cc:18
openmsx::EventDistributor::OTHER
@ OTHER
Definition: EventDistributor.hh:25
openmsx::EventDistributor::EventPtr
std::shared_ptr< const Event > EventPtr
Definition: EventDistributor.hh:19
openmsx::EventDistributor
Definition: EventDistributor.hh:17
openmsx::Reactor
Contains the main loop of openMSX.
Definition: Reactor.hh:67
openmsx::EventDistributor::Priority
Priority
Priorities from high to low, higher priority listeners can block events for lower priority listeners.
Definition: EventDistributor.hh:24
openmsx::EventDistributor::sleep
bool sleep(unsigned us)
Sleep for the specified amount of time, but return early when (another thread) called the distributeE...
Definition: EventDistributor.cc:114
openmsx::EventDistributor::HOTKEY
@ HOTKEY
Definition: EventDistributor.hh:27
openmsx::EventDistributor::MSX
@ MSX
Definition: EventDistributor.hh:28
openmsx::EventListener
Definition: EventListener.hh:11
openmsx::EventDistributor::CONSOLE
@ CONSOLE
Definition: EventDistributor.hh:26
openmsx::EventType
EventType
Definition: Event.hh:11
Event.hh
openmsx::EventDistributor::registerEventListener
void registerEventListener(EventType type, EventListener &listener, Priority priority=OTHER)
Registers a given object to receive certain events.
Definition: EventDistributor.cc:23
openmsx::NUM_EVENT_TYPES
@ NUM_EVENT_TYPES
Definition: Event.hh:80
openmsx::EventDistributor::unregisterEventListener
void unregisterEventListener(EventType type, EventListener &listener)
Unregisters a previously registered event listener.
Definition: EventDistributor.cc:35
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::EventDistributor::distributeEvent
void distributeEvent(const EventPtr &event)
Schedule the given event for delivery.
Definition: EventDistributor.cc:44