openMSX
Reactor.hh
Go to the documentation of this file.
1 #ifndef REACTOR_HH
2 #define REACTOR_HH
3 
4 #include "Observer.hh"
5 #include "EventListener.hh"
6 #include "string_ref.hh"
7 #include "openmsx.hh"
8 #include <string>
9 #include <memory>
10 #include <mutex>
11 #include <vector>
12 
13 namespace openmsx {
14 
15 class RTScheduler;
16 class EventDistributor;
17 class CommandController;
18 class InfoCommand;
19 class GlobalCliComm;
20 class GlobalCommandController;
21 class GlobalSettings;
22 class CliComm;
23 class Interpreter;
24 class Display;
25 class Mixer;
26 class InputEventGenerator;
27 class DiskFactory;
28 class DiskManipulator;
29 class DiskChanger;
30 class FilePool;
31 class UserSettings;
32 class RomDatabase;
33 class TclCallbackMessages;
34 class MSXMotherBoard;
35 class Setting;
36 class CommandLineParser;
37 class AfterCommand;
38 class QuitCommand;
39 class MessageCommand;
40 class MachineCommand;
41 class TestMachineCommand;
42 class CreateMachineCommand;
43 class DeleteMachineCommand;
44 class ListMachinesCommand;
45 class ActivateMachineCommand;
46 class StoreMachineCommand;
47 class RestoreMachineCommand;
48 class AviRecorder;
49 class ConfigInfo;
50 class RealTimeInfo;
51 template <typename T> class EnumSetting;
52 
61 class Reactor final : private Observer<Setting>, private EventListener
62 {
63 public:
64  Reactor();
65  void init();
66  ~Reactor();
67 
71  void run(CommandLineParser& parser);
72 
73  void enterMainLoop();
74 
75  RTScheduler& getRTScheduler() { return *rtScheduler; }
76  EventDistributor& getEventDistributor() { return *eventDistributor; }
77  GlobalCliComm& getGlobalCliComm() { return *globalCliComm; }
78  GlobalCommandController& getGlobalCommandController() { return *globalCommandController; }
79  InputEventGenerator& getInputEventGenerator() { return *inputEventGenerator; }
80  Display& getDisplay() { assert(display); return *display; }
81  Mixer& getMixer() { return *mixer; }
82  DiskFactory& getDiskFactory() { return *diskFactory; }
83  DiskManipulator& getDiskManipulator() { return *diskManipulator; }
84  EnumSetting<int>& getMachineSetting() { return *machineSetting; }
85  RomDatabase& getSoftwareDatabase() { return *softwareDatabase; }
86  FilePool& getFilePool() { return *filePool; }
87 
88  void switchMachine(const std::string& machine);
90 
91  static std::vector<std::string> getHwConfigs(string_ref type);
92 
93  void block();
94  void unblock();
95 
96  // convenience methods
97  GlobalSettings& getGlobalSettings() { return *globalSettings; }
100  CliComm& getCliComm();
102  std::string getMachineID() const;
103 
104  using Board = std::unique_ptr<MSXMotherBoard>;
106  void replaceBoard(MSXMotherBoard& oldBoard, Board newBoard); // for reverse
107 
108 private:
109  using Boards = std::vector<Board>;
110 
111  void createMachineSetting();
112  void switchBoard(MSXMotherBoard* newBoard);
113  void deleteBoard(MSXMotherBoard* board);
114  MSXMotherBoard& getMachine(string_ref machineID) const;
115  std::vector<string_ref> getMachineIDs() const;
116 
117  // Observer<Setting>
118  void update(const Setting& setting) override;
119 
120  // EventListener
121  int signalEvent(const std::shared_ptr<const Event>& event) override;
122 
123  void unpause();
124  void pause();
125 
126  std::mutex mbMutex; // this should come first, because it's still used by
127  // the destructors of the unique_ptr below
128 
129  // note: order of unique_ptr's is important
130  std::unique_ptr<RTScheduler> rtScheduler;
131  std::unique_ptr<EventDistributor> eventDistributor;
132  std::unique_ptr<GlobalCliComm> globalCliComm;
133  std::unique_ptr<GlobalCommandController> globalCommandController;
134  std::unique_ptr<GlobalSettings> globalSettings;
135  std::unique_ptr<InputEventGenerator> inputEventGenerator;
136 #if UNIQUE_PTR_BUG // see openmsx.hh
137  std::unique_ptr<Display> display2;
138  Display* display;
139 #else
140  std::unique_ptr<Display> display;
141 #endif
142  std::unique_ptr<Mixer> mixer;
143  std::unique_ptr<DiskFactory> diskFactory;
144  std::unique_ptr<DiskManipulator> diskManipulator;
145  std::unique_ptr<DiskChanger> virtualDrive;
146  std::unique_ptr<FilePool> filePool;
147 
148  std::unique_ptr<EnumSetting<int>> machineSetting;
149  std::unique_ptr<UserSettings> userSettings;
150  std::unique_ptr<RomDatabase> softwareDatabase;
151 
152  std::unique_ptr<AfterCommand> afterCommand;
153  std::unique_ptr<QuitCommand> quitCommand;
154  std::unique_ptr<MessageCommand> messageCommand;
155  std::unique_ptr<MachineCommand> machineCommand;
156  std::unique_ptr<TestMachineCommand> testMachineCommand;
157  std::unique_ptr<CreateMachineCommand> createMachineCommand;
158  std::unique_ptr<DeleteMachineCommand> deleteMachineCommand;
159  std::unique_ptr<ListMachinesCommand> listMachinesCommand;
160  std::unique_ptr<ActivateMachineCommand> activateMachineCommand;
161  std::unique_ptr<StoreMachineCommand> storeMachineCommand;
162  std::unique_ptr<RestoreMachineCommand> restoreMachineCommand;
163  std::unique_ptr<AviRecorder> aviRecordCommand;
164  std::unique_ptr<ConfigInfo> extensionInfo;
165  std::unique_ptr<ConfigInfo> machineInfo;
166  std::unique_ptr<RealTimeInfo> realTimeInfo;
167  std::unique_ptr<TclCallbackMessages> tclCallbackMessages;
168 
169  // Locking rules for activeBoard access:
170  // - main thread can always access activeBoard without taking a lock
171  // - changing activeBoard handle can only be done in the main thread
172  // and needs to take the mbMutex lock
173  // - non-main thread can only access activeBoard via specific
174  // member functions (atm only via enterMainLoop()), it needs to take
175  // the mbMutex lock
176  Boards boards; // unordered
177  Boards garbageBoards;
178  MSXMotherBoard* activeBoard; // either nullptr or a board inside 'boards'
179 
180  int blockedCounter;
181  bool paused;
182 
188  bool running;
189 
190  bool isInit; // has the init() method been run successfully
191 
192  friend class MachineCommand;
193  friend class TestMachineCommand;
194  friend class CreateMachineCommand;
195  friend class DeleteMachineCommand;
196  friend class ListMachinesCommand;
198  friend class StoreMachineCommand;
199  friend class RestoreMachineCommand;
200 };
201 
202 } // namespace openmsx
203 
204 #endif // REACTOR_HH
Contains the main loop of openMSX.
Definition: Reactor.hh:61
GlobalCommandController & getGlobalCommandController()
Definition: Reactor.hh:78
Represents the output window/screen of openMSX.
Definition: Display.hh:32
static std::vector< std::string > getHwConfigs(string_ref type)
Definition: Reactor.cc:285
void replaceBoard(MSXMotherBoard &oldBoard, Board newBoard)
Definition: Reactor.cc:363
std::unique_ptr< MSXMotherBoard > Board
Definition: Reactor.hh:104
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
InputEventGenerator & getInputEventGenerator()
Definition: Reactor.hh:79
CommandController & getCommandController()
Definition: Reactor.cc:275
EventDistributor & getEventDistributor()
Definition: Reactor.hh:76
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:328
EnumSetting< int > & getMachineSetting()
Definition: Reactor.hh:84
std::string getMachineID() const
Definition: Reactor.cc:334
void run(CommandLineParser &parser)
Main loop.
Definition: Reactor.cc:489
RTScheduler & getRTScheduler()
Definition: Reactor.hh:75
GlobalSettings & getGlobalSettings()
Definition: Reactor.hh:97
RomDatabase & getSoftwareDatabase()
Definition: Reactor.hh:85
DiskFactory & getDiskFactory()
Definition: Reactor.hh:82
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
GlobalCliComm & getGlobalCliComm()
Definition: Reactor.hh:77
InfoCommand & getOpenMSXInfoCommand()
Definition: Reactor.cc:280
FilePool & getFilePool()
Definition: Reactor.hh:86
DiskManipulator & getDiskManipulator()
Definition: Reactor.hh:83
void unblock()
Definition: Reactor.cc:570
Mixer & getMixer()
Definition: Reactor.hh:81
void enterMainLoop()
Definition: Reactor.cc:473
CliComm & getCliComm()
Definition: Reactor.cc:265
Board createEmptyMotherBoard()
Definition: Reactor.cc:358
Display & getDisplay()
Definition: Reactor.hh:80
void switchMachine(const std::string &machine)
Definition: Reactor.cc:387
Generic Gang-of-Four Observer class, templatized edition.
Definition: Observer.hh:9
This class contains settings that are used by several other class (including some singletons)...
Interpreter & getInterpreter()
Definition: Reactor.cc:270