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;
191void rb_ractor_mark_terminated_join_value(
rb_ractor_t *r);
201enum ractor_wakeup_status {
209 enum ractor_wakeup_status wakeup_status;
211 struct ccan_list_node node;
215 const rb_hrtime_t *end;
235int rb_ractor_living_thread_num(
const rb_ractor_t *);
236VALUE rb_ractor_thread_list(
void);
237bool rb_ractor_p(
VALUE rv);
252void rb_ractor_blocking_threads_inc(
rb_ractor_t *r,
const char *file,
int line);
253void rb_ractor_blocking_threads_dec(
rb_ractor_t *r,
const char *file,
int line);
255void rb_ractor_vm_barrier_interrupt_running_thread(
rb_ractor_t *r);
256void rb_ractor_terminate_interrupt_main_thread(
rb_ractor_t *r);
257void rb_ractor_terminate_all(
void);
258bool rb_ractor_main_p_(
void);
261VALUE rb_ractor_require(
VALUE feature,
bool silent);
267RUBY_SYMBOL_EXPORT_BEGIN
268void rb_ractor_finish_marking(
bool full_mark);
270bool rb_ractor_shareable_p_continue(
VALUE obj);
276RUBY_SYMBOL_EXPORT_END
279rb_ractor_main_p(
void)
281 if (ruby_single_main_ractor) {
285 return rb_ractor_main_p_();
290rb_ractor_status_p(
rb_ractor_t *r,
enum ractor_status status)
292 return r->status_ == status;
298 r->threads.sleeper++;
304 r->threads.sleeper--;
310 r->threads.sleeper = 0;
316 return r->threads.sleeper;
322 RUBY_DEBUG_LOG(
"th:%d->%u%s",
323 cr->threads.running_ec ? (
int)rb_th_serial(cr->threads.running_ec->thread_ptr) : -1,
324 rb_th_serial(th), cr->threads.running_ec == th->ec ?
" (same)" :
"");
326 if (cr->threads.running_ec != th->ec || always_reset) {
327 th->running_time_us = 0;
330 if (cr->threads.running_ec != th->ec) {
332 ruby_debug_printf(
"rb_ractor_thread_switch ec:%p->%p\n",
333 (
void *)cr->threads.running_ec, (
void *)th->ec);
340 cr->threads.running_ec = th->ec;
342 VM_ASSERT(cr == GET_RACTOR());
345#define rb_ractor_set_current_ec(cr, ec) rb_ractor_set_current_ec_(cr, ec, __FILE__, __LINE__)
346#ifdef RB_THREAD_LOCAL_SPECIFIER
353#ifdef RB_THREAD_LOCAL_SPECIFIER
354 rb_current_ec_set(ec);
356 native_tls_set(ruby_current_ec_key, ec);
358 RUBY_DEBUG_LOG2(file, line,
"ec:%p->%p", (
void *)cr->threads.running_ec, (
void *)ec);
359 VM_ASSERT(ec == NULL || cr->threads.running_ec != ec);
360 cr->threads.running_ec = ec;
363void rb_vm_ractor_blocking_cnt_inc(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line);
364void rb_vm_ractor_blocking_cnt_dec(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line);
366static inline rb_serial_t
375 cr->pub.targeted_hooks_cnt++;
382 cr->pub.targeted_hooks_cnt--;
385static inline unsigned int
388 return cr->pub.targeted_hooks_cnt;
391#if RACTOR_CHECK_MODE > 0
393extern bool rb_ractor_ignore_belonging_flag;
399rb_ractor_confirm_belonging(
VALUE obj)
401 if (rb_ractor_ignore_belonging_flag)
return obj;
404 if (UNLIKELY(rb_gc_obj_foreign_p(obj))) {
406 rb_bug(
"rb_ractor_confirm_belonging: unshareable object of another Ractor's objspace");
412rb_ractor_ignore_belonging(
bool flag)
414 rb_ractor_ignore_belonging_flag = flag;
418#define rb_ractor_confirm_belonging(obj) obj
419#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.