1#include "internal/gc.h"
8#ifndef RACTOR_CHECK_MODE
9#define RACTOR_CHECK_MODE (VM_CHECK_MODE || RUBY_DEBUG) && (SIZEOF_UINT64_T == SIZEOF_VALUE)
13#define RUBY_TYPED_FROZEN_SHAREABLE_NO_REC RUBY_FL_FINALIZE
17 rb_nativethread_lock_t lock;
19#if RACTOR_CHECK_MODE > 0
23#ifndef RUBY_THREAD_PTHREAD_H
24 rb_nativethread_cond_t wakeup_cond;
31 struct ccan_list_head waiters;
34 VALUE default_port_value;
39 struct ccan_list_head monitors;
74 struct ccan_list_head set;
76 unsigned int blocking_cnt;
83 VALUE thgroup_default;
88 enum ractor_status status_;
90 struct ccan_list_node vmlr_node;
94 rb_serial_t next_ec_serial;
98 VALUE local_storage_store_lock;
106 bool malloc_gc_disabled;
127int rb_ractor_living_thread_num(
const rb_ractor_t *);
128VALUE rb_ractor_thread_list(
void);
129bool rb_ractor_p(
VALUE rv);
134void rb_ractor_blocking_threads_inc(
rb_ractor_t *r,
const char *file,
int line);
135void rb_ractor_blocking_threads_dec(
rb_ractor_t *r,
const char *file,
int line);
137void rb_ractor_vm_barrier_interrupt_running_thread(
rb_ractor_t *r);
138void rb_ractor_terminate_interrupt_main_thread(
rb_ractor_t *r);
139void rb_ractor_terminate_all(
void);
140bool rb_ractor_main_p_(
void);
143VALUE rb_ractor_require(
VALUE feature,
bool silent);
149RUBY_SYMBOL_EXPORT_BEGIN
150void rb_ractor_finish_marking(
void);
152bool rb_ractor_shareable_p_continue(
VALUE obj);
158RUBY_SYMBOL_EXPORT_END
161rb_ractor_main_p(
void)
163 if (ruby_single_main_ractor) {
167 return rb_ractor_main_p_();
172rb_ractor_status_p(
rb_ractor_t *r,
enum ractor_status status)
174 return r->status_ == status;
180 r->threads.sleeper++;
186 r->threads.sleeper--;
192 r->threads.sleeper = 0;
198 return r->threads.sleeper;
204 RUBY_DEBUG_LOG(
"th:%d->%u%s",
205 cr->threads.running_ec ? (
int)rb_th_serial(cr->threads.running_ec->thread_ptr) : -1,
206 rb_th_serial(th), cr->threads.running_ec == th->ec ?
" (same)" :
"");
208 if (cr->threads.running_ec != th->ec || always_reset) {
209 th->running_time_us = 0;
212 if (cr->threads.running_ec != th->ec) {
214 ruby_debug_printf(
"rb_ractor_thread_switch ec:%p->%p\n",
215 (
void *)cr->threads.running_ec, (
void *)th->ec);
222 cr->threads.running_ec = th->ec;
224 VM_ASSERT(cr == GET_RACTOR());
227#define rb_ractor_set_current_ec(cr, ec) rb_ractor_set_current_ec_(cr, ec, __FILE__, __LINE__)
228#ifdef RB_THREAD_LOCAL_SPECIFIER
235#ifdef RB_THREAD_LOCAL_SPECIFIER
236 rb_current_ec_set(ec);
238 native_tls_set(ruby_current_ec_key, ec);
240 RUBY_DEBUG_LOG2(file, line,
"ec:%p->%p", (
void *)cr->threads.running_ec, (
void *)ec);
241 VM_ASSERT(ec == NULL || cr->threads.running_ec != ec);
242 cr->threads.running_ec = ec;
245void rb_vm_ractor_blocking_cnt_inc(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line);
246void rb_vm_ractor_blocking_cnt_dec(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line);
248static inline uint32_t
257 cr->pub.targeted_hooks_cnt++;
264 cr->pub.targeted_hooks_cnt--;
267static inline unsigned int
270 return cr->pub.targeted_hooks_cnt;
273#if RACTOR_CHECK_MODE > 0
274# define RACTOR_BELONGING_ID(obj) (*(uint32_t *)(((uintptr_t)(obj)) + rb_gc_obj_slot_size(obj)))
276uint32_t rb_ractor_current_id(
void);
279rb_ractor_setup_belonging_to(
VALUE obj, uint32_t rid)
281 RACTOR_BELONGING_ID(obj) = rid;
284static inline uint32_t
285rb_ractor_belonging(
VALUE obj)
291 return RACTOR_BELONGING_ID(obj);
295extern bool rb_ractor_ignore_belonging_flag;
298rb_ractor_confirm_belonging(
VALUE obj)
300 if (rb_ractor_ignore_belonging_flag)
return obj;
302 uint32_t
id = rb_ractor_belonging(obj);
307 rb_bug(
"id == 0 but not shareable");
310 else if (UNLIKELY(
id != rb_ractor_current_id())) {
316 rb_bug(
"rb_ractor_confirm_belonging object-ractor id:%u, current-ractor id:%u",
id, rb_ractor_current_id());
323rb_ractor_ignore_belonging(
bool flag)
325 rb_ractor_ignore_belonging_flag = flag;
329#define rb_ractor_confirm_belonging(obj) obj
330#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.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
int len
Length of the buffer.
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
#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.