1#ifndef RUBY_RACTOR_CORE_H
2#define RUBY_RACTOR_CORE_H
3#include "internal/gc.h"
4#include "internal/sanitizers.h"
12#ifndef RACTOR_CHECK_MODE
13#define RACTOR_CHECK_MODE (VM_CHECK_MODE || RUBY_DEBUG) && (SIZEOF_UINT64_T == SIZEOF_VALUE)
19#ifndef RB_GC_REGISTERED_ADDR_CHECK
20# if RUBY_DEBUG || defined(RUBY_ASAN_ENABLED)
21# define RB_GC_REGISTERED_ADDR_CHECK 1
23# define RB_GC_REGISTERED_ADDR_CHECK 0
28#define RUBY_TYPED_FROZEN_SHAREABLE_NO_REC RUBY_FL_FINALIZE
35 rb_nativethread_lock_t lock;
37#if RACTOR_CHECK_MODE > 0
45 struct ccan_list_head waiters;
48 VALUE default_port_value;
55 struct ccan_list_head off_queue_baskets;
58 struct ccan_list_head monitors;
92#if RB_GC_REGISTERED_ADDR_CHECK
105 VALUE *registered_marks;
106 size_t registered_marks_cnt, registered_marks_capa;
109 size_t registered_addrs_cnt, registered_addrs_capa;
110 bool registered_addrs_listed;
119 struct ccan_list_head set;
121 unsigned int blocking_cnt;
122 unsigned int sleeper;
140 VALUE thgroup_default;
145 enum ractor_status status_;
147 struct ccan_list_node vmlr_node;
148 bool in_terminated_set;
156 rb_serial_t next_ec_serial;
160 VALUE local_storage_store_lock;
171 bool malloc_gc_disabled;
184 void *creating_child_objspace;
190void rb_ractor_mark_terminated_join_value(
rb_ractor_t *r);
200enum ractor_wakeup_status {
208 enum ractor_wakeup_status wakeup_status;
210 struct ccan_list_node node;
214 const rb_hrtime_t *end;
234int rb_ractor_living_thread_num(
const rb_ractor_t *);
235VALUE rb_ractor_thread_list(
void);
236bool rb_ractor_p(
VALUE rv);
251void rb_ractor_blocking_threads_inc(
rb_ractor_t *r,
const char *file,
int line);
252void rb_ractor_blocking_threads_dec(
rb_ractor_t *r,
const char *file,
int line);
254void rb_ractor_vm_barrier_interrupt_running_thread(
rb_ractor_t *r);
255void rb_ractor_terminate_interrupt_main_thread(
rb_ractor_t *r);
256void rb_ractor_terminate_all(
void);
257bool rb_ractor_main_p_(
void);
260VALUE rb_ractor_require(
VALUE feature,
bool silent);
266RUBY_SYMBOL_EXPORT_BEGIN
267void rb_ractor_finish_marking(
bool full_mark);
269bool rb_ractor_shareable_p_continue(
VALUE obj);
275RUBY_SYMBOL_EXPORT_END
278rb_ractor_main_p(
void)
280 if (ruby_single_main_ractor) {
284 return rb_ractor_main_p_();
289rb_ractor_status_p(
rb_ractor_t *r,
enum ractor_status status)
291 return r->status_ == status;
297 r->threads.sleeper++;
303 r->threads.sleeper--;
309 r->threads.sleeper = 0;
315 return r->threads.sleeper;
321 RUBY_DEBUG_LOG(
"th:%d->%u%s",
322 cr->threads.running_ec ? (
int)rb_th_serial(cr->threads.running_ec->thread_ptr) : -1,
323 rb_th_serial(th), cr->threads.running_ec == th->ec ?
" (same)" :
"");
325 if (cr->threads.running_ec != th->ec || always_reset) {
326 th->running_time_us = 0;
329 if (cr->threads.running_ec != th->ec) {
331 ruby_debug_printf(
"rb_ractor_thread_switch ec:%p->%p\n",
332 (
void *)cr->threads.running_ec, (
void *)th->ec);
339 cr->threads.running_ec = th->ec;
341 VM_ASSERT(cr == GET_RACTOR());
344#define rb_ractor_set_current_ec(cr, ec) rb_ractor_set_current_ec_(cr, ec, __FILE__, __LINE__)
345#ifdef RB_THREAD_LOCAL_SPECIFIER
352#ifdef RB_THREAD_LOCAL_SPECIFIER
353 rb_current_ec_set(ec);
355 native_tls_set(ruby_current_ec_key, ec);
357 RUBY_DEBUG_LOG2(file, line,
"ec:%p->%p", (
void *)cr->threads.running_ec, (
void *)ec);
358 VM_ASSERT(ec == NULL || cr->threads.running_ec != ec);
359 cr->threads.running_ec = ec;
362void rb_vm_ractor_blocking_cnt_inc(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line);
363void rb_vm_ractor_blocking_cnt_dec(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line);
365static inline rb_serial_t
374 cr->pub.targeted_hooks_cnt++;
381 cr->pub.targeted_hooks_cnt--;
384static inline unsigned int
387 return cr->pub.targeted_hooks_cnt;
390#if RACTOR_CHECK_MODE > 0
392extern bool rb_ractor_ignore_belonging_flag;
398rb_ractor_confirm_belonging(
VALUE obj)
400 if (rb_ractor_ignore_belonging_flag)
return obj;
403 if (UNLIKELY(rb_gc_obj_foreign_p(obj))) {
405 rb_bug(
"rb_ractor_confirm_belonging: unshareable object of another Ractor's objspace");
411rb_ractor_ignore_belonging(
bool flag)
413 rb_ractor_ignore_belonging_flag = flag;
417#define rb_ractor_confirm_belonging(obj) obj
418#define rb_ractor_ignore_belonging(flag) (0)
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
int len
Length of the buffer.
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.