PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/vm_core.h

https://github.com/nazy/ruby
C Header | 727 lines | 490 code | 143 blank | 94 comment | 23 complexity | 2d045873dd908f684dfbefef96e1c8c4 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0, 0BSD, Unlicense
  1. /**********************************************************************
  2. vm_core.h -
  3. $Author$
  4. created at: 04/01/01 19:41:38 JST
  5. Copyright (C) 2004-2007 Koichi Sasada
  6. **********************************************************************/
  7. #ifndef RUBY_VM_CORE_H
  8. #define RUBY_VM_CORE_H
  9. #define RUBY_VM_THREAD_MODEL 2
  10. #include "ruby/ruby.h"
  11. #include "ruby/st.h"
  12. #include "node.h"
  13. #include "debug.h"
  14. #include "vm_opts.h"
  15. #include "id.h"
  16. #include "method.h"
  17. #if defined(_WIN32)
  18. #include "thread_win32.h"
  19. #elif defined(HAVE_PTHREAD_H)
  20. #include "thread_pthread.h"
  21. #else
  22. #error "unsupported thread type"
  23. #endif
  24. #ifndef ENABLE_VM_OBJSPACE
  25. #ifdef _WIN32
  26. /*
  27. * TODO: object space independent st_table.
  28. * socklist needs st_table in rb_w32_sysinit(), before object space
  29. * initialization.
  30. * It is too early now to change st_hash_type, since it breaks binary
  31. * compatibility.
  32. */
  33. #define ENABLE_VM_OBJSPACE 0
  34. #else
  35. #define ENABLE_VM_OBJSPACE 1
  36. #endif
  37. #endif
  38. #include <setjmp.h>
  39. #include <signal.h>
  40. #ifndef NSIG
  41. # define NSIG (_SIGMAX + 1) /* For QNX */
  42. #endif
  43. #define RUBY_NSIG NSIG
  44. #ifdef HAVE_STDARG_PROTOTYPES
  45. #include <stdarg.h>
  46. #define va_init_list(a,b) va_start(a,b)
  47. #else
  48. #include <varargs.h>
  49. #define va_init_list(a,b) va_start(a)
  50. #endif
  51. #if defined(SIGSEGV) && defined(HAVE_SIGALTSTACK) && defined(SA_SIGINFO) && !defined(__NetBSD__)
  52. #define USE_SIGALTSTACK
  53. #endif
  54. /*****************/
  55. /* configuration */
  56. /*****************/
  57. /* gcc ver. check */
  58. #if defined(__GNUC__) && __GNUC__ >= 2
  59. #if OPT_TOKEN_THREADED_CODE
  60. #if OPT_DIRECT_THREADED_CODE
  61. #undef OPT_DIRECT_THREADED_CODE
  62. #endif
  63. #endif
  64. #else /* defined(__GNUC__) && __GNUC__ >= 2 */
  65. /* disable threaded code options */
  66. #if OPT_DIRECT_THREADED_CODE
  67. #undef OPT_DIRECT_THREADED_CODE
  68. #endif
  69. #if OPT_TOKEN_THREADED_CODE
  70. #undef OPT_TOKEN_THREADED_CODE
  71. #endif
  72. #endif
  73. /* call threaded code */
  74. #if OPT_CALL_THREADED_CODE
  75. #if OPT_DIRECT_THREADED_CODE
  76. #undef OPT_DIRECT_THREADED_CODE
  77. #endif /* OPT_DIRECT_THREADED_CODE */
  78. #if OPT_STACK_CACHING
  79. #undef OPT_STACK_CACHING
  80. #endif /* OPT_STACK_CACHING */
  81. #endif /* OPT_CALL_THREADED_CODE */
  82. /* likely */
  83. #if __GNUC__ >= 3
  84. #define LIKELY(x) (__builtin_expect((x), 1))
  85. #define UNLIKELY(x) (__builtin_expect((x), 0))
  86. #else /* __GNUC__ >= 3 */
  87. #define LIKELY(x) (x)
  88. #define UNLIKELY(x) (x)
  89. #endif /* __GNUC__ >= 3 */
  90. #if __GNUC__ >= 3
  91. #define UNINITIALIZED_VAR(x) x = x
  92. #else
  93. #define UNINITIALIZED_VAR(x) x
  94. #endif
  95. typedef unsigned long rb_num_t;
  96. /* iseq data type */
  97. struct iseq_compile_data_ensure_node_stack;
  98. typedef struct rb_compile_option_struct {
  99. int inline_const_cache;
  100. int peephole_optimization;
  101. int tailcall_optimization;
  102. int specialized_instruction;
  103. int operands_unification;
  104. int instructions_unification;
  105. int stack_caching;
  106. int trace_instruction;
  107. int debug_level;
  108. } rb_compile_option_t;
  109. struct iseq_inline_cache_entry {
  110. VALUE ic_vmstat;
  111. VALUE ic_class;
  112. union {
  113. VALUE value;
  114. rb_method_entry_t *method;
  115. long index;
  116. } ic_value;
  117. };
  118. #if 1
  119. #define GetCoreDataFromValue(obj, type, ptr) do { \
  120. ptr = (type*)DATA_PTR(obj); \
  121. } while (0)
  122. #else
  123. #define GetCoreDataFromValue(obj, type, ptr) Data_Get_Struct(obj, type, ptr)
  124. #endif
  125. #define GetISeqPtr(obj, ptr) \
  126. GetCoreDataFromValue(obj, rb_iseq_t, ptr)
  127. struct rb_iseq_struct;
  128. struct rb_iseq_struct {
  129. /***************/
  130. /* static data */
  131. /***************/
  132. VALUE type; /* instruction sequence type */
  133. VALUE name; /* String: iseq name */
  134. VALUE filename; /* file information where this sequence from */
  135. VALUE filepath; /* real file path or nil */
  136. VALUE *iseq; /* iseq (insn number and operands) */
  137. VALUE *iseq_encoded; /* encoded iseq */
  138. unsigned long iseq_size;
  139. VALUE mark_ary; /* Array: includes operands which should be GC marked */
  140. VALUE coverage; /* coverage array */
  141. unsigned short line_no;
  142. /* insn info, must be freed */
  143. struct iseq_insn_info_entry *insn_info_table;
  144. size_t insn_info_size;
  145. ID *local_table; /* must free */
  146. int local_table_size;
  147. /* method, class frame: sizeof(vars) + 1, block frame: sizeof(vars) */
  148. int local_size;
  149. struct iseq_inline_cache_entry *ic_entries;
  150. int ic_size;
  151. /**
  152. * argument information
  153. *
  154. * def m(a1, a2, ..., aM, # mandatory
  155. * b1=(...), b2=(...), ..., bN=(...), # optional
  156. * *c, # rest
  157. * d1, d2, ..., dO, # post
  158. * &e) # block
  159. * =>
  160. *
  161. * argc = M
  162. * arg_rest = M+N+1 // or -1 if no rest arg
  163. * arg_opts = N
  164. * arg_opts_tbl = [ (N entries) ]
  165. * arg_post_len = O // 0 if no post arguments
  166. * arg_post_start = M+N+2
  167. * arg_block = M+N + 1 + O + 1 // -1 if no block arg
  168. * arg_simple = 0 if not simple arguments.
  169. * = 1 if no opt, rest, post, block.
  170. * = 2 if ambiguos block parameter ({|a|}).
  171. * arg_size = argument size.
  172. */
  173. int argc;
  174. int arg_simple;
  175. int arg_rest;
  176. int arg_block;
  177. int arg_opts;
  178. int arg_post_len;
  179. int arg_post_start;
  180. int arg_size;
  181. VALUE *arg_opt_table;
  182. size_t stack_max; /* for stack overflow check */
  183. /* catch table */
  184. struct iseq_catch_table_entry *catch_table;
  185. int catch_table_size;
  186. /* for child iseq */
  187. struct rb_iseq_struct *parent_iseq;
  188. struct rb_iseq_struct *local_iseq;
  189. /****************/
  190. /* dynamic data */
  191. /****************/
  192. VALUE self;
  193. VALUE orig; /* non-NULL if its data have origin */
  194. /* block inlining */
  195. /*
  196. * NODE *node;
  197. * void *special_block_builder;
  198. * void *cached_special_block_builder;
  199. * VALUE cached_special_block;
  200. */
  201. /* klass/module nest information stack (cref) */
  202. NODE *cref_stack;
  203. VALUE klass;
  204. /* misc */
  205. ID defined_method_id; /* for define_method */
  206. /* used at compile time */
  207. struct iseq_compile_data *compile_data;
  208. };
  209. enum ruby_special_exceptions {
  210. ruby_error_reenter,
  211. ruby_error_nomemory,
  212. ruby_error_sysstack,
  213. ruby_special_error_count
  214. };
  215. #define GetVMPtr(obj, ptr) \
  216. GetCoreDataFromValue(obj, rb_vm_t, ptr)
  217. #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
  218. struct rb_objspace;
  219. void rb_objspace_free(struct rb_objspace *);
  220. #endif
  221. typedef struct rb_vm_struct {
  222. VALUE self;
  223. rb_thread_lock_t global_vm_lock;
  224. struct rb_thread_struct *main_thread;
  225. struct rb_thread_struct *running_thread;
  226. st_table *living_threads;
  227. VALUE thgroup_default;
  228. int running;
  229. int thread_abort_on_exception;
  230. unsigned long trace_flag;
  231. volatile int sleeper;
  232. /* object management */
  233. VALUE mark_object_ary;
  234. VALUE special_exceptions[ruby_special_error_count];
  235. /* load */
  236. VALUE top_self;
  237. VALUE load_path;
  238. VALUE loaded_features;
  239. struct st_table *loading_table;
  240. /* signal */
  241. struct {
  242. VALUE cmd;
  243. int safe;
  244. } trap_list[RUBY_NSIG];
  245. /* hook */
  246. rb_event_hook_t *event_hooks;
  247. int src_encoding_index;
  248. VALUE verbose, debug, progname;
  249. VALUE coverages;
  250. struct unlinked_method_entry_list_entry *unlinked_method_entry_list;
  251. #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
  252. struct rb_objspace *objspace;
  253. #endif
  254. } rb_vm_t;
  255. typedef struct {
  256. VALUE *pc; /* cfp[0] */
  257. VALUE *sp; /* cfp[1] */
  258. VALUE *bp; /* cfp[2] */
  259. rb_iseq_t *iseq; /* cfp[3] */
  260. VALUE flag; /* cfp[4] */
  261. VALUE self; /* cfp[5] / block[0] */
  262. VALUE *lfp; /* cfp[6] / block[1] */
  263. VALUE *dfp; /* cfp[7] / block[2] */
  264. rb_iseq_t *block_iseq; /* cfp[8] / block[3] */
  265. VALUE proc; /* cfp[9] / block[4] */
  266. const rb_method_entry_t *me;/* cfp[10] */
  267. } rb_control_frame_t;
  268. typedef struct rb_block_struct {
  269. VALUE self; /* share with method frame if it's only block */
  270. VALUE *lfp; /* share with method frame if it's only block */
  271. VALUE *dfp; /* share with method frame if it's only block */
  272. rb_iseq_t *iseq;
  273. VALUE proc;
  274. } rb_block_t;
  275. #define GetThreadPtr(obj, ptr) \
  276. GetCoreDataFromValue(obj, rb_thread_t, ptr)
  277. enum rb_thread_status {
  278. THREAD_TO_KILL,
  279. THREAD_RUNNABLE,
  280. THREAD_STOPPED,
  281. THREAD_STOPPED_FOREVER,
  282. THREAD_KILLED
  283. };
  284. typedef RUBY_JMP_BUF rb_jmpbuf_t;
  285. struct rb_vm_tag {
  286. rb_jmpbuf_t buf;
  287. VALUE tag;
  288. VALUE retval;
  289. struct rb_vm_tag *prev;
  290. };
  291. struct rb_vm_protect_tag {
  292. struct rb_vm_protect_tag *prev;
  293. };
  294. #define RUBY_VM_VALUE_CACHE_SIZE 0x1000
  295. #define USE_VALUE_CACHE 0
  296. struct rb_unblock_callback {
  297. rb_unblock_function_t *func;
  298. void *arg;
  299. };
  300. struct rb_mutex_struct;
  301. typedef struct rb_thread_struct
  302. {
  303. VALUE self;
  304. rb_vm_t *vm;
  305. /* execution information */
  306. VALUE *stack; /* must free, must mark */
  307. unsigned long stack_size;
  308. rb_control_frame_t *cfp;
  309. int safe_level;
  310. int raised_flag;
  311. VALUE last_status; /* $? */
  312. /* passing state */
  313. int state;
  314. /* for rb_iterate */
  315. const rb_block_t *passed_block;
  316. /* for bmethod */
  317. const rb_method_entry_t *passed_me;
  318. /* for load(true) */
  319. VALUE top_self;
  320. VALUE top_wrapper;
  321. /* eval env */
  322. rb_block_t *base_block;
  323. VALUE *local_lfp;
  324. VALUE local_svar;
  325. /* thread control */
  326. rb_thread_id_t thread_id;
  327. enum rb_thread_status status;
  328. int priority;
  329. int slice;
  330. native_thread_data_t native_thread_data;
  331. void *blocking_region_buffer;
  332. VALUE thgroup;
  333. VALUE value;
  334. VALUE errinfo;
  335. VALUE thrown_errinfo;
  336. int exec_signal;
  337. int interrupt_flag;
  338. rb_thread_lock_t interrupt_lock;
  339. struct rb_unblock_callback unblock;
  340. VALUE locking_mutex;
  341. struct rb_mutex_struct *keeping_mutexes;
  342. int transition_for_lock;
  343. struct rb_vm_tag *tag;
  344. struct rb_vm_protect_tag *protect_tag;
  345. int parse_in_eval;
  346. int mild_compile_error;
  347. /* storage */
  348. st_table *local_storage;
  349. #if USE_VALUE_CACHE
  350. VALUE value_cache[RUBY_VM_VALUE_CACHE_SIZE + 1];
  351. VALUE *value_cache_ptr;
  352. #endif
  353. struct rb_thread_struct *join_list_next;
  354. struct rb_thread_struct *join_list_head;
  355. VALUE first_proc;
  356. VALUE first_args;
  357. VALUE (*first_func)(ANYARGS);
  358. /* for GC */
  359. VALUE *machine_stack_start;
  360. VALUE *machine_stack_end;
  361. size_t machine_stack_maxsize;
  362. #ifdef __ia64
  363. VALUE *machine_register_stack_start;
  364. VALUE *machine_register_stack_end;
  365. size_t machine_register_stack_maxsize;
  366. #endif
  367. jmp_buf machine_regs;
  368. int mark_stack_len;
  369. /* statistics data for profiler */
  370. VALUE stat_insn_usage;
  371. /* tracer */
  372. rb_event_hook_t *event_hooks;
  373. rb_event_flag_t event_flags;
  374. int tracing;
  375. /* fiber */
  376. VALUE fiber;
  377. VALUE root_fiber;
  378. rb_jmpbuf_t root_jmpbuf;
  379. /* misc */
  380. int method_missing_reason;
  381. int abort_on_exception;
  382. #ifdef USE_SIGALTSTACK
  383. void *altstack;
  384. #endif
  385. } rb_thread_t;
  386. /* iseq.c */
  387. #if defined __GNUC__ && __GNUC__ >= 4
  388. #pragma GCC visibility push(default)
  389. #endif
  390. VALUE rb_iseq_new(NODE*, VALUE, VALUE, VALUE, VALUE, VALUE);
  391. VALUE rb_iseq_new_top(NODE *node, VALUE name, VALUE filename, VALUE filepath, VALUE parent);
  392. VALUE rb_iseq_new_main(NODE *node, VALUE filename, VALUE filepath);
  393. VALUE rb_iseq_new_with_bopt(NODE*, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE);
  394. VALUE rb_iseq_new_with_opt(NODE*, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE, const rb_compile_option_t*);
  395. VALUE rb_iseq_compile(VALUE src, VALUE file, VALUE line);
  396. VALUE rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE filepath, VALUE line, VALUE opt);
  397. VALUE rb_iseq_disasm(VALUE self);
  398. int rb_iseq_disasm_insn(VALUE str, VALUE *iseqval, size_t pos, rb_iseq_t *iseq, VALUE child);
  399. const char *ruby_node_name(int node);
  400. int rb_iseq_first_lineno(rb_iseq_t *iseq);
  401. RUBY_EXTERN VALUE rb_cISeq;
  402. RUBY_EXTERN VALUE rb_cRubyVM;
  403. RUBY_EXTERN VALUE rb_cEnv;
  404. RUBY_EXTERN VALUE rb_mRubyVMFrozenCore;
  405. #if defined __GNUC__ && __GNUC__ >= 4
  406. #pragma GCC visibility pop
  407. #endif
  408. /* each thread has this size stack : 128KB */
  409. #define RUBY_VM_THREAD_STACK_SIZE (128 * 1024)
  410. #define GetProcPtr(obj, ptr) \
  411. GetCoreDataFromValue(obj, rb_proc_t, ptr)
  412. typedef struct {
  413. rb_block_t block;
  414. VALUE envval; /* for GC mark */
  415. VALUE blockprocval;
  416. int safe_level;
  417. int is_from_method;
  418. int is_lambda;
  419. } rb_proc_t;
  420. #define GetEnvPtr(obj, ptr) \
  421. GetCoreDataFromValue(obj, rb_env_t, ptr)
  422. typedef struct {
  423. VALUE *env;
  424. int env_size;
  425. int local_size;
  426. VALUE prev_envval; /* for GC mark */
  427. rb_block_t block;
  428. } rb_env_t;
  429. #define GetBindingPtr(obj, ptr) \
  430. GetCoreDataFromValue(obj, rb_binding_t, ptr)
  431. typedef struct {
  432. VALUE env;
  433. VALUE filename;
  434. unsigned short line_no;
  435. } rb_binding_t;
  436. /* used by compile time and send insn */
  437. #define VM_CALL_ARGS_SPLAT_BIT (0x01 << 1)
  438. #define VM_CALL_ARGS_BLOCKARG_BIT (0x01 << 2)
  439. #define VM_CALL_FCALL_BIT (0x01 << 3)
  440. #define VM_CALL_VCALL_BIT (0x01 << 4)
  441. #define VM_CALL_TAILCALL_BIT (0x01 << 5)
  442. #define VM_CALL_TAILRECURSION_BIT (0x01 << 6)
  443. #define VM_CALL_SUPER_BIT (0x01 << 7)
  444. #define VM_CALL_OPT_SEND_BIT (0x01 << 8)
  445. #define VM_SPECIAL_OBJECT_VMCORE 0x01
  446. #define VM_SPECIAL_OBJECT_CBASE 0x02
  447. #define VM_SPECIAL_OBJECT_CONST_BASE 0x03
  448. #define VM_FRAME_MAGIC_METHOD 0x11
  449. #define VM_FRAME_MAGIC_BLOCK 0x21
  450. #define VM_FRAME_MAGIC_CLASS 0x31
  451. #define VM_FRAME_MAGIC_TOP 0x41
  452. #define VM_FRAME_MAGIC_FINISH 0x51
  453. #define VM_FRAME_MAGIC_CFUNC 0x61
  454. #define VM_FRAME_MAGIC_PROC 0x71
  455. #define VM_FRAME_MAGIC_IFUNC 0x81
  456. #define VM_FRAME_MAGIC_EVAL 0x91
  457. #define VM_FRAME_MAGIC_LAMBDA 0xa1
  458. #define VM_FRAME_MAGIC_MASK_BITS 8
  459. #define VM_FRAME_MAGIC_MASK (~(~0<<VM_FRAME_MAGIC_MASK_BITS))
  460. #define VM_FRAME_TYPE(cfp) ((cfp)->flag & VM_FRAME_MAGIC_MASK)
  461. /* other frame flag */
  462. #define VM_FRAME_FLAG_PASSED 0x0100
  463. #define RUBYVM_CFUNC_FRAME_P(cfp) \
  464. (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC)
  465. /* inline cache */
  466. typedef struct iseq_inline_cache_entry *IC;
  467. extern VALUE ruby_vm_global_state_version;
  468. #define GET_VM_STATE_VERSION() (ruby_vm_global_state_version)
  469. #define INC_VM_STATE_VERSION() \
  470. (ruby_vm_global_state_version = (ruby_vm_global_state_version+1) & 0x8fffffff)
  471. void rb_vm_change_state(void);
  472. typedef VALUE CDHASH;
  473. #ifndef FUNC_FASTCALL
  474. #define FUNC_FASTCALL(x) x
  475. #endif
  476. typedef rb_control_frame_t *
  477. (FUNC_FASTCALL(*rb_insn_func_t))(rb_thread_t *, rb_control_frame_t *);
  478. #define GC_GUARDED_PTR(p) ((VALUE)((VALUE)(p) | 0x01))
  479. #define GC_GUARDED_PTR_REF(p) ((void *)(((VALUE)p) & ~0x03))
  480. #define GC_GUARDED_PTR_P(p) (((VALUE)p) & 0x01)
  481. #define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp) (cfp+1)
  482. #define RUBY_VM_NEXT_CONTROL_FRAME(cfp) (cfp-1)
  483. #define RUBY_VM_END_CONTROL_FRAME(th) \
  484. ((rb_control_frame_t *)((th)->stack + (th)->stack_size))
  485. #define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp) \
  486. ((void *)(ecfp) > (void *)(cfp))
  487. #define RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, cfp) \
  488. (!RUBY_VM_VALID_CONTROL_FRAME_P((cfp), RUBY_VM_END_CONTROL_FRAME(th)))
  489. #define RUBY_VM_IFUNC_P(ptr) (BUILTIN_TYPE(ptr) == T_NODE)
  490. #define RUBY_VM_NORMAL_ISEQ_P(ptr) \
  491. (ptr && !RUBY_VM_IFUNC_P(ptr))
  492. #define RUBY_VM_GET_BLOCK_PTR_IN_CFP(cfp) ((rb_block_t *)(&(cfp)->self))
  493. #define RUBY_VM_GET_CFP_FROM_BLOCK_PTR(b) \
  494. ((rb_control_frame_t *)((VALUE *)(b) - 5))
  495. /* VM related object allocate functions */
  496. VALUE rb_thread_alloc(VALUE klass);
  497. VALUE rb_proc_alloc(VALUE klass);
  498. /* for debug */
  499. extern void rb_vmdebug_stack_dump_raw(rb_thread_t *, rb_control_frame_t *);
  500. #define SDR() rb_vmdebug_stack_dump_raw(GET_THREAD(), GET_THREAD()->cfp)
  501. #define SDR2(cfp) rb_vmdebug_stack_dump_raw(GET_THREAD(), (cfp))
  502. void rb_vm_bugreport(void);
  503. /* functions about thread/vm execution */
  504. #if defined __GNUC__ && __GNUC__ >= 4
  505. #pragma GCC visibility push(default)
  506. #endif
  507. VALUE rb_iseq_eval(VALUE iseqval);
  508. VALUE rb_iseq_eval_main(VALUE iseqval);
  509. void rb_enable_interrupt(void);
  510. void rb_disable_interrupt(void);
  511. #if defined __GNUC__ && __GNUC__ >= 4
  512. #pragma GCC visibility pop
  513. #endif
  514. int rb_thread_method_id_and_class(rb_thread_t *th, ID *idp, VALUE *klassp);
  515. VALUE rb_vm_invoke_proc(rb_thread_t *th, rb_proc_t *proc, VALUE self,
  516. int argc, const VALUE *argv, const rb_block_t *blockptr);
  517. VALUE rb_vm_make_proc(rb_thread_t *th, const rb_block_t *block, VALUE klass);
  518. VALUE rb_vm_make_env_object(rb_thread_t *th, rb_control_frame_t *cfp);
  519. void *rb_thread_call_with_gvl(void *(*func)(void *), void *data1);
  520. int ruby_thread_has_gvl_p(void);
  521. VALUE rb_make_backtrace(void);
  522. typedef int rb_backtrace_iter_func(void *, VALUE, int, VALUE);
  523. int rb_backtrace_each(rb_backtrace_iter_func *iter, void *arg);
  524. rb_control_frame_t *rb_vm_get_ruby_level_next_cfp(rb_thread_t *th, rb_control_frame_t *cfp);
  525. int rb_vm_get_sourceline(const rb_control_frame_t *);
  526. VALUE rb_name_err_mesg_new(VALUE obj, VALUE mesg, VALUE recv, VALUE method);
  527. NOINLINE(void rb_gc_save_machine_context(rb_thread_t *));
  528. #define sysstack_error GET_VM()->special_exceptions[ruby_error_sysstack]
  529. /* for thread */
  530. #if RUBY_VM_THREAD_MODEL == 2
  531. RUBY_EXTERN rb_thread_t *ruby_current_thread;
  532. extern rb_vm_t *ruby_current_vm;
  533. #define GET_VM() ruby_current_vm
  534. #define GET_THREAD() ruby_current_thread
  535. #define rb_thread_set_current_raw(th) (void)(ruby_current_thread = (th))
  536. #define rb_thread_set_current(th) do { \
  537. rb_thread_set_current_raw(th); \
  538. th->vm->running_thread = th; \
  539. } while (0)
  540. #else
  541. #error "unsupported thread model"
  542. #endif
  543. #define RUBY_VM_SET_INTERRUPT(th) ((th)->interrupt_flag |= 0x02)
  544. #define RUBY_VM_SET_TIMER_INTERRUPT(th) ((th)->interrupt_flag |= 0x01)
  545. #define RUBY_VM_SET_FINALIZER_INTERRUPT(th) ((th)->interrupt_flag |= 0x04)
  546. #define RUBY_VM_INTERRUPTED(th) ((th)->interrupt_flag & 0x02)
  547. void rb_threadptr_signal_raise(rb_thread_t *th, int sig);
  548. void rb_threadptr_signal_exit(rb_thread_t *th);
  549. void rb_threadptr_execute_interrupts(rb_thread_t *);
  550. void rb_thread_lock_unlock(rb_thread_lock_t *);
  551. void rb_thread_lock_destroy(rb_thread_lock_t *);
  552. #define RUBY_VM_CHECK_INTS_TH(th) do { \
  553. if (UNLIKELY(th->interrupt_flag)) { \
  554. rb_threadptr_execute_interrupts(th); \
  555. } \
  556. } while (0)
  557. #define RUBY_VM_CHECK_INTS() \
  558. RUBY_VM_CHECK_INTS_TH(GET_THREAD())
  559. /* tracer */
  560. void
  561. rb_threadptr_exec_event_hooks(rb_thread_t *th, rb_event_flag_t flag, VALUE self, ID id, VALUE klass);
  562. #define EXEC_EVENT_HOOK(th, flag, self, id, klass) do { \
  563. rb_event_flag_t wait_event__ = th->event_flags; \
  564. if (UNLIKELY(wait_event__)) { \
  565. if (wait_event__ & (flag | RUBY_EVENT_VM)) { \
  566. rb_threadptr_exec_event_hooks(th, flag, self, id, klass); \
  567. } \
  568. } \
  569. } while (0)
  570. #if defined __GNUC__ && __GNUC__ >= 4
  571. #pragma GCC visibility push(default)
  572. #endif
  573. int rb_thread_check_trap_pending(void);
  574. extern VALUE rb_get_coverages(void);
  575. extern void rb_set_coverages(VALUE);
  576. extern void rb_reset_coverages(void);
  577. #if defined __GNUC__ && __GNUC__ >= 4
  578. #pragma GCC visibility pop
  579. #endif
  580. #endif /* RUBY_VM_CORE_H */