Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 468 lines (367 sloc) 11.838 kb
d769c94 Initial work on new C++ VM
Evan Phoenix authored
1 #include "vm.hpp"
2 #include "objectmemory.hpp"
9eb4748 Flesh out Task considerable more
Evan Phoenix authored
3 #include "event.hpp"
4 #include "global_cache.hpp"
baf87dc GC performance tweeks
Evan Phoenix authored
5 #include "gc/gc.hpp"
3a0e9a4 Eero Saynatkari Renamed vm/object.hpp to vm/object_utils.hpp. File/#include shuffling.
rue authored
6
7 #include "vm/object_utils.hpp"
8
6ac48bd Fixed a LOT of dependencies by stripping objects.hpp and prelude.hpp as ...
Ryan Davis authored
9 #include "builtin/class.hpp"
10 #include "builtin/fixnum.hpp"
11 #include "builtin/list.hpp"
255a070 Brian Shirai Re-enabled instrumenting profiler.
brixen authored
12 #include "builtin/lookuptable.hpp"
6ac48bd Fixed a LOT of dependencies by stripping objects.hpp and prelude.hpp as ...
Ryan Davis authored
13 #include "builtin/symbol.hpp"
14 #include "builtin/thread.hpp"
15 #include "builtin/tuple.hpp"
16 #include "builtin/string.hpp"
58f050c Brian Shirai Ensure probe variables are initialized.
brixen authored
17 #include "builtin/taskprobe.hpp"
5d3b39f Populate a StackError with a backtrace
Evan Phoenix authored
18 #include "builtin/system.hpp"
d769c94 Initial work on new C++ VM
Evan Phoenix authored
19
255a070 Brian Shirai Re-enabled instrumenting profiler.
brixen authored
20 #include "instruments/profiler.hpp"
21
b09a24c Wire the JIT in via MachineMethod
Evan Phoenix authored
22 #include "config_parser.hpp"
6c31ab6 Add cli switch to enable jit
Evan Phoenix authored
23 #include "config.h"
1dd0497 Hooked a bit of Threads back up
Evan Phoenix authored
24
25 #include "native_thread.hpp"
3d7bc75 Fix reraising off non-Exception raise values
Evan Phoenix authored
26 #include "call_frame.hpp"
e6bb96a Fix a number of IO/Signal interaction issues
Evan Phoenix authored
27 #include "signal.hpp"
bf09647 Add ability to turn on GIL debugging at runtime
Evan Phoenix authored
28 #include "configuration.hpp"
8dc150e Implement ConfigParser, wire in into RUBY_CONFIG.
Evan Phoenix authored
29
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
30 #include <iostream>
4e26843 Improve young GC and add more GC stats
Evan Phoenix authored
31 #include <iomanip>
f9ec582 Add preemptive thread scheduling
Evan Phoenix authored
32 #include <signal.h>
3d3c195 Dirkjan Bussink Small build fixes for FreeBSD
dbussink authored
33 #include <sys/resource.h>
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
34
f5cf833 Reorganize ontology and class initialization
Evan Phoenix authored
35 // Reset macros since we're inside state
36 #undef G
37 #undef GO
38 #define G(whatever) globals.whatever.get()
39 #define GO(whatever) globals.whatever
40
d769c94 Initial work on new C++ VM
Evan Phoenix authored
41 namespace rubinius {
6cfae75 Add VMManager, fix exit to behave well
Evan Phoenix authored
42
bf09647 Add ability to turn on GIL debugging at runtime
Evan Phoenix authored
43 bool GlobalLock::debug_locking = false;
38100a0 Add simple recursion protection
Evan Phoenix authored
44 int VM::cStackDepthMax = 655300;
45
41400f4 Remove VMManager, simply SharedState/VM workflow
Evan Phoenix authored
46 VM::VM(SharedState& shared)
47 : saved_call_frame_(0)
17b1959 Add deletion of unused VMs
Evan Phoenix authored
48 , alive_(true)
51cb55a Brian Shirai Fixed profiler.
brixen authored
49 , profiler_(0)
e6bb96a Fix a number of IO/Signal interaction issues
Evan Phoenix authored
50 , run_signals_(false)
6cfae75 Add VMManager, fix exit to behave well
Evan Phoenix authored
51 , shared(shared)
0d55903 Brian Shirai Initialize waiter_ in VM constructor.
brixen authored
52 , waiter_(NULL)
6cfae75 Add VMManager, fix exit to behave well
Evan Phoenix authored
53 , globals(shared.globals)
54 , om(shared.om)
55 , global_cache(shared.global_cache)
56 , interrupts(shared.interrupts)
57 , symbols(shared.symbols)
07fc27b Hook up signal delivery
Evan Phoenix authored
58 , check_local_interrupts(false)
12aeac9 Fix return-in-ensure, a stack problem, and more
Evan Phoenix authored
59 , thread_state_(this)
1dd0497 Hooked a bit of Threads back up
Evan Phoenix authored
60 , thread(this, (Thread*)Qnil)
6cfae75 Add VMManager, fix exit to behave well
Evan Phoenix authored
61 , current_mark(NULL)
31ad5c2 Introduces the dynamic interpreter
Evan Phoenix authored
62 , reuse_llvm(true)
63 , use_safe_position(false)
6cfae75 Add VMManager, fix exit to behave well
Evan Phoenix authored
64 {}
65
41400f4 Remove VMManager, simply SharedState/VM workflow
Evan Phoenix authored
66 void VM::discard(VM* vm) {
67 vm->alive_ = false;
68 vm->saved_call_frame_ = 0;
69 if(vm->profiler_) {
70 vm->shared.remove_profiler(vm, vm->profiler_);
255a070 Brian Shirai Re-enabled instrumenting profiler.
brixen authored
71 }
41400f4 Remove VMManager, simply SharedState/VM workflow
Evan Phoenix authored
72
73 vm->shared.remove_vm(vm);
74 delete vm;
17b1959 Add deletion of unused VMs
Evan Phoenix authored
75 }
76
37dc489 Add real Configuration infrastructure
Evan Phoenix authored
77 void VM::initialize() {
1615ead Refactoring of ObjectMemory's object creation
Evan Phoenix authored
78 VM::register_state(this);
79
37dc489 Add real Configuration infrastructure
Evan Phoenix authored
80 om = new ObjectMemory(this, shared.config);
6cfae75 Add VMManager, fix exit to behave well
Evan Phoenix authored
81 shared.om = om;
82
8906a3f A slew of GC fixes
Evan Phoenix authored
83 probe.set(Qnil, &globals.roots);
5be79de Implement simple context cache
Evan Phoenix authored
84
6cfae75 Add VMManager, fix exit to behave well
Evan Phoenix authored
85 global_cache = new GlobalCache;
86 shared.global_cache = global_cache;
6c31ab6 Add cli switch to enable jit
Evan Phoenix authored
87
86fe9a8 Eero Saynatkari Some notes on Thread/VM interaction.
rue authored
88 /** @todo Done by Environment::boot_vm(), and Thread::s_new()
89 * does not boot at all. Should this be removed? --rue */
90 // this->boot();
6cfae75 Add VMManager, fix exit to behave well
Evan Phoenix authored
91
92 shared.set_initialized();
e53284c Wire background compiling thread into JIT mode
Evan Phoenix authored
93
94 // This seems like we should do this in VM(), ie, for every VM and
95 // therefore every Thread object in the process. But in fact, because
96 // we're using the GIL atm, we only do it once. When the GIL goes
97 // away, this needs to be moved to VM().
98
99 shared.gc_dependent();
6c31ab6 Add cli switch to enable jit
Evan Phoenix authored
100 }
101
102 void VM::boot() {
5e917ee Move TypeInfo::init under ObjectMemory
Evan Phoenix authored
103 TypeInfo::auto_learn_fields(this);
5be79de Implement simple context cache
Evan Phoenix authored
104
d769c94 Initial work on new C++ VM
Evan Phoenix authored
105 bootstrap_ontology();
da8639a Added libev integration. 60% coverage.
Evan Phoenix authored
106
3f594b6 Eero Saynatkari Changed all TODO:s to @todo so that doxygen picks them up.
rue authored
107 /* @todo Using a single default loop, revisit when many loops.
108 * @todo This needs to be handled through the environment.
6767c76 Charles Comstock disabled EVBACKEND_EPOLL as it seems to make linux unhappy -- this shoul...
dgtized authored
109 * (disabled epoll backend as it frequently caused hangs on epoll_wait)
d75d4be Eero Saynatkari Add forkcheck flag to event loop and init SIGCHLD handler at VM startup.
rue authored
110 */
b4cfdcb Fix subtend, irb runs
Evan Phoenix authored
111 // signal_events = new event::Loop(EVFLAG_FORKCHECK | EVBACKEND_SELECT | EVBACKEND_POLL);
112 // events = signal_events;
432b29d Get send_on_signal working and couple simple fixes.
Evan Phoenix authored
113
b4cfdcb Fix subtend, irb runs
Evan Phoenix authored
114 // signal_events->start(new event::Child::Event(this));
115
116 events = 0;
117 signal_events = 0;
d75d4be Eero Saynatkari Add forkcheck flag to event loop and init SIGCHLD handler at VM startup.
rue authored
118
31ad5c2 Introduces the dynamic interpreter
Evan Phoenix authored
119 VMMethod::init(this);
120
86fe9a8 Eero Saynatkari Some notes on Thread/VM interaction.
rue authored
121 /** @todo Should a thread be starting a VM or is it the other way around? */
49d27d7 Move LLVM's lazy code generation into the execute pipeline
Evan Phoenix authored
122 boot_threads();
f9ec582 Add preemptive thread scheduling
Evan Phoenix authored
123
124 // Force these back to false because creating the default Thread
125 // sets them to true.
126 interrupts.enable_preempt = false;
bf09647 Add ability to turn on GIL debugging at runtime
Evan Phoenix authored
127
128 GlobalLock::debug_locking = shared.config.gil_debug;
d769c94 Initial work on new C++ VM
Evan Phoenix authored
129 }
130
bc4b80f Remove STATE argument from debug functions
Evan Phoenix authored
131 // HACK so not thread safe or anything!
132 static VM* __state = NULL;
133
134 VM* VM::current_state() {
135 return __state;
136 }
137
138 void VM::register_state(VM *vm) {
139 __state = vm;
140 }
141
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
142 void VM::boot_threads() {
d0dbabb Rework how pthread_t is passed down
Evan Phoenix authored
143 thread.set(Thread::create(this, this, pthread_self()), &globals.roots);
2cc2e45 Eero Saynatkari Update/fix Thread handling in VM. Possibly over-cautious again.
rue authored
144 thread->sleep(this, Qfalse);
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
145 }
146
78632ae Remove using Type::fields infavor of sizeof(Type)
Evan Phoenix authored
147 Object* VM::new_object_typed(Class* cls, size_t bytes, object_type type) {
148 return om->new_object_typed(cls, bytes, type);
149 }
150
2fd060c Fix spurious wakeup of Condition
Evan Phoenix authored
151 Object* VM::new_object_typed_mature(Class* cls, size_t bytes, object_type type) {
152 return om->new_object_typed_mature(cls, bytes, type);
153 }
154
78632ae Remove using Type::fields infavor of sizeof(Type)
Evan Phoenix authored
155 Object* VM::new_object_from_type(Class* cls, TypeInfo* ti) {
156 return om->new_object_typed(cls, ti->instance_size, ti->type);
15fc505 More builtin objects, and a README file
Evan Phoenix authored
157 }
158
e8107bb Remove all traces of ::fields
Evan Phoenix authored
159 Class* VM::new_basic_class(Class* sup) {
ea2b516 Allocate Class/Module/IncludedModule as enduring objects
Evan Phoenix authored
160 Class *cls = om->new_object_enduring<Class>(G(klass));
3cee96d Add ability to inline ivar accessors
Evan Phoenix authored
161 cls->set_class_id(shared.inc_class_count());
0be116a Add prelim support for packed object layout
Evan Phoenix authored
162 cls->set_packed_size(0);
3cee96d Add ability to inline ivar accessors
Evan Phoenix authored
163
f5cf833 Reorganize ontology and class initialization
Evan Phoenix authored
164 if(sup->nil_p()) {
387bc2d Brian Shirai Converted to attr_accessor for C++ member variable access.
brixen authored
165 cls->instance_type(this, Fixnum::from(ObjectType));
481c5e5 Class / TypeInfo cleanup
Evan Phoenix authored
166 cls->set_type_info(find_type(ObjectType));
f5cf833 Reorganize ontology and class initialization
Evan Phoenix authored
167 } else {
387bc2d Brian Shirai Converted to attr_accessor for C++ member variable access.
brixen authored
168 cls->instance_type(this, sup->instance_type()); // HACK test that this is always true
481c5e5 Class / TypeInfo cleanup
Evan Phoenix authored
169 cls->set_type_info(sup->type_info());
f5cf833 Reorganize ontology and class initialization
Evan Phoenix authored
170 }
387bc2d Brian Shirai Converted to attr_accessor for C++ member variable access.
brixen authored
171 cls->superclass(this, sup);
f5cf833 Reorganize ontology and class initialization
Evan Phoenix authored
172
173 return cls;
174 }
175
176 Class* VM::new_class(const char* name) {
e8107bb Remove all traces of ::fields
Evan Phoenix authored
177 return new_class(name, G(object), G(object));
f5cf833 Reorganize ontology and class initialization
Evan Phoenix authored
178 }
179
180 Class* VM::new_class(const char* name, Class* super_class) {
e8107bb Remove all traces of ::fields
Evan Phoenix authored
181 return new_class(name, super_class, G(object));
f5cf833 Reorganize ontology and class initialization
Evan Phoenix authored
182 }
183
e8107bb Remove all traces of ::fields
Evan Phoenix authored
184 Class* VM::new_class(const char* name, Class* sup, Module* under) {
185 Class* cls = new_basic_class(sup);
f5cf833 Reorganize ontology and class initialization
Evan Phoenix authored
186 cls->setup(this, name, under);
187
188 // HACK test that we've got the MOP setup properly
189 MetaClass::attach(this, cls, sup->metaclass(this));
190 return cls;
191 }
192
193 Class* VM::new_class_under(const char* name, Module* under) {
e8107bb Remove all traces of ::fields
Evan Phoenix authored
194 return new_class(name, G(object), under);
f5cf833 Reorganize ontology and class initialization
Evan Phoenix authored
195 }
196
197 Module* VM::new_module(const char* name, Module* under) {
78632ae Remove using Type::fields infavor of sizeof(Type)
Evan Phoenix authored
198 Module *mod = new_object<Module>(G(module));
f5cf833 Reorganize ontology and class initialization
Evan Phoenix authored
199 mod->setup(this, name, under);
200 return mod;
201 }
202
203
1fdd151 Eero Saynatkari Changed all OBJECT, SYMBOL, FIXNUM, INTEGER to normal syntax.
rue authored
204 Symbol* VM::symbol(const char* str) {
5f2adf4 Brian Shirai Replaced SymbolTable with native C++ class using STL.
brixen authored
205 return symbols.lookup(this, str);
304caf3 Bunch more work.
Evan Phoenix authored
206 }
207
1fdd151 Eero Saynatkari Changed all OBJECT, SYMBOL, FIXNUM, INTEGER to normal syntax.
rue authored
208 Symbol* VM::symbol(String* str) {
5f2adf4 Brian Shirai Replaced SymbolTable with native C++ class using STL.
brixen authored
209 return symbols.lookup(this, str);
195ef1b Brian Shirai Isolated access to SymbolTable behind VM methods.
brixen authored
210 }
211
1fdd151 Eero Saynatkari Changed all OBJECT, SYMBOL, FIXNUM, INTEGER to normal syntax.
rue authored
212 void type_assert(STATE, Object* obj, object_type type, const char* reason) {
481c5e5 Class / TypeInfo cleanup
Evan Phoenix authored
213 if((obj->reference_p() && obj->type_id() != type)
a1704be Brian Shirai Rework exceptions in the VM.
brixen authored
214 || (type == FixnumType && !obj->fixnum_p())) {
215 Exception::type_error(state, type, obj, reason);
fceff3d NativeFunction, Selector, SendSite
Evan Phoenix authored
216 }
304caf3 Bunch more work.
Evan Phoenix authored
217 }
4c08cc4 Add object cleanup post collect and on shutdown
Evan Phoenix authored
218
5d3b39f Populate a StackError with a backtrace
Evan Phoenix authored
219 void VM::raise_stack_error(CallFrame* call_frame) {
220 G(stack_error)->locations(this, System::vm_backtrace(this, Fixnum::from(0), call_frame));
38100a0 Add simple recursion protection
Evan Phoenix authored
221 thread_state()->raise_exception(G(stack_error));
222 }
223
224 void VM::init_stack_size() {
225 struct rlimit rlim;
226 if (getrlimit(RLIMIT_STACK, &rlim) == 0) {
227 unsigned int space = rlim.rlim_cur/5;
228
229 if (space > 1024*1024) space = 1024*1024;
230 cStackDepthMax = (rlim.rlim_cur - space);
231 }
232 }
233
388b751 Wire up primitives to be resolved and executed.
Evan Phoenix authored
234 TypeInfo* VM::find_type(int type) {
235 return om->type_info[type];
236 }
237
f1fa512 Eric Hodel Oh yeah, C++ isn't ruby.
drbrain authored
238 Thread *VM::current_thread() {
6f546f0 Eric Hodel Implement VM::current_thread().
drbrain authored
239 return globals.current_thread.get();
da8639a Added libev integration. 60% coverage.
Evan Phoenix authored
240 }
f10f155 Pass in valgrind to find some GC bugs
Evan Phoenix authored
241
3dc347c Fix memory thrash and interrupt checks
Evan Phoenix authored
242 void VM::run_gc_soon() {
243 om->collect_young_now = true;
244 om->collect_mature_now = true;
d390938 Cleanup interrupt checking, improve JIT debugging output
Evan Phoenix authored
245 interrupts.set_perform_gc();
3dc347c Fix memory thrash and interrupt checks
Evan Phoenix authored
246 }
247
f0e8625 Remove task from execution logic, add code to run GC
Evan Phoenix authored
248 void VM::collect(CallFrame* call_frame) {
e04d3da Performance fix for seeing all top CallFrames
Evan Phoenix authored
249 this->set_call_frame(call_frame);
bb9618c Fix wait, Channels, and GC for multiple threads
Evan Phoenix authored
250
0c8eb66 Healthy living through better thread management
Evan Phoenix authored
251 // Stops all other threads, so we're only here by ourselves.
252 StopTheWorld guard(this);
253
1431083 Add OnStack class to protect C++ variables from GC
Evan Phoenix authored
254 GCData gc_data(this);
255
256 om->collect_young(gc_data);
257 om->collect_mature(gc_data);
f10f155 Pass in valgrind to find some GC bugs
Evan Phoenix authored
258 }
52c078b Fleshing out Instruction tests, more type safety
Evan Phoenix authored
259
f0e8625 Remove task from execution logic, add code to run GC
Evan Phoenix authored
260 void VM::collect_maybe(CallFrame* call_frame) {
e04d3da Performance fix for seeing all top CallFrames
Evan Phoenix authored
261 this->set_call_frame(call_frame);
e53284c Wire background compiling thread into JIT mode
Evan Phoenix authored
262
263 // Stops all other threads, so we're only here by ourselves.
264 StopTheWorld guard(this);
265
1431083 Add OnStack class to protect C++ variables from GC
Evan Phoenix authored
266 GCData gc_data(this);
bb9618c Fix wait, Channels, and GC for multiple threads
Evan Phoenix authored
267
4e26843 Improve young GC and add more GC stats
Evan Phoenix authored
268 uint64_t start_time = 0;
269
f781b6a Move GC running into VM, remove Task::check_interrupts
Evan Phoenix authored
270 if(om->collect_young_now) {
720b6a4 Add -Xgc.show to print out when the GC runs
Evan Phoenix authored
271 if(shared.config.gc_show) {
4e26843 Improve young GC and add more GC stats
Evan Phoenix authored
272 start_time = get_current_time();
720b6a4 Add -Xgc.show to print out when the GC runs
Evan Phoenix authored
273 }
ef185e2 Refactor timing code and add GC timing
Evan Phoenix authored
274
4e26843 Improve young GC and add more GC stats
Evan Phoenix authored
275 YoungCollectStats stats;
5599aa0 Brian Shirai Added pseudo profiler methods for GC runs.
brixen authored
276
277 #ifdef RBX_PROFILER
278 if(unlikely(shared.profiling())) {
279 profiler::MethodEntry method(this, profiler::kYoungGC);
280 om->collect_young(gc_data, &stats);
281 } else {
282 om->collect_young(gc_data, &stats);
283 }
284 #else
4e26843 Improve young GC and add more GC stats
Evan Phoenix authored
285 om->collect_young(gc_data, &stats);
5599aa0 Brian Shirai Added pseudo profiler methods for GC runs.
brixen authored
286 #endif
651bdc0 Show GC stops as well with -Xgc.show
Evan Phoenix authored
287
288 if(shared.config.gc_show) {
4e26843 Improve young GC and add more GC stats
Evan Phoenix authored
289 uint64_t fin_time = get_current_time();
290 int diff = (fin_time - start_time) / 1000000;
291
292 fprintf(stderr, "[GC %0.1f%% %d/%d %d %2dms]\n",
293 stats.percentage_used,
294 stats.promoted_objects,
295 stats.excess_objects,
296 stats.lifetime,
297 diff);
651bdc0 Show GC stops as well with -Xgc.show
Evan Phoenix authored
298 }
f781b6a Move GC running into VM, remove Task::check_interrupts
Evan Phoenix authored
299 }
300
301 if(om->collect_mature_now) {
4e26843 Improve young GC and add more GC stats
Evan Phoenix authored
302 int before_kb = 0;
303
720b6a4 Add -Xgc.show to print out when the GC runs
Evan Phoenix authored
304 if(shared.config.gc_show) {
4e26843 Improve young GC and add more GC stats
Evan Phoenix authored
305 start_time = get_current_time();
306 before_kb = om->mature_bytes_allocated() / 1024;
720b6a4 Add -Xgc.show to print out when the GC runs
Evan Phoenix authored
307 }
ef185e2 Refactor timing code and add GC timing
Evan Phoenix authored
308
5599aa0 Brian Shirai Added pseudo profiler methods for GC runs.
brixen authored
309 #ifdef RBX_PROFILER
310 if(unlikely(shared.profiling())) {
311 profiler::MethodEntry method(this, profiler::kMatureGC);
312 om->collect_mature(gc_data);
313 } else {
314 om->collect_mature(gc_data);
315 }
316 #else
1431083 Add OnStack class to protect C++ variables from GC
Evan Phoenix authored
317 om->collect_mature(gc_data);
5599aa0 Brian Shirai Added pseudo profiler methods for GC runs.
brixen authored
318 #endif
ef185e2 Refactor timing code and add GC timing
Evan Phoenix authored
319
651bdc0 Show GC stops as well with -Xgc.show
Evan Phoenix authored
320 if(shared.config.gc_show) {
4e26843 Improve young GC and add more GC stats
Evan Phoenix authored
321 uint64_t fin_time = get_current_time();
322 int diff = (fin_time - start_time) / 1000000;
323 int kb = om->mature_bytes_allocated() / 1024;
324 fprintf(stderr, "[Full GC %dkB => %dkB %2dms]\n",
325 before_kb,
326 kb,
327 diff);
651bdc0 Show GC stops as well with -Xgc.show
Evan Phoenix authored
328 }
329
f781b6a Move GC running into VM, remove Task::check_interrupts
Evan Phoenix authored
330 }
331 }
332
1fdd151 Eero Saynatkari Changed all OBJECT, SYMBOL, FIXNUM, INTEGER to normal syntax.
rue authored
333 void VM::set_const(const char* name, Object* val) {
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
334 globals.object->set_const(this, (char*)name, val);
335 }
879a046 A whole slew. Enhanced type-info, debugged GC.
Evan Phoenix authored
336
1fdd151 Eero Saynatkari Changed all OBJECT, SYMBOL, FIXNUM, INTEGER to normal syntax.
rue authored
337 void VM::set_const(Module* mod, const char* name, Object* val) {
feb91b4 Tons of stuff (too much probably)
Evan Phoenix authored
338 mod->set_const(this, (char*)name, val);
339 }
310a461 Cleanup stack usage and fix interpreter
Evan Phoenix authored
340
341 void VM::print_backtrace() {
bb9618c Fix wait, Channels, and GC for multiple threads
Evan Phoenix authored
342 abort();
310a461 Cleanup stack usage and fix interpreter
Evan Phoenix authored
343 }
344
31ad5c2 Introduces the dynamic interpreter
Evan Phoenix authored
345 void VM::raise_exception_safely(Exception* exc) {
12aeac9 Fix return-in-ensure, a stack problem, and more
Evan Phoenix authored
346 abort();
31ad5c2 Introduces the dynamic interpreter
Evan Phoenix authored
347 safe_position_data.exc = exc;
348 siglongjmp(safe_position, cReasonException);
349 // Never reached.
350 }
351
352 void VM::raise_typeerror_safely(TypeError* err) {
12aeac9 Fix return-in-ensure, a stack problem, and more
Evan Phoenix authored
353 abort();
31ad5c2 Introduces the dynamic interpreter
Evan Phoenix authored
354 safe_position_data.type_error = err;
355 siglongjmp(safe_position, cReasonTypeError);
356 // Never reached.
357 }
358
359 void VM::raise_assertion_safely(Assertion* err) {
12aeac9 Fix return-in-ensure, a stack problem, and more
Evan Phoenix authored
360 abort();
31ad5c2 Introduces the dynamic interpreter
Evan Phoenix authored
361 safe_position_data.assertion = err;
362 siglongjmp(safe_position, cReasonAssertion);
363 // Never reached.
364 }
365
f9ec582 Add preemptive thread scheduling
Evan Phoenix authored
366 // Trampoline to call scheduler_loop()
367 static void* __thread_tramp__(void* arg) {
368 VM* state = static_cast<VM*>(arg);
369 state->scheduler_loop();
370 return NULL;
371 }
372
373 // Create the preemption thread and call scheduler_loop() in the new thread
374 void VM::setup_preemption() {
375 if(pthread_create(&preemption_thread, NULL, __thread_tramp__, this) != 0) {
376 std::cout << "Unable to create preemption thread!\n";
377 }
378 }
379
380 // Runs forever, telling the VM to reschedule threads ever 10 milliseconds
381 void VM::scheduler_loop() {
382 // First off, we don't want this thread ever receiving a signal.
383 sigset_t mask;
384 sigfillset(&mask);
b4cfdcb Fix subtend, irb runs
Evan Phoenix authored
385 if(pthread_sigmask(SIG_SETMASK, &mask, NULL) != 0) {
f9ec582 Add preemptive thread scheduling
Evan Phoenix authored
386 abort();
387 }
388
389 struct timespec requested;
390 struct timespec actual;
391
392 requested.tv_sec = 0;
393 requested.tv_nsec = 10000000; // 10 milliseconds
394
395 for(;;) {
396 nanosleep(&requested, &actual);
397 if(interrupts.enable_preempt) {
d390938 Cleanup interrupt checking, improve JIT debugging output
Evan Phoenix authored
398 interrupts.set_timer();
f9ec582 Add preemptive thread scheduling
Evan Phoenix authored
399 }
400 }
401 }
402
1dd0497 Hooked a bit of Threads back up
Evan Phoenix authored
403 void VM::install_waiter(Waiter& waiter) {
404 waiter_ = &waiter;
405 }
406
407 bool VM::wakeup() {
408 if(waiter_) {
e53284c Wire background compiling thread into JIT mode
Evan Phoenix authored
409 waiter_->run();
b4cfdcb Fix subtend, irb runs
Evan Phoenix authored
410 waiter_ = NULL;
1dd0497 Hooked a bit of Threads back up
Evan Phoenix authored
411 return true;
412 }
413
414 return false;
415 }
416
b4cfdcb Fix subtend, irb runs
Evan Phoenix authored
417 void VM::clear_waiter() {
418 waiter_ = NULL;
419 }
420
07fc27b Hook up signal delivery
Evan Phoenix authored
421 void VM::send_async_signal(int sig) {
422 mailbox_.add(ASyncMessage(ASyncMessage::cSignal, sig));
423 check_local_interrupts = true;
424
425 // TODO I'm worried there might be a race calling
426 // wakeup without the lock held...
427 wakeup();
428 }
429
430 bool VM::process_async(CallFrame* call_frame) {
431 check_local_interrupts = false;
432
e6bb96a Fix a number of IO/Signal interaction issues
Evan Phoenix authored
433 if(run_signals_) {
434 shared.signal_handler()->deliver_signals(call_frame);
c17afc3 Fix up Thread
Evan Phoenix authored
435 }
436
e6bb96a Fix a number of IO/Signal interaction issues
Evan Phoenix authored
437 if(thread_state_.raise_reason() != cNone) return false;
438
07fc27b Hook up signal delivery
Evan Phoenix authored
439 return true;
440 }
c17afc3 Fix up Thread
Evan Phoenix authored
441
442 void VM::register_raise(Exception* exc) {
443 thread_state_.raise_exception(exc);
444 check_local_interrupts = true;
445 }
446
3d7bc75 Fix reraising off non-Exception raise values
Evan Phoenix authored
447 void VM::check_exception(CallFrame* call_frame) {
448 if(thread_state()->raise_reason() == cNone) {
449 std::cout << "Exception propogating, but none registered!\n";
450 call_frame->print_backtrace(this);
451 rubinius::abort();
452 }
453 }
454
d815cbb Brian Shirai Fixed responsibilities for creating, registering, removing, deleting pro...
brixen authored
455 profiler::Profiler* VM::profiler() {
456 if(unlikely(!profiler_)) {
457 profiler_ = new profiler::Profiler(this);
458 shared.add_profiler(this, profiler_);
459 }
460
461 return profiler_;
462 }
463
464 void VM::remove_profiler() {
465 profiler_ = 0;
255a070 Brian Shirai Re-enabled instrumenting profiler.
brixen authored
466 }
d769c94 Initial work on new C++ VM
Evan Phoenix authored
467 };
Something went wrong with that request. Please try again.