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