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 <string>
10 #include <memory>
11 #include <vector>
12 
13 namespace openmsx {
14 
15 class EventDistributor;
16 class CommandController;
17 class InfoCommand;
18 class GlobalCliComm;
19 class GlobalCommandController;
20 class GlobalSettings;
21 class CliComm;
22 class Display;
23 class Mixer;
24 class InputEventGenerator;
25 class DiskFactory;
26 class DiskManipulator;
27 class DiskChanger;
28 class FilePool;
29 class UserSettings;
30 class RomDatabase;
31 class TclCallbackMessages;
32 class BooleanSetting;
33 class MSXMotherBoard;
34 class Setting;
35 class CommandLineParser;
36 class AfterCommand;
37 class QuitCommand;
38 class MessageCommand;
39 class MachineCommand;
40 class TestMachineCommand;
41 class CreateMachineCommand;
42 class DeleteMachineCommand;
43 class ListMachinesCommand;
44 class ActivateMachineCommand;
45 class StoreMachineCommand;
46 class RestoreMachineCommand;
47 class AviRecorder;
48 class ConfigInfo;
49 class RealTimeInfo;
50 class GlobalSettings;
51 class PollEventGenerator;
52 template <typename T> class EnumSetting;
53 
62 class Reactor : 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  void pollNow();
77 
83  Mixer& getMixer();
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
102  CliComm& getCliComm();
103  std::string getMachineID() const;
104 
105  typedef std::unique_ptr<MSXMotherBoard> Board;
107  void replaceBoard(MSXMotherBoard& oldBoard, Board newBoard); // for reverse
108 
109 private:
110  typedef std::vector<Board> Boards;
111 
112  void createMachineSetting();
113  void switchBoard(MSXMotherBoard* newBoard);
114  void deleteBoard(MSXMotherBoard* board);
115  MSXMotherBoard& getMachine(const std::string& machineID) const;
116  std::vector<string_ref> getMachineIDs() const;
117 
118  // Observer<Setting>
119  virtual void update(const Setting& setting);
120 
121  // EventListener
122  virtual int signalEvent(const std::shared_ptr<const Event>& event);
123 
124  void unpause();
125  void pause();
126 
127  Semaphore mbSem; // this should come first, because it's still used by
128  // the destructors of the unique_ptr below
129 
130  // note: order of unique_ptr's is important
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  std::unique_ptr<Display> display;
137  std::unique_ptr<Mixer> mixer;
138  std::unique_ptr<DiskFactory> diskFactory;
139  std::unique_ptr<DiskManipulator> diskManipulator;
140  std::unique_ptr<DiskChanger> virtualDrive;
141  std::unique_ptr<FilePool> filePool;
142 
143  std::unique_ptr<EnumSetting<int>> machineSetting;
144  std::unique_ptr<UserSettings> userSettings;
145  std::unique_ptr<RomDatabase> softwareDatabase;
146 
147  std::unique_ptr<AfterCommand> afterCommand;
148  std::unique_ptr<QuitCommand> quitCommand;
149  std::unique_ptr<MessageCommand> messageCommand;
150  std::unique_ptr<MachineCommand> machineCommand;
151  std::unique_ptr<TestMachineCommand> testMachineCommand;
152  std::unique_ptr<CreateMachineCommand> createMachineCommand;
153  std::unique_ptr<DeleteMachineCommand> deleteMachineCommand;
154  std::unique_ptr<ListMachinesCommand> listMachinesCommand;
155  std::unique_ptr<ActivateMachineCommand> activateMachineCommand;
156  std::unique_ptr<StoreMachineCommand> storeMachineCommand;
157  std::unique_ptr<RestoreMachineCommand> restoreMachineCommand;
158  std::unique_ptr<AviRecorder> aviRecordCommand;
159  std::unique_ptr<ConfigInfo> extensionInfo;
160  std::unique_ptr<ConfigInfo> machineInfo;
161  std::unique_ptr<RealTimeInfo> realTimeInfo;
162  std::unique_ptr<TclCallbackMessages> tclCallbackMessages;
163  std::unique_ptr<PollEventGenerator> pollEventGenerator;
164 
165  // Locking rules for activeBoard access:
166  // - main thread can always access activeBoard without taking a lock
167  // - changing activeBoard handle can only be done in the main thread
168  // and needs to take the mbSem lock
169  // - non-main thread can only access activeBoard via specific
170  // member functions (atm only via enterMainLoop()), it needs to take
171  // the mbSem lock
172  Boards boards;
173  Boards garbageBoards;
174  MSXMotherBoard* activeBoard; // either nullptr or a board inside 'boards'
175 
176  int blockedCounter;
177  bool paused;
178 
184  bool running;
185 
186  bool isInit; // has the init() method been run successfully
187 
188  friend class MachineCommand;
189  friend class TestMachineCommand;
190  friend class CreateMachineCommand;
191  friend class DeleteMachineCommand;
192  friend class ListMachinesCommand;
194  friend class StoreMachineCommand;
195  friend class RestoreMachineCommand;
196 };
197 
198 } // namespace openmsx
199 
200 #endif // REACTOR_HH