61#undef __USE_FORTIFY_LEVEL
62#define __USE_FORTIFY_LEVEL 0
66#include "ruby/internal/config.h"
73#define TH_SCHED(th) (&(th)->ractor->threads.sched)
75#include "eval_intern.h"
78#include "internal/class.h"
79#include "internal/cont.h"
80#include "internal/error.h"
81#include "internal/gc.h"
82#include "internal/hash.h"
83#include "internal/io.h"
84#include "internal/object.h"
85#include "internal/proc.h"
87#include "internal/signal.h"
88#include "internal/thread.h"
89#include "internal/time.h"
90#include "internal/warnings.h"
99#include "ractor_core.h"
103#if USE_RJIT && defined(HAVE_SYS_WAIT_H)
107#ifndef USE_NATIVE_THREAD_PRIORITY
108#define USE_NATIVE_THREAD_PRIORITY 0
109#define RUBY_THREAD_PRIORITY_MAX 3
110#define RUBY_THREAD_PRIORITY_MIN -3
113static VALUE rb_cThreadShield;
115static VALUE sym_immediate;
116static VALUE sym_on_blocking;
117static VALUE sym_never;
119#define THREAD_LOCAL_STORAGE_INITIALISED FL_USER13
120#define THREAD_LOCAL_STORAGE_INITIALISED_P(th) RB_FL_TEST_RAW((th), THREAD_LOCAL_STORAGE_INITIALISED)
123rb_thread_local_storage(
VALUE thread)
125 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
133 SLEEP_DEADLOCKABLE = 0x01,
134 SLEEP_SPURIOUS_CHECK = 0x02,
135 SLEEP_ALLOW_SPURIOUS = 0x04,
136 SLEEP_NO_CHECKINTS = 0x08,
139static void sleep_forever(
rb_thread_t *th,
unsigned int fl);
140static int sleep_hrtime(
rb_thread_t *, rb_hrtime_t,
unsigned int fl);
142static void rb_thread_sleep_deadly_allow_spurious_wakeup(
VALUE blocker,
VALUE timeout, rb_hrtime_t end);
145static int rb_threadptr_pending_interrupt_empty_p(
const rb_thread_t *th);
146static const char *thread_status_name(
rb_thread_t *th,
int detail);
147static int hrtime_update_expire(rb_hrtime_t *,
const rb_hrtime_t);
148NORETURN(
static void async_bug_fd(
const char *mesg,
int errno_arg,
int fd));
149static int consume_communication_pipe(
int fd);
151static volatile int system_working = 1;
152static rb_internal_thread_specific_key_t specific_key_count;
155 struct ccan_list_node wfd_node;
163#define THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
166 enum rb_thread_status prev_status;
170static void unblock_function_clear(
rb_thread_t *th);
176#define THREAD_BLOCKING_BEGIN(th) do { \
177 struct rb_thread_sched * const sched = TH_SCHED(th); \
178 RB_VM_SAVE_MACHINE_CONTEXT(th); \
179 thread_sched_to_waiting((sched), (th));
181#define THREAD_BLOCKING_END(th) \
182 thread_sched_to_running((sched), (th)); \
183 rb_ractor_thread_switch(th->ractor, th); \
187#ifdef HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR_CONSTANT_P
188#define only_if_constant(expr, notconst) __builtin_choose_expr(__builtin_constant_p(expr), (expr), (notconst))
190#define only_if_constant(expr, notconst) (__builtin_constant_p(expr) ? (expr) : (notconst))
193#define only_if_constant(expr, notconst) notconst
195#define BLOCKING_REGION(th, exec, ubf, ubfarg, fail_if_interrupted) do { \
196 struct rb_blocking_region_buffer __region; \
197 if (blocking_region_begin(th, &__region, (ubf), (ubfarg), fail_if_interrupted) || \
199 !only_if_constant(fail_if_interrupted, TRUE)) { \
201 blocking_region_end(th, &__region); \
209#define RUBY_VM_CHECK_INTS_BLOCKING(ec) vm_check_ints_blocking(ec)
215 if (LIKELY(rb_threadptr_pending_interrupt_empty_p(th))) {
216 if (LIKELY(!RUBY_VM_INTERRUPTED_ANY(ec)))
return FALSE;
219 th->pending_interrupt_queue_checked = 0;
220 RUBY_VM_SET_INTERRUPT(ec);
222 return rb_threadptr_execute_interrupts(th, 1);
228 return vm_check_ints_blocking(ec);
236#if defined(HAVE_POLL)
237# if defined(__linux__)
240# if defined(__FreeBSD_version) && __FreeBSD_version >= 1100000
243# define POLLERR_SET (POLLHUP | POLLERR)
248timeout_prepare(rb_hrtime_t **to, rb_hrtime_t *rel, rb_hrtime_t *end,
252 *rel = rb_timeval2hrtime(timeout);
253 *end = rb_hrtime_add(rb_hrtime_now(), *rel);
261MAYBE_UNUSED(NOINLINE(
static int thread_start_func_2(
rb_thread_t *th,
VALUE *stack_start)));
263#include THREAD_IMPL_SRC
270#ifndef BUSY_WAIT_SIGNALS
271# define BUSY_WAIT_SIGNALS (0)
275# define USE_EVENTFD (0)
278#include "thread_sync.c"
308 if (fail_if_interrupted) {
309 if (RUBY_VM_INTERRUPTED_ANY(th->ec)) {
314 RUBY_VM_CHECK_INTS(th->ec);
318 }
while (!th->ec->raised_flag && RUBY_VM_INTERRUPTED_ANY(th->ec) &&
321 VM_ASSERT(th->unblock.func == NULL);
323 th->unblock.func = func;
324 th->unblock.arg = arg;
334 th->unblock.func = 0;
339rb_threadptr_interrupt_common(
rb_thread_t *th,
int trap)
341 RUBY_DEBUG_LOG(
"th:%u trap:%d", rb_th_serial(th), trap);
346 RUBY_VM_SET_TRAP_INTERRUPT(th->ec);
349 RUBY_VM_SET_INTERRUPT(th->ec);
352 if (th->unblock.func != NULL) {
353 (th->unblock.func)(th->unblock.arg);
365 RUBY_DEBUG_LOG(
"th:%u", rb_th_serial(th));
366 rb_threadptr_interrupt_common(th, 0);
372 rb_threadptr_interrupt_common(th, 1);
380 ccan_list_for_each(&r->threads.set, th, lt_node) {
381 if (th != main_thread) {
382 RUBY_DEBUG_LOG(
"terminate start th:%u status:%s", rb_th_serial(th), thread_status_name(th, TRUE));
384 rb_threadptr_pending_interrupt_enque(th, RUBY_FATAL_THREAD_TERMINATED);
385 rb_threadptr_interrupt(th);
387 RUBY_DEBUG_LOG(
"terminate done th:%u status:%s", rb_th_serial(th), thread_status_name(th, TRUE));
390 RUBY_DEBUG_LOG(
"main thread th:%u", rb_th_serial(th));
398 while (thread->join_list) {
402 thread->join_list = join_list->next;
406 if (target_thread->scheduler !=
Qnil && join_list->fiber) {
410 rb_threadptr_interrupt(target_thread);
412 switch (target_thread->status) {
414 case THREAD_STOPPED_FOREVER:
415 target_thread->status = THREAD_RUNNABLE;
425rb_threadptr_unlock_all_locking_mutexes(
rb_thread_t *th)
427 while (th->keeping_mutexes) {
429 th->keeping_mutexes = mutex->next_mutex;
433 const char *error_message = rb_mutex_unlock_th(mutex, th, mutex->fiber);
434 if (error_message) rb_bug(
"invalid keeping_mutexes: %s", error_message);
443 volatile int sleeping = 0;
445 if (cr->threads.main != th) {
446 rb_bug(
"rb_thread_terminate_all: called by child thread (%p, %p)",
447 (
void *)cr->threads.main, (
void *)th);
451 rb_threadptr_unlock_all_locking_mutexes(th);
454 if (EC_EXEC_TAG() == TAG_NONE) {
456 RUBY_DEBUG_LOG(
"th:%u", rb_th_serial(th));
458 terminate_all(cr, th);
460 while (rb_ractor_living_thread_num(cr) > 1) {
461 rb_hrtime_t rel = RB_HRTIME_PER_SEC;
467 native_sleep(th, &rel);
468 RUBY_VM_CHECK_INTS_BLOCKING(ec);
486void rb_threadptr_root_fiber_terminate(
rb_thread_t *th);
489thread_cleanup_func_before_exec(
void *th_ptr)
492 th->status = THREAD_KILLED;
495 th->ec->machine.stack_start = th->ec->machine.stack_end = NULL;
497 rb_threadptr_root_fiber_terminate(th);
501thread_cleanup_func(
void *th_ptr,
int atfork)
505 th->locking_mutex =
Qfalse;
506 thread_cleanup_func_before_exec(th_ptr);
527 native_thread_init_stack(th);
531rb_vm_proc_local_ep(
VALUE proc)
533 const VALUE *ep = vm_proc_ep(proc);
536 return rb_vm_ep_local_ep(ep);
545 int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler);
550 VALUE args = th->invoke_arg.proc.args;
551 const VALUE *args_ptr;
553 VALUE procval = th->invoke_arg.proc.proc;
555 GetProcPtr(procval, proc);
557 th->ec->errinfo =
Qnil;
558 th->ec->root_lep = rb_vm_proc_local_ep(procval);
559 th->ec->root_svar =
Qfalse;
561 vm_check_ints_blocking(th->ec);
563 if (th->invoke_type == thread_invoke_type_ractor_proc) {
564 VALUE self = rb_ractor_self(th->ractor);
568 rb_ractor_receive_parameters(th->ec, th->ractor, args_len, (
VALUE *)args_ptr);
569 vm_check_ints_blocking(th->ec);
571 return rb_vm_invoke_proc_with_self(
574 th->invoke_arg.proc.kw_splat,
575 VM_BLOCK_HANDLER_NONE
584 th->invoke_arg.proc.args =
Qnil;
590 vm_check_ints_blocking(th->ec);
592 return rb_vm_invoke_proc(
595 th->invoke_arg.proc.kw_splat,
596 VM_BLOCK_HANDLER_NONE
604 native_set_thread_name(th);
609 switch (th->invoke_type) {
610 case thread_invoke_type_proc:
611 result = thread_do_start_proc(th);
614 case thread_invoke_type_ractor_proc:
615 result = thread_do_start_proc(th);
616 rb_ractor_atexit(th->ec, result);
619 case thread_invoke_type_func:
620 result = (*th->invoke_arg.func.func)(th->invoke_arg.func.arg);
623 case thread_invoke_type_none:
624 rb_bug(
"unreachable");
639 STACK_GROW_DIR_DETECTION;
641 RUBY_DEBUG_LOG(
"th:%u", rb_th_serial(th));
642 VM_ASSERT(th != th->vm->ractor.main_thread);
644 enum ruby_tag_type state;
646 rb_thread_t *ractor_main_th = th->ractor->threads.main;
649 if (rb_ractor_status_p(th->ractor, ractor_blocking)) {
652 rb_vm_ractor_blocking_cnt_dec(th->vm, th->ractor, __FILE__, __LINE__);
654 r->r_stdin = rb_io_prep_stdin();
655 r->r_stdout = rb_io_prep_stdout();
656 r->r_stderr = rb_io_prep_stderr();
662 VM_ASSERT(UNDEF_P(th->value));
666 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
667 SAVE_ROOT_JMPBUF(th, thread_do_start(th));
670 errinfo = th->ec->errinfo;
672 VALUE exc = rb_vm_make_jump_tag_but_local_jump(state,
Qundef);
673 if (!
NIL_P(exc)) errinfo = exc;
675 if (state == TAG_FATAL) {
676 if (th->invoke_type == thread_invoke_type_ractor_proc) {
677 rb_ractor_atexit(th->ec,
Qnil);
685 if (th->report_on_exception) {
686 VALUE mesg = rb_thread_to_s(th->self);
687 rb_str_cat_cstr(mesg,
" terminated with exception (report_on_exception is true):\n");
688 rb_write_error_str(mesg);
689 rb_ec_error_print(th->ec, errinfo);
692 if (th->invoke_type == thread_invoke_type_ractor_proc) {
693 rb_ractor_atexit_exception(th->ec);
696 if (th->vm->thread_abort_on_exception ||
708 VM_ASSERT(!UNDEF_P(th->value));
710 rb_threadptr_join_list_wakeup(th);
711 rb_threadptr_unlock_all_locking_mutexes(th);
713 if (th->invoke_type == thread_invoke_type_ractor_proc) {
714 rb_thread_terminate_all(th);
715 rb_ractor_teardown(th->ec);
718 th->status = THREAD_KILLED;
719 RUBY_DEBUG_LOG(
"killed th:%u", rb_th_serial(th));
721 if (th->vm->ractor.main_thread == th) {
727 rb_threadptr_raise(ractor_main_th, 1, &errinfo);
732 rb_ec_clear_current_thread_trace_func(th->ec);
735 if (th->locking_mutex !=
Qfalse) {
736 rb_bug(
"thread_start_func_2: locking_mutex must not be set (%p:%"PRIxVALUE
")",
737 (
void *)th, th->locking_mutex);
740 if (ractor_main_th->status == THREAD_KILLED &&
741 th->ractor->threads.cnt <= 2 ) {
743 rb_threadptr_interrupt(ractor_main_th);
746 rb_check_deadlock(th->ractor);
748 rb_fiber_close(th->ec->fiber_ptr);
750 thread_cleanup_func(th, FALSE);
751 VM_ASSERT(th->ec->vm_stack == NULL);
753 if (th->invoke_type == thread_invoke_type_ractor_proc) {
757 thread_sched_to_dead(TH_SCHED(th), th);
758 rb_ractor_living_threads_remove(th->ractor, th);
761 rb_ractor_living_threads_remove(th->ractor, th);
762 thread_sched_to_dead(TH_SCHED(th), th);
769 enum thread_invoke_type type;
782static void thread_specific_storage_alloc(
rb_thread_t *th);
788 rb_thread_t *th = rb_thread_ptr(thval), *current_th = rb_ec_thread_ptr(ec);
791 thread_specific_storage_alloc(th);
795 "can't start a new thread (frozen ThreadGroup)");
798 rb_fiber_inherit_storage(ec, th->ec->fiber_ptr);
800 switch (params->type) {
801 case thread_invoke_type_proc:
802 th->invoke_type = thread_invoke_type_proc;
803 th->invoke_arg.proc.args = params->args;
804 th->invoke_arg.proc.proc = params->proc;
808 case thread_invoke_type_ractor_proc:
809#if RACTOR_CHECK_MODE > 0
810 rb_ractor_setup_belonging_to(thval, rb_ractor_id(params->g));
812 th->invoke_type = thread_invoke_type_ractor_proc;
813 th->ractor = params->g;
814 th->ractor->threads.main = th;
815 th->invoke_arg.proc.proc = rb_proc_isolate_bang(params->proc);
818 rb_ractor_send_parameters(ec, params->g, params->args);
821 case thread_invoke_type_func:
822 th->invoke_type = thread_invoke_type_func;
823 th->invoke_arg.func.func = params->fn;
824 th->invoke_arg.func.arg = (
void *)params->args;
828 rb_bug(
"unreachable");
831 th->priority = current_th->priority;
832 th->thgroup = current_th->thgroup;
834 th->pending_interrupt_queue = rb_ary_hidden_new(0);
835 th->pending_interrupt_queue_checked = 0;
836 th->pending_interrupt_mask_stack = rb_ary_dup(current_th->pending_interrupt_mask_stack);
837 RBASIC_CLEAR_CLASS(th->pending_interrupt_mask_stack);
841 RUBY_DEBUG_LOG(
"r:%u th:%u", rb_ractor_id(th->ractor), rb_th_serial(th));
843 rb_ractor_living_threads_insert(th->ractor, th);
846 err = native_thread_create(th);
848 th->status = THREAD_KILLED;
849 rb_ractor_living_threads_remove(th->ractor, th);
855#define threadptr_initialized(th) ((th)->invoke_type != thread_invoke_type_none)
878thread_s_new(
int argc,
VALUE *argv,
VALUE klass)
881 VALUE thread = rb_thread_alloc(klass);
883 if (GET_RACTOR()->threads.main->status == THREAD_KILLED) {
888 th = rb_thread_ptr(thread);
889 if (!threadptr_initialized(th)) {
890 rb_raise(
rb_eThreadError,
"uninitialized thread - check `%"PRIsVALUE
"#initialize'",
910 .type = thread_invoke_type_proc,
914 return thread_create_core(rb_thread_alloc(klass), ¶ms);
920 if (th->invoke_type == thread_invoke_type_proc) {
921 return rb_proc_location(th->invoke_arg.proc.proc);
937 else if (th->invoke_type != thread_invoke_type_none) {
938 VALUE loc = threadptr_invoke_proc_location(th);
941 "already initialized thread - %"PRIsVALUE
":%"PRIsVALUE,
950 .type = thread_invoke_type_proc,
954 return thread_create_core(thread, ¶ms);
962 .type = thread_invoke_type_func,
966 return thread_create_core(rb_thread_alloc(
rb_cThread), ¶ms);
973 .type = thread_invoke_type_ractor_proc,
978 return thread_create_core(rb_thread_alloc(
rb_cThread), ¶ms);
990remove_from_join_list(
VALUE arg)
995 if (target_thread->status != THREAD_KILLED) {
999 if (*join_list == p->waiter) {
1000 *join_list = (*join_list)->next;
1004 join_list = &(*join_list)->next;
1014 return th->status == THREAD_KILLED || !UNDEF_P(th->value);
1018thread_join_sleep(
VALUE arg)
1021 rb_thread_t *target_th = p->target, *th = p->waiter->thread;
1022 rb_hrtime_t end = 0, *limit = p->limit;
1025 end = rb_hrtime_add(*limit, rb_hrtime_now());
1028 while (!thread_finished(target_th)) {
1031 if (scheduler !=
Qnil) {
1034 if (thread_finished(target_th))
break;
1039 sleep_forever(th, SLEEP_DEADLOCKABLE | SLEEP_ALLOW_SPURIOUS | SLEEP_NO_CHECKINTS);
1042 if (hrtime_update_expire(limit, end)) {
1043 RUBY_DEBUG_LOG(
"timeout target_th:%u", rb_th_serial(target_th));
1046 th->status = THREAD_STOPPED;
1047 native_sleep(th, limit);
1049 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1050 th->status = THREAD_RUNNABLE;
1052 RUBY_DEBUG_LOG(
"interrupted target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1065 if (th == target_th) {
1066 rb_raise(
rb_eThreadError,
"Target thread must not be current thread");
1069 if (th->ractor->threads.main == target_th) {
1073 RUBY_DEBUG_LOG(
"target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1075 if (target_th->status != THREAD_KILLED) {
1077 waiter.next = target_th->join_list;
1079 waiter.fiber = rb_fiberptr_blocking(fiber) ? NULL : fiber;
1080 target_th->join_list = &waiter;
1083 arg.waiter = &waiter;
1084 arg.target = target_th;
1085 arg.timeout = timeout;
1093 RUBY_DEBUG_LOG(
"success target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1095 if (target_th->ec->errinfo !=
Qnil) {
1096 VALUE err = target_th->ec->errinfo;
1101 RUBY_DEBUG_LOG(
"terminated target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1106 rb_bug(
"thread_join: Fixnum (%d) should not reach here.",
FIX2INT(err));
1109 else if (THROW_DATA_P(target_th->ec->errinfo)) {
1110 rb_bug(
"thread_join: THROW_DATA should not reach here.");
1117 return target_th->self;
1160thread_join_m(
int argc,
VALUE *argv,
VALUE self)
1163 rb_hrtime_t rel = 0, *limit = 0;
1174 if (
NIL_P(timeout)) {
1178 rel = rb_sec2hrtime(NUM2TIMET(timeout));
1182 limit = double2hrtime(&rel,
rb_num2dbl(timeout));
1185 return thread_join(rb_thread_ptr(self), timeout, limit);
1203thread_value(
VALUE self)
1206 thread_join(th,
Qnil, 0);
1207 if (UNDEF_P(th->value)) {
1221#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
1222 if (clock_gettime(CLOCK_MONOTONIC, ts) == 0)
1232NOINLINE(rb_hrtime_t rb_hrtime_now(
void));
1239 return rb_timespec2hrtime(&ts);
1246COMPILER_WARNING_PUSH
1247#if defined(__GNUC__) && __GNUC__ == 7 && __GNUC_MINOR__ <= 3
1248COMPILER_WARNING_IGNORED(-Wmaybe-uninitialized)
1251#define PRIu64 PRI_64_PREFIX "u"
1259hrtime_update_expire(rb_hrtime_t *timeout,
const rb_hrtime_t end)
1261 rb_hrtime_t now = rb_hrtime_now();
1263 if (now > end)
return 1;
1265 RUBY_DEBUG_LOG(
"%"PRIu64
" > %"PRIu64
"", (uint64_t)end, (uint64_t)now);
1267 *timeout = end - now;
1273sleep_hrtime(
rb_thread_t *th, rb_hrtime_t rel,
unsigned int fl)
1275 enum rb_thread_status prev_status = th->status;
1277 rb_hrtime_t end = rb_hrtime_add(rb_hrtime_now(), rel);
1279 th->status = THREAD_STOPPED;
1280 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1281 while (th->status == THREAD_STOPPED) {
1282 native_sleep(th, &rel);
1283 woke = vm_check_ints_blocking(th->ec);
1284 if (woke && !(fl & SLEEP_SPURIOUS_CHECK))
1286 if (hrtime_update_expire(&rel, end))
1290 th->status = prev_status;
1295sleep_hrtime_until(
rb_thread_t *th, rb_hrtime_t end,
unsigned int fl)
1297 enum rb_thread_status prev_status = th->status;
1299 rb_hrtime_t rel = rb_hrtime_sub(end, rb_hrtime_now());
1301 th->status = THREAD_STOPPED;
1302 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1303 while (th->status == THREAD_STOPPED) {
1304 native_sleep(th, &rel);
1305 woke = vm_check_ints_blocking(th->ec);
1306 if (woke && !(fl & SLEEP_SPURIOUS_CHECK))
1308 if (hrtime_update_expire(&rel, end))
1312 th->status = prev_status;
1319 enum rb_thread_status prev_status = th->status;
1320 enum rb_thread_status status;
1323 status = fl & SLEEP_DEADLOCKABLE ? THREAD_STOPPED_FOREVER : THREAD_STOPPED;
1324 th->status = status;
1326 if (!(fl & SLEEP_NO_CHECKINTS)) RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1328 while (th->status == status) {
1329 if (fl & SLEEP_DEADLOCKABLE) {
1330 rb_ractor_sleeper_threads_inc(th->ractor);
1331 rb_check_deadlock(th->ractor);
1334 native_sleep(th, 0);
1336 if (fl & SLEEP_DEADLOCKABLE) {
1337 rb_ractor_sleeper_threads_dec(th->ractor);
1339 if (fl & SLEEP_ALLOW_SPURIOUS) {
1343 woke = vm_check_ints_blocking(th->ec);
1345 if (woke && !(fl & SLEEP_SPURIOUS_CHECK)) {
1349 th->status = prev_status;
1355 RUBY_DEBUG_LOG(
"forever");
1356 sleep_forever(GET_THREAD(), SLEEP_SPURIOUS_CHECK);
1362 RUBY_DEBUG_LOG(
"deadly");
1363 sleep_forever(GET_THREAD(), SLEEP_DEADLOCKABLE|SLEEP_SPURIOUS_CHECK);
1367rb_thread_sleep_deadly_allow_spurious_wakeup(
VALUE blocker,
VALUE timeout, rb_hrtime_t end)
1370 if (scheduler !=
Qnil) {
1374 RUBY_DEBUG_LOG(
"...");
1376 sleep_hrtime_until(GET_THREAD(), end, SLEEP_SPURIOUS_CHECK);
1379 sleep_forever(GET_THREAD(), SLEEP_DEADLOCKABLE);
1389 sleep_hrtime(th, rb_timeval2hrtime(&time), SLEEP_SPURIOUS_CHECK);
1402 RUBY_VM_CHECK_INTS_BLOCKING(GET_EC());
1410rb_thread_check_trap_pending(
void)
1412 return rb_signal_buff_size() != 0;
1419 return (
int)RUBY_VM_INTERRUPTED(rb_thread_ptr(thval)->ec);
1429rb_thread_schedule_limits(uint32_t limits_us)
1433 RUBY_DEBUG_LOG(
"us:%u", (
unsigned int)limits_us);
1435 if (th->running_time_us >= limits_us) {
1436 RUBY_DEBUG_LOG(
"switch %s",
"start");
1438 RB_VM_SAVE_MACHINE_CONTEXT(th);
1439 thread_sched_yield(TH_SCHED(th), th);
1440 rb_ractor_thread_switch(th->ractor, th);
1442 RUBY_DEBUG_LOG(
"switch %s",
"done");
1450 rb_thread_schedule_limits(0);
1451 RUBY_VM_CHECK_INTS(GET_EC());
1460#ifdef RUBY_VM_CRITICAL_SECTION
1461 VM_ASSERT(ruby_assert_critical_section_entered == 0);
1463 VM_ASSERT(th == GET_THREAD());
1465 region->prev_status = th->status;
1466 if (unblock_function_set(th, ubf, arg, fail_if_interrupted)) {
1467 th->blocking_region_buffer = region;
1468 th->status = THREAD_STOPPED;
1469 rb_ractor_blocking_threads_inc(th->ractor, __FILE__, __LINE__);
1471 RUBY_DEBUG_LOG(
"thread_id:%p", (
void *)th->nt->thread_id);
1473 RB_VM_SAVE_MACHINE_CONTEXT(th);
1474 thread_sched_to_waiting(TH_SCHED(th), th);
1486 unblock_function_clear(th);
1488 unregister_ubf_list(th);
1490 thread_sched_to_running(TH_SCHED(th), th);
1491 rb_ractor_thread_switch(th->ractor, th);
1493 th->blocking_region_buffer = 0;
1494 rb_ractor_blocking_threads_dec(th->ractor, __FILE__, __LINE__);
1495 if (th->status == THREAD_STOPPED) {
1496 th->status = region->prev_status;
1499 RUBY_DEBUG_LOG(
"end");
1503 VM_ASSERT(th == GET_THREAD());
1515 rb_vm_t *vm = rb_ec_vm_ptr(ec);
1516 bool is_main_thread = vm->ractor.main_thread == th;
1517 int saved_errno = 0;
1524 else if (ubf && rb_ractor_living_thread_num(th->ractor) == 1 && is_main_thread) {
1526 vm->ubf_async_safe = 1;
1530 BLOCKING_REGION(th, {
1532 saved_errno = rb_errno();
1535 if (is_main_thread) vm->ubf_async_safe = 0;
1538 RUBY_VM_CHECK_INTS_BLOCKING(ec);
1545 rb_errno_set(saved_errno);
1646 return rb_nogvl(func, data1, ubf, data2, 0);
1650waitfd_to_waiting_flag(
int wfd_event)
1652 return wfd_event << 1;
1664 ccan_list_add(&th->vm->waiting_fds, &wfd->wfd_node);
1670thread_io_wake_pending_closer(
struct waiting_fd *wfd)
1672 bool has_waiter = wfd->busy &&
RB_TEST(wfd->busy->wakeup_mutex);
1681 ccan_list_del(&wfd->wfd_node);
1693#if defined(USE_MN_THREADS) && USE_MN_THREADS
1694 if (!th_has_dedicated_nt(th) &&
1695 (events || timeout) &&
1699 rb_hrtime_t rel, *prel;
1702 rel = rb_timeval2hrtime(timeout);
1709 VM_ASSERT(prel || (events & (RB_WAITFD_IN | RB_WAITFD_OUT)));
1711 thread_io_setup_wfd(th, fd, wfd);
1714 r = thread_sched_wait_events(TH_SCHED(th), th, fd, waitfd_to_waiting_flag(events), prel);
1716 thread_io_wake_pending_closer(wfd);
1718 RUBY_VM_CHECK_INTS_BLOCKING(ec);
1728rb_thread_io_blocking_call(rb_blocking_function_t *func,
void *data1,
int fd,
int events)
1733 RUBY_DEBUG_LOG(
"th:%u fd:%d ev:%d", rb_th_serial(th), fd, events);
1737 thread_io_wait_events(th, ec, fd, events, NULL, &
waiting_fd);
1740 volatile int saved_errno = 0;
1741 enum ruby_tag_type state;
1752 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1755 saved_errno = errno;
1767 EC_JUMP_TAG(ec, state);
1770 RUBY_VM_CHECK_INTS_BLOCKING(ec);
1773 if (saved_errno == ETIMEDOUT) {
1777 errno = saved_errno;
1783rb_thread_io_blocking_region(rb_blocking_function_t *func,
void *data1,
int fd)
1785 return rb_thread_io_blocking_call(func, data1, fd, 0);
1830 fprintf(stderr,
"[BUG] rb_thread_call_with_gvl() is called by non-ruby thread\n");
1835 prev_unblock = th->unblock;
1838 rb_bug(
"rb_thread_call_with_gvl: called by a thread which has GVL.");
1841 blocking_region_end(th, brb);
1845 int released = blocking_region_begin(th, brb, prev_unblock.func, prev_unblock.arg, FALSE);
1860ruby_thread_has_gvl_p(
void)
1864 if (th && th->blocking_region_buffer == 0) {
1881thread_s_pass(
VALUE klass)
1906rb_threadptr_pending_interrupt_clear(
rb_thread_t *th)
1908 rb_ary_clear(th->pending_interrupt_queue);
1914 rb_ary_push(th->pending_interrupt_queue, v);
1915 th->pending_interrupt_queue_checked = 0;
1919threadptr_check_pending_interrupt_queue(
rb_thread_t *th)
1921 if (!th->pending_interrupt_queue) {
1926enum handle_interrupt_timing {
1928 INTERRUPT_IMMEDIATE,
1929 INTERRUPT_ON_BLOCKING,
1933static enum handle_interrupt_timing
1936 if (sym == sym_immediate) {
1937 return INTERRUPT_IMMEDIATE;
1939 else if (sym == sym_on_blocking) {
1940 return INTERRUPT_ON_BLOCKING;
1942 else if (sym == sym_never) {
1943 return INTERRUPT_NEVER;
1950static enum handle_interrupt_timing
1954 long mask_stack_len =
RARRAY_LEN(th->pending_interrupt_mask_stack);
1959 for (i=0; i<mask_stack_len; i++) {
1960 mask = mask_stack[mask_stack_len-(i+1)];
1965 return rb_threadptr_pending_interrupt_from_symbol(th, mask);
1977 klass =
RBASIC(mod)->klass;
1979 else if (mod != RCLASS_ORIGIN(mod)) {
1983 if ((sym = rb_hash_aref(mask, klass)) !=
Qnil) {
1984 return rb_threadptr_pending_interrupt_from_symbol(th, sym);
1989 return INTERRUPT_NONE;
1993rb_threadptr_pending_interrupt_empty_p(
const rb_thread_t *th)
1995 return RARRAY_LEN(th->pending_interrupt_queue) == 0;
2002 for (i=0; i<
RARRAY_LEN(th->pending_interrupt_queue); i++) {
2012rb_threadptr_pending_interrupt_deque(
rb_thread_t *th,
enum handle_interrupt_timing timing)
2017 for (i=0; i<
RARRAY_LEN(th->pending_interrupt_queue); i++) {
2020 enum handle_interrupt_timing mask_timing = rb_threadptr_pending_interrupt_check_mask(th,
CLASS_OF(err));
2022 switch (mask_timing) {
2023 case INTERRUPT_ON_BLOCKING:
2024 if (timing != INTERRUPT_ON_BLOCKING) {
2028 case INTERRUPT_NONE:
2029 case INTERRUPT_IMMEDIATE:
2030 rb_ary_delete_at(th->pending_interrupt_queue, i);
2032 case INTERRUPT_NEVER:
2037 th->pending_interrupt_queue_checked = 1;
2040 VALUE err = rb_ary_shift(th->pending_interrupt_queue);
2041 if (rb_threadptr_pending_interrupt_empty_p(th)) {
2042 th->pending_interrupt_queue_checked = 1;
2049threadptr_pending_interrupt_active_p(
rb_thread_t *th)
2056 if (th->pending_interrupt_queue_checked) {
2060 if (rb_threadptr_pending_interrupt_empty_p(th)) {
2072 if (val != sym_immediate && val != sym_on_blocking && val != sym_never) {
2073 rb_raise(rb_eArgError,
"unknown mask signature");
2081 if (
RTEST(*maskp)) {
2082 if (!RB_TYPE_P(*maskp,
T_HASH)) {
2083 VALUE prev = *maskp;
2084 *maskp = rb_ident_hash_new();
2089 rb_hash_aset(*maskp, key, val);
2207rb_thread_s_handle_interrupt(
VALUE self,
VALUE mask_arg)
2213 enum ruby_tag_type state;
2216 rb_raise(rb_eArgError,
"block is needed.");
2219 mask_arg = rb_to_hash_type(mask_arg);
2221 if (
OBJ_FROZEN(mask_arg) && rb_hash_compare_by_id_p(mask_arg)) {
2227 if (UNDEF_P(mask)) {
2234 else if (RB_TYPE_P(mask,
T_HASH)) {
2238 rb_ary_push(th->pending_interrupt_mask_stack, mask);
2239 if (!rb_threadptr_pending_interrupt_empty_p(th)) {
2240 th->pending_interrupt_queue_checked = 0;
2241 RUBY_VM_SET_INTERRUPT(th->ec);
2244 EC_PUSH_TAG(th->ec);
2245 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2250 rb_ary_pop(th->pending_interrupt_mask_stack);
2251 if (!rb_threadptr_pending_interrupt_empty_p(th)) {
2252 th->pending_interrupt_queue_checked = 0;
2253 RUBY_VM_SET_INTERRUPT(th->ec);
2256 RUBY_VM_CHECK_INTS(th->ec);
2259 EC_JUMP_TAG(th->ec, state);
2276rb_thread_pending_interrupt_p(
int argc,
VALUE *argv,
VALUE target_thread)
2278 rb_thread_t *target_th = rb_thread_ptr(target_thread);
2280 if (!target_th->pending_interrupt_queue) {
2283 if (rb_threadptr_pending_interrupt_empty_p(target_th)) {
2287 VALUE err = argv[0];
2289 rb_raise(
rb_eTypeError,
"class or module required for rescue clause");
2291 return RBOOL(rb_threadptr_pending_interrupt_include_p(target_th, err));
2356rb_thread_s_pending_interrupt_p(
int argc,
VALUE *argv,
VALUE self)
2358 return rb_thread_pending_interrupt_p(argc, argv, GET_THREAD()->self);
2361NORETURN(
static void rb_threadptr_to_kill(
rb_thread_t *th));
2366 rb_threadptr_pending_interrupt_clear(th);
2367 th->status = THREAD_RUNNABLE;
2369 th->ec->errinfo =
INT2FIX(TAG_FATAL);
2370 EC_JUMP_TAG(th->ec, TAG_FATAL);
2381 interrupt = ec->interrupt_flag;
2382 old = ATOMIC_CAS(ec->interrupt_flag, interrupt, interrupt & ec->interrupt_mask);
2383 }
while (old != interrupt);
2384 return interrupt & (
rb_atomic_t)~ec->interrupt_mask;
2388rb_threadptr_execute_interrupts(
rb_thread_t *th,
int blocking_timing)
2391 int postponed_job_interrupt = 0;
2394 if (th->ec->raised_flag)
return ret;
2396 while ((interrupt = threadptr_get_interrupts(th)) != 0) {
2398 int timer_interrupt;
2399 int pending_interrupt;
2401 int terminate_interrupt;
2403 timer_interrupt = interrupt & TIMER_INTERRUPT_MASK;
2404 pending_interrupt = interrupt & PENDING_INTERRUPT_MASK;
2405 postponed_job_interrupt = interrupt & POSTPONED_JOB_INTERRUPT_MASK;
2406 trap_interrupt = interrupt & TRAP_INTERRUPT_MASK;
2407 terminate_interrupt = interrupt & TERMINATE_INTERRUPT_MASK;
2409 if (interrupt & VM_BARRIER_INTERRUPT_MASK) {
2414 if (postponed_job_interrupt) {
2415 rb_postponed_job_flush(th->vm);
2419 if (trap_interrupt && (th == th->vm->ractor.main_thread)) {
2420 enum rb_thread_status prev_status = th->status;
2422 th->status = THREAD_RUNNABLE;
2424 while ((sig = rb_get_next_signal()) != 0) {
2425 ret |= rb_signal_exec(th, sig);
2428 th->status = prev_status;
2432 if (pending_interrupt && threadptr_pending_interrupt_active_p(th)) {
2433 VALUE err = rb_threadptr_pending_interrupt_deque(th, blocking_timing ? INTERRUPT_ON_BLOCKING : INTERRUPT_NONE);
2434 RUBY_DEBUG_LOG(
"err:%"PRIdVALUE, err);
2440 else if (err == RUBY_FATAL_THREAD_KILLED ||
2441 err == RUBY_FATAL_THREAD_TERMINATED ||
2443 terminate_interrupt = 1;
2446 if (err == th->vm->special_exceptions[ruby_error_stream_closed]) {
2448 err = ruby_vm_special_exception_copy(err);
2451 if (th->status == THREAD_STOPPED ||
2452 th->status == THREAD_STOPPED_FOREVER)
2453 th->status = THREAD_RUNNABLE;
2458 if (terminate_interrupt) {
2459 rb_threadptr_to_kill(th);
2462 if (timer_interrupt) {
2463 uint32_t limits_us = TIME_QUANTUM_USEC;
2465 if (th->priority > 0)
2466 limits_us <<= th->priority;
2468 limits_us >>= -th->priority;
2470 if (th->status == THREAD_RUNNABLE)
2471 th->running_time_us += 10 * 1000;
2473 VM_ASSERT(th->ec->cfp);
2477 rb_thread_schedule_limits(limits_us);
2484rb_thread_execute_interrupts(
VALUE thval)
2486 rb_threadptr_execute_interrupts(rb_thread_ptr(thval), 1);
2492 rb_threadptr_interrupt(th);
2500 if (rb_threadptr_dead(target_th)) {
2508 exc = rb_make_exception(argc, argv);
2513 if (rb_threadptr_dead(target_th)) {
2517 rb_ec_setup_exception(GET_EC(), exc,
Qundef);
2518 rb_threadptr_pending_interrupt_enque(target_th, exc);
2519 rb_threadptr_interrupt(target_th);
2524rb_threadptr_signal_raise(
rb_thread_t *th,
int sig)
2530 rb_threadptr_raise(th->vm->ractor.main_thread, 2, argv);
2542 rb_threadptr_raise(th->vm->ractor.main_thread, 2, argv);
2548 if (ec->raised_flag & RAISED_EXCEPTION) {
2551 ec->raised_flag |= RAISED_EXCEPTION;
2558 if (!(ec->raised_flag & RAISED_EXCEPTION)) {
2561 ec->raised_flag &= ~RAISED_EXCEPTION;
2568 rb_vm_t *vm = GET_THREAD()->vm;
2570 ccan_list_head_init(&busy->pending_fd_users);
2576 ccan_list_for_each_safe(&vm->waiting_fds, wfd, next, wfd_node) {
2577 if (wfd->fd == fd) {
2581 ccan_list_del(&wfd->wfd_node);
2582 ccan_list_add(&busy->pending_fd_users, &wfd->wfd_node);
2585 err = th->vm->special_exceptions[ruby_error_stream_closed];
2586 rb_threadptr_pending_interrupt_enque(th, err);
2587 rb_threadptr_interrupt(th);
2592 has_any = !ccan_list_empty(&busy->pending_fd_users);
2594 wakeup_mutex =
Qnil;
2597 RBASIC_CLEAR_CLASS(wakeup_mutex);
2599 busy->wakeup_mutex = wakeup_mutex;
2613 if (!
RB_TEST(busy->wakeup_mutex)) {
2620 while (!ccan_list_empty(&busy->pending_fd_users)) {
2631 if (rb_notify_fd_close(fd, &busy)) {
2632 rb_notify_fd_close_wait(&busy);
2658thread_raise_m(
int argc,
VALUE *argv,
VALUE self)
2663 threadptr_check_pending_interrupt_queue(target_th);
2664 rb_threadptr_raise(target_th, argc, argv);
2667 if (current_th == target_th) {
2668 RUBY_VM_CHECK_INTS(target_th->ec);
2690 if (target_th->to_kill || target_th->status == THREAD_KILLED) {
2693 if (target_th == target_th->vm->ractor.main_thread) {
2697 RUBY_DEBUG_LOG(
"target_th:%u", rb_th_serial(target_th));
2699 if (target_th == GET_THREAD()) {
2701 rb_threadptr_to_kill(target_th);
2704 threadptr_check_pending_interrupt_queue(target_th);
2705 rb_threadptr_pending_interrupt_enque(target_th, RUBY_FATAL_THREAD_KILLED);
2706 rb_threadptr_interrupt(target_th);
2713rb_thread_to_be_killed(
VALUE thread)
2717 if (target_th->to_kill || target_th->status == THREAD_KILLED) {
2793 if (target_th->status == THREAD_KILLED)
return Qnil;
2795 rb_threadptr_ready(target_th);
2797 if (target_th->status == THREAD_STOPPED ||
2798 target_th->status == THREAD_STOPPED_FOREVER) {
2799 target_th->status = THREAD_RUNNABLE;
2841 "stopping only thread\n\tnote: use sleep to stop forever");
2874 return rb_ractor_thread_list();
2900 return rb_thread_list();
2906 return GET_THREAD()->self;
2919thread_s_current(
VALUE klass)
2927 return GET_RACTOR()->threads.main->self;
2938rb_thread_s_main(
VALUE klass)
2965rb_thread_s_abort_exc(
VALUE _)
2967 return RBOOL(GET_THREAD()->vm->thread_abort_on_exception);
3002rb_thread_s_abort_exc_set(
VALUE self,
VALUE val)
3004 GET_THREAD()->vm->thread_abort_on_exception =
RTEST(val);
3025rb_thread_abort_exc(
VALUE thread)
3027 return RBOOL(rb_thread_ptr(thread)->abort_on_exception);
3045rb_thread_abort_exc_set(
VALUE thread,
VALUE val)
3047 rb_thread_ptr(thread)->abort_on_exception =
RTEST(val);
3095rb_thread_s_report_exc(
VALUE _)
3097 return RBOOL(GET_THREAD()->vm->thread_report_on_exception);
3132rb_thread_s_report_exc_set(
VALUE self,
VALUE val)
3134 GET_THREAD()->vm->thread_report_on_exception =
RTEST(val);
3151rb_thread_s_ignore_deadlock(
VALUE _)
3153 return RBOOL(GET_THREAD()->vm->thread_ignore_deadlock);
3178rb_thread_s_ignore_deadlock_set(
VALUE self,
VALUE val)
3180 GET_THREAD()->vm->thread_ignore_deadlock =
RTEST(val);
3202rb_thread_report_exc(
VALUE thread)
3204 return RBOOL(rb_thread_ptr(thread)->report_on_exception);
3222rb_thread_report_exc_set(
VALUE thread,
VALUE val)
3224 rb_thread_ptr(thread)->report_on_exception =
RTEST(val);
3239rb_thread_group(
VALUE thread)
3241 return rb_thread_ptr(thread)->thgroup;
3247 switch (th->status) {
3248 case THREAD_RUNNABLE:
3249 return th->to_kill ?
"aborting" :
"run";
3250 case THREAD_STOPPED_FOREVER:
3251 if (detail)
return "sleep_forever";
3252 case THREAD_STOPPED:
3264 return th->status == THREAD_KILLED;
3300rb_thread_status(
VALUE thread)
3304 if (rb_threadptr_dead(target_th)) {
3305 if (!
NIL_P(target_th->ec->errinfo) &&
3306 !
FIXNUM_P(target_th->ec->errinfo)) {
3314 return rb_str_new2(thread_status_name(target_th, FALSE));
3334rb_thread_alive_p(
VALUE thread)
3336 return RBOOL(!thread_finished(rb_thread_ptr(thread)));
3354rb_thread_stop_p(
VALUE thread)
3358 if (rb_threadptr_dead(th)) {
3361 return RBOOL(th->status == THREAD_STOPPED || th->status == THREAD_STOPPED_FOREVER);
3372rb_thread_getname(
VALUE thread)
3374 return rb_thread_ptr(thread)->name;
3393 enc = rb_enc_get(name);
3394 if (!rb_enc_asciicompat(enc)) {
3395 rb_raise(rb_eArgError,
"ASCII incompatible encoding (%s)",
3398 name = rb_str_new_frozen(name);
3400 target_th->name = name;
3401 if (threadptr_initialized(target_th) && target_th->has_dedicated_nt) {
3402 native_set_another_thread_name(target_th->nt->thread_id, name);
3407#if USE_NATIVE_THREAD_NATIVE_THREAD_ID
3431rb_thread_native_thread_id(
VALUE thread)
3434 if (rb_threadptr_dead(target_th))
return Qnil;
3435 return native_thread_native_thread_id(target_th);
3438# define rb_thread_native_thread_id rb_f_notimplement
3449rb_thread_to_s(
VALUE thread)
3456 status = thread_status_name(target_th, TRUE);
3457 str = rb_sprintf(
"#<%"PRIsVALUE
":%p", cname, (
void *)thread);
3458 if (!
NIL_P(target_th->name)) {
3459 rb_str_catf(str,
"@%"PRIsVALUE, target_th->name);
3461 if ((loc = threadptr_invoke_proc_location(target_th)) !=
Qnil) {
3462 rb_str_catf(str,
" %"PRIsVALUE
":%"PRIsVALUE,
3465 rb_str_catf(str,
" %s>", status);
3471#define recursive_key id__recursive_key__
3476 if (
id == recursive_key) {
3477 return th->ec->local_storage_recursive_hash;
3481 struct rb_id_table *local_storage = th->ec->local_storage;
3483 if (local_storage != NULL && rb_id_table_lookup(local_storage,
id, &val)) {
3495 return threadptr_local_aref(rb_thread_ptr(thread),
id);
3562 if (!
id)
return Qnil;
3580rb_thread_fetch(
int argc,
VALUE *argv,
VALUE self)
3591 if (block_given && argc == 2) {
3592 rb_warn(
"block supersedes default value argument");
3597 if (
id == recursive_key) {
3598 return target_th->ec->local_storage_recursive_hash;
3600 else if (
id && target_th->ec->local_storage &&
3601 rb_id_table_lookup(target_th->ec->local_storage,
id, &val)) {
3604 else if (block_given) {
3607 else if (argc == 1) {
3608 rb_key_err_raise(rb_sprintf(
"key not found: %+"PRIsVALUE, key), self, key);
3618 if (
id == recursive_key) {
3619 th->ec->local_storage_recursive_hash = val;
3623 struct rb_id_table *local_storage = th->ec->local_storage;
3626 if (!local_storage)
return Qnil;
3627 rb_id_table_delete(local_storage,
id);
3631 if (local_storage == NULL) {
3632 th->ec->local_storage = local_storage = rb_id_table_create(0);
3634 rb_id_table_insert(local_storage,
id, val);
3647 return threadptr_local_aset(rb_thread_ptr(thread),
id, val);
3698rb_thread_variable_get(
VALUE thread,
VALUE key)
3702 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
3705 locals = rb_thread_local_storage(thread);
3727 locals = rb_thread_local_storage(thread);
3749 struct rb_id_table *local_storage = rb_thread_ptr(self)->ec->local_storage;
3751 if (!
id || local_storage == NULL) {
3754 return RBOOL(rb_id_table_lookup(local_storage,
id, &val));
3757static enum rb_id_table_iterator_result
3758thread_keys_i(
ID key,
VALUE value,
void *ary)
3761 return ID_TABLE_CONTINUE;
3768 return rb_ractor_living_thread_num(GET_RACTOR()) == 1;
3786rb_thread_keys(
VALUE self)
3788 struct rb_id_table *local_storage = rb_thread_ptr(self)->ec->local_storage;
3789 VALUE ary = rb_ary_new();
3791 if (local_storage) {
3792 rb_id_table_foreach(local_storage, thread_keys_i, (
void *)ary);
3800 rb_ary_push(ary, key);
3822rb_thread_variables(
VALUE thread)
3828 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
3831 locals = rb_thread_local_storage(thread);
3858 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
3861 locals = rb_thread_local_storage(thread);
3882rb_thread_priority(
VALUE thread)
3884 return INT2NUM(rb_thread_ptr(thread)->priority);
3915rb_thread_priority_set(
VALUE thread,
VALUE prio)
3920#if USE_NATIVE_THREAD_PRIORITY
3921 target_th->priority =
NUM2INT(prio);
3922 native_thread_apply_priority(th);
3925 if (priority > RUBY_THREAD_PRIORITY_MAX) {
3926 priority = RUBY_THREAD_PRIORITY_MAX;
3928 else if (priority < RUBY_THREAD_PRIORITY_MIN) {
3929 priority = RUBY_THREAD_PRIORITY_MIN;
3931 target_th->priority = (int8_t)priority;
3933 return INT2NUM(target_th->priority);
3938#if defined(NFDBITS) && defined(HAVE_RB_FD_INIT)
3974 FD_ZERO(fds->
fdset);
3980 size_t size = howmany(
rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
3982 if (size <
sizeof(fd_set))
3983 size =
sizeof(fd_set);
4007 size_t m = howmany(n + 1, NFDBITS) *
sizeof(fd_mask);
4008 size_t o = howmany(fds->
maxfd, NFDBITS) *
sizeof(fd_mask);
4010 if (m <
sizeof(fd_set)) m =
sizeof(fd_set);
4011 if (o <
sizeof(fd_set)) o =
sizeof(fd_set);
4015 memset((
char *)fds->
fdset + o, 0, m - o);
4024 FD_SET(n, fds->
fdset);
4030 if (n >= fds->
maxfd)
return;
4031 FD_CLR(n, fds->
fdset);
4037 if (n >= fds->
maxfd)
return 0;
4038 return FD_ISSET(n, fds->
fdset) != 0;
4044 size_t size = howmany(max, NFDBITS) *
sizeof(fd_mask);
4046 if (size <
sizeof(fd_set)) size =
sizeof(fd_set);
4049 memcpy(dst->
fdset, src, size);
4055 size_t size = howmany(
rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
4057 if (size <
sizeof(fd_set))
4058 size =
sizeof(fd_set);
4067 fd_set *r = NULL, *w = NULL, *e = NULL;
4080 return select(n, r, w, e, timeout);
4083#define rb_fd_no_init(fds) ((void)((fds)->fdset = 0), (void)((fds)->maxfd = 0))
4090#define FD_ZERO(f) rb_fd_zero(f)
4091#define FD_SET(i, f) rb_fd_set((i), (f))
4092#define FD_CLR(i, f) rb_fd_clr((i), (f))
4093#define FD_ISSET(i, f) rb_fd_isset((i), (f))
4095#elif defined(_WIN32)
4100 set->
capa = FD_SETSIZE;
4102 FD_ZERO(set->
fdset);
4124 SOCKET s = rb_w32_get_osfhandle(fd);
4126 for (i = 0; i < set->
fdset->fd_count; i++) {
4127 if (set->
fdset->fd_array[i] == s) {
4131 if (set->
fdset->fd_count >= (
unsigned)set->
capa) {
4132 set->
capa = (set->
fdset->fd_count / FD_SETSIZE + 1) * FD_SETSIZE;
4134 rb_xrealloc_mul_add(
4135 set->
fdset, set->
capa,
sizeof(SOCKET),
sizeof(
unsigned int));
4137 set->
fdset->fd_array[set->
fdset->fd_count++] = s;
4145#define FD_ZERO(f) rb_fd_zero(f)
4146#define FD_SET(i, f) rb_fd_set((i), (f))
4147#define FD_CLR(i, f) rb_fd_clr((i), (f))
4148#define FD_ISSET(i, f) rb_fd_isset((i), (f))
4150#define rb_fd_no_init(fds) (void)((fds)->fdset = 0)
4154#ifndef rb_fd_no_init
4155#define rb_fd_no_init(fds) (void)(fds)
4159wait_retryable(
int *result,
int errnum, rb_hrtime_t *rel, rb_hrtime_t end)
4168 if (rel && hrtime_update_expire(rel, end)) {
4175 else if (*result == 0) {
4178 return !hrtime_update_expire(rel, end);
4198select_set_free(
VALUE p)
4215 rb_hrtime_t *to, rel, end = 0;
4217 timeout_prepare(&to, &rel, &end, set->timeout);
4218#define restore_fdset(dst, src) \
4219 ((dst) ? rb_fd_dup(dst, src) : (void)0)
4220#define do_select_update() \
4221 (restore_fdset(set->rset, &set->orig_rset), \
4222 restore_fdset(set->wset, &set->orig_wset), \
4223 restore_fdset(set->eset, &set->orig_eset), \
4229 BLOCKING_REGION(set->th, {
4232 if (!RUBY_VM_INTERRUPTED(set->th->ec)) {
4233 result = native_fd_select(set->max,
4234 set->rset, set->wset, set->eset,
4235 rb_hrtime2timeval(&tv, to), set->th);
4236 if (result < 0) lerrno = errno;
4238 }, ubf_select, set->th, TRUE);
4240 RUBY_VM_CHECK_INTS_BLOCKING(set->th->ec);
4241 }
while (wait_retryable(&result, lerrno, to, end) && do_select_update());
4247 return (
VALUE)result;
4256 set.th = GET_THREAD();
4257 RUBY_VM_CHECK_INTS_BLOCKING(set.th->ec);
4262 set.timeout = timeout;
4264 if (!set.rset && !set.wset && !set.eset) {
4273#define fd_init_copy(f) do { \
4275 rb_fd_resize(set.max - 1, set.f); \
4276 if (&set.orig_##f != set.f) { \
4277 rb_fd_init_copy(&set.orig_##f, set.f); \
4281 rb_fd_no_init(&set.orig_##f); \
4295#define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
4296#define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
4297#define POLLEX_SET (POLLPRI)
4300# define POLLERR_SET (0)
4307rb_thread_wait_for_single_fd(
int fd,
int events,
struct timeval *timeout)
4309 struct pollfd fds[1];
4314 volatile int lerrno;
4319 if (thread_io_wait_events(th, ec, fd, events, timeout, &wfd)) {
4323 thread_io_setup_wfd(th, fd, &wfd);
4325 EC_PUSH_TAG(wfd.th->ec);
4326 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
4327 rb_hrtime_t *to, rel, end = 0;
4328 RUBY_VM_CHECK_INTS_BLOCKING(wfd.th->ec);
4329 timeout_prepare(&to, &rel, &end, timeout);
4331 fds[0].events = (short)events;
4337 BLOCKING_REGION(wfd.th, {
4340 if (!RUBY_VM_INTERRUPTED(wfd.th->ec)) {
4341 result = ppoll(fds, nfds, rb_hrtime2timespec(&ts, to), 0);
4342 if (result < 0) lerrno = errno;
4344 }, ubf_select, wfd.th, TRUE);
4346 RUBY_VM_CHECK_INTS_BLOCKING(wfd.th->ec);
4347 }
while (wait_retryable(&result, lerrno, to, end));
4351 thread_io_wake_pending_closer(&wfd);
4354 EC_JUMP_TAG(wfd.th->ec, state);
4362 if (fds[0].revents & POLLNVAL) {
4372 if (fds[0].revents & POLLIN_SET)
4373 result |= RB_WAITFD_IN;
4374 if (fds[0].revents & POLLOUT_SET)
4375 result |= RB_WAITFD_OUT;
4376 if (fds[0].revents & POLLEX_SET)
4377 result |= RB_WAITFD_PRI;
4380 if (fds[0].revents & POLLERR_SET)
4399select_single(
VALUE ptr)
4405 args->read, args->write, args->except, args->tv);
4407 args->as.error =
errno;
4410 if (args->read &&
rb_fd_isset(args->as.fd, args->read))
4412 if (args->write &&
rb_fd_isset(args->as.fd, args->write))
4414 if (args->except &&
rb_fd_isset(args->as.fd, args->except))
4421select_single_cleanup(
VALUE ptr)
4425 thread_io_wake_pending_closer(&args->wfd);
4446rb_thread_wait_for_single_fd(
int fd,
int events,
struct timeval *timeout)
4455 if (thread_io_wait_events(th, ec, fd, events, timeout, &args.wfd)) {
4460 args.read = (events & RB_WAITFD_IN) ? init_set_fd(fd, &rfds) : NULL;
4461 args.write = (events & RB_WAITFD_OUT) ? init_set_fd(fd, &wfds) : NULL;
4462 args.except = (events & RB_WAITFD_PRI) ? init_set_fd(fd, &efds) : NULL;
4466 args.wfd.busy = NULL;
4470 ccan_list_add(&args.wfd.th->vm->waiting_fds, &args.wfd.wfd_node);
4474 r = (int)
rb_ensure(select_single, ptr, select_single_cleanup, ptr);
4476 errno = args.as.error;
4486#ifdef USE_CONSERVATIVE_STACK_END
4488rb_gc_set_stack_end(
VALUE **stack_end_p)
4491 *stack_end_p = &stack_end;
4503 if (rb_signal_buff_size() > 0) {
4505 threadptr_trap_interrupt(mth);
4510async_bug_fd(
const char *mesg,
int errno_arg,
int fd)
4513 size_t n = strlcpy(buff, mesg,
sizeof(buff));
4514 if (n <
sizeof(buff)-3) {
4515 ruby_snprintf(buff+n,
sizeof(buff)-n,
"(%d)", fd);
4517 rb_async_bug_errno(buff, errno_arg);
4522consume_communication_pipe(
int fd)
4528 static char buff[1024];
4534 result = read(fd, buff,
sizeof(buff));
4536 RUBY_DEBUG_LOG(
"resultf:%d buff:%lu", (
int)result, (
unsigned long)buff[0]);
4538 RUBY_DEBUG_LOG(
"result:%d", (
int)result);
4542 if (USE_EVENTFD || result < (ssize_t)
sizeof(buff)) {
4546 else if (result == 0) {
4549 else if (result < 0) {
4555#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
4560 async_bug_fd(
"consume_communication_pipe: read", e, fd);
4567rb_thread_stop_timer_thread(
void)
4569 if (TIMER_THREAD_CREATED_P() && native_stop_timer_thread()) {
4570 native_reset_timer_thread();
4575rb_thread_reset_timer_thread(
void)
4577 native_reset_timer_thread();
4581rb_thread_start_timer_thread(
void)
4584 rb_thread_create_timer_thread();
4588clear_coverage_i(st_data_t key, st_data_t val, st_data_t dummy)
4596 if (GET_VM()->coverage_mode & COVERAGE_TARGET_ONESHOT_LINES) {
4597 rb_ary_clear(lines);
4618rb_clear_coverages(
void)
4620 VALUE coverages = rb_get_coverages();
4621 if (
RTEST(coverages)) {
4626#if defined(HAVE_WORKING_FORK)
4634 vm->ractor.main_ractor = r;
4635 vm->ractor.main_thread = th;
4636 r->threads.main = th;
4637 r->status_ = ractor_created;
4639 thread_sched_atfork(TH_SCHED(th));
4643 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
4644 ccan_list_for_each(&r->threads.set, i, lt_node) {
4648 rb_vm_living_threads_init(vm);
4650 rb_ractor_atfork(vm, th);
4651 rb_vm_postponed_job_atfork();
4660 rb_ractor_sleeper_threads_clear(th->ractor);
4661 rb_clear_coverages();
4664 rb_thread_reset_timer_thread();
4665 rb_thread_start_timer_thread();
4667 VM_ASSERT(vm->ractor.blocking_cnt == 0);
4668 VM_ASSERT(vm->ractor.cnt == 1);
4674 if (th != current_th) {
4675 rb_mutex_abandon_keeping_mutexes(th);
4676 rb_mutex_abandon_locking_mutex(th);
4677 thread_cleanup_func(th, TRUE);
4686 rb_thread_atfork_internal(th, terminate_atfork_i);
4687 th->join_list = NULL;
4688 rb_fiber_atfork(th);
4697 if (th != current_th) {
4698 thread_cleanup_func_before_exec(th);
4706 rb_thread_atfork_internal(th, terminate_atfork_before_exec_i);
4731 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
4754thgroup_s_alloc(
VALUE klass)
4775thgroup_list(
VALUE group)
4777 VALUE ary = rb_ary_new();
4781 ccan_list_for_each(&r->threads.set, th, lt_node) {
4782 if (th->thgroup == group) {
4783 rb_ary_push(ary, th->self);
4807thgroup_enclose(
VALUE group)
4826thgroup_enclosed_p(
VALUE group)
4831 return RBOOL(data->enclosed);
4871 if (data->enclosed) {
4879 if (data->enclosed) {
4881 "can't move from the enclosed thread group");
4884 target_th->thgroup = group;
4892thread_shield_mark(
void *ptr)
4894 rb_gc_mark((
VALUE)ptr);
4899 {thread_shield_mark, 0, 0,},
4900 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
4904thread_shield_alloc(
VALUE klass)
4909#define GetThreadShieldPtr(obj) ((VALUE)rb_check_typeddata((obj), &thread_shield_data_type))
4910#define THREAD_SHIELD_WAITING_MASK (((FL_USER19-1)&~(FL_USER0-1))|FL_USER19)
4911#define THREAD_SHIELD_WAITING_SHIFT (FL_USHIFT)
4912#define THREAD_SHIELD_WAITING_MAX (THREAD_SHIELD_WAITING_MASK>>THREAD_SHIELD_WAITING_SHIFT)
4913STATIC_ASSERT(THREAD_SHIELD_WAITING_MAX, THREAD_SHIELD_WAITING_MAX <= UINT_MAX);
4914static inline unsigned int
4915rb_thread_shield_waiting(
VALUE b)
4917 return ((
RBASIC(b)->flags&THREAD_SHIELD_WAITING_MASK)>>THREAD_SHIELD_WAITING_SHIFT);
4921rb_thread_shield_waiting_inc(
VALUE b)
4923 unsigned int w = rb_thread_shield_waiting(b);
4925 if (w > THREAD_SHIELD_WAITING_MAX)
4927 RBASIC(b)->flags &= ~THREAD_SHIELD_WAITING_MASK;
4928 RBASIC(b)->flags |= ((
VALUE)w << THREAD_SHIELD_WAITING_SHIFT);
4932rb_thread_shield_waiting_dec(
VALUE b)
4934 unsigned int w = rb_thread_shield_waiting(b);
4937 RBASIC(b)->flags &= ~THREAD_SHIELD_WAITING_MASK;
4938 RBASIC(b)->flags |= ((
VALUE)w << THREAD_SHIELD_WAITING_SHIFT);
4942rb_thread_shield_new(
void)
4944 VALUE thread_shield = thread_shield_alloc(rb_cThreadShield);
4946 return thread_shield;
4950rb_thread_shield_owned(
VALUE self)
4952 VALUE mutex = GetThreadShieldPtr(self);
4953 if (!mutex)
return false;
4957 return m->fiber == GET_EC()->fiber_ptr;
4969rb_thread_shield_wait(
VALUE self)
4971 VALUE mutex = GetThreadShieldPtr(self);
4974 if (!mutex)
return Qfalse;
4975 m = mutex_ptr(mutex);
4976 if (m->fiber == GET_EC()->fiber_ptr)
return Qnil;
4977 rb_thread_shield_waiting_inc(self);
4979 rb_thread_shield_waiting_dec(self);
4982 return rb_thread_shield_waiting(self) > 0 ?
Qnil :
Qfalse;
4986thread_shield_get_mutex(
VALUE self)
4988 VALUE mutex = GetThreadShieldPtr(self);
4990 rb_raise(
rb_eThreadError,
"destroyed thread shield - %p", (
void *)self);
4998rb_thread_shield_release(
VALUE self)
5000 VALUE mutex = thread_shield_get_mutex(self);
5002 return RBOOL(rb_thread_shield_waiting(self) > 0);
5009rb_thread_shield_destroy(
VALUE self)
5011 VALUE mutex = thread_shield_get_mutex(self);
5014 return RBOOL(rb_thread_shield_waiting(self) > 0);
5020 return th->ec->local_storage_recursive_hash;
5026 th->ec->local_storage_recursive_hash = hash;
5038recursive_list_access(
VALUE sym)
5041 VALUE hash = threadptr_recursive_hash(th);
5044 hash = rb_ident_hash_new();
5045 threadptr_recursive_hash_set(th, hash);
5049 list = rb_hash_aref(hash, sym);
5052 list = rb_ident_hash_new();
5053 rb_hash_aset(hash, sym, list);
5067#if SIZEOF_LONG == SIZEOF_VOIDP
5068 #define OBJ_ID_EQL(obj_id, other) ((obj_id) == (other))
5069#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
5070 #define OBJ_ID_EQL(obj_id, other) (RB_BIGNUM_TYPE_P((obj_id)) ? \
5071 rb_big_eql((obj_id), (other)) : ((obj_id) == (other)))
5074 VALUE pair_list = rb_hash_lookup2(list, obj,
Qundef);
5075 if (UNDEF_P(pair_list))
5077 if (paired_obj_id) {
5078 if (!RB_TYPE_P(pair_list,
T_HASH)) {
5079 if (!OBJ_ID_EQL(paired_obj_id, pair_list))
5083 if (
NIL_P(rb_hash_lookup(pair_list, paired_obj_id)))
5105 rb_hash_aset(list, obj,
Qtrue);
5107 else if (UNDEF_P(pair_list = rb_hash_lookup2(list, obj,
Qundef))) {
5108 rb_hash_aset(list, obj, paired_obj);
5111 if (!RB_TYPE_P(pair_list,
T_HASH)){
5112 VALUE other_paired_obj = pair_list;
5113 pair_list = rb_hash_new();
5114 rb_hash_aset(pair_list, other_paired_obj,
Qtrue);
5115 rb_hash_aset(list, obj, pair_list);
5117 rb_hash_aset(pair_list, paired_obj,
Qtrue);
5133 VALUE pair_list = rb_hash_lookup2(list, obj,
Qundef);
5134 if (UNDEF_P(pair_list)) {
5137 if (RB_TYPE_P(pair_list,
T_HASH)) {
5138 rb_hash_delete_entry(pair_list, paired_obj);
5144 rb_hash_delete_entry(list, obj);
5160 return (*p->func)(p->obj, p->arg, FALSE);
5181 p.list = recursive_list_access(sym);
5185 outermost = outer && !recursive_check(p.list,
ID2SYM(recursive_key), 0);
5187 if (recursive_check(p.list, p.obj, pairid)) {
5188 if (outer && !outermost) {
5191 return (*func)(obj, arg, TRUE);
5194 enum ruby_tag_type state;
5199 recursive_push(p.list,
ID2SYM(recursive_key), 0);
5200 recursive_push(p.list, p.obj, p.pairid);
5201 result = rb_catch_protect(p.list, exec_recursive_i, (
VALUE)&p, &state);
5202 if (!recursive_pop(p.list, p.obj, p.pairid))
goto invalid;
5203 if (!recursive_pop(p.list,
ID2SYM(recursive_key), 0))
goto invalid;
5204 if (state != TAG_NONE) EC_JUMP_TAG(GET_EC(), state);
5205 if (result == p.list) {
5206 result = (*func)(obj, arg, TRUE);
5211 recursive_push(p.list, p.obj, p.pairid);
5212 EC_PUSH_TAG(GET_EC());
5213 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
5214 ret = (*func)(obj, arg, FALSE);
5217 if (!recursive_pop(p.list, p.obj, p.pairid)) {
5220 if (state != TAG_NONE) EC_JUMP_TAG(GET_EC(), state);
5229 "for %+"PRIsVALUE
" in %+"PRIsVALUE,
5253 return exec_recursive(func, obj, rb_memory_id(paired_obj), arg, 0,
rb_frame_last_func());
5271 return exec_recursive(func, obj, 0, arg, 1, mid);
5283 return exec_recursive(func, obj, rb_memory_id(paired_obj), arg, 1,
rb_frame_last_func());
5295rb_thread_backtrace_m(
int argc,
VALUE *argv,
VALUE thval)
5297 return rb_vm_thread_backtrace(argc, argv, thval);
5312rb_thread_backtrace_locations_m(
int argc,
VALUE *argv,
VALUE thval)
5314 return rb_vm_thread_backtrace_locations(argc, argv, thval);
5318Init_Thread_Mutex(
void)
5408 rb_vm_register_special_exception(ruby_error_stream_closed,
rb_eIOError,
5409 "stream closed in another thread");
5419 th->thgroup = th->ractor->thgroup_default =
rb_obj_alloc(cThGroup);
5430#ifdef HAVE_PTHREAD_NP_H
5431 VM_ASSERT(TH_SCHED(th)->running == th);
5437 th->pending_interrupt_queue = rb_ary_hidden_new(0);
5438 th->pending_interrupt_queue_checked = 0;
5439 th->pending_interrupt_mask_stack = rb_ary_hidden_new(0);
5443 rb_thread_create_timer_thread();
5459#ifdef NON_SCALAR_THREAD_ID
5460 #define thread_id_str(th) (NULL)
5462 #define thread_id_str(th) ((void *)(uintptr_t)(th)->nt->thread_id)
5471 rb_str_catf(msg,
"\n%d threads, %d sleeps current:%p main thread:%p\n",
5472 rb_ractor_living_thread_num(r), rb_ractor_sleeper_thread_num(r),
5473 (
void *)GET_THREAD(), (
void *)r->threads.main);
5475 ccan_list_for_each(&r->threads.set, th, lt_node) {
5476 rb_str_catf(msg,
"* %+"PRIsVALUE
"\n rb_thread_t:%p "
5478 th->self, (
void *)th, th->nt ? thread_id_str(th) :
"N/A", th->ec->interrupt_flag);
5480 if (th->locking_mutex) {
5481 rb_mutex_t *mutex = mutex_ptr(th->locking_mutex);
5482 rb_str_catf(msg,
" mutex:%p cond:%"PRIuSIZE,
5483 (
void *)mutex->fiber, rb_mutex_num_waiting(mutex));
5489 rb_str_catf(msg,
"\n depended by: tb_thread_id:%p", (
void *)list->thread);
5493 rb_str_catf(msg,
"\n ");
5494 rb_str_concat(msg, rb_ary_join(rb_ec_backtrace_str_ary(th->ec, 0, 0), sep));
5495 rb_str_catf(msg,
"\n");
5502 if (GET_THREAD()->vm->thread_ignore_deadlock)
return;
5504#ifdef RUBY_THREAD_PTHREAD_H
5505 if (r->threads.sched.readyq_cnt > 0)
return;
5508 int sleeper_num = rb_ractor_sleeper_thread_num(r);
5509 int ltnum = rb_ractor_living_thread_num(r);
5511 if (ltnum > sleeper_num)
return;
5512 if (ltnum < sleeper_num) rb_bug(
"sleeper must not be more than vm_living_thread_num(vm)");
5517 ccan_list_for_each(&r->threads.set, th, lt_node) {
5518 if (th->status != THREAD_STOPPED_FOREVER || RUBY_VM_INTERRUPTED(th->ec)) {
5521 else if (th->locking_mutex) {
5522 rb_mutex_t *mutex = mutex_ptr(th->locking_mutex);
5523 if (mutex->fiber == th->ec->fiber_ptr || (!mutex->fiber && !ccan_list_empty(&mutex->waitq))) {
5534 argv[1] =
rb_str_new2(
"No live threads left. Deadlock?");
5535 debug_deadlock_check(r, argv[1]);
5536 rb_ractor_sleeper_threads_dec(GET_RACTOR());
5537 rb_threadptr_raise(r->threads.main, 2, argv);
5544rb_vm_memsize_waiting_fds(
struct ccan_list_head *waiting_fds)
5549 ccan_list_for_each(waiting_fds, waitfd, wfd_node) {
5560 VALUE coverage = rb_iseq_coverage(cfp->iseq);
5568 if (GET_VM()->coverage_mode & COVERAGE_TARGET_ONESHOT_LINES) {
5569 rb_iseq_clear_event_flags(cfp->iseq, cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded - 1, RUBY_EVENT_COVERAGE_LINE);
5570 rb_ary_push(lines,
LONG2FIX(line + 1));
5590 VALUE coverage = rb_iseq_coverage(cfp->iseq);
5594 long pc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded - 1;
5609 VALUE path, beg_pos_lineno, beg_pos_column, end_pos_lineno, end_pos_column;
5611 if (!me->def)
return NULL;
5614 switch (me->def->type) {
5615 case VM_METHOD_TYPE_ISEQ: {
5618 path = rb_iseq_path(iseq);
5619 beg_pos_lineno =
INT2FIX(loc->code_location.beg_pos.lineno);
5620 beg_pos_column =
INT2FIX(loc->code_location.beg_pos.column);
5621 end_pos_lineno =
INT2FIX(loc->code_location.end_pos.lineno);
5622 end_pos_column =
INT2FIX(loc->code_location.end_pos.column);
5625 case VM_METHOD_TYPE_BMETHOD: {
5626 const rb_iseq_t *iseq = rb_proc_get_iseq(me->def->body.bmethod.proc, 0);
5629 rb_iseq_check(iseq);
5630 path = rb_iseq_path(iseq);
5631 loc = &ISEQ_BODY(iseq)->location;
5632 beg_pos_lineno =
INT2FIX(loc->code_location.beg_pos.lineno);
5633 beg_pos_column =
INT2FIX(loc->code_location.beg_pos.column);
5634 end_pos_lineno =
INT2FIX(loc->code_location.end_pos.lineno);
5635 end_pos_column =
INT2FIX(loc->code_location.end_pos.column);
5640 case VM_METHOD_TYPE_ALIAS:
5641 me = me->def->body.alias.original_me;
5643 case VM_METHOD_TYPE_REFINED:
5644 me = me->def->body.refined.orig_me;
5645 if (!me)
return NULL;
5652 if (RB_TYPE_P(path,
T_ARRAY)) {
5653 path = rb_ary_entry(path, 1);
5654 if (!RB_TYPE_P(path,
T_STRING))
return NULL;
5656 if (resolved_location) {
5657 resolved_location[0] = path;
5658 resolved_location[1] = beg_pos_lineno;
5659 resolved_location[2] = beg_pos_column;
5660 resolved_location[3] = end_pos_lineno;
5661 resolved_location[4] = end_pos_column;
5675 me = rb_resolve_me_location(me, 0);
5678 rcount = rb_hash_aref(me2counter, (
VALUE) me);
5686rb_get_coverages(
void)
5688 return GET_VM()->coverages;
5692rb_get_coverage_mode(
void)
5694 return GET_VM()->coverage_mode;
5698rb_set_coverages(
VALUE coverages,
int mode,
VALUE me2counter)
5700 GET_VM()->coverages = coverages;
5701 GET_VM()->me2counter = me2counter;
5702 GET_VM()->coverage_mode = mode;
5706rb_resume_coverages(
void)
5708 int mode = GET_VM()->coverage_mode;
5709 VALUE me2counter = GET_VM()->me2counter;
5710 rb_add_event_hook2((
rb_event_hook_func_t) update_line_coverage, RUBY_EVENT_COVERAGE_LINE,
Qnil, RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
5711 if (mode & COVERAGE_TARGET_BRANCHES) {
5712 rb_add_event_hook2((
rb_event_hook_func_t) update_branch_coverage, RUBY_EVENT_COVERAGE_BRANCH,
Qnil, RUBY_EVENT_HOOK_FLAG_SAFE | RUBY_EVENT_HOOK_FLAG_RAW_ARG);
5714 if (mode & COVERAGE_TARGET_METHODS) {
5720rb_suspend_coverages(
void)
5723 if (GET_VM()->coverage_mode & COVERAGE_TARGET_BRANCHES) {
5726 if (GET_VM()->coverage_mode & COVERAGE_TARGET_METHODS) {
5733rb_reset_coverages(
void)
5735 rb_clear_coverages();
5736 rb_iseq_remove_coverage_all();
5737 GET_VM()->coverages =
Qfalse;
5741rb_default_coverage(
int n)
5743 VALUE coverage = rb_ary_hidden_new_fill(3);
5745 int mode = GET_VM()->coverage_mode;
5747 if (mode & COVERAGE_TARGET_LINES) {
5748 lines = n > 0 ? rb_ary_hidden_new_fill(n) : rb_ary_hidden_new(0);
5750 RARRAY_ASET(coverage, COVERAGE_INDEX_LINES, lines);
5752 if (mode & COVERAGE_TARGET_BRANCHES) {
5753 branches = rb_ary_hidden_new_fill(2);
5775 VALUE structure = rb_hash_new();
5776 rb_obj_hide(structure);
5781 RARRAY_ASET(coverage, COVERAGE_INDEX_BRANCHES, branches);
5787uninterruptible_exit(
VALUE v)
5790 rb_ary_pop(cur_th->pending_interrupt_mask_stack);
5792 cur_th->pending_interrupt_queue_checked = 0;
5793 if (!rb_threadptr_pending_interrupt_empty_p(cur_th)) {
5794 RUBY_VM_SET_INTERRUPT(cur_th->ec);
5802 VALUE interrupt_mask = rb_ident_hash_new();
5805 rb_hash_aset(interrupt_mask, rb_cObject, sym_never);
5807 rb_ary_push(cur_th->pending_interrupt_mask_stack, interrupt_mask);
5811 RUBY_VM_CHECK_INTS(cur_th->ec);
5818 VM_ASSERT(th->specific_storage == NULL);
5820 if (UNLIKELY(specific_key_count > 0)) {
5821 th->specific_storage =
ZALLOC_N(
void *, RB_INTERNAL_THREAD_SPECIFIC_KEY_MAX);
5825rb_internal_thread_specific_key_t
5830 if (specific_key_count == 0 && vm->ractor.cnt > 1) {
5831 rb_raise(
rb_eThreadError,
"The first rb_internal_thread_specific_key_create() is called with multiple ractors");
5833 else if (specific_key_count > RB_INTERNAL_THREAD_SPECIFIC_KEY_MAX) {
5834 rb_raise(
rb_eThreadError,
"rb_internal_thread_specific_key_create() is called more than %d times", RB_INTERNAL_THREAD_SPECIFIC_KEY_MAX);
5837 rb_internal_thread_specific_key_t key = specific_key_count++;
5844 ccan_list_for_each(&cr->threads.set, th, lt_node) {
5845 thread_specific_storage_alloc(th);
5858 VM_ASSERT(rb_thread_ptr(thread_val) == th);
5859 VM_ASSERT(key < RB_INTERNAL_THREAD_SPECIFIC_KEY_MAX);
5860 VM_ASSERT(th->specific_storage);
5862 return th->specific_storage[key];
5871 VM_ASSERT(rb_thread_ptr(thread_val) == th);
5872 VM_ASSERT(key < RB_INTERNAL_THREAD_SPECIFIC_KEY_MAX);
5873 VM_ASSERT(th->specific_storage);
5875 th->specific_storage[key] = data;
#define RUBY_ASSERT_ALWAYS(expr)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define RUBY_INTERNAL_EVENT_SWITCH
Thread switched.
int rb_remove_event_hook(rb_event_hook_func_t func)
Removes the passed function from the list of event hooks.
#define RUBY_EVENT_THREAD_BEGIN
Encountered a new thread.
void(* rb_event_hook_func_t)(rb_event_flag_t evflag, VALUE data, VALUE self, ID mid, VALUE klass)
Type of event hooks.
uint32_t rb_event_flag_t
Represents event(s).
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
#define RUBY_EVENT_THREAD_END
Encountered an end of a thread.
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
ID rb_frame_last_func(void)
Returns the ID of the last method in the call stack.
int rb_keyword_given_p(void)
Determines if the current method is given a keyword argument.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define ALLOC
Old name of RB_ALLOC.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#define xrealloc
Old name of ruby_xrealloc.
#define ID2SYM
Old name of RB_ID2SYM.
#define OBJ_FREEZE_RAW
Old name of RB_OBJ_FREEZE_RAW.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define CLASS_OF
Old name of rb_class_of.
#define xmalloc
Old name of ruby_xmalloc.
#define LONG2FIX
Old name of RB_INT2FIX.
#define FIX2INT
Old name of RB_FIX2INT.
#define ZALLOC_N
Old name of RB_ZALLOC_N.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define T_HASH
Old name of RUBY_T_HASH.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
#define NIL_P
Old name of RB_NIL_P.
#define POSFIXABLE
Old name of RB_POSFIXABLE.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void ruby_stop(int ex)
Calls ruby_cleanup() and exits the process.
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
VALUE rb_eSystemExit
SystemExit exception.
VALUE rb_eIOError
IOError exception.
VALUE rb_eStandardError
StandardError exception.
VALUE rb_eTypeError
TypeError exception.
void rb_frozen_error_raise(VALUE frozen_obj, const char *fmt,...)
Raises an instance of rb_eFrozenError.
VALUE rb_eFatal
fatal exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Creates an instance of the passed exception class.
VALUE rb_eException
Mother of all exceptions.
VALUE rb_eThreadError
ThreadError exception.
void rb_exit(int status)
Terminates the current execution context.
VALUE rb_eSignal
SignalException exception.
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
VALUE rb_cInteger
Module class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_cThread
Thread class.
VALUE rb_cModule
Module class.
double rb_num2dbl(VALUE num)
Converts an instance of rb_cNumeric into C's double.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
void rb_reset_random_seed(void)
Resets the RNG behind rb_genrand_int32()/rb_genrand_real().
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
int rb_thread_interrupted(VALUE thval)
Checks if the thread's execution was recently interrupted.
VALUE rb_thread_local_aref(VALUE thread, ID key)
This badly named function reads from a Fiber local storage.
VALUE rb_mutex_new(void)
Creates a mutex.
VALUE rb_thread_kill(VALUE thread)
Terminates the given thread.
#define RUBY_UBF_IO
A special UBF for blocking IO operations.
VALUE rb_thread_main(void)
Obtains the "main" thread.
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
void rb_thread_sleep_forever(void)
Blocks indefinitely.
void rb_thread_fd_close(int fd)
Notifies a closing of a file descriptor to other threads.
void rb_thread_wait_for(struct timeval time)
Identical to rb_thread_sleep(), except it takes struct timeval instead.
VALUE rb_thread_stop(void)
Stops the current thread.
VALUE rb_mutex_sleep(VALUE self, VALUE timeout)
Releases the lock held in the mutex and waits for the period of time; reacquires the lock on wakeup.
VALUE rb_exec_recursive_paired(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h)
Identical to rb_exec_recursive(), except it checks for the recursion on the ordered pair of { g,...
void rb_unblock_function_t(void *)
This is the type of UBFs.
void rb_thread_atfork_before_exec(void)
:FIXME: situation of this function is unclear.
void rb_thread_check_ints(void)
Checks for interrupts.
VALUE rb_thread_run(VALUE thread)
This is a rb_thread_wakeup() + rb_thread_schedule() combo.
VALUE rb_thread_wakeup(VALUE thread)
Marks a given thread as eligible for scheduling.
VALUE rb_mutex_unlock(VALUE mutex)
Releases the mutex.
VALUE rb_exec_recursive_paired_outer(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h)
Identical to rb_exec_recursive_outer(), except it checks for the recursion on the ordered pair of { g...
void rb_thread_sleep_deadly(void)
Identical to rb_thread_sleep_forever(), except the thread calling this function is considered "dead" ...
void rb_thread_atfork(void)
A pthread_atfork(3posix)-like API.
VALUE rb_thread_current(void)
Obtains the "current" thread.
int rb_thread_alone(void)
Checks if the thread this function is running is the only thread that is currently alive.
VALUE rb_thread_local_aset(VALUE thread, ID key, VALUE val)
This badly named function writes to a Fiber local storage.
void rb_thread_schedule(void)
Tries to switch to another thread.
#define RUBY_UBF_PROCESS
A special UBF for blocking process operations.
VALUE rb_exec_recursive_outer(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
Identical to rb_exec_recursive(), except it calls f for outermost recursion only.
VALUE rb_thread_wakeup_alive(VALUE thread)
Identical to rb_thread_wakeup(), except it doesn't raise on an already killed thread.
VALUE rb_mutex_lock(VALUE mutex)
Attempts to lock the mutex.
void rb_thread_sleep(int sec)
Blocks for the given period of time.
void rb_timespec_now(struct timespec *ts)
Fills the current time into the given struct.
struct timeval rb_time_timeval(VALUE time)
Converts an instance of rb_cTime to a struct timeval that represents the identical point of time.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
int rb_sourceline(void)
Resembles __LINE__.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
void rb_define_const(VALUE klass, const char *name, VALUE val)
Defines a Ruby level constant under a namespace.
VALUE rb_eIOTimeoutError
Indicates that a timeout has occurred while performing an IO operation.
#define RB_NOGVL_UBF_ASYNC_SAFE
Passing this flag to rb_nogvl() indicates that the passed UBF is async-signal-safe.
void * rb_internal_thread_specific_get(VALUE thread_val, rb_internal_thread_specific_key_t key)
Get thread and tool specific data.
#define RB_NOGVL_INTR_FAIL
Passing this flag to rb_nogvl() prevents it from checking interrupts.
void rb_internal_thread_specific_set(VALUE thread_val, rb_internal_thread_specific_key_t key, void *data)
Set thread and tool specific data.
rb_internal_thread_specific_key_t rb_internal_thread_specific_key_create(void)
Create a key to store thread specific data.
void * rb_nogvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2, int flags)
Identical to rb_thread_call_without_gvl(), except it additionally takes "flags" that change the behav...
void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
(Re-)acquires the GVL.
void * rb_thread_call_without_gvl2(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
Identical to rb_thread_call_without_gvl(), except it does not interface with signals etc.
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
Allows the passed function to run in parallel with other Ruby threads.
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
VALUE rb_yield(VALUE val)
Yields the block.
void rb_throw_obj(VALUE tag, VALUE val)
Identical to rb_throw(), except it allows arbitrary Ruby object to become a tag.
static int rb_fd_max(const rb_fdset_t *f)
It seems this function has no use.
void rb_fd_copy(rb_fdset_t *dst, const fd_set *src, int max)
Destructively overwrites an fdset with another.
void rb_fd_dup(rb_fdset_t *dst, const rb_fdset_t *src)
Identical to rb_fd_copy(), except it copies unlimited number of file descriptors.
void rb_fd_term(rb_fdset_t *f)
Destroys the rb_fdset_t, releasing any memory and resources it used.
static fd_set * rb_fd_ptr(const rb_fdset_t *f)
Raw pointer to fd_set.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define ALLOCA_N(type, n)
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE rb_thread_create(type *q, void *w)
Creates a rb_cThread instance.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
#define rb_fd_isset
Queries if the given fd is in the rb_fdset_t.
#define rb_fd_select
Waits for multiple file descriptors at once.
#define rb_fd_init
Initialises the :given :rb_fdset_t.
#define rb_fd_set
Sets the given fd to the rb_fdset_t.
#define rb_fd_zero
Clears the given rb_fdset_t.
#define rb_fd_clr
Unsets the given fd from the rb_fdset_t.
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RBASIC(obj)
Convenient casting macro.
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
#define DATA_PTR(obj)
Convenient getter macro.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
#define errno
Ractor-aware version of errno.
int ruby_native_thread_p(void)
Queries if the thread which calls this function is a ruby's thread.
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
VALUE rb_fiber_scheduler_current(void)
Identical to rb_fiber_scheduler_get(), except it also returns RUBY_Qnil in case of a blocking fiber.
VALUE rb_fiber_scheduler_block(VALUE scheduler, VALUE blocker, VALUE timeout)
Non-blocking wait for the passed "blocker", which is for instance Thread.join or Mutex....
VALUE rb_fiber_scheduler_set(VALUE scheduler)
Destructively assigns the passed scheduler to that of the current thread that is calling this functio...
VALUE rb_fiber_scheduler_unblock(VALUE scheduler, VALUE blocker, VALUE fiber)
Wakes up a fiber previously blocked using rb_fiber_scheduler_block().
int rb_thread_fd_select(int nfds, rb_fdset_t *rfds, rb_fdset_t *wfds, rb_fdset_t *efds, struct timeval *timeout)
Waits for multiple file descriptors at once.
#define rb_fd_resize(n, f)
Does nothing (defined for compatibility).
static bool RB_TEST(VALUE obj)
Emulates Ruby's "if" statement.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
This is the struct that holds necessary info for a struct.
The data structure which wraps the fd_set bitmap used by select(2).
int maxfd
Maximum allowed number of FDs.
fd_set * fdset
File descriptors buffer.
int capa
Maximum allowed number of FDs.
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
void rb_nativethread_lock_lock(rb_nativethread_lock_t *lock)
Blocks until the current thread obtains a lock.
void rb_native_mutex_lock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_lock.
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_unlock.
void rb_nativethread_lock_unlock(rb_nativethread_lock_t *lock)
Releases a lock.
void rb_native_mutex_destroy(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_destroy.
void rb_nativethread_lock_initialize(rb_nativethread_lock_t *lock)
Fills the passed lock with an initial value.
void rb_nativethread_lock_destroy(rb_nativethread_lock_t *lock)
Destroys the passed mutex.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.