forked from rubinius/rubinius
/
vm.hpp
505 lines (377 loc) · 10.8 KB
/
vm.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#ifndef RBX_VM_H
#define RBX_VM_H
#include "missing/time.h"
#include "globals.hpp"
#include "gc/object_mark.hpp"
#include "gc/managed.hpp"
#include "thread_state.hpp"
#include "util/refcount.hpp"
#include "util/thread.hpp"
#include "call_frame_list.hpp"
#include "gc/variable_buffer.hpp"
#include "gc/root_buffer.hpp"
#include "gc/slab.hpp"
#include "shared_state.hpp"
#include "unwind_info.hpp"
#include "fiber_stack.hpp"
#include <vector>
#include <setjmp.h>
namespace llvm {
class Module;
}
namespace rbxti {
class Env;
}
namespace rubinius {
class Exception;
class LLVMState;
namespace event {
class Loop;
}
namespace gc {
class WriteBarrier;
}
class Channel;
class GlobalCache;
class Primitives;
class ObjectMemory;
class TypeInfo;
class String;
class Symbol;
class ConfigParser;
class TypeError;
class Assertion;
struct CallFrame;
class Object;
class Configuration;
class VMManager;
class LookupTable;
class SymbolTable;
class SharedState;
class Fiber;
class GarbageCollector;
class Park;
enum MethodMissingReason {
eNone, ePrivate, eProtected, eSuper, eVCall, eNormal
};
/**
* Represents an execution context for running Ruby code.
*
* Each Ruby thread is backed by an instance of this class, as well as an
* instance of the Thread class. Thread manages the (Ruby visible) thread-
* related state, while this class manages the execution machinery for
* running Ruby code.
*/
class VM : public ManagedThread {
friend class State;
private:
UnwindInfoSet unwinds_;
CallFrame* saved_call_frame_;
uintptr_t stack_start_;
uintptr_t stack_limit_;
int stack_size_;
uintptr_t root_stack_start_;
uintptr_t root_stack_size_;
bool run_signals_;
MethodMissingReason method_missing_reason_;
void* young_start_;
void* young_end_;
bool thread_step_;
rbxti::Env* tooling_env_;
bool tooling_;
bool allocation_tracking_;
FiberStacks fiber_stacks_;
Park* park_;
public:
/* Data members */
SharedState& shared;
TypedRoot<Channel*> waiting_channel_;
TypedRoot<Exception*> interrupted_exception_;
bool interrupt_with_signal_;
bool interrupt_by_kill_;
InflatedHeader* waiting_header_;
void (*custom_wakeup_)(void*);
void* custom_wakeup_data_;
ObjectMemory* om;
bool check_local_interrupts;
ThreadState thread_state_;
/// The Thread object for this VM state
TypedRoot<Thread*> thread;
/// The current fiber running on this thread
TypedRoot<Fiber*> current_fiber;
/// Root fiber, if any (lazily initialized)
TypedRoot<Fiber*> root_fiber;
static unsigned long cStackDepthMax;
public: /* Inline methods */
UnwindInfoSet& unwinds() {
return unwinds_;
}
uint32_t thread_id() {
return id_;
}
bool run_signals_p() {
return run_signals_;
}
void set_run_signals(bool val) {
run_signals_ = val;
}
ThreadState* thread_state() {
return &thread_state_;
}
CallFrame** call_frame_location() {
return &saved_call_frame_;
}
void set_call_frame(CallFrame* frame) {
saved_call_frame_ = frame;
}
CallFrame* saved_call_frame() {
return saved_call_frame_;
}
GlobalCache* global_cache() {
return shared.global_cache;
}
Globals& globals() {
return shared.globals;
}
void* stack_start() {
return reinterpret_cast<void*>(stack_start_);
}
int stack_size() {
return stack_size_;
}
void reset_stack_limit() {
// @TODO assumes stack growth direction
stack_limit_ = (stack_start_ - stack_size_) + (4096 * 10);
}
void set_stack_bounds(uintptr_t start, int length) {
if(start == 0) {
start = root_stack_start_;
length = root_stack_size_;
}
stack_start_ = start;
stack_size_ = length;
reset_stack_limit();
}
void set_root_stack(uintptr_t start, int length) {
root_stack_start_ = start;
root_stack_size_ = length;
set_stack_bounds(root_stack_start_, root_stack_size_);
}
void get_attention() {
stack_limit_ = stack_start_;
}
bool detect_stack_condition(void* end) {
// @TODO assumes stack growth direction
return reinterpret_cast<uintptr_t>(end) < stack_limit_;
}
MethodMissingReason method_missing_reason() {
return method_missing_reason_;
}
void set_method_missing_reason(MethodMissingReason reason) {
method_missing_reason_ = reason;
}
bool young_object_p(Object* obj) {
return obj >= young_start_ && obj <= young_end_;
}
bool thread_step() {
return thread_step_;
}
void clear_thread_step() {
thread_step_ = false;
}
void set_thread_step() {
thread_step_ = true;
}
Exception* interrupted_exception() {
return interrupted_exception_.get();
}
void clear_interrupted_exception() {
interrupted_exception_.set(cNil);
}
rbxti::Env* tooling_env() {
return tooling_env_;
}
bool tooling() {
return tooling_;
}
void enable_tooling() {
tooling_ = true;
}
void disable_tooling() {
tooling_ = false;
}
bool allocation_tracking() {
return allocation_tracking_;
}
void enable_allocation_tracking() {
allocation_tracking_ = true;
}
void disable_allocation_tracking() {
allocation_tracking_ = false;
}
FiberStack* allocate_fiber_stack() {
return fiber_stacks_.allocate();
}
void* fiber_trampoline() {
return fiber_stacks_.trampoline();
}
FiberData* new_fiber_data(bool root=false) {
return fiber_stacks_.new_data(root);
}
void remove_fiber_data(FiberData* data) {
fiber_stacks_.remove_data(data);
}
VariableRootBuffers& current_root_buffers();
public:
static void init_stack_size();
static VM* current();
static void set_current(VM* vm, std::string name);
static void discard(STATE, VM*);
public:
/* Prototypes */
VM(uint32_t id, SharedState& shared);
~VM();
void initialize_as_root();
void bootstrap_class(STATE);
void bootstrap_ontology(STATE);
void bootstrap_symbol(STATE);
void initialize_config();
void setup_errno(STATE, int num, const char* name, Class* sce, Module* ern);
void bootstrap_exceptions(STATE);
void initialize_fundamental_constants(STATE);
void initialize_builtin_classes(STATE);
void initialize_platform_data(STATE);
void set_current_fiber(Fiber* fib);
Object* new_object_typed(Class* cls, size_t bytes, object_type type);
Object* new_object_typed_mature(Class* cls, size_t bytes, object_type type);
template <class T>
T* new_object(Class *cls) {
return reinterpret_cast<T*>(new_object_typed(cls, sizeof(T), T::type));
}
template <class T>
T* new_struct(Class* cls, size_t bytes = 0) {
T* obj = reinterpret_cast<T*>(new_object_typed(cls, sizeof(T) + bytes, T::type));
return obj;
}
template <class T>
T* new_object_mature(Class *cls) {
return reinterpret_cast<T*>(new_object_typed_mature(cls, sizeof(T), T::type));
}
template <class T>
T* new_object_bytes(Class* cls, size_t& bytes) {
bytes = ObjectHeader::align(sizeof(T) + bytes);
T* obj = reinterpret_cast<T*>(new_object_typed(cls, bytes, T::type));
return obj;
}
template <class T>
T* new_object_variable(Class* cls, size_t fields, size_t& bytes) {
bytes = sizeof(T) + (fields * sizeof(Object*));
return reinterpret_cast<T*>(new_object_typed(cls, bytes, T::type));
}
/// Create a Tuple in the young GC space, return NULL if not possible.
Tuple* new_young_tuple_dirty(size_t fields);
Symbol* symbol(const char* str);
Symbol* symbol(std::string str);
Symbol* symbol(String* str);
TypeInfo* find_type(int type);
void init_ffi(STATE);
void init_native_libraries();
/// Check the GC flags in ObjectMemory and collect if we need to.
void collect_maybe(GCToken gct, CallFrame* call_frame);
void raise_from_errno(const char* reason);
void raise_exception(Exception* exc);
Exception* new_exception(Class* cls, const char* msg);
Object* current_block();
void set_const(const char* name, Object* val);
void set_const(Module* mod, const char* name, Object* val);
Object* path2class(const char* name);
#ifdef ENABLE_LLVM
llvm::Module* llvm_module();
void llvm_cleanup();
#endif
void print_backtrace();
/// Run the garbage collectors as soon as you can
void run_gc_soon();
void wait_on_channel(Channel* channel);
void wait_on_inflated_lock(InflatedHeader* ih);
void wait_on_custom_function(void (*func)(void*), void* data);
void clear_waiter();
bool wakeup(STATE, GCToken gct);
bool waiting_p();
void set_parked();
void set_unparked();
void set_sleeping();
void clear_sleeping();
void interrupt_with_signal();
bool should_interrupt_with_signal() {
return interrupt_with_signal_;
}
void register_raise(STATE, Exception* exc);
void register_kill(STATE);
void gc_scan(GarbageCollector* gc);
// For thread-local roots
static std::list<Roots*>* roots;
};
}
#include "state.hpp"
namespace rubinius {
/**
* Instantiation of an instance of this class causes Ruby execution on all
* threads to be suspended. Upon destruction of the instance, Ruby execution
* is resumed.
*/
class StopTheWorld {
State* state_;
public:
StopTheWorld(STATE, GCToken gct, CallFrame* cf) :
state_(state)
{
while(!state->stop_the_world()) {
state->checkpoint(gct, cf);
}
}
~StopTheWorld() {
state_->restart_world();
}
};
class NativeMethodEnvironment;
class GCIndependent {
State* state_;
public:
GCIndependent(STATE, CallFrame* call_frame)
: state_(state)
{
GCTokenImpl gct;
state_->set_call_frame(call_frame);
state_->gc_independent(gct);
}
GCIndependent(STATE)
: state_(state)
{
GCTokenImpl gct;
state_->gc_independent(gct);
}
GCIndependent(NativeMethodEnvironment* env);
~GCIndependent() {
state_->gc_dependent();
}
};
template <class T>
class GCIndependentLockGuard : public utilities::thread::LockGuardTemplate<T> {
State* state_;
public:
GCIndependentLockGuard(STATE, GCToken gct, T& in_lock)
: utilities::thread::LockGuardTemplate<T>(in_lock, false)
, state_(state)
{
state_->shared().gc_independent(state_);
this->lock();
state->shared().gc_dependent(state_);
}
~GCIndependentLockGuard() {
this->unlock();
}
};
typedef GCIndependentLockGuard<utilities::thread::Mutex> GCLockGuard;
};
#endif