Clean up some ThreadNexus remnants.
[rubinius.git] / machine / machine.hpp
blobbd20d548373b4949725baa2ce0def02a716e65ce
1 #ifndef RBX_MACHINE_H
2 #define RBX_MACHINE_H
4 #include "defines.hpp"
6 #include <atomic>
7 #include <condition_variable>
8 #include <cstdint>
9 #include <functional>
10 #include <list>
11 #include <mutex>
13 namespace rubinius {
14 class Fixnum;
15 class Object;
16 class Machine;
17 class Thread;
19 class ngLogger { };
20 class Environment;
21 class Configuration;
23 class Diagnostics;
25 namespace diagnostics {
26 class Diagnostic;
29 class Memory;
31 namespace memory {
32 class Collector;
35 class Signals;
36 class ngCodeDB { };
38 class C_API;
40 namespace jit {
41 class MachineCompiler;
44 class ngDebugger { };
46 class Profiler {
47 public:
48 Profiler() { }
49 virtual ~Profiler() { }
52 class Console;
54 class MachineState {
55 public:
56 enum Phase {
57 eBooting,
58 eRunning,
59 eHalting,
62 private:
63 ThreadState* _main_thread_;
64 Object* _loader_;
65 uint64_t _start_time_;
66 uint32_t _hash_seed_;
67 int _exit_code_;
68 std::atomic<Phase> _phase_;
70 public:
71 MachineState();
72 virtual ~MachineState() { }
74 const ThreadState* main_thread() {
75 return _main_thread_;
78 Object* loader() {
79 return _loader_;
82 void set_loader(Object* loader) {
83 _loader_ = loader;
86 const uint32_t hash_seed() {
87 return _hash_seed_;
90 const int exit_code() {
91 return _exit_code_;
94 void exit_code(int code) {
95 _exit_code_ = code;
98 bool booting_p() {
99 return _phase_ == eBooting;
102 bool running_p() {
103 return _phase_ == eRunning;
106 bool halting_p() {
107 return _phase_ == eHalting;
110 void set_halting() {
111 _phase_ = eHalting;
114 void set_start_time();
115 double run_time();
118 class Threads {
119 public:
120 typedef std::list<ThreadState*> ThreadList;
122 private:
123 Machine* machine_;
124 ThreadList threads_;
125 std::mutex threads_mutex_;
126 uint32_t thread_ids_;
128 public:
129 Threads(Machine* machine)
130 : machine_(machine)
131 , threads_()
132 , threads_mutex_()
133 , thread_ids_(0)
136 virtual ~Threads() { }
138 ThreadState* create_thread_state(const char* name = NULL);
139 void remove_thread_state(ThreadState* thread_state);
141 void after_fork_child(STATE);
143 void each(STATE, std::function<void (STATE, ThreadState*)> f);
146 class Machine {
147 MachineState* _machine_state_;
148 ngLogger* _logger_;
149 Threads* _threads_;
150 Configuration* _configuration_;
151 Environment* _environment_;
152 Diagnostics* _diagnostics_;
153 Memory* _memory_;
154 memory::Collector* _collector_;
155 Signals* _signals_;
156 ngCodeDB* _codedb_;
157 C_API* _c_api_;
158 jit::MachineCompiler* _compiler_;
159 ngDebugger* _debugger_;
160 Profiler* _profiler_;
161 Console* _console_;
163 static std::mutex _waiting_mutex_;
164 static std::condition_variable _waiting_condition_;
165 static std::atomic<uint32_t> _threads_lock_;
166 static std::atomic<uint64_t> _halting_;
167 static std::atomic<bool> _stop_;
169 public:
170 Machine(int argc, char** argv);
171 virtual ~Machine();
173 std::mutex& waiting_mutex() {
174 return _waiting_mutex_;
177 std::condition_variable& waiting_condition() {
178 return _waiting_condition_;
181 std::atomic<uint32_t>& threads_lock() {
182 return _threads_lock_;
185 std::atomic<uint64_t>& halting() {
186 return _halting_;
189 std::atomic<bool>& stop() {
190 return _stop_;
193 MachineState* const machine_state() {
194 return _machine_state_;
197 Threads* const threads() {
198 return _threads_;
201 Configuration* const configuration() {
202 return _configuration_;
205 Environment* const environment() const {
206 return _environment_;
209 Diagnostics* const diagnostics() {
210 return _diagnostics_;
213 Memory* const memory() {
214 return _memory_;
217 memory::Collector* const collector() {
218 return _collector_;
221 Signals* const signals() {
222 return _signals_;
225 C_API* const c_api() {
226 return _c_api_;
229 jit::MachineCompiler* const compiler() {
230 return _compiler_;
233 Profiler* const profiler() {
234 return _profiler_;
237 Console* const console() {
238 return _console_;
241 void boot();
243 int halt(STATE, Object* exit_code);
244 int halt(int exit_code=0);
245 int halt(STATE, int exit_code=0);
247 void before_fork(STATE);
248 void after_fork_parent(STATE);
249 void after_fork_child(STATE);
251 void trace_objects(STATE, std::function<void (STATE, Object**)> f);
253 Diagnostics* start_diagnostics(STATE);
254 void report_diagnostics(diagnostics::Diagnostic* diagnostic);
256 jit::MachineCompiler* start_compiler(STATE);
258 void halt_console(STATE);
259 void halt_profiler(STATE);
260 void halt_debugger(STATE);
261 void halt_compiler(STATE);
262 void halt_c_api(STATE);
263 void halt_codedb(STATE);
264 void halt_signals(STATE);
265 void halt_collector(STATE);
266 void halt_memory(STATE);
267 void halt_threads(STATE);
268 void halt_diagnostics(STATE);
269 void halt_configuration(STATE);
270 void halt_environment(STATE);
271 void halt_logger(STATE);
272 void halt_machine_state(STATE);
273 void halt_machine(STATE);
276 #endif