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)) { \
202 RB_VM_SAVE_MACHINE_CONTEXT(th); \
203 thread_sched_to_waiting(TH_SCHED(th), th); \
205 blocking_region_end(th, &__region); \
213#define RUBY_VM_CHECK_INTS_BLOCKING(ec) vm_check_ints_blocking(ec)
219 if (LIKELY(rb_threadptr_pending_interrupt_empty_p(th))) {
220 if (LIKELY(!RUBY_VM_INTERRUPTED_ANY(ec)))
return FALSE;
223 th->pending_interrupt_queue_checked = 0;
224 RUBY_VM_SET_INTERRUPT(ec);
226 return rb_threadptr_execute_interrupts(th, 1);
232 return vm_check_ints_blocking(ec);
240#if defined(HAVE_POLL)
241# if defined(__linux__)
244# if defined(__FreeBSD_version) && __FreeBSD_version >= 1100000
247# define POLLERR_SET (POLLHUP | POLLERR)
252timeout_prepare(rb_hrtime_t **to, rb_hrtime_t *rel, rb_hrtime_t *end,
256 *rel = rb_timeval2hrtime(timeout);
257 *end = rb_hrtime_add(rb_hrtime_now(), *rel);
265MAYBE_UNUSED(NOINLINE(
static int thread_start_func_2(
rb_thread_t *th,
VALUE *stack_start)));
267#include THREAD_IMPL_SRC
274#ifndef BUSY_WAIT_SIGNALS
275# define BUSY_WAIT_SIGNALS (0)
279# define USE_EVENTFD (0)
282#include "thread_sync.c"
312 if (fail_if_interrupted) {
313 if (RUBY_VM_INTERRUPTED_ANY(th->ec)) {
318 RUBY_VM_CHECK_INTS(th->ec);
322 }
while (!th->ec->raised_flag && RUBY_VM_INTERRUPTED_ANY(th->ec) &&
325 VM_ASSERT(th->unblock.func == NULL);
327 th->unblock.func = func;
328 th->unblock.arg = arg;
338 th->unblock.func = 0;
343rb_threadptr_interrupt_common(
rb_thread_t *th,
int trap)
345 RUBY_DEBUG_LOG(
"th:%u trap:%d", rb_th_serial(th), trap);
350 RUBY_VM_SET_TRAP_INTERRUPT(th->ec);
353 RUBY_VM_SET_INTERRUPT(th->ec);
356 if (th->unblock.func != NULL) {
357 (th->unblock.func)(th->unblock.arg);
369 RUBY_DEBUG_LOG(
"th:%u", rb_th_serial(th));
370 rb_threadptr_interrupt_common(th, 0);
376 rb_threadptr_interrupt_common(th, 1);
384 ccan_list_for_each(&r->threads.set, th, lt_node) {
385 if (th != main_thread) {
386 RUBY_DEBUG_LOG(
"terminate start th:%u status:%s", rb_th_serial(th), thread_status_name(th, TRUE));
388 rb_threadptr_pending_interrupt_enque(th, RUBY_FATAL_THREAD_TERMINATED);
389 rb_threadptr_interrupt(th);
391 RUBY_DEBUG_LOG(
"terminate done th:%u status:%s", rb_th_serial(th), thread_status_name(th, TRUE));
394 RUBY_DEBUG_LOG(
"main thread th:%u", rb_th_serial(th));
402 while (thread->join_list) {
406 thread->join_list = join_list->next;
410 if (target_thread->scheduler !=
Qnil && join_list->fiber) {
414 rb_threadptr_interrupt(target_thread);
416 switch (target_thread->status) {
418 case THREAD_STOPPED_FOREVER:
419 target_thread->status = THREAD_RUNNABLE;
429rb_threadptr_unlock_all_locking_mutexes(
rb_thread_t *th)
431 while (th->keeping_mutexes) {
433 th->keeping_mutexes = mutex->next_mutex;
437 const char *error_message = rb_mutex_unlock_th(mutex, th, mutex->fiber);
438 if (error_message) rb_bug(
"invalid keeping_mutexes: %s", error_message);
447 volatile int sleeping = 0;
449 if (cr->threads.main != th) {
450 rb_bug(
"rb_thread_terminate_all: called by child thread (%p, %p)",
451 (
void *)cr->threads.main, (
void *)th);
455 rb_threadptr_unlock_all_locking_mutexes(th);
458 if (EC_EXEC_TAG() == TAG_NONE) {
460 RUBY_DEBUG_LOG(
"th:%u", rb_th_serial(th));
462 terminate_all(cr, th);
464 while (rb_ractor_living_thread_num(cr) > 1) {
465 rb_hrtime_t rel = RB_HRTIME_PER_SEC;
471 native_sleep(th, &rel);
472 RUBY_VM_CHECK_INTS_BLOCKING(ec);
490void rb_threadptr_root_fiber_terminate(
rb_thread_t *th);
493thread_cleanup_func_before_exec(
void *th_ptr)
496 th->status = THREAD_KILLED;
499 th->ec->machine.stack_start = th->ec->machine.stack_end = NULL;
501 rb_threadptr_root_fiber_terminate(th);
505thread_cleanup_func(
void *th_ptr,
int atfork)
509 th->locking_mutex =
Qfalse;
510 thread_cleanup_func_before_exec(th_ptr);
531 native_thread_init_stack(th);
535rb_vm_proc_local_ep(
VALUE proc)
537 const VALUE *ep = vm_proc_ep(proc);
540 return rb_vm_ep_local_ep(ep);
549 int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler);
554 VALUE args = th->invoke_arg.proc.args;
555 const VALUE *args_ptr;
557 VALUE procval = th->invoke_arg.proc.proc;
559 GetProcPtr(procval, proc);
561 th->ec->errinfo =
Qnil;
562 th->ec->root_lep = rb_vm_proc_local_ep(procval);
563 th->ec->root_svar =
Qfalse;
565 vm_check_ints_blocking(th->ec);
567 if (th->invoke_type == thread_invoke_type_ractor_proc) {
568 VALUE self = rb_ractor_self(th->ractor);
572 rb_ractor_receive_parameters(th->ec, th->ractor, args_len, (
VALUE *)args_ptr);
573 vm_check_ints_blocking(th->ec);
575 return rb_vm_invoke_proc_with_self(
578 th->invoke_arg.proc.kw_splat,
579 VM_BLOCK_HANDLER_NONE
588 th->invoke_arg.proc.args =
Qnil;
594 vm_check_ints_blocking(th->ec);
596 return rb_vm_invoke_proc(
599 th->invoke_arg.proc.kw_splat,
600 VM_BLOCK_HANDLER_NONE
608 native_set_thread_name(th);
611 switch (th->invoke_type) {
612 case thread_invoke_type_proc:
613 result = thread_do_start_proc(th);
616 case thread_invoke_type_ractor_proc:
617 result = thread_do_start_proc(th);
618 rb_ractor_atexit(th->ec, result);
621 case thread_invoke_type_func:
622 result = (*th->invoke_arg.func.func)(th->invoke_arg.func.arg);
625 case thread_invoke_type_none:
626 rb_bug(
"unreachable");
637 STACK_GROW_DIR_DETECTION;
639 RUBY_DEBUG_LOG(
"th:%u", rb_th_serial(th));
640 VM_ASSERT(th != th->vm->ractor.main_thread);
642 enum ruby_tag_type state;
644 rb_thread_t *ractor_main_th = th->ractor->threads.main;
647 if (rb_ractor_status_p(th->ractor, ractor_blocking)) {
650 rb_vm_ractor_blocking_cnt_dec(th->vm, th->ractor, __FILE__, __LINE__);
652 r->r_stdin = rb_io_prep_stdin();
653 r->r_stdout = rb_io_prep_stdout();
654 r->r_stderr = rb_io_prep_stderr();
660 VM_ASSERT(UNDEF_P(th->value));
662 int fiber_scheduler_closed = 0, event_thread_end_hooked = 0;
667 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
670 SAVE_ROOT_JMPBUF(th, result = thread_do_start(th));
673 if (!fiber_scheduler_closed) {
674 fiber_scheduler_closed = 1;
678 if (!event_thread_end_hooked) {
679 event_thread_end_hooked = 1;
683 if (state == TAG_NONE) {
687 errinfo = th->ec->errinfo;
689 VALUE exc = rb_vm_make_jump_tag_but_local_jump(state,
Qundef);
690 if (!
NIL_P(exc)) errinfo = exc;
692 if (state == TAG_FATAL) {
693 if (th->invoke_type == thread_invoke_type_ractor_proc) {
694 rb_ractor_atexit(th->ec,
Qnil);
702 if (th->report_on_exception) {
703 VALUE mesg = rb_thread_to_s(th->self);
704 rb_str_cat_cstr(mesg,
" terminated with exception (report_on_exception is true):\n");
705 rb_write_error_str(mesg);
706 rb_ec_error_print(th->ec, errinfo);
709 if (th->invoke_type == thread_invoke_type_ractor_proc) {
710 rb_ractor_atexit_exception(th->ec);
713 if (th->vm->thread_abort_on_exception ||
725 VM_ASSERT(!UNDEF_P(th->value));
727 rb_threadptr_join_list_wakeup(th);
728 rb_threadptr_unlock_all_locking_mutexes(th);
730 if (th->invoke_type == thread_invoke_type_ractor_proc) {
731 rb_thread_terminate_all(th);
732 rb_ractor_teardown(th->ec);
735 th->status = THREAD_KILLED;
736 RUBY_DEBUG_LOG(
"killed th:%u", rb_th_serial(th));
738 if (th->vm->ractor.main_thread == th) {
744 rb_threadptr_raise(ractor_main_th, 1, &errinfo);
749 rb_ec_clear_current_thread_trace_func(th->ec);
752 if (th->locking_mutex !=
Qfalse) {
753 rb_bug(
"thread_start_func_2: locking_mutex must not be set (%p:%"PRIxVALUE
")",
754 (
void *)th, th->locking_mutex);
757 if (ractor_main_th->status == THREAD_KILLED &&
758 th->ractor->threads.cnt <= 2 ) {
760 rb_threadptr_interrupt(ractor_main_th);
763 rb_check_deadlock(th->ractor);
765 rb_fiber_close(th->ec->fiber_ptr);
767 thread_cleanup_func(th, FALSE);
768 VM_ASSERT(th->ec->vm_stack == NULL);
770 if (th->invoke_type == thread_invoke_type_ractor_proc) {
774 thread_sched_to_dead(TH_SCHED(th), th);
775 rb_ractor_living_threads_remove(th->ractor, th);
778 rb_ractor_living_threads_remove(th->ractor, th);
779 thread_sched_to_dead(TH_SCHED(th), th);
786 enum thread_invoke_type type;
799static void thread_specific_storage_alloc(
rb_thread_t *th);
805 rb_thread_t *th = rb_thread_ptr(thval), *current_th = rb_ec_thread_ptr(ec);
808 thread_specific_storage_alloc(th);
812 "can't start a new thread (frozen ThreadGroup)");
815 rb_fiber_inherit_storage(ec, th->ec->fiber_ptr);
817 switch (params->type) {
818 case thread_invoke_type_proc:
819 th->invoke_type = thread_invoke_type_proc;
820 th->invoke_arg.proc.args = params->args;
821 th->invoke_arg.proc.proc = params->proc;
825 case thread_invoke_type_ractor_proc:
826#if RACTOR_CHECK_MODE > 0
827 rb_ractor_setup_belonging_to(thval, rb_ractor_id(params->g));
829 th->invoke_type = thread_invoke_type_ractor_proc;
830 th->ractor = params->g;
831 th->ractor->threads.main = th;
832 th->invoke_arg.proc.proc = rb_proc_isolate_bang(params->proc);
835 rb_ractor_send_parameters(ec, params->g, params->args);
838 case thread_invoke_type_func:
839 th->invoke_type = thread_invoke_type_func;
840 th->invoke_arg.func.func = params->fn;
841 th->invoke_arg.func.arg = (
void *)params->args;
845 rb_bug(
"unreachable");
848 th->priority = current_th->priority;
849 th->thgroup = current_th->thgroup;
851 th->pending_interrupt_queue = rb_ary_hidden_new(0);
852 th->pending_interrupt_queue_checked = 0;
853 th->pending_interrupt_mask_stack = rb_ary_dup(current_th->pending_interrupt_mask_stack);
854 RBASIC_CLEAR_CLASS(th->pending_interrupt_mask_stack);
858 RUBY_DEBUG_LOG(
"r:%u th:%u", rb_ractor_id(th->ractor), rb_th_serial(th));
860 rb_ractor_living_threads_insert(th->ractor, th);
863 err = native_thread_create(th);
865 th->status = THREAD_KILLED;
866 rb_ractor_living_threads_remove(th->ractor, th);
872#define threadptr_initialized(th) ((th)->invoke_type != thread_invoke_type_none)
895thread_s_new(
int argc,
VALUE *argv,
VALUE klass)
898 VALUE thread = rb_thread_alloc(klass);
900 if (GET_RACTOR()->threads.main->status == THREAD_KILLED) {
905 th = rb_thread_ptr(thread);
906 if (!threadptr_initialized(th)) {
907 rb_raise(
rb_eThreadError,
"uninitialized thread - check `%"PRIsVALUE
"#initialize'",
927 .type = thread_invoke_type_proc,
931 return thread_create_core(rb_thread_alloc(klass), ¶ms);
937 if (th->invoke_type == thread_invoke_type_proc) {
938 return rb_proc_location(th->invoke_arg.proc.proc);
954 else if (th->invoke_type != thread_invoke_type_none) {
955 VALUE loc = threadptr_invoke_proc_location(th);
958 "already initialized thread - %"PRIsVALUE
":%"PRIsVALUE,
967 .type = thread_invoke_type_proc,
971 return thread_create_core(thread, ¶ms);
979 .type = thread_invoke_type_func,
983 return thread_create_core(rb_thread_alloc(
rb_cThread), ¶ms);
990 .type = thread_invoke_type_ractor_proc,
995 return thread_create_core(rb_thread_alloc(
rb_cThread), ¶ms);
1007remove_from_join_list(
VALUE arg)
1012 if (target_thread->status != THREAD_KILLED) {
1015 while (*join_list) {
1016 if (*join_list == p->waiter) {
1017 *join_list = (*join_list)->next;
1021 join_list = &(*join_list)->next;
1031 return th->status == THREAD_KILLED || !UNDEF_P(th->value);
1035thread_join_sleep(
VALUE arg)
1038 rb_thread_t *target_th = p->target, *th = p->waiter->thread;
1039 rb_hrtime_t end = 0, *limit = p->limit;
1042 end = rb_hrtime_add(*limit, rb_hrtime_now());
1045 while (!thread_finished(target_th)) {
1048 if (scheduler !=
Qnil) {
1051 if (thread_finished(target_th))
break;
1056 sleep_forever(th, SLEEP_DEADLOCKABLE | SLEEP_ALLOW_SPURIOUS | SLEEP_NO_CHECKINTS);
1059 if (hrtime_update_expire(limit, end)) {
1060 RUBY_DEBUG_LOG(
"timeout target_th:%u", rb_th_serial(target_th));
1063 th->status = THREAD_STOPPED;
1064 native_sleep(th, limit);
1066 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1067 th->status = THREAD_RUNNABLE;
1069 RUBY_DEBUG_LOG(
"interrupted target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1082 if (th == target_th) {
1083 rb_raise(
rb_eThreadError,
"Target thread must not be current thread");
1086 if (th->ractor->threads.main == target_th) {
1090 RUBY_DEBUG_LOG(
"target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1092 if (target_th->status != THREAD_KILLED) {
1094 waiter.next = target_th->join_list;
1096 waiter.fiber = rb_fiberptr_blocking(fiber) ? NULL : fiber;
1097 target_th->join_list = &waiter;
1100 arg.waiter = &waiter;
1101 arg.target = target_th;
1102 arg.timeout = timeout;
1110 RUBY_DEBUG_LOG(
"success target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1112 if (target_th->ec->errinfo !=
Qnil) {
1113 VALUE err = target_th->ec->errinfo;
1118 RUBY_DEBUG_LOG(
"terminated target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1123 rb_bug(
"thread_join: Fixnum (%d) should not reach here.",
FIX2INT(err));
1126 else if (THROW_DATA_P(target_th->ec->errinfo)) {
1127 rb_bug(
"thread_join: THROW_DATA should not reach here.");
1134 return target_th->self;
1177thread_join_m(
int argc,
VALUE *argv,
VALUE self)
1180 rb_hrtime_t rel = 0, *limit = 0;
1191 if (
NIL_P(timeout)) {
1195 rel = rb_sec2hrtime(NUM2TIMET(timeout));
1199 limit = double2hrtime(&rel,
rb_num2dbl(timeout));
1202 return thread_join(rb_thread_ptr(self), timeout, limit);
1220thread_value(
VALUE self)
1223 thread_join(th,
Qnil, 0);
1224 if (UNDEF_P(th->value)) {
1238#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
1239 if (clock_gettime(CLOCK_MONOTONIC, ts) == 0)
1249NOINLINE(rb_hrtime_t rb_hrtime_now(
void));
1256 return rb_timespec2hrtime(&ts);
1263COMPILER_WARNING_PUSH
1264#if defined(__GNUC__) && __GNUC__ == 7 && __GNUC_MINOR__ <= 3
1265COMPILER_WARNING_IGNORED(-Wmaybe-uninitialized)
1268#define PRIu64 PRI_64_PREFIX "u"
1276hrtime_update_expire(rb_hrtime_t *timeout,
const rb_hrtime_t end)
1278 rb_hrtime_t now = rb_hrtime_now();
1280 if (now > end)
return 1;
1282 RUBY_DEBUG_LOG(
"%"PRIu64
" > %"PRIu64
"", (uint64_t)end, (uint64_t)now);
1284 *timeout = end - now;
1290sleep_hrtime(
rb_thread_t *th, rb_hrtime_t rel,
unsigned int fl)
1292 enum rb_thread_status prev_status = th->status;
1294 rb_hrtime_t end = rb_hrtime_add(rb_hrtime_now(), rel);
1296 th->status = THREAD_STOPPED;
1297 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1298 while (th->status == THREAD_STOPPED) {
1299 native_sleep(th, &rel);
1300 woke = vm_check_ints_blocking(th->ec);
1301 if (woke && !(fl & SLEEP_SPURIOUS_CHECK))
1303 if (hrtime_update_expire(&rel, end))
1307 th->status = prev_status;
1312sleep_hrtime_until(
rb_thread_t *th, rb_hrtime_t end,
unsigned int fl)
1314 enum rb_thread_status prev_status = th->status;
1316 rb_hrtime_t rel = rb_hrtime_sub(end, rb_hrtime_now());
1318 th->status = THREAD_STOPPED;
1319 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1320 while (th->status == THREAD_STOPPED) {
1321 native_sleep(th, &rel);
1322 woke = vm_check_ints_blocking(th->ec);
1323 if (woke && !(fl & SLEEP_SPURIOUS_CHECK))
1325 if (hrtime_update_expire(&rel, end))
1329 th->status = prev_status;
1336 enum rb_thread_status prev_status = th->status;
1337 enum rb_thread_status status;
1340 status = fl & SLEEP_DEADLOCKABLE ? THREAD_STOPPED_FOREVER : THREAD_STOPPED;
1341 th->status = status;
1343 if (!(fl & SLEEP_NO_CHECKINTS)) RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1345 while (th->status == status) {
1346 if (fl & SLEEP_DEADLOCKABLE) {
1347 rb_ractor_sleeper_threads_inc(th->ractor);
1348 rb_check_deadlock(th->ractor);
1351 native_sleep(th, 0);
1353 if (fl & SLEEP_DEADLOCKABLE) {
1354 rb_ractor_sleeper_threads_dec(th->ractor);
1356 if (fl & SLEEP_ALLOW_SPURIOUS) {
1360 woke = vm_check_ints_blocking(th->ec);
1362 if (woke && !(fl & SLEEP_SPURIOUS_CHECK)) {
1366 th->status = prev_status;
1372 RUBY_DEBUG_LOG(
"forever");
1373 sleep_forever(GET_THREAD(), SLEEP_SPURIOUS_CHECK);
1379 RUBY_DEBUG_LOG(
"deadly");
1380 sleep_forever(GET_THREAD(), SLEEP_DEADLOCKABLE|SLEEP_SPURIOUS_CHECK);
1384rb_thread_sleep_deadly_allow_spurious_wakeup(
VALUE blocker,
VALUE timeout, rb_hrtime_t end)
1387 if (scheduler !=
Qnil) {
1391 RUBY_DEBUG_LOG(
"...");
1393 sleep_hrtime_until(GET_THREAD(), end, SLEEP_SPURIOUS_CHECK);
1396 sleep_forever(GET_THREAD(), SLEEP_DEADLOCKABLE);
1406 sleep_hrtime(th, rb_timeval2hrtime(&time), SLEEP_SPURIOUS_CHECK);
1419 RUBY_VM_CHECK_INTS_BLOCKING(GET_EC());
1427rb_thread_check_trap_pending(
void)
1429 return rb_signal_buff_size() != 0;
1436 return (
int)RUBY_VM_INTERRUPTED(rb_thread_ptr(thval)->ec);
1446rb_thread_schedule_limits(uint32_t limits_us)
1450 RUBY_DEBUG_LOG(
"us:%u", (
unsigned int)limits_us);
1452 if (th->running_time_us >= limits_us) {
1453 RUBY_DEBUG_LOG(
"switch %s",
"start");
1455 RB_VM_SAVE_MACHINE_CONTEXT(th);
1456 thread_sched_yield(TH_SCHED(th), th);
1457 rb_ractor_thread_switch(th->ractor, th);
1459 RUBY_DEBUG_LOG(
"switch %s",
"done");
1467 rb_thread_schedule_limits(0);
1468 RUBY_VM_CHECK_INTS(GET_EC());
1477#ifdef RUBY_VM_CRITICAL_SECTION
1478 VM_ASSERT(ruby_assert_critical_section_entered == 0);
1480 VM_ASSERT(th == GET_THREAD());
1482 region->prev_status = th->status;
1483 if (unblock_function_set(th, ubf, arg, fail_if_interrupted)) {
1484 th->blocking_region_buffer = region;
1485 th->status = THREAD_STOPPED;
1486 rb_ractor_blocking_threads_inc(th->ractor, __FILE__, __LINE__);
1488 RUBY_DEBUG_LOG(
"thread_id:%p", (
void *)th->nt->thread_id);
1500 unblock_function_clear(th);
1502 unregister_ubf_list(th);
1504 thread_sched_to_running(TH_SCHED(th), th);
1505 rb_ractor_thread_switch(th->ractor, th);
1507 th->blocking_region_buffer = 0;
1508 rb_ractor_blocking_threads_dec(th->ractor, __FILE__, __LINE__);
1509 if (th->status == THREAD_STOPPED) {
1510 th->status = region->prev_status;
1513 RUBY_DEBUG_LOG(
"end");
1517 VM_ASSERT(th == GET_THREAD());
1522rb_nogvl(
void *(*func)(
void *),
void *data1,
1529 rb_vm_t *vm = rb_ec_vm_ptr(ec);
1530 bool is_main_thread = vm->ractor.main_thread == th;
1531 int saved_errno = 0;
1538 else if (ubf && rb_ractor_living_thread_num(th->ractor) == 1 && is_main_thread) {
1540 vm->ubf_async_safe = 1;
1544 BLOCKING_REGION(th, {
1546 saved_errno = rb_errno();
1549 if (is_main_thread) vm->ubf_async_safe = 0;
1552 RUBY_VM_CHECK_INTS_BLOCKING(ec);
1559 rb_errno_set(saved_errno);
1660 return rb_nogvl(func, data1, ubf, data2, 0);
1664waitfd_to_waiting_flag(
int wfd_event)
1666 return wfd_event << 1;
1678 ccan_list_add(&th->vm->waiting_fds, &wfd->wfd_node);
1684thread_io_wake_pending_closer(
struct waiting_fd *wfd)
1686 bool has_waiter = wfd->busy &&
RB_TEST(wfd->busy->wakeup_mutex);
1695 ccan_list_del(&wfd->wfd_node);
1707#if defined(USE_MN_THREADS) && USE_MN_THREADS
1708 if (!th_has_dedicated_nt(th) &&
1709 (events || timeout) &&
1713 rb_hrtime_t rel, *prel;
1716 rel = rb_timeval2hrtime(timeout);
1723 VM_ASSERT(prel || (events & (RB_WAITFD_IN | RB_WAITFD_OUT)));
1725 thread_io_setup_wfd(th, fd, wfd);
1728 r = thread_sched_wait_events(TH_SCHED(th), th, fd, waitfd_to_waiting_flag(events), prel);
1730 thread_io_wake_pending_closer(wfd);
1732 RUBY_VM_CHECK_INTS_BLOCKING(ec);
1742rb_thread_io_blocking_call(rb_blocking_function_t *func,
void *data1,
int fd,
int events)
1747 RUBY_DEBUG_LOG(
"th:%u fd:%d ev:%d", rb_th_serial(th), fd, events);
1751 thread_io_wait_events(th, ec, fd, events, NULL, &
waiting_fd);
1754 volatile int saved_errno = 0;
1755 enum ruby_tag_type state;
1766 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1769 saved_errno = errno;
1781 EC_JUMP_TAG(ec, state);
1784 RUBY_VM_CHECK_INTS_BLOCKING(ec);
1787 if (saved_errno == ETIMEDOUT) {
1791 errno = saved_errno;
1797rb_thread_io_blocking_region(rb_blocking_function_t *func,
void *data1,
int fd)
1799 return rb_thread_io_blocking_call(func, data1, fd, 0);
1844 fprintf(stderr,
"[BUG] rb_thread_call_with_gvl() is called by non-ruby thread\n");
1849 prev_unblock = th->unblock;
1852 rb_bug(
"rb_thread_call_with_gvl: called by a thread which has GVL.");
1855 blocking_region_end(th, brb);
1859 int released = blocking_region_begin(th, brb, prev_unblock.func, prev_unblock.arg, FALSE);
1861 RB_VM_SAVE_MACHINE_CONTEXT(th);
1862 thread_sched_to_waiting(TH_SCHED(th), th);
1876ruby_thread_has_gvl_p(
void)
1880 if (th && th->blocking_region_buffer == 0) {
1897thread_s_pass(
VALUE klass)
1922rb_threadptr_pending_interrupt_clear(
rb_thread_t *th)
1924 rb_ary_clear(th->pending_interrupt_queue);
1930 rb_ary_push(th->pending_interrupt_queue, v);
1931 th->pending_interrupt_queue_checked = 0;
1935threadptr_check_pending_interrupt_queue(
rb_thread_t *th)
1937 if (!th->pending_interrupt_queue) {
1942enum handle_interrupt_timing {
1944 INTERRUPT_IMMEDIATE,
1945 INTERRUPT_ON_BLOCKING,
1949static enum handle_interrupt_timing
1952 if (sym == sym_immediate) {
1953 return INTERRUPT_IMMEDIATE;
1955 else if (sym == sym_on_blocking) {
1956 return INTERRUPT_ON_BLOCKING;
1958 else if (sym == sym_never) {
1959 return INTERRUPT_NEVER;
1966static enum handle_interrupt_timing
1970 long mask_stack_len =
RARRAY_LEN(th->pending_interrupt_mask_stack);
1975 for (i=0; i<mask_stack_len; i++) {
1976 mask = mask_stack[mask_stack_len-(i+1)];
1981 return rb_threadptr_pending_interrupt_from_symbol(th, mask);
1993 klass =
RBASIC(mod)->klass;
1995 else if (mod != RCLASS_ORIGIN(mod)) {
1999 if ((sym = rb_hash_aref(mask, klass)) !=
Qnil) {
2000 return rb_threadptr_pending_interrupt_from_symbol(th, sym);
2005 return INTERRUPT_NONE;
2009rb_threadptr_pending_interrupt_empty_p(
const rb_thread_t *th)
2011 return RARRAY_LEN(th->pending_interrupt_queue) == 0;
2018 for (i=0; i<
RARRAY_LEN(th->pending_interrupt_queue); i++) {
2028rb_threadptr_pending_interrupt_deque(
rb_thread_t *th,
enum handle_interrupt_timing timing)
2033 for (i=0; i<
RARRAY_LEN(th->pending_interrupt_queue); i++) {
2036 enum handle_interrupt_timing mask_timing = rb_threadptr_pending_interrupt_check_mask(th,
CLASS_OF(err));
2038 switch (mask_timing) {
2039 case INTERRUPT_ON_BLOCKING:
2040 if (timing != INTERRUPT_ON_BLOCKING) {
2044 case INTERRUPT_NONE:
2045 case INTERRUPT_IMMEDIATE:
2046 rb_ary_delete_at(th->pending_interrupt_queue, i);
2048 case INTERRUPT_NEVER:
2053 th->pending_interrupt_queue_checked = 1;
2056 VALUE err = rb_ary_shift(th->pending_interrupt_queue);
2057 if (rb_threadptr_pending_interrupt_empty_p(th)) {
2058 th->pending_interrupt_queue_checked = 1;
2065threadptr_pending_interrupt_active_p(
rb_thread_t *th)
2072 if (th->pending_interrupt_queue_checked) {
2076 if (rb_threadptr_pending_interrupt_empty_p(th)) {
2088 if (val != sym_immediate && val != sym_on_blocking && val != sym_never) {
2089 rb_raise(rb_eArgError,
"unknown mask signature");
2097 if (
RTEST(*maskp)) {
2098 if (!RB_TYPE_P(*maskp,
T_HASH)) {
2099 VALUE prev = *maskp;
2100 *maskp = rb_ident_hash_new();
2105 rb_hash_aset(*maskp, key, val);
2223rb_thread_s_handle_interrupt(
VALUE self,
VALUE mask_arg)
2229 enum ruby_tag_type state;
2232 rb_raise(rb_eArgError,
"block is needed.");
2235 mask_arg = rb_to_hash_type(mask_arg);
2237 if (
OBJ_FROZEN(mask_arg) && rb_hash_compare_by_id_p(mask_arg)) {
2243 if (UNDEF_P(mask)) {
2250 else if (RB_TYPE_P(mask,
T_HASH)) {
2254 rb_ary_push(th->pending_interrupt_mask_stack, mask);
2255 if (!rb_threadptr_pending_interrupt_empty_p(th)) {
2256 th->pending_interrupt_queue_checked = 0;
2257 RUBY_VM_SET_INTERRUPT(th->ec);
2260 EC_PUSH_TAG(th->ec);
2261 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2266 rb_ary_pop(th->pending_interrupt_mask_stack);
2267 if (!rb_threadptr_pending_interrupt_empty_p(th)) {
2268 th->pending_interrupt_queue_checked = 0;
2269 RUBY_VM_SET_INTERRUPT(th->ec);
2272 RUBY_VM_CHECK_INTS(th->ec);
2275 EC_JUMP_TAG(th->ec, state);
2292rb_thread_pending_interrupt_p(
int argc,
VALUE *argv,
VALUE target_thread)
2294 rb_thread_t *target_th = rb_thread_ptr(target_thread);
2296 if (!target_th->pending_interrupt_queue) {
2299 if (rb_threadptr_pending_interrupt_empty_p(target_th)) {
2303 VALUE err = argv[0];
2305 rb_raise(
rb_eTypeError,
"class or module required for rescue clause");
2307 return RBOOL(rb_threadptr_pending_interrupt_include_p(target_th, err));
2372rb_thread_s_pending_interrupt_p(
int argc,
VALUE *argv,
VALUE self)
2374 return rb_thread_pending_interrupt_p(argc, argv, GET_THREAD()->self);
2377NORETURN(
static void rb_threadptr_to_kill(
rb_thread_t *th));
2382 rb_threadptr_pending_interrupt_clear(th);
2383 th->status = THREAD_RUNNABLE;
2385 th->ec->errinfo =
INT2FIX(TAG_FATAL);
2386 EC_JUMP_TAG(th->ec, TAG_FATAL);
2397 interrupt = ec->interrupt_flag;
2398 old = ATOMIC_CAS(ec->interrupt_flag, interrupt, interrupt & ec->interrupt_mask);
2399 }
while (old != interrupt);
2400 return interrupt & (
rb_atomic_t)~ec->interrupt_mask;
2404rb_threadptr_execute_interrupts(
rb_thread_t *th,
int blocking_timing)
2407 int postponed_job_interrupt = 0;
2410 if (th->ec->raised_flag)
return ret;
2412 while ((interrupt = threadptr_get_interrupts(th)) != 0) {
2414 int timer_interrupt;
2415 int pending_interrupt;
2417 int terminate_interrupt;
2419 timer_interrupt = interrupt & TIMER_INTERRUPT_MASK;
2420 pending_interrupt = interrupt & PENDING_INTERRUPT_MASK;
2421 postponed_job_interrupt = interrupt & POSTPONED_JOB_INTERRUPT_MASK;
2422 trap_interrupt = interrupt & TRAP_INTERRUPT_MASK;
2423 terminate_interrupt = interrupt & TERMINATE_INTERRUPT_MASK;
2425 if (interrupt & VM_BARRIER_INTERRUPT_MASK) {
2430 if (postponed_job_interrupt) {
2431 rb_postponed_job_flush(th->vm);
2435 if (trap_interrupt && (th == th->vm->ractor.main_thread)) {
2436 enum rb_thread_status prev_status = th->status;
2438 th->status = THREAD_RUNNABLE;
2440 while ((sig = rb_get_next_signal()) != 0) {
2441 ret |= rb_signal_exec(th, sig);
2444 th->status = prev_status;
2448 if (pending_interrupt && threadptr_pending_interrupt_active_p(th)) {
2449 VALUE err = rb_threadptr_pending_interrupt_deque(th, blocking_timing ? INTERRUPT_ON_BLOCKING : INTERRUPT_NONE);
2450 RUBY_DEBUG_LOG(
"err:%"PRIdVALUE, err);
2456 else if (err == RUBY_FATAL_THREAD_KILLED ||
2457 err == RUBY_FATAL_THREAD_TERMINATED ||
2459 terminate_interrupt = 1;
2462 if (err == th->vm->special_exceptions[ruby_error_stream_closed]) {
2464 err = ruby_vm_special_exception_copy(err);
2467 if (th->status == THREAD_STOPPED ||
2468 th->status == THREAD_STOPPED_FOREVER)
2469 th->status = THREAD_RUNNABLE;
2474 if (terminate_interrupt) {
2475 rb_threadptr_to_kill(th);
2478 if (timer_interrupt) {
2479 uint32_t limits_us = TIME_QUANTUM_USEC;
2481 if (th->priority > 0)
2482 limits_us <<= th->priority;
2484 limits_us >>= -th->priority;
2486 if (th->status == THREAD_RUNNABLE)
2487 th->running_time_us += 10 * 1000;
2489 VM_ASSERT(th->ec->cfp);
2493 rb_thread_schedule_limits(limits_us);
2500rb_thread_execute_interrupts(
VALUE thval)
2502 rb_threadptr_execute_interrupts(rb_thread_ptr(thval), 1);
2508 rb_threadptr_interrupt(th);
2516 if (rb_threadptr_dead(target_th)) {
2524 exc = rb_make_exception(argc, argv);
2529 if (rb_threadptr_dead(target_th)) {
2533 rb_ec_setup_exception(GET_EC(), exc,
Qundef);
2534 rb_threadptr_pending_interrupt_enque(target_th, exc);
2535 rb_threadptr_interrupt(target_th);
2540rb_threadptr_signal_raise(
rb_thread_t *th,
int sig)
2546 rb_threadptr_raise(th->vm->ractor.main_thread, 2, argv);
2558 rb_threadptr_raise(th->vm->ractor.main_thread, 2, argv);
2564 if (ec->raised_flag & RAISED_EXCEPTION) {
2567 ec->raised_flag |= RAISED_EXCEPTION;
2574 if (!(ec->raised_flag & RAISED_EXCEPTION)) {
2577 ec->raised_flag &= ~RAISED_EXCEPTION;
2584 rb_vm_t *vm = GET_THREAD()->vm;
2586 ccan_list_head_init(&busy->pending_fd_users);
2592 ccan_list_for_each_safe(&vm->waiting_fds, wfd, next, wfd_node) {
2593 if (wfd->fd == fd) {
2597 ccan_list_del(&wfd->wfd_node);
2598 ccan_list_add(&busy->pending_fd_users, &wfd->wfd_node);
2601 err = th->vm->special_exceptions[ruby_error_stream_closed];
2602 rb_threadptr_pending_interrupt_enque(th, err);
2603 rb_threadptr_interrupt(th);
2608 has_any = !ccan_list_empty(&busy->pending_fd_users);
2610 wakeup_mutex =
Qnil;
2613 RBASIC_CLEAR_CLASS(wakeup_mutex);
2615 busy->wakeup_mutex = wakeup_mutex;
2629 if (!
RB_TEST(busy->wakeup_mutex)) {
2636 while (!ccan_list_empty(&busy->pending_fd_users)) {
2647 if (rb_notify_fd_close(fd, &busy)) {
2648 rb_notify_fd_close_wait(&busy);
2674thread_raise_m(
int argc,
VALUE *argv,
VALUE self)
2679 threadptr_check_pending_interrupt_queue(target_th);
2680 rb_threadptr_raise(target_th, argc, argv);
2683 if (current_th == target_th) {
2684 RUBY_VM_CHECK_INTS(target_th->ec);
2706 if (target_th->to_kill || target_th->status == THREAD_KILLED) {
2709 if (target_th == target_th->vm->ractor.main_thread) {
2713 RUBY_DEBUG_LOG(
"target_th:%u", rb_th_serial(target_th));
2715 if (target_th == GET_THREAD()) {
2717 rb_threadptr_to_kill(target_th);
2720 threadptr_check_pending_interrupt_queue(target_th);
2721 rb_threadptr_pending_interrupt_enque(target_th, RUBY_FATAL_THREAD_KILLED);
2722 rb_threadptr_interrupt(target_th);
2729rb_thread_to_be_killed(
VALUE thread)
2733 if (target_th->to_kill || target_th->status == THREAD_KILLED) {
2809 if (target_th->status == THREAD_KILLED)
return Qnil;
2811 rb_threadptr_ready(target_th);
2813 if (target_th->status == THREAD_STOPPED ||
2814 target_th->status == THREAD_STOPPED_FOREVER) {
2815 target_th->status = THREAD_RUNNABLE;
2857 "stopping only thread\n\tnote: use sleep to stop forever");
2890 return rb_ractor_thread_list();
2916 return rb_thread_list();
2922 return GET_THREAD()->self;
2935thread_s_current(
VALUE klass)
2943 return GET_RACTOR()->threads.main->self;
2954rb_thread_s_main(
VALUE klass)
2981rb_thread_s_abort_exc(
VALUE _)
2983 return RBOOL(GET_THREAD()->vm->thread_abort_on_exception);
3018rb_thread_s_abort_exc_set(
VALUE self,
VALUE val)
3020 GET_THREAD()->vm->thread_abort_on_exception =
RTEST(val);
3041rb_thread_abort_exc(
VALUE thread)
3043 return RBOOL(rb_thread_ptr(thread)->abort_on_exception);
3061rb_thread_abort_exc_set(
VALUE thread,
VALUE val)
3063 rb_thread_ptr(thread)->abort_on_exception =
RTEST(val);
3111rb_thread_s_report_exc(
VALUE _)
3113 return RBOOL(GET_THREAD()->vm->thread_report_on_exception);
3148rb_thread_s_report_exc_set(
VALUE self,
VALUE val)
3150 GET_THREAD()->vm->thread_report_on_exception =
RTEST(val);
3167rb_thread_s_ignore_deadlock(
VALUE _)
3169 return RBOOL(GET_THREAD()->vm->thread_ignore_deadlock);
3194rb_thread_s_ignore_deadlock_set(
VALUE self,
VALUE val)
3196 GET_THREAD()->vm->thread_ignore_deadlock =
RTEST(val);
3218rb_thread_report_exc(
VALUE thread)
3220 return RBOOL(rb_thread_ptr(thread)->report_on_exception);
3238rb_thread_report_exc_set(
VALUE thread,
VALUE val)
3240 rb_thread_ptr(thread)->report_on_exception =
RTEST(val);
3255rb_thread_group(
VALUE thread)
3257 return rb_thread_ptr(thread)->thgroup;
3263 switch (th->status) {
3264 case THREAD_RUNNABLE:
3265 return th->to_kill ?
"aborting" :
"run";
3266 case THREAD_STOPPED_FOREVER:
3267 if (detail)
return "sleep_forever";
3268 case THREAD_STOPPED:
3280 return th->status == THREAD_KILLED;
3316rb_thread_status(
VALUE thread)
3320 if (rb_threadptr_dead(target_th)) {
3321 if (!
NIL_P(target_th->ec->errinfo) &&
3322 !
FIXNUM_P(target_th->ec->errinfo)) {
3330 return rb_str_new2(thread_status_name(target_th, FALSE));
3350rb_thread_alive_p(
VALUE thread)
3352 return RBOOL(!thread_finished(rb_thread_ptr(thread)));
3370rb_thread_stop_p(
VALUE thread)
3374 if (rb_threadptr_dead(th)) {
3377 return RBOOL(th->status == THREAD_STOPPED || th->status == THREAD_STOPPED_FOREVER);
3388rb_thread_getname(
VALUE thread)
3390 return rb_thread_ptr(thread)->name;
3409 enc = rb_enc_get(name);
3410 if (!rb_enc_asciicompat(enc)) {
3411 rb_raise(rb_eArgError,
"ASCII incompatible encoding (%s)",
3414 name = rb_str_new_frozen(name);
3416 target_th->name = name;
3417 if (threadptr_initialized(target_th) && target_th->has_dedicated_nt) {
3418 native_set_another_thread_name(target_th->nt->thread_id, name);
3423#if USE_NATIVE_THREAD_NATIVE_THREAD_ID
3447rb_thread_native_thread_id(
VALUE thread)
3450 if (rb_threadptr_dead(target_th))
return Qnil;
3451 return native_thread_native_thread_id(target_th);
3454# define rb_thread_native_thread_id rb_f_notimplement
3465rb_thread_to_s(
VALUE thread)
3472 status = thread_status_name(target_th, TRUE);
3473 str = rb_sprintf(
"#<%"PRIsVALUE
":%p", cname, (
void *)thread);
3474 if (!
NIL_P(target_th->name)) {
3475 rb_str_catf(str,
"@%"PRIsVALUE, target_th->name);
3477 if ((loc = threadptr_invoke_proc_location(target_th)) !=
Qnil) {
3478 rb_str_catf(str,
" %"PRIsVALUE
":%"PRIsVALUE,
3481 rb_str_catf(str,
" %s>", status);
3487#define recursive_key id__recursive_key__
3492 if (
id == recursive_key) {
3493 return th->ec->local_storage_recursive_hash;
3497 struct rb_id_table *local_storage = th->ec->local_storage;
3499 if (local_storage != NULL && rb_id_table_lookup(local_storage,
id, &val)) {
3511 return threadptr_local_aref(rb_thread_ptr(thread),
id);
3578 if (!
id)
return Qnil;
3596rb_thread_fetch(
int argc,
VALUE *argv,
VALUE self)
3607 if (block_given && argc == 2) {
3608 rb_warn(
"block supersedes default value argument");
3613 if (
id == recursive_key) {
3614 return target_th->ec->local_storage_recursive_hash;
3616 else if (
id && target_th->ec->local_storage &&
3617 rb_id_table_lookup(target_th->ec->local_storage,
id, &val)) {
3620 else if (block_given) {
3623 else if (argc == 1) {
3624 rb_key_err_raise(rb_sprintf(
"key not found: %+"PRIsVALUE, key), self, key);
3634 if (
id == recursive_key) {
3635 th->ec->local_storage_recursive_hash = val;
3639 struct rb_id_table *local_storage = th->ec->local_storage;
3642 if (!local_storage)
return Qnil;
3643 rb_id_table_delete(local_storage,
id);
3647 if (local_storage == NULL) {
3648 th->ec->local_storage = local_storage = rb_id_table_create(0);
3650 rb_id_table_insert(local_storage,
id, val);
3663 return threadptr_local_aset(rb_thread_ptr(thread),
id, val);
3714rb_thread_variable_get(
VALUE thread,
VALUE key)
3718 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
3721 locals = rb_thread_local_storage(thread);
3743 locals = rb_thread_local_storage(thread);
3765 struct rb_id_table *local_storage = rb_thread_ptr(self)->ec->local_storage;
3767 if (!
id || local_storage == NULL) {
3770 return RBOOL(rb_id_table_lookup(local_storage,
id, &val));
3773static enum rb_id_table_iterator_result
3774thread_keys_i(
ID key,
VALUE value,
void *ary)
3777 return ID_TABLE_CONTINUE;
3784 return rb_ractor_living_thread_num(GET_RACTOR()) == 1;
3802rb_thread_keys(
VALUE self)
3804 struct rb_id_table *local_storage = rb_thread_ptr(self)->ec->local_storage;
3805 VALUE ary = rb_ary_new();
3807 if (local_storage) {
3808 rb_id_table_foreach(local_storage, thread_keys_i, (
void *)ary);
3816 rb_ary_push(ary, key);
3838rb_thread_variables(
VALUE thread)
3844 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
3847 locals = rb_thread_local_storage(thread);
3874 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
3877 locals = rb_thread_local_storage(thread);
3898rb_thread_priority(
VALUE thread)
3900 return INT2NUM(rb_thread_ptr(thread)->priority);
3931rb_thread_priority_set(
VALUE thread,
VALUE prio)
3936#if USE_NATIVE_THREAD_PRIORITY
3937 target_th->priority =
NUM2INT(prio);
3938 native_thread_apply_priority(th);
3941 if (priority > RUBY_THREAD_PRIORITY_MAX) {
3942 priority = RUBY_THREAD_PRIORITY_MAX;
3944 else if (priority < RUBY_THREAD_PRIORITY_MIN) {
3945 priority = RUBY_THREAD_PRIORITY_MIN;
3947 target_th->priority = (int8_t)priority;
3949 return INT2NUM(target_th->priority);
3954#if defined(NFDBITS) && defined(HAVE_RB_FD_INIT)
3990 FD_ZERO(fds->
fdset);
3996 size_t size = howmany(
rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
3998 if (size <
sizeof(fd_set))
3999 size =
sizeof(fd_set);
4023 size_t m = howmany(n + 1, NFDBITS) *
sizeof(fd_mask);
4024 size_t o = howmany(fds->
maxfd, NFDBITS) *
sizeof(fd_mask);
4026 if (m <
sizeof(fd_set)) m =
sizeof(fd_set);
4027 if (o <
sizeof(fd_set)) o =
sizeof(fd_set);
4031 memset((
char *)fds->
fdset + o, 0, m - o);
4040 FD_SET(n, fds->
fdset);
4046 if (n >= fds->
maxfd)
return;
4047 FD_CLR(n, fds->
fdset);
4053 if (n >= fds->
maxfd)
return 0;
4054 return FD_ISSET(n, fds->
fdset) != 0;
4060 size_t size = howmany(max, NFDBITS) *
sizeof(fd_mask);
4062 if (size <
sizeof(fd_set)) size =
sizeof(fd_set);
4065 memcpy(dst->
fdset, src, size);
4071 size_t size = howmany(
rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
4073 if (size <
sizeof(fd_set))
4074 size =
sizeof(fd_set);
4083 fd_set *r = NULL, *w = NULL, *e = NULL;
4096 return select(n, r, w, e, timeout);
4099#define rb_fd_no_init(fds) ((void)((fds)->fdset = 0), (void)((fds)->maxfd = 0))
4106#define FD_ZERO(f) rb_fd_zero(f)
4107#define FD_SET(i, f) rb_fd_set((i), (f))
4108#define FD_CLR(i, f) rb_fd_clr((i), (f))
4109#define FD_ISSET(i, f) rb_fd_isset((i), (f))
4111#elif defined(_WIN32)
4116 set->
capa = FD_SETSIZE;
4118 FD_ZERO(set->
fdset);
4140 SOCKET s = rb_w32_get_osfhandle(fd);
4142 for (i = 0; i < set->
fdset->fd_count; i++) {
4143 if (set->
fdset->fd_array[i] == s) {
4147 if (set->
fdset->fd_count >= (
unsigned)set->
capa) {
4148 set->
capa = (set->
fdset->fd_count / FD_SETSIZE + 1) * FD_SETSIZE;
4150 rb_xrealloc_mul_add(
4151 set->
fdset, set->
capa,
sizeof(SOCKET),
sizeof(
unsigned int));
4153 set->
fdset->fd_array[set->
fdset->fd_count++] = s;
4161#define FD_ZERO(f) rb_fd_zero(f)
4162#define FD_SET(i, f) rb_fd_set((i), (f))
4163#define FD_CLR(i, f) rb_fd_clr((i), (f))
4164#define FD_ISSET(i, f) rb_fd_isset((i), (f))
4166#define rb_fd_no_init(fds) (void)((fds)->fdset = 0)
4170#ifndef rb_fd_no_init
4171#define rb_fd_no_init(fds) (void)(fds)
4175wait_retryable(
volatile int *result,
int errnum, rb_hrtime_t *rel, rb_hrtime_t end)
4184 if (rel && hrtime_update_expire(rel, end)) {
4191 else if (*result == 0) {
4194 return !hrtime_update_expire(rel, end);
4214select_set_free(
VALUE p)
4229 volatile int result = 0;
4231 rb_hrtime_t *to, rel, end = 0;
4233 timeout_prepare(&to, &rel, &end, set->timeout);
4234 volatile rb_hrtime_t endtime = end;
4235#define restore_fdset(dst, src) \
4236 ((dst) ? rb_fd_dup(dst, src) : (void)0)
4237#define do_select_update() \
4238 (restore_fdset(set->rset, &set->orig_rset), \
4239 restore_fdset(set->wset, &set->orig_wset), \
4240 restore_fdset(set->eset, &set->orig_eset), \
4246 BLOCKING_REGION(set->th, {
4249 if (!RUBY_VM_INTERRUPTED(set->th->ec)) {
4250 result = native_fd_select(set->max,
4251 set->rset, set->wset, set->eset,
4252 rb_hrtime2timeval(&tv, to), set->th);
4253 if (result < 0) lerrno = errno;
4255 }, ubf_select, set->th, TRUE);
4257 RUBY_VM_CHECK_INTS_BLOCKING(set->th->ec);
4258 }
while (wait_retryable(&result, lerrno, to, endtime) && do_select_update());
4264 return (
VALUE)result;
4273 set.th = GET_THREAD();
4274 RUBY_VM_CHECK_INTS_BLOCKING(set.th->ec);
4279 set.timeout = timeout;
4281 if (!set.rset && !set.wset && !set.eset) {
4290#define fd_init_copy(f) do { \
4292 rb_fd_resize(set.max - 1, set.f); \
4293 if (&set.orig_##f != set.f) { \
4294 rb_fd_init_copy(&set.orig_##f, set.f); \
4298 rb_fd_no_init(&set.orig_##f); \
4312#define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
4313#define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
4314#define POLLEX_SET (POLLPRI)
4317# define POLLERR_SET (0)
4324rb_thread_wait_for_single_fd(
int fd,
int events,
struct timeval *timeout)
4326 struct pollfd fds[1];
4327 volatile int result = 0;
4331 volatile int lerrno;
4336 if (thread_io_wait_events(th, ec, fd, events, timeout, &wfd)) {
4340 thread_io_setup_wfd(th, fd, &wfd);
4342 EC_PUSH_TAG(wfd.th->ec);
4343 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
4344 rb_hrtime_t *to, rel, end = 0;
4345 RUBY_VM_CHECK_INTS_BLOCKING(wfd.th->ec);
4346 timeout_prepare(&to, &rel, &end, timeout);
4347 volatile rb_hrtime_t endtime = end;
4349 fds[0].events = (short)events;
4355 BLOCKING_REGION(wfd.th, {
4358 if (!RUBY_VM_INTERRUPTED(wfd.th->ec)) {
4359 result = ppoll(fds, nfds, rb_hrtime2timespec(&ts, to), 0);
4360 if (result < 0) lerrno = errno;
4362 }, ubf_select, wfd.th, TRUE);
4364 RUBY_VM_CHECK_INTS_BLOCKING(wfd.th->ec);
4365 }
while (wait_retryable(&result, lerrno, to, endtime));
4369 thread_io_wake_pending_closer(&wfd);
4372 EC_JUMP_TAG(wfd.th->ec, state);
4380 if (fds[0].revents & POLLNVAL) {
4390 if (fds[0].revents & POLLIN_SET)
4391 result |= RB_WAITFD_IN;
4392 if (fds[0].revents & POLLOUT_SET)
4393 result |= RB_WAITFD_OUT;
4394 if (fds[0].revents & POLLEX_SET)
4395 result |= RB_WAITFD_PRI;
4398 if (fds[0].revents & POLLERR_SET)
4417select_single(
VALUE ptr)
4423 args->read, args->write, args->except, args->tv);
4425 args->as.error =
errno;
4428 if (args->read &&
rb_fd_isset(args->as.fd, args->read))
4430 if (args->write &&
rb_fd_isset(args->as.fd, args->write))
4432 if (args->except &&
rb_fd_isset(args->as.fd, args->except))
4439select_single_cleanup(
VALUE ptr)
4443 thread_io_wake_pending_closer(&args->wfd);
4464rb_thread_wait_for_single_fd(
int fd,
int events,
struct timeval *timeout)
4473 if (thread_io_wait_events(th, ec, fd, events, timeout, &args.wfd)) {
4478 args.read = (events & RB_WAITFD_IN) ? init_set_fd(fd, &rfds) : NULL;
4479 args.write = (events & RB_WAITFD_OUT) ? init_set_fd(fd, &wfds) : NULL;
4480 args.except = (events & RB_WAITFD_PRI) ? init_set_fd(fd, &efds) : NULL;
4484 args.wfd.busy = NULL;
4488 ccan_list_add(&args.wfd.th->vm->waiting_fds, &args.wfd.wfd_node);
4492 r = (int)
rb_ensure(select_single, ptr, select_single_cleanup, ptr);
4494 errno = args.as.error;
4504#ifdef USE_CONSERVATIVE_STACK_END
4506rb_gc_set_stack_end(
VALUE **stack_end_p)
4509 *stack_end_p = &stack_end;
4521 if (rb_signal_buff_size() > 0) {
4523 threadptr_trap_interrupt(mth);
4528async_bug_fd(
const char *mesg,
int errno_arg,
int fd)
4531 size_t n = strlcpy(buff, mesg,
sizeof(buff));
4532 if (n <
sizeof(buff)-3) {
4533 ruby_snprintf(buff+n,
sizeof(buff)-n,
"(%d)", fd);
4535 rb_async_bug_errno(buff, errno_arg);
4540consume_communication_pipe(
int fd)
4546 static char buff[1024];
4552 result = read(fd, buff,
sizeof(buff));
4554 RUBY_DEBUG_LOG(
"resultf:%d buff:%lu", (
int)result, (
unsigned long)buff[0]);
4556 RUBY_DEBUG_LOG(
"result:%d", (
int)result);
4560 if (USE_EVENTFD || result < (ssize_t)
sizeof(buff)) {
4564 else if (result == 0) {
4567 else if (result < 0) {
4573#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
4578 async_bug_fd(
"consume_communication_pipe: read", e, fd);
4585rb_thread_stop_timer_thread(
void)
4587 if (TIMER_THREAD_CREATED_P() && native_stop_timer_thread()) {
4588 native_reset_timer_thread();
4593rb_thread_reset_timer_thread(
void)
4595 native_reset_timer_thread();
4599rb_thread_start_timer_thread(
void)
4602 rb_thread_create_timer_thread();
4606clear_coverage_i(st_data_t key, st_data_t val, st_data_t dummy)
4614 if (GET_VM()->coverage_mode & COVERAGE_TARGET_ONESHOT_LINES) {
4615 rb_ary_clear(lines);
4636rb_clear_coverages(
void)
4638 VALUE coverages = rb_get_coverages();
4639 if (
RTEST(coverages)) {
4644#if defined(HAVE_WORKING_FORK)
4652 vm->ractor.main_ractor = r;
4653 vm->ractor.main_thread = th;
4654 r->threads.main = th;
4655 r->status_ = ractor_created;
4657 thread_sched_atfork(TH_SCHED(th));
4661 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
4662 ccan_list_for_each(&r->threads.set, i, lt_node) {
4666 rb_vm_living_threads_init(vm);
4668 rb_ractor_atfork(vm, th);
4669 rb_vm_postponed_job_atfork();
4678 rb_ractor_sleeper_threads_clear(th->ractor);
4679 rb_clear_coverages();
4682 rb_thread_reset_timer_thread();
4683 rb_thread_start_timer_thread();
4685 VM_ASSERT(vm->ractor.blocking_cnt == 0);
4686 VM_ASSERT(vm->ractor.cnt == 1);
4692 if (th != current_th) {
4693 rb_mutex_abandon_keeping_mutexes(th);
4694 rb_mutex_abandon_locking_mutex(th);
4695 thread_cleanup_func(th, TRUE);
4704 rb_threadptr_pending_interrupt_clear(th);
4705 rb_thread_atfork_internal(th, terminate_atfork_i);
4706 th->join_list = NULL;
4707 rb_fiber_atfork(th);
4716 if (th != current_th) {
4717 thread_cleanup_func_before_exec(th);
4725 rb_thread_atfork_internal(th, terminate_atfork_before_exec_i);
4750 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
4773thgroup_s_alloc(
VALUE klass)
4794thgroup_list(
VALUE group)
4796 VALUE ary = rb_ary_new();
4800 ccan_list_for_each(&r->threads.set, th, lt_node) {
4801 if (th->thgroup == group) {
4802 rb_ary_push(ary, th->self);
4826thgroup_enclose(
VALUE group)
4845thgroup_enclosed_p(
VALUE group)
4850 return RBOOL(data->enclosed);
4890 if (data->enclosed) {
4898 if (data->enclosed) {
4900 "can't move from the enclosed thread group");
4903 target_th->thgroup = group;
4911thread_shield_mark(
void *ptr)
4913 rb_gc_mark((
VALUE)ptr);
4918 {thread_shield_mark, 0, 0,},
4919 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
4923thread_shield_alloc(
VALUE klass)
4928#define GetThreadShieldPtr(obj) ((VALUE)rb_check_typeddata((obj), &thread_shield_data_type))
4929#define THREAD_SHIELD_WAITING_MASK (((FL_USER19-1)&~(FL_USER0-1))|FL_USER19)
4930#define THREAD_SHIELD_WAITING_SHIFT (FL_USHIFT)
4931#define THREAD_SHIELD_WAITING_MAX (THREAD_SHIELD_WAITING_MASK>>THREAD_SHIELD_WAITING_SHIFT)
4932STATIC_ASSERT(THREAD_SHIELD_WAITING_MAX, THREAD_SHIELD_WAITING_MAX <= UINT_MAX);
4933static inline unsigned int
4934rb_thread_shield_waiting(
VALUE b)
4936 return ((
RBASIC(b)->flags&THREAD_SHIELD_WAITING_MASK)>>THREAD_SHIELD_WAITING_SHIFT);
4940rb_thread_shield_waiting_inc(
VALUE b)
4942 unsigned int w = rb_thread_shield_waiting(b);
4944 if (w > THREAD_SHIELD_WAITING_MAX)
4946 RBASIC(b)->flags &= ~THREAD_SHIELD_WAITING_MASK;
4947 RBASIC(b)->flags |= ((
VALUE)w << THREAD_SHIELD_WAITING_SHIFT);
4951rb_thread_shield_waiting_dec(
VALUE b)
4953 unsigned int w = rb_thread_shield_waiting(b);
4956 RBASIC(b)->flags &= ~THREAD_SHIELD_WAITING_MASK;
4957 RBASIC(b)->flags |= ((
VALUE)w << THREAD_SHIELD_WAITING_SHIFT);
4961rb_thread_shield_new(
void)
4963 VALUE thread_shield = thread_shield_alloc(rb_cThreadShield);
4965 return thread_shield;
4969rb_thread_shield_owned(
VALUE self)
4971 VALUE mutex = GetThreadShieldPtr(self);
4972 if (!mutex)
return false;
4976 return m->fiber == GET_EC()->fiber_ptr;
4988rb_thread_shield_wait(
VALUE self)
4990 VALUE mutex = GetThreadShieldPtr(self);
4993 if (!mutex)
return Qfalse;
4994 m = mutex_ptr(mutex);
4995 if (m->fiber == GET_EC()->fiber_ptr)
return Qnil;
4996 rb_thread_shield_waiting_inc(self);
4998 rb_thread_shield_waiting_dec(self);
5001 return rb_thread_shield_waiting(self) > 0 ?
Qnil :
Qfalse;
5005thread_shield_get_mutex(
VALUE self)
5007 VALUE mutex = GetThreadShieldPtr(self);
5009 rb_raise(
rb_eThreadError,
"destroyed thread shield - %p", (
void *)self);
5017rb_thread_shield_release(
VALUE self)
5019 VALUE mutex = thread_shield_get_mutex(self);
5021 return RBOOL(rb_thread_shield_waiting(self) > 0);
5028rb_thread_shield_destroy(
VALUE self)
5030 VALUE mutex = thread_shield_get_mutex(self);
5033 return RBOOL(rb_thread_shield_waiting(self) > 0);
5039 return th->ec->local_storage_recursive_hash;
5045 th->ec->local_storage_recursive_hash = hash;
5057recursive_list_access(
VALUE sym)
5060 VALUE hash = threadptr_recursive_hash(th);
5063 hash = rb_ident_hash_new();
5064 threadptr_recursive_hash_set(th, hash);
5068 list = rb_hash_aref(hash, sym);
5071 list = rb_ident_hash_new();
5072 rb_hash_aset(hash, sym, list);
5086#if SIZEOF_LONG == SIZEOF_VOIDP
5087 #define OBJ_ID_EQL(obj_id, other) ((obj_id) == (other))
5088#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
5089 #define OBJ_ID_EQL(obj_id, other) (RB_BIGNUM_TYPE_P((obj_id)) ? \
5090 rb_big_eql((obj_id), (other)) : ((obj_id) == (other)))
5093 VALUE pair_list = rb_hash_lookup2(list, obj,
Qundef);
5094 if (UNDEF_P(pair_list))
5096 if (paired_obj_id) {
5097 if (!RB_TYPE_P(pair_list,
T_HASH)) {
5098 if (!OBJ_ID_EQL(paired_obj_id, pair_list))
5102 if (
NIL_P(rb_hash_lookup(pair_list, paired_obj_id)))
5124 rb_hash_aset(list, obj,
Qtrue);
5126 else if (UNDEF_P(pair_list = rb_hash_lookup2(list, obj,
Qundef))) {
5127 rb_hash_aset(list, obj, paired_obj);
5130 if (!RB_TYPE_P(pair_list,
T_HASH)){
5131 VALUE other_paired_obj = pair_list;
5132 pair_list = rb_hash_new();
5133 rb_hash_aset(pair_list, other_paired_obj,
Qtrue);
5134 rb_hash_aset(list, obj, pair_list);
5136 rb_hash_aset(pair_list, paired_obj,
Qtrue);
5152 VALUE pair_list = rb_hash_lookup2(list, obj,
Qundef);
5153 if (UNDEF_P(pair_list)) {
5156 if (RB_TYPE_P(pair_list,
T_HASH)) {
5157 rb_hash_delete_entry(pair_list, paired_obj);
5163 rb_hash_delete_entry(list, obj);
5179 return (*p->func)(p->obj, p->arg, FALSE);
5200 p.list = recursive_list_access(sym);
5204 outermost = outer && !recursive_check(p.list,
ID2SYM(recursive_key), 0);
5206 if (recursive_check(p.list, p.obj, pairid)) {
5207 if (outer && !outermost) {
5210 return (*func)(obj, arg, TRUE);
5213 enum ruby_tag_type state;
5218 recursive_push(p.list,
ID2SYM(recursive_key), 0);
5219 recursive_push(p.list, p.obj, p.pairid);
5220 result = rb_catch_protect(p.list, exec_recursive_i, (
VALUE)&p, &state);
5221 if (!recursive_pop(p.list, p.obj, p.pairid))
goto invalid;
5222 if (!recursive_pop(p.list,
ID2SYM(recursive_key), 0))
goto invalid;
5223 if (state != TAG_NONE) EC_JUMP_TAG(GET_EC(), state);
5224 if (result == p.list) {
5225 result = (*func)(obj, arg, TRUE);
5230 recursive_push(p.list, p.obj, p.pairid);
5231 EC_PUSH_TAG(GET_EC());
5232 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
5233 ret = (*func)(obj, arg, FALSE);
5236 if (!recursive_pop(p.list, p.obj, p.pairid)) {
5239 if (state != TAG_NONE) EC_JUMP_TAG(GET_EC(), state);
5248 "for %+"PRIsVALUE
" in %+"PRIsVALUE,
5272 return exec_recursive(func, obj, rb_memory_id(paired_obj), arg, 0,
rb_frame_last_func());
5290 return exec_recursive(func, obj, 0, arg, 1, mid);
5302 return exec_recursive(func, obj, rb_memory_id(paired_obj), arg, 1,
rb_frame_last_func());
5314rb_thread_backtrace_m(
int argc,
VALUE *argv,
VALUE thval)
5316 return rb_vm_thread_backtrace(argc, argv, thval);
5331rb_thread_backtrace_locations_m(
int argc,
VALUE *argv,
VALUE thval)
5333 return rb_vm_thread_backtrace_locations(argc, argv, thval);
5337Init_Thread_Mutex(
void)
5427 rb_vm_register_special_exception(ruby_error_stream_closed,
rb_eIOError,
5428 "stream closed in another thread");
5438 th->thgroup = th->ractor->thgroup_default =
rb_obj_alloc(cThGroup);
5449#ifdef HAVE_PTHREAD_NP_H
5450 VM_ASSERT(TH_SCHED(th)->running == th);
5456 th->pending_interrupt_queue = rb_ary_hidden_new(0);
5457 th->pending_interrupt_queue_checked = 0;
5458 th->pending_interrupt_mask_stack = rb_ary_hidden_new(0);
5462 rb_thread_create_timer_thread();
5478#ifdef NON_SCALAR_THREAD_ID
5479 #define thread_id_str(th) (NULL)
5481 #define thread_id_str(th) ((void *)(uintptr_t)(th)->nt->thread_id)
5490 rb_str_catf(msg,
"\n%d threads, %d sleeps current:%p main thread:%p\n",
5491 rb_ractor_living_thread_num(r), rb_ractor_sleeper_thread_num(r),
5492 (
void *)GET_THREAD(), (
void *)r->threads.main);
5494 ccan_list_for_each(&r->threads.set, th, lt_node) {
5495 rb_str_catf(msg,
"* %+"PRIsVALUE
"\n rb_thread_t:%p "
5497 th->self, (
void *)th, th->nt ? thread_id_str(th) :
"N/A", th->ec->interrupt_flag);
5499 if (th->locking_mutex) {
5500 rb_mutex_t *mutex = mutex_ptr(th->locking_mutex);
5501 rb_str_catf(msg,
" mutex:%p cond:%"PRIuSIZE,
5502 (
void *)mutex->fiber, rb_mutex_num_waiting(mutex));
5508 rb_str_catf(msg,
"\n depended by: tb_thread_id:%p", (
void *)list->thread);
5512 rb_str_catf(msg,
"\n ");
5513 rb_str_concat(msg, rb_ary_join(rb_ec_backtrace_str_ary(th->ec, 0, 0), sep));
5514 rb_str_catf(msg,
"\n");
5521 if (GET_THREAD()->vm->thread_ignore_deadlock)
return;
5523#ifdef RUBY_THREAD_PTHREAD_H
5524 if (r->threads.sched.readyq_cnt > 0)
return;
5527 int sleeper_num = rb_ractor_sleeper_thread_num(r);
5528 int ltnum = rb_ractor_living_thread_num(r);
5530 if (ltnum > sleeper_num)
return;
5531 if (ltnum < sleeper_num) rb_bug(
"sleeper must not be more than vm_living_thread_num(vm)");
5536 ccan_list_for_each(&r->threads.set, th, lt_node) {
5537 if (th->status != THREAD_STOPPED_FOREVER || RUBY_VM_INTERRUPTED(th->ec)) {
5540 else if (th->locking_mutex) {
5541 rb_mutex_t *mutex = mutex_ptr(th->locking_mutex);
5542 if (mutex->fiber == th->ec->fiber_ptr || (!mutex->fiber && !ccan_list_empty(&mutex->waitq))) {
5553 argv[1] =
rb_str_new2(
"No live threads left. Deadlock?");
5554 debug_deadlock_check(r, argv[1]);
5555 rb_ractor_sleeper_threads_dec(GET_RACTOR());
5556 rb_threadptr_raise(r->threads.main, 2, argv);
5563rb_vm_memsize_waiting_fds(
struct ccan_list_head *waiting_fds)
5568 ccan_list_for_each(waiting_fds, waitfd, wfd_node) {
5579 VALUE coverage = rb_iseq_coverage(cfp->iseq);
5587 if (GET_VM()->coverage_mode & COVERAGE_TARGET_ONESHOT_LINES) {
5588 rb_iseq_clear_event_flags(cfp->iseq, cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded - 1, RUBY_EVENT_COVERAGE_LINE);
5589 rb_ary_push(lines,
LONG2FIX(line + 1));
5609 VALUE coverage = rb_iseq_coverage(cfp->iseq);
5613 long pc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded - 1;
5628 VALUE path, beg_pos_lineno, beg_pos_column, end_pos_lineno, end_pos_column;
5630 if (!me->def)
return NULL;
5633 switch (me->def->type) {
5634 case VM_METHOD_TYPE_ISEQ: {
5637 path = rb_iseq_path(iseq);
5638 beg_pos_lineno =
INT2FIX(loc->code_location.beg_pos.lineno);
5639 beg_pos_column =
INT2FIX(loc->code_location.beg_pos.column);
5640 end_pos_lineno =
INT2FIX(loc->code_location.end_pos.lineno);
5641 end_pos_column =
INT2FIX(loc->code_location.end_pos.column);
5644 case VM_METHOD_TYPE_BMETHOD: {
5645 const rb_iseq_t *iseq = rb_proc_get_iseq(me->def->body.bmethod.proc, 0);
5648 rb_iseq_check(iseq);
5649 path = rb_iseq_path(iseq);
5650 loc = &ISEQ_BODY(iseq)->location;
5651 beg_pos_lineno =
INT2FIX(loc->code_location.beg_pos.lineno);
5652 beg_pos_column =
INT2FIX(loc->code_location.beg_pos.column);
5653 end_pos_lineno =
INT2FIX(loc->code_location.end_pos.lineno);
5654 end_pos_column =
INT2FIX(loc->code_location.end_pos.column);
5659 case VM_METHOD_TYPE_ALIAS:
5660 me = me->def->body.alias.original_me;
5662 case VM_METHOD_TYPE_REFINED:
5663 me = me->def->body.refined.orig_me;
5664 if (!me)
return NULL;
5671 if (RB_TYPE_P(path,
T_ARRAY)) {
5672 path = rb_ary_entry(path, 1);
5673 if (!RB_TYPE_P(path,
T_STRING))
return NULL;
5675 if (resolved_location) {
5676 resolved_location[0] = path;
5677 resolved_location[1] = beg_pos_lineno;
5678 resolved_location[2] = beg_pos_column;
5679 resolved_location[3] = end_pos_lineno;
5680 resolved_location[4] = end_pos_column;
5694 me = rb_resolve_me_location(me, 0);
5697 rcount = rb_hash_aref(me2counter, (
VALUE) me);
5705rb_get_coverages(
void)
5707 return GET_VM()->coverages;
5711rb_get_coverage_mode(
void)
5713 return GET_VM()->coverage_mode;
5717rb_set_coverages(
VALUE coverages,
int mode,
VALUE me2counter)
5719 GET_VM()->coverages = coverages;
5720 GET_VM()->me2counter = me2counter;
5721 GET_VM()->coverage_mode = mode;
5725rb_resume_coverages(
void)
5727 int mode = GET_VM()->coverage_mode;
5728 VALUE me2counter = GET_VM()->me2counter;
5729 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);
5730 if (mode & COVERAGE_TARGET_BRANCHES) {
5731 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);
5733 if (mode & COVERAGE_TARGET_METHODS) {
5739rb_suspend_coverages(
void)
5742 if (GET_VM()->coverage_mode & COVERAGE_TARGET_BRANCHES) {
5745 if (GET_VM()->coverage_mode & COVERAGE_TARGET_METHODS) {
5752rb_reset_coverages(
void)
5754 rb_clear_coverages();
5755 rb_iseq_remove_coverage_all();
5756 GET_VM()->coverages =
Qfalse;
5760rb_default_coverage(
int n)
5762 VALUE coverage = rb_ary_hidden_new_fill(3);
5764 int mode = GET_VM()->coverage_mode;
5766 if (mode & COVERAGE_TARGET_LINES) {
5767 lines = n > 0 ? rb_ary_hidden_new_fill(n) : rb_ary_hidden_new(0);
5769 RARRAY_ASET(coverage, COVERAGE_INDEX_LINES, lines);
5771 if (mode & COVERAGE_TARGET_BRANCHES) {
5772 branches = rb_ary_hidden_new_fill(2);
5794 VALUE structure = rb_hash_new();
5795 rb_obj_hide(structure);
5800 RARRAY_ASET(coverage, COVERAGE_INDEX_BRANCHES, branches);
5806uninterruptible_exit(
VALUE v)
5809 rb_ary_pop(cur_th->pending_interrupt_mask_stack);
5811 cur_th->pending_interrupt_queue_checked = 0;
5812 if (!rb_threadptr_pending_interrupt_empty_p(cur_th)) {
5813 RUBY_VM_SET_INTERRUPT(cur_th->ec);
5821 VALUE interrupt_mask = rb_ident_hash_new();
5824 rb_hash_aset(interrupt_mask, rb_cObject, sym_never);
5826 rb_ary_push(cur_th->pending_interrupt_mask_stack, interrupt_mask);
5830 RUBY_VM_CHECK_INTS(cur_th->ec);
5837 VM_ASSERT(th->specific_storage == NULL);
5839 if (UNLIKELY(specific_key_count > 0)) {
5840 th->specific_storage =
ZALLOC_N(
void *, RB_INTERNAL_THREAD_SPECIFIC_KEY_MAX);
5844rb_internal_thread_specific_key_t
5849 if (specific_key_count == 0 && vm->ractor.cnt > 1) {
5850 rb_raise(
rb_eThreadError,
"The first rb_internal_thread_specific_key_create() is called with multiple ractors");
5852 else if (specific_key_count > RB_INTERNAL_THREAD_SPECIFIC_KEY_MAX) {
5853 rb_raise(
rb_eThreadError,
"rb_internal_thread_specific_key_create() is called more than %d times", RB_INTERNAL_THREAD_SPECIFIC_KEY_MAX);
5856 rb_internal_thread_specific_key_t key = specific_key_count++;
5863 ccan_list_for_each(&cr->threads.set, th, lt_node) {
5864 thread_specific_storage_alloc(th);
5877 VM_ASSERT(rb_thread_ptr(thread_val) == th);
5878 VM_ASSERT(key < RB_INTERNAL_THREAD_SPECIFIC_KEY_MAX);
5879 VM_ASSERT(th->specific_storage);
5881 return th->specific_storage[key];
5890 VM_ASSERT(rb_thread_ptr(thread_val) == th);
5891 VM_ASSERT(key < RB_INTERNAL_THREAD_SPECIFIC_KEY_MAX);
5892 VM_ASSERT(th->specific_storage);
5894 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.