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_view.hh"
7 #include <cassert>
8 #include <memory>
9 #include <mutex>
10 #include <string>
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 ExitCommand;
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 class SoftwareInfoTopic;
52 template <typename T> class EnumSetting;
53 
54 extern int exitCode;
55 
64 class Reactor final : private Observer<Setting>, private EventListener
65 {
66 public:
67  Reactor();
68  void init();
69  ~Reactor();
70 
74  void run(CommandLineParser& parser);
75 
76  void enterMainLoop();
77 
78  RTScheduler& getRTScheduler() { return *rtScheduler; }
79  EventDistributor& getEventDistributor() { return *eventDistributor; }
80  GlobalCliComm& getGlobalCliComm() { return *globalCliComm; }
81  GlobalCommandController& getGlobalCommandController() { return *globalCommandController; }
82  InputEventGenerator& getInputEventGenerator() { return *inputEventGenerator; }
83  Display& getDisplay() { assert(display); return *display; }
84  Mixer& getMixer() { return *mixer; }
85  DiskFactory& getDiskFactory() { return *diskFactory; }
86  DiskManipulator& getDiskManipulator() { return *diskManipulator; }
87  EnumSetting<int>& getMachineSetting() { return *machineSetting; }
88  FilePool& getFilePool() { return *filePool; }
89 
91 
92  void switchMachine(const std::string& machine);
94 
95  static std::vector<std::string> getHwConfigs(string_view type);
96 
97  void block();
98  void unblock();
99 
100  // convenience methods
101  GlobalSettings& getGlobalSettings() { return *globalSettings; }
104  CliComm& getCliComm();
106  string_view getMachineID() const;
107 
108  using Board = std::unique_ptr<MSXMotherBoard>;
110  void replaceBoard(MSXMotherBoard& oldBoard, Board newBoard); // for reverse
111 
112 private:
113  using Boards = std::vector<Board>;
114 
115  void createMachineSetting();
116  void switchBoard(MSXMotherBoard* newBoard);
117  void deleteBoard(MSXMotherBoard* board);
118  MSXMotherBoard& getMachine(string_view machineID) const;
119  std::vector<string_view> getMachineIDs() const;
120 
121  // Observer<Setting>
122  void update(const Setting& setting) override;
123 
124  // EventListener
125  int signalEvent(const std::shared_ptr<const Event>& event) override;
126 
127  void unpause();
128  void pause();
129 
130  std::mutex mbMutex; // this should come first, because it's still used by
131  // the destructors of the unique_ptr below
132 
133  // note: order of unique_ptr's is important
134  std::unique_ptr<RTScheduler> rtScheduler;
135  std::unique_ptr<EventDistributor> eventDistributor;
136  std::unique_ptr<GlobalCliComm> globalCliComm;
137  std::unique_ptr<GlobalCommandController> globalCommandController;
138  std::unique_ptr<GlobalSettings> globalSettings;
139  std::unique_ptr<InputEventGenerator> inputEventGenerator;
140  std::unique_ptr<Display> display;
141  std::unique_ptr<Mixer> mixer;
142  std::unique_ptr<DiskFactory> diskFactory;
143  std::unique_ptr<DiskManipulator> diskManipulator;
144  std::unique_ptr<DiskChanger> virtualDrive;
145  std::unique_ptr<FilePool> filePool;
146 
147  std::unique_ptr<EnumSetting<int>> machineSetting;
148  std::unique_ptr<UserSettings> userSettings;
149  std::unique_ptr<RomDatabase> softwareDatabase;
150 
151  std::unique_ptr<AfterCommand> afterCommand;
152  std::unique_ptr<ExitCommand> exitCommand;
153  std::unique_ptr<MessageCommand> messageCommand;
154  std::unique_ptr<MachineCommand> machineCommand;
155  std::unique_ptr<TestMachineCommand> testMachineCommand;
156  std::unique_ptr<CreateMachineCommand> createMachineCommand;
157  std::unique_ptr<DeleteMachineCommand> deleteMachineCommand;
158  std::unique_ptr<ListMachinesCommand> listMachinesCommand;
159  std::unique_ptr<ActivateMachineCommand> activateMachineCommand;
160  std::unique_ptr<StoreMachineCommand> storeMachineCommand;
161  std::unique_ptr<RestoreMachineCommand> restoreMachineCommand;
162  std::unique_ptr<AviRecorder> aviRecordCommand;
163  std::unique_ptr<ConfigInfo> extensionInfo;
164  std::unique_ptr<ConfigInfo> machineInfo;
165  std::unique_ptr<RealTimeInfo> realTimeInfo;
166  std::unique_ptr<SoftwareInfoTopic> softwareInfoTopic;
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 = nullptr; // either nullptr or a board inside 'boards'
179 
180  int blockedCounter = 0;
181  bool paused = false;
182 
188  bool running = true;
189 
190  bool isInit = false; // 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:64
GlobalCommandController & getGlobalCommandController()
Definition: Reactor.hh:81
Represents the output window/screen of openMSX.
Definition: Display.hh:31
void replaceBoard(MSXMotherBoard &oldBoard, Board newBoard)
Definition: Reactor.cc:374
std::unique_ptr< MSXMotherBoard > Board
Definition: Reactor.hh:108
static std::vector< std::string > getHwConfigs(string_view type)
Definition: Reactor.cc:300
InputEventGenerator & getInputEventGenerator()
Definition: Reactor.hh:82
CommandController & getCommandController()
Definition: Reactor.cc:290
EventDistributor & getEventDistributor()
Definition: Reactor.hh:79
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:342
EnumSetting< int > & getMachineSetting()
Definition: Reactor.hh:87
RomDatabase & getSoftwareDatabase()
Definition: Reactor.cc:272
void run(CommandLineParser &parser)
Main loop.
Definition: Reactor.cc:493
RTScheduler & getRTScheduler()
Definition: Reactor.hh:78
GlobalSettings & getGlobalSettings()
Definition: Reactor.hh:101
DiskFactory & getDiskFactory()
Definition: Reactor.hh:85
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
GlobalCliComm & getGlobalCliComm()
Definition: Reactor.hh:80
InfoCommand & getOpenMSXInfoCommand()
Definition: Reactor.cc:295
FilePool & getFilePool()
Definition: Reactor.hh:88
DiskManipulator & getDiskManipulator()
Definition: Reactor.hh:86
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
void unblock()
Definition: Reactor.cc:574
Mixer & getMixer()
Definition: Reactor.hh:84
void enterMainLoop()
Definition: Reactor.cc:477
CliComm & getCliComm()
Definition: Reactor.cc:280
Board createEmptyMotherBoard()
Definition: Reactor.cc:369
Display & getDisplay()
Definition: Reactor.hh:83
void switchMachine(const std::string &machine)
Definition: Reactor.cc:398
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)...
string_view getMachineID() const
Definition: Reactor.cc:348
Interpreter & getInterpreter()
Definition: Reactor.cc:285
int exitCode
Definition: Reactor.cc:59