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"
79#include "internal/class.h"
80#include "internal/cont.h"
81#include "internal/error.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_MJIT && 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;
120 SLEEP_DEADLOCKABLE = 0x1,
121 SLEEP_SPURIOUS_CHECK = 0x2
124#define THREAD_LOCAL_STORAGE_INITIALISED FL_USER13
125#define THREAD_LOCAL_STORAGE_INITIALISED_P(th) RB_FL_TEST_RAW((th), THREAD_LOCAL_STORAGE_INITIALISED)
128rb_thread_local_storage(
VALUE thread)
130 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
132 RB_FL_SET_RAW(thread, THREAD_LOCAL_STORAGE_INITIALISED);
137static int sleep_hrtime(
rb_thread_t *, rb_hrtime_t,
unsigned int fl);
138static void sleep_forever(
rb_thread_t *th,
unsigned int fl);
139static void rb_thread_sleep_deadly_allow_spurious_wakeup(
VALUE blocker,
VALUE timeout, rb_hrtime_t end);
142static int rb_threadptr_pending_interrupt_empty_p(
const rb_thread_t *th);
143static const char *thread_status_name(
rb_thread_t *th,
int detail);
144static int hrtime_update_expire(rb_hrtime_t *,
const rb_hrtime_t);
145NORETURN(
static void async_bug_fd(
const char *mesg,
int errno_arg,
int fd));
146static int consume_communication_pipe(
int fd);
147static int check_signals_nogvl(
rb_thread_t *,
int sigwait_fd);
148void rb_sigwait_fd_migrate(
rb_vm_t *);
150#define eKillSignal INT2FIX(0)
151#define eTerminateSignal INT2FIX(1)
152static volatile int system_working = 1;
162#define THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
165 enum rb_thread_status prev_status;
169static void unblock_function_clear(
rb_thread_t *th);
175#define THREAD_BLOCKING_BEGIN(th) do { \
176 struct rb_thread_sched * const sched = TH_SCHED(th); \
177 RB_GC_SAVE_MACHINE_CONTEXT(th); \
178 thread_sched_to_waiting(sched);
180#define THREAD_BLOCKING_END(th) \
181 thread_sched_to_running(sched, th); \
182 rb_ractor_thread_switch(th->ractor, th); \
186#ifdef HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR_CONSTANT_P
187#define only_if_constant(expr, notconst) __builtin_choose_expr(__builtin_constant_p(expr), (expr), (notconst))
189#define only_if_constant(expr, notconst) (__builtin_constant_p(expr) ? (expr) : (notconst))
192#define only_if_constant(expr, notconst) notconst
194#define BLOCKING_REGION(th, exec, ubf, ubfarg, fail_if_interrupted) do { \
195 struct rb_blocking_region_buffer __region; \
196 if (blocking_region_begin(th, &__region, (ubf), (ubfarg), fail_if_interrupted) || \
198 !only_if_constant(fail_if_interrupted, TRUE)) { \
200 blocking_region_end(th, &__region); \
208#define RUBY_VM_CHECK_INTS_BLOCKING(ec) vm_check_ints_blocking(ec)
214 if (LIKELY(rb_threadptr_pending_interrupt_empty_p(th))) {
215 if (LIKELY(!RUBY_VM_INTERRUPTED_ANY(ec)))
return FALSE;
218 th->pending_interrupt_queue_checked = 0;
219 RUBY_VM_SET_INTERRUPT(ec);
221 return rb_threadptr_execute_interrupts(th, 1);
227 return vm_check_ints_blocking(ec);
235#if defined(HAVE_POLL)
236# if defined(__linux__)
239# if defined(__FreeBSD_version) && __FreeBSD_version >= 1100000
242# define POLLERR_SET (POLLHUP | POLLERR)
247timeout_prepare(rb_hrtime_t **to, rb_hrtime_t *rel, rb_hrtime_t *end,
251 *rel = rb_timeval2hrtime(timeout);
252 *end = rb_hrtime_add(rb_hrtime_now(), *rel);
260MAYBE_UNUSED(NOINLINE(
static int thread_start_func_2(
rb_thread_t *th,
VALUE *stack_start)));
261void ruby_sigchld_handler(
rb_vm_t *);
264ubf_sigwait(
void *ignore)
266 rb_thread_wakeup_timer_thread(0);
269#include THREAD_IMPL_SRC
276#ifndef BUSY_WAIT_SIGNALS
277# define BUSY_WAIT_SIGNALS (0)
281# define USE_EVENTFD (0)
284#include "thread_sync.c"
314 if (fail_if_interrupted) {
315 if (RUBY_VM_INTERRUPTED_ANY(th->ec)) {
320 RUBY_VM_CHECK_INTS(th->ec);
324 }
while (!th->ec->raised_flag && RUBY_VM_INTERRUPTED_ANY(th->ec) &&
327 VM_ASSERT(th->unblock.func == NULL);
329 th->unblock.func = func;
330 th->unblock.arg = arg;
340 th->unblock.func = 0;
345rb_threadptr_interrupt_common(
rb_thread_t *th,
int trap)
350 RUBY_VM_SET_TRAP_INTERRUPT(th->ec);
353 RUBY_VM_SET_INTERRUPT(th->ec);
355 if (th->unblock.func != NULL) {
356 (th->unblock.func)(th->unblock.arg);
367 rb_threadptr_interrupt_common(th, 0);
373 rb_threadptr_interrupt_common(th, 1);
381 ccan_list_for_each(&r->threads.set, th, lt_node) {
382 if (th != main_thread) {
383 RUBY_DEBUG_LOG(
"terminate start th:%u status:%s", rb_th_serial(th), thread_status_name(th, TRUE));
385 rb_threadptr_pending_interrupt_enque(th, eTerminateSignal);
386 rb_threadptr_interrupt(th);
388 RUBY_DEBUG_LOG(
"terminate done th:%u status:%s", rb_th_serial(th), thread_status_name(th, TRUE));
391 RUBY_DEBUG_LOG(
"main thread th:%u", rb_th_serial(th));
399 while (thread->join_list) {
403 thread->join_list = join_list->next;
407 if (target_thread->scheduler !=
Qnil && join_list->fiber) {
411 rb_threadptr_interrupt(target_thread);
413 switch (target_thread->status) {
415 case THREAD_STOPPED_FOREVER:
416 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);
521 native_thread_destroy(th);
530 native_thread_init_stack(th);
534rb_vm_proc_local_ep(
VALUE proc)
536 const VALUE *ep = vm_proc_ep(proc);
539 return rb_vm_ep_local_ep(ep);
548 int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler);
553 VALUE args = th->invoke_arg.proc.args;
554 const VALUE *args_ptr;
556 VALUE procval = th->invoke_arg.proc.proc;
558 GetProcPtr(procval, proc);
560 th->ec->errinfo =
Qnil;
561 th->ec->root_lep = rb_vm_proc_local_ep(procval);
562 th->ec->root_svar =
Qfalse;
564 vm_check_ints_blocking(th->ec);
566 if (th->invoke_type == thread_invoke_type_ractor_proc) {
567 VALUE self = rb_ractor_self(th->ractor);
571 rb_ractor_receive_parameters(th->ec, th->ractor, args_len, (
VALUE *)args_ptr);
572 vm_check_ints_blocking(th->ec);
574 return rb_vm_invoke_proc_with_self(
577 th->invoke_arg.proc.kw_splat,
578 VM_BLOCK_HANDLER_NONE
582 args_len = RARRAY_LENINT(args);
587 th->invoke_arg.proc.args =
Qnil;
593 vm_check_ints_blocking(th->ec);
595 return rb_vm_invoke_proc(
598 th->invoke_arg.proc.kw_splat,
599 VM_BLOCK_HANDLER_NONE
607 native_set_thread_name(th);
612 switch (th->invoke_type) {
613 case thread_invoke_type_proc:
614 result = thread_do_start_proc(th);
617 case thread_invoke_type_ractor_proc:
618 result = thread_do_start_proc(th);
619 rb_ractor_atexit(th->ec, result);
622 case thread_invoke_type_func:
623 result = (*th->invoke_arg.func.func)(th->invoke_arg.func.arg);
626 case thread_invoke_type_none:
638#define thread_sched_to_dead thread_sched_to_waiting
643 STACK_GROW_DIR_DETECTION;
644 enum ruby_tag_type state;
646 size_t size = th->vm->default_params.thread_vm_stack_size /
sizeof(
VALUE);
647 rb_thread_t *ractor_main_th = th->ractor->threads.main;
648 VALUE * vm_stack = NULL;
650 VM_ASSERT(th != th->vm->ractor.main_thread);
651 RUBY_DEBUG_LOG(
"th:%u", rb_th_serial(th));
654 thread_sched_to_running(TH_SCHED(th), th);
655 ruby_thread_set_native(th);
657 RUBY_DEBUG_LOG(
"got lock. th:%u", rb_th_serial(th));
660 if (rb_ractor_status_p(th->ractor, ractor_blocking)) {
663 rb_vm_ractor_blocking_cnt_dec(th->vm, th->ractor, __FILE__, __LINE__);
665 r->r_stdin = rb_io_prep_stdin();
666 r->r_stdout = rb_io_prep_stdout();
667 r->r_stderr = rb_io_prep_stderr();
676 vm_stack = alloca(size *
sizeof(
VALUE));
679 rb_ec_initialize_vm_stack(th->ec, vm_stack, size);
680 th->ec->machine.stack_start = STACK_DIR_UPPER(vm_stack + size, vm_stack);
681 th->ec->machine.stack_maxsize -= size *
sizeof(
VALUE);
684 VM_ASSERT(UNDEF_P(th->value));
688 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
689 SAVE_ROOT_JMPBUF(th, thread_do_start(th));
692 errinfo = th->ec->errinfo;
694 VALUE exc = rb_vm_make_jump_tag_but_local_jump(state,
Qundef);
695 if (!
NIL_P(exc)) errinfo = exc;
697 if (state == TAG_FATAL) {
698 if (th->invoke_type == thread_invoke_type_ractor_proc) {
699 rb_ractor_atexit(th->ec,
Qnil);
707 if (th->report_on_exception) {
708 VALUE mesg = rb_thread_to_s(th->self);
709 rb_str_cat_cstr(mesg,
" terminated with exception (report_on_exception is true):\n");
710 rb_write_error_str(mesg);
711 rb_ec_error_print(th->ec, errinfo);
714 if (th->invoke_type == thread_invoke_type_ractor_proc) {
715 rb_ractor_atexit_exception(th->ec);
718 if (th->vm->thread_abort_on_exception ||
730 VM_ASSERT(!UNDEF_P(th->value));
732 rb_threadptr_join_list_wakeup(th);
733 rb_threadptr_unlock_all_locking_mutexes(th);
735 if (th->invoke_type == thread_invoke_type_ractor_proc) {
736 rb_thread_terminate_all(th);
737 rb_ractor_teardown(th->ec);
740 th->status = THREAD_KILLED;
741 RUBY_DEBUG_LOG(
"killed th:%u", rb_th_serial(th));
743 if (th->vm->ractor.main_thread == th) {
749 rb_threadptr_raise(ractor_main_th, 1, &errinfo);
754 rb_ec_clear_current_thread_trace_func(th->ec);
757 if (th->locking_mutex !=
Qfalse) {
758 rb_bug(
"thread_start_func_2: locking_mutex must not be set (%p:%"PRIxVALUE
")",
759 (
void *)th, th->locking_mutex);
762 if (ractor_main_th->status == THREAD_KILLED &&
763 th->ractor->threads.cnt <= 2 ) {
765 rb_threadptr_interrupt(ractor_main_th);
768 rb_check_deadlock(th->ractor);
770 rb_fiber_close(th->ec->fiber_ptr);
772 thread_cleanup_func(th, FALSE);
773 VM_ASSERT(th->ec->vm_stack == NULL);
775 if (th->invoke_type == thread_invoke_type_ractor_proc) {
779 thread_sched_to_dead(TH_SCHED(th));
780 rb_ractor_living_threads_remove(th->ractor, th);
783 rb_ractor_living_threads_remove(th->ractor, th);
784 thread_sched_to_dead(TH_SCHED(th));
791 enum thread_invoke_type type;
808 rb_thread_t *th = rb_thread_ptr(thval), *current_th = rb_ec_thread_ptr(ec);
813 "can't start a new thread (frozen ThreadGroup)");
816 rb_fiber_inherit_storage(ec, th->ec->fiber_ptr);
818 switch (params->type) {
819 case thread_invoke_type_proc:
820 th->invoke_type = thread_invoke_type_proc;
821 th->invoke_arg.proc.args = params->args;
822 th->invoke_arg.proc.proc = params->proc;
826 case thread_invoke_type_ractor_proc:
827#if RACTOR_CHECK_MODE > 0
828 rb_ractor_setup_belonging_to(thval, rb_ractor_id(params->g));
830 th->invoke_type = thread_invoke_type_ractor_proc;
831 th->ractor = params->g;
832 th->ractor->threads.main = th;
833 th->invoke_arg.proc.proc = rb_proc_isolate_bang(params->proc);
834 th->invoke_arg.proc.args =
INT2FIX(RARRAY_LENINT(params->args));
836 rb_ractor_send_parameters(ec, params->g, params->args);
839 case thread_invoke_type_func:
840 th->invoke_type = thread_invoke_type_func;
841 th->invoke_arg.func.func = params->fn;
842 th->invoke_arg.func.arg = (
void *)params->args;
849 th->priority = current_th->priority;
850 th->thgroup = current_th->thgroup;
852 th->pending_interrupt_queue = rb_ary_hidden_new(0);
853 th->pending_interrupt_queue_checked = 0;
854 th->pending_interrupt_mask_stack = rb_ary_dup(current_th->pending_interrupt_mask_stack);
855 RBASIC_CLEAR_CLASS(th->pending_interrupt_mask_stack);
859 RUBY_DEBUG_LOG(
"r:%u th:%u", rb_ractor_id(th->ractor), rb_th_serial(th));
861 rb_ractor_living_threads_insert(th->ractor, th);
864 err = native_thread_create(th);
866 th->status = THREAD_KILLED;
867 rb_ractor_living_threads_remove(th->ractor, th);
873#define threadptr_initialized(th) ((th)->invoke_type != thread_invoke_type_none)
896thread_s_new(
int argc,
VALUE *argv,
VALUE klass)
899 VALUE thread = rb_thread_alloc(klass);
901 if (GET_RACTOR()->threads.main->status == THREAD_KILLED) {
906 th = rb_thread_ptr(thread);
907 if (!threadptr_initialized(th)) {
928 .type = thread_invoke_type_proc,
932 return thread_create_core(rb_thread_alloc(klass), ¶ms);
938 if (th->invoke_type == thread_invoke_type_proc) {
939 return rb_proc_location(th->invoke_arg.proc.proc);
955 else if (th->invoke_type != thread_invoke_type_none) {
956 VALUE loc = threadptr_invoke_proc_location(th);
959 "already initialized thread - %"PRIsVALUE
":%"PRIsVALUE,
968 .type = thread_invoke_type_proc,
972 return thread_create_core(thread, ¶ms);
980 .type = thread_invoke_type_func,
984 return thread_create_core(rb_thread_alloc(
rb_cThread), ¶ms);
991 .type = thread_invoke_type_ractor_proc,
996 return thread_create_core(rb_thread_alloc(
rb_cThread), ¶ms);
1008remove_from_join_list(
VALUE arg)
1013 if (target_thread->status != THREAD_KILLED) {
1016 while (*join_list) {
1017 if (*join_list == p->waiter) {
1018 *join_list = (*join_list)->next;
1022 join_list = &(*join_list)->next;
1032 return th->status == THREAD_KILLED || !UNDEF_P(th->value);
1036thread_join_sleep(
VALUE arg)
1039 rb_thread_t *target_th = p->target, *th = p->waiter->thread;
1040 rb_hrtime_t end = 0, *limit = p->limit;
1043 end = rb_hrtime_add(*limit, rb_hrtime_now());
1046 while (!thread_finished(target_th)) {
1049 if (scheduler !=
Qnil) {
1053 th->status = THREAD_STOPPED_FOREVER;
1054 rb_ractor_sleeper_threads_inc(th->ractor);
1055 rb_check_deadlock(th->ractor);
1056 native_sleep(th, 0);
1057 rb_ractor_sleeper_threads_dec(th->ractor);
1060 if (hrtime_update_expire(limit, end)) {
1061 RUBY_DEBUG_LOG(
"timeout target_th:%u", rb_th_serial(target_th));
1064 th->status = THREAD_STOPPED;
1065 native_sleep(th, limit);
1067 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1068 th->status = THREAD_RUNNABLE;
1070 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) {
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;
1182 if (rb_check_arity(argc, 0, 1)) {
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);
1262 enum rb_thread_status prev_status = th->status;
1263 enum rb_thread_status status;
1266 status = fl & SLEEP_DEADLOCKABLE ? THREAD_STOPPED_FOREVER : THREAD_STOPPED;
1267 th->status = status;
1268 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1269 while (th->status == status) {
1270 if (fl & SLEEP_DEADLOCKABLE) {
1271 rb_ractor_sleeper_threads_inc(th->ractor);
1272 rb_check_deadlock(th->ractor);
1274 native_sleep(th, 0);
1275 if (fl & SLEEP_DEADLOCKABLE) {
1276 rb_ractor_sleeper_threads_dec(th->ractor);
1278 woke = vm_check_ints_blocking(th->ec);
1279 if (woke && !(fl & SLEEP_SPURIOUS_CHECK))
1282 th->status = prev_status;
1289COMPILER_WARNING_PUSH
1290#if defined(__GNUC__) && __GNUC__ == 7 && __GNUC_MINOR__ <= 3
1291COMPILER_WARNING_IGNORED(-Wmaybe-uninitialized)
1294#define PRIu64 PRI_64_PREFIX "u"
1302hrtime_update_expire(rb_hrtime_t *timeout,
const rb_hrtime_t end)
1304 rb_hrtime_t now = rb_hrtime_now();
1306 if (now > end)
return 1;
1308 RUBY_DEBUG_LOG(
"%"PRIu64
" > %"PRIu64
"", (uint64_t)end, (uint64_t)now);
1310 *timeout = end - now;
1316sleep_hrtime(
rb_thread_t *th, rb_hrtime_t rel,
unsigned int fl)
1318 enum rb_thread_status prev_status = th->status;
1320 rb_hrtime_t end = rb_hrtime_add(rb_hrtime_now(), rel);
1322 th->status = THREAD_STOPPED;
1323 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1324 while (th->status == THREAD_STOPPED) {
1325 native_sleep(th, &rel);
1326 woke = vm_check_ints_blocking(th->ec);
1327 if (woke && !(fl & SLEEP_SPURIOUS_CHECK))
1329 if (hrtime_update_expire(&rel, end))
1333 th->status = prev_status;
1338sleep_hrtime_until(
rb_thread_t *th, rb_hrtime_t end,
unsigned int fl)
1340 enum rb_thread_status prev_status = th->status;
1342 rb_hrtime_t rel = rb_hrtime_sub(end, rb_hrtime_now());
1344 th->status = THREAD_STOPPED;
1345 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1346 while (th->status == THREAD_STOPPED) {
1347 native_sleep(th, &rel);
1348 woke = vm_check_ints_blocking(th->ec);
1349 if (woke && !(fl & SLEEP_SPURIOUS_CHECK))
1351 if (hrtime_update_expire(&rel, end))
1355 th->status = prev_status;
1363 sleep_forever(GET_THREAD(), SLEEP_SPURIOUS_CHECK);
1370 sleep_forever(GET_THREAD(), SLEEP_DEADLOCKABLE|SLEEP_SPURIOUS_CHECK);
1374rb_thread_sleep_interruptible(
void)
1377 enum rb_thread_status prev_status = th->status;
1379 th->status = THREAD_STOPPED;
1380 native_sleep(th, 0);
1381 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1382 th->status = prev_status;
1386rb_thread_sleep_deadly_allow_spurious_wakeup(
VALUE blocker,
VALUE timeout, rb_hrtime_t end)
1389 if (scheduler !=
Qnil) {
1395 sleep_hrtime_until(GET_THREAD(), end, SLEEP_SPURIOUS_CHECK);
1398 sleep_forever(GET_THREAD(), SLEEP_DEADLOCKABLE);
1408 sleep_hrtime(th, rb_timeval2hrtime(&time), SLEEP_SPURIOUS_CHECK);
1421 RUBY_VM_CHECK_INTS_BLOCKING(GET_EC());
1429rb_thread_check_trap_pending(
void)
1431 return rb_signal_buff_size() != 0;
1438 return (
int)RUBY_VM_INTERRUPTED(rb_thread_ptr(thval)->ec);
1448rb_thread_schedule_limits(uint32_t limits_us)
1452 RUBY_DEBUG_LOG(
"us:%u", (
unsigned int)limits_us);
1454 if (th->running_time_us >= limits_us) {
1455 RUBY_DEBUG_LOG(
"switch %s",
"start");
1457 RB_GC_SAVE_MACHINE_CONTEXT(th);
1458 thread_sched_yield(TH_SCHED(th), th);
1459 rb_ractor_thread_switch(th->ractor, th);
1461 RUBY_DEBUG_LOG(
"switch %s",
"done");
1469 rb_thread_schedule_limits(0);
1470 RUBY_VM_CHECK_INTS(GET_EC());
1479#ifdef RUBY_VM_CRITICAL_SECTION
1480 VM_ASSERT(ruby_assert_critical_section_entered == 0);
1482 VM_ASSERT(th == GET_THREAD());
1484 region->prev_status = th->status;
1485 if (unblock_function_set(th, ubf, arg, fail_if_interrupted)) {
1486 th->blocking_region_buffer = region;
1487 th->status = THREAD_STOPPED;
1488 rb_ractor_blocking_threads_inc(th->ractor, __FILE__, __LINE__);
1492 RB_GC_SAVE_MACHINE_CONTEXT(th);
1493 thread_sched_to_waiting(TH_SCHED(th));
1505 unblock_function_clear(th);
1507 unregister_ubf_list(th);
1509 thread_sched_to_running(TH_SCHED(th), th);
1510 rb_ractor_thread_switch(th->ractor, th);
1512 th->blocking_region_buffer = 0;
1513 rb_ractor_blocking_threads_dec(th->ractor, __FILE__, __LINE__);
1514 if (th->status == THREAD_STOPPED) {
1515 th->status = region->prev_status;
1519 VM_ASSERT(th == GET_THREAD());
1530 rb_vm_t *vm = rb_ec_vm_ptr(ec);
1531 bool is_main_thread = vm->ractor.main_thread == th;
1532 int saved_errno = 0;
1539 else if (ubf && rb_ractor_living_thread_num(th->ractor) == 1 && is_main_thread) {
1541 vm->ubf_async_safe = 1;
1544 ubf_th = rb_thread_start_unblock_thread();
1548 BLOCKING_REGION(th, {
1550 saved_errno = errno;
1553 if (is_main_thread) vm->ubf_async_safe = 0;
1556 RUBY_VM_CHECK_INTS_BLOCKING(ec);
1563 errno = saved_errno;
1664 return rb_nogvl(func, data1, ubf, data2, 0);
1668rb_thread_io_blocking_region(rb_blocking_function_t *func,
void *data1,
int fd)
1672 volatile int saved_errno = 0;
1673 enum ruby_tag_type state;
1677 .th = rb_ec_thread_ptr(ec)
1688 ccan_list_add(&rb_ec_vm_ptr(ec)->waiting_fds, &
waiting_fd.wfd_node);
1693 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1696 saved_errno = errno;
1712 EC_JUMP_TAG(ec, state);
1715 RUBY_VM_CHECK_INTS_BLOCKING(ec);
1718 if (saved_errno == ETIMEDOUT) {
1722 errno = saved_errno;
1769 fprintf(stderr,
"[BUG] rb_thread_call_with_gvl() is called by non-ruby thread\n");
1774 prev_unblock = th->unblock;
1777 rb_bug(
"rb_thread_call_with_gvl: called by a thread which has GVL.");
1780 blocking_region_end(th, brb);
1784 int released = blocking_region_begin(th, brb, prev_unblock.func, prev_unblock.arg, FALSE);
1799ruby_thread_has_gvl_p(
void)
1803 if (th && th->blocking_region_buffer == 0) {
1820thread_s_pass(
VALUE klass)
1845rb_threadptr_pending_interrupt_clear(
rb_thread_t *th)
1847 rb_ary_clear(th->pending_interrupt_queue);
1853 rb_ary_push(th->pending_interrupt_queue, v);
1854 th->pending_interrupt_queue_checked = 0;
1858threadptr_check_pending_interrupt_queue(
rb_thread_t *th)
1860 if (!th->pending_interrupt_queue) {
1865enum handle_interrupt_timing {
1867 INTERRUPT_IMMEDIATE,
1868 INTERRUPT_ON_BLOCKING,
1872static enum handle_interrupt_timing
1876 long mask_stack_len =
RARRAY_LEN(th->pending_interrupt_mask_stack);
1881 for (i=0; i<mask_stack_len; i++) {
1882 mask = mask_stack[mask_stack_len-(i+1)];
1889 klass =
RBASIC(mod)->klass;
1891 else if (mod != RCLASS_ORIGIN(mod)) {
1895 if ((sym = rb_hash_aref(mask, klass)) !=
Qnil) {
1896 if (sym == sym_immediate) {
1897 return INTERRUPT_IMMEDIATE;
1899 else if (sym == sym_on_blocking) {
1900 return INTERRUPT_ON_BLOCKING;
1902 else if (sym == sym_never) {
1903 return INTERRUPT_NEVER;
1912 return INTERRUPT_NONE;
1916rb_threadptr_pending_interrupt_empty_p(
const rb_thread_t *th)
1918 return RARRAY_LEN(th->pending_interrupt_queue) == 0;
1925 for (i=0; i<
RARRAY_LEN(th->pending_interrupt_queue); i++) {
1935rb_threadptr_pending_interrupt_deque(
rb_thread_t *th,
enum handle_interrupt_timing timing)
1940 for (i=0; i<
RARRAY_LEN(th->pending_interrupt_queue); i++) {
1943 enum handle_interrupt_timing mask_timing = rb_threadptr_pending_interrupt_check_mask(th,
CLASS_OF(err));
1945 switch (mask_timing) {
1946 case INTERRUPT_ON_BLOCKING:
1947 if (timing != INTERRUPT_ON_BLOCKING) {
1951 case INTERRUPT_NONE:
1952 case INTERRUPT_IMMEDIATE:
1953 rb_ary_delete_at(th->pending_interrupt_queue, i);
1955 case INTERRUPT_NEVER:
1960 th->pending_interrupt_queue_checked = 1;
1963 VALUE err = rb_ary_shift(th->pending_interrupt_queue);
1964 if (rb_threadptr_pending_interrupt_empty_p(th)) {
1965 th->pending_interrupt_queue_checked = 1;
1972threadptr_pending_interrupt_active_p(
rb_thread_t *th)
1979 if (th->pending_interrupt_queue_checked) {
1983 if (rb_threadptr_pending_interrupt_empty_p(th)) {
1995 if (val != sym_immediate && val != sym_on_blocking && val != sym_never) {
2000 *maskp = rb_ident_hash_new();
2002 rb_hash_aset(*maskp, key, val);
2116rb_thread_s_handle_interrupt(
VALUE self,
VALUE mask_arg)
2122 enum ruby_tag_type state;
2129 mask_arg = rb_to_hash_type(mask_arg);
2135 rb_ary_push(th->pending_interrupt_mask_stack, mask);
2136 if (!rb_threadptr_pending_interrupt_empty_p(th)) {
2137 th->pending_interrupt_queue_checked = 0;
2138 RUBY_VM_SET_INTERRUPT(th->ec);
2141 EC_PUSH_TAG(th->ec);
2142 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2147 rb_ary_pop(th->pending_interrupt_mask_stack);
2148 if (!rb_threadptr_pending_interrupt_empty_p(th)) {
2149 th->pending_interrupt_queue_checked = 0;
2150 RUBY_VM_SET_INTERRUPT(th->ec);
2153 RUBY_VM_CHECK_INTS(th->ec);
2156 EC_JUMP_TAG(th->ec, state);
2173rb_thread_pending_interrupt_p(
int argc,
VALUE *argv,
VALUE target_thread)
2175 rb_thread_t *target_th = rb_thread_ptr(target_thread);
2177 if (!target_th->pending_interrupt_queue) {
2180 if (rb_threadptr_pending_interrupt_empty_p(target_th)) {
2183 if (rb_check_arity(argc, 0, 1)) {
2184 VALUE err = argv[0];
2188 return RBOOL(rb_threadptr_pending_interrupt_include_p(target_th, err));
2253rb_thread_s_pending_interrupt_p(
int argc,
VALUE *argv,
VALUE self)
2255 return rb_thread_pending_interrupt_p(argc, argv, GET_THREAD()->self);
2258NORETURN(
static void rb_threadptr_to_kill(
rb_thread_t *th));
2263 rb_threadptr_pending_interrupt_clear(th);
2264 th->status = THREAD_RUNNABLE;
2266 th->ec->errinfo =
INT2FIX(TAG_FATAL);
2267 EC_JUMP_TAG(th->ec, TAG_FATAL);
2278 interrupt = ec->interrupt_flag;
2279 old = ATOMIC_CAS(ec->interrupt_flag, interrupt, interrupt & ec->interrupt_mask);
2280 }
while (old != interrupt);
2281 return interrupt & (
rb_atomic_t)~ec->interrupt_mask;
2286extern bool mjit_waitpid_finished;
2287extern int mjit_waitpid_status;
2290MJIT_FUNC_EXPORTED
int
2291rb_threadptr_execute_interrupts(
rb_thread_t *th,
int blocking_timing)
2294 int postponed_job_interrupt = 0;
2297 if (th->ec->raised_flag)
return ret;
2299 while ((interrupt = threadptr_get_interrupts(th)) != 0) {
2301 int timer_interrupt;
2302 int pending_interrupt;
2304 int terminate_interrupt;
2306 timer_interrupt = interrupt & TIMER_INTERRUPT_MASK;
2307 pending_interrupt = interrupt & PENDING_INTERRUPT_MASK;
2308 postponed_job_interrupt = interrupt & POSTPONED_JOB_INTERRUPT_MASK;
2309 trap_interrupt = interrupt & TRAP_INTERRUPT_MASK;
2310 terminate_interrupt = interrupt & TERMINATE_INTERRUPT_MASK;
2312 if (interrupt & VM_BARRIER_INTERRUPT_MASK) {
2317 if (postponed_job_interrupt) {
2318 rb_postponed_job_flush(th->vm);
2322 if (trap_interrupt && (th == th->vm->ractor.main_thread)) {
2323 enum rb_thread_status prev_status = th->status;
2324 int sigwait_fd = rb_sigwait_fd_get(th);
2326 if (sigwait_fd >= 0) {
2327 (void)consume_communication_pipe(sigwait_fd);
2328 ruby_sigchld_handler(th->vm);
2329 rb_sigwait_fd_put(th, sigwait_fd);
2330 rb_sigwait_fd_migrate(th->vm);
2332 th->status = THREAD_RUNNABLE;
2333 while ((sig = rb_get_next_signal()) != 0) {
2334 ret |= rb_signal_exec(th, sig);
2336 th->status = prev_status;
2342 if (mjit_waitpid_finished && th == th->vm->ractor.main_thread) {
2343 mjit_waitpid_finished =
false;
2344 mjit_notify_waitpid(WIFEXITED(mjit_waitpid_status) ? WEXITSTATUS(mjit_waitpid_status) : -1);
2349 if (pending_interrupt && threadptr_pending_interrupt_active_p(th)) {
2350 VALUE err = rb_threadptr_pending_interrupt_deque(th, blocking_timing ? INTERRUPT_ON_BLOCKING : INTERRUPT_NONE);
2351 RUBY_DEBUG_LOG(
"err:%"PRIdVALUE
"\n", err);
2357 else if (err == eKillSignal ||
2358 err == eTerminateSignal ||
2360 terminate_interrupt = 1;
2363 if (err == th->vm->special_exceptions[ruby_error_stream_closed]) {
2365 err = ruby_vm_special_exception_copy(err);
2368 if (th->status == THREAD_STOPPED ||
2369 th->status == THREAD_STOPPED_FOREVER)
2370 th->status = THREAD_RUNNABLE;
2375 if (terminate_interrupt) {
2376 rb_threadptr_to_kill(th);
2379 if (timer_interrupt) {
2380 uint32_t limits_us = TIME_QUANTUM_USEC;
2382 if (th->priority > 0)
2383 limits_us <<= th->priority;
2385 limits_us >>= -th->priority;
2387 if (th->status == THREAD_RUNNABLE)
2388 th->running_time_us += TIME_QUANTUM_USEC;
2390 VM_ASSERT(th->ec->cfp);
2394 rb_thread_schedule_limits(limits_us);
2401rb_thread_execute_interrupts(
VALUE thval)
2403 rb_threadptr_execute_interrupts(rb_thread_ptr(thval), 1);
2409 rb_threadptr_interrupt(th);
2417 if (rb_threadptr_dead(target_th)) {
2425 exc = rb_make_exception(argc, argv);
2430 if (rb_threadptr_dead(target_th)) {
2434 rb_ec_setup_exception(GET_EC(), exc,
Qundef);
2435 rb_threadptr_pending_interrupt_enque(target_th, exc);
2436 rb_threadptr_interrupt(target_th);
2441rb_threadptr_signal_raise(
rb_thread_t *th,
int sig)
2447 rb_threadptr_raise(th->vm->ractor.main_thread, 2, argv);
2459 rb_threadptr_raise(th->vm->ractor.main_thread, 2, argv);
2465 if (ec->raised_flag & RAISED_EXCEPTION) {
2468 ec->raised_flag |= RAISED_EXCEPTION;
2475 if (!(ec->raised_flag & RAISED_EXCEPTION)) {
2478 ec->raised_flag &= ~RAISED_EXCEPTION;
2485 rb_vm_t *vm = GET_THREAD()->vm;
2490 ccan_list_for_each_safe(&vm->waiting_fds, wfd, next, wfd_node) {
2491 if (wfd->fd == fd) {
2495 ccan_list_del(&wfd->wfd_node);
2496 ccan_list_add(busy, &wfd->wfd_node);
2498 err = th->vm->special_exceptions[ruby_error_stream_closed];
2499 rb_threadptr_pending_interrupt_enque(th, err);
2500 rb_threadptr_interrupt(th);
2506 return !ccan_list_empty(busy);
2514 ccan_list_head_init(&busy);
2515 if (rb_notify_fd_close(fd, &busy)) {
2542thread_raise_m(
int argc,
VALUE *argv,
VALUE self)
2547 threadptr_check_pending_interrupt_queue(target_th);
2548 rb_threadptr_raise(target_th, argc, argv);
2551 if (current_th == target_th) {
2552 RUBY_VM_CHECK_INTS(target_th->ec);
2574 if (target_th->to_kill || target_th->status == THREAD_KILLED) {
2577 if (target_th == target_th->vm->ractor.main_thread) {
2581 RUBY_DEBUG_LOG(
"target_th:%u", rb_th_serial(target_th));
2583 if (target_th == GET_THREAD()) {
2585 rb_threadptr_to_kill(target_th);
2588 threadptr_check_pending_interrupt_queue(target_th);
2589 rb_threadptr_pending_interrupt_enque(target_th, eKillSignal);
2590 rb_threadptr_interrupt(target_th);
2597rb_thread_to_be_killed(
VALUE thread)
2601 if (target_th->to_kill || target_th->status == THREAD_KILLED) {
2677 if (target_th->status == THREAD_KILLED)
return Qnil;
2679 rb_threadptr_ready(target_th);
2681 if (target_th->status == THREAD_STOPPED ||
2682 target_th->status == THREAD_STOPPED_FOREVER) {
2683 target_th->status = THREAD_RUNNABLE;
2725 "stopping only thread\n\tnote: use sleep to stop forever");
2758 return rb_ractor_thread_list(GET_RACTOR());
2784 return rb_thread_list();
2790 return GET_THREAD()->self;
2803thread_s_current(
VALUE klass)
2811 return GET_RACTOR()->threads.main->self;
2822rb_thread_s_main(
VALUE klass)
2849rb_thread_s_abort_exc(
VALUE _)
2851 return RBOOL(GET_THREAD()->vm->thread_abort_on_exception);
2886rb_thread_s_abort_exc_set(
VALUE self,
VALUE val)
2888 GET_THREAD()->vm->thread_abort_on_exception =
RTEST(val);
2909rb_thread_abort_exc(
VALUE thread)
2911 return RBOOL(rb_thread_ptr(thread)->abort_on_exception);
2929rb_thread_abort_exc_set(
VALUE thread,
VALUE val)
2931 rb_thread_ptr(thread)->abort_on_exception =
RTEST(val);
2979rb_thread_s_report_exc(
VALUE _)
2981 return RBOOL(GET_THREAD()->vm->thread_report_on_exception);
3016rb_thread_s_report_exc_set(
VALUE self,
VALUE val)
3018 GET_THREAD()->vm->thread_report_on_exception =
RTEST(val);
3035rb_thread_s_ignore_deadlock(
VALUE _)
3037 return RBOOL(GET_THREAD()->vm->thread_ignore_deadlock);
3062rb_thread_s_ignore_deadlock_set(
VALUE self,
VALUE val)
3064 GET_THREAD()->vm->thread_ignore_deadlock =
RTEST(val);
3086rb_thread_report_exc(
VALUE thread)
3088 return RBOOL(rb_thread_ptr(thread)->report_on_exception);
3106rb_thread_report_exc_set(
VALUE thread,
VALUE val)
3108 rb_thread_ptr(thread)->report_on_exception =
RTEST(val);
3123rb_thread_group(
VALUE thread)
3125 return rb_thread_ptr(thread)->thgroup;
3131 switch (th->status) {
3132 case THREAD_RUNNABLE:
3133 return th->to_kill ?
"aborting" :
"run";
3134 case THREAD_STOPPED_FOREVER:
3135 if (detail)
return "sleep_forever";
3136 case THREAD_STOPPED:
3148 return th->status == THREAD_KILLED;
3184rb_thread_status(
VALUE thread)
3188 if (rb_threadptr_dead(target_th)) {
3189 if (!
NIL_P(target_th->ec->errinfo) &&
3190 !
FIXNUM_P(target_th->ec->errinfo)) {
3198 return rb_str_new2(thread_status_name(target_th, FALSE));
3218rb_thread_alive_p(
VALUE thread)
3220 return RBOOL(!thread_finished(rb_thread_ptr(thread)));
3238rb_thread_stop_p(
VALUE thread)
3242 if (rb_threadptr_dead(th)) {
3245 return RBOOL(th->status == THREAD_STOPPED || th->status == THREAD_STOPPED_FOREVER);
3256rb_thread_getname(
VALUE thread)
3258 return rb_thread_ptr(thread)->name;
3277 enc = rb_enc_get(name);
3278 if (!rb_enc_asciicompat(enc)) {
3284 target_th->name = name;
3285 if (threadptr_initialized(target_th)) {
3286 native_set_another_thread_name(target_th->nt->thread_id, name);
3291#if USE_NATIVE_THREAD_NATIVE_THREAD_ID
3315rb_thread_native_thread_id(
VALUE thread)
3318 if (rb_threadptr_dead(target_th))
return Qnil;
3319 return native_thread_native_thread_id(target_th);
3322# define rb_thread_native_thread_id rb_f_notimplement
3333rb_thread_to_s(
VALUE thread)
3340 status = thread_status_name(target_th, TRUE);
3341 str =
rb_sprintf(
"#<%"PRIsVALUE
":%p", cname, (
void *)thread);
3342 if (!
NIL_P(target_th->name)) {
3345 if ((loc = threadptr_invoke_proc_location(target_th)) !=
Qnil) {
3355#define recursive_key id__recursive_key__
3360 if (
id == recursive_key) {
3361 return th->ec->local_storage_recursive_hash;
3365 struct rb_id_table *local_storage = th->ec->local_storage;
3367 if (local_storage != NULL && rb_id_table_lookup(local_storage,
id, &val)) {
3379 return threadptr_local_aref(rb_thread_ptr(thread),
id);
3446 if (!
id)
return Qnil;
3464rb_thread_fetch(
int argc,
VALUE *argv,
VALUE self)
3471 rb_check_arity(argc, 1, 2);
3475 if (block_given && argc == 2) {
3476 rb_warn(
"block supersedes default value argument");
3481 if (
id == recursive_key) {
3482 return target_th->ec->local_storage_recursive_hash;
3484 else if (
id && target_th->ec->local_storage &&
3485 rb_id_table_lookup(target_th->ec->local_storage,
id, &val)) {
3488 else if (block_given) {
3491 else if (argc == 1) {
3492 rb_key_err_raise(
rb_sprintf(
"key not found: %+"PRIsVALUE, key), self, key);
3502 if (
id == recursive_key) {
3503 th->ec->local_storage_recursive_hash = val;
3507 struct rb_id_table *local_storage = th->ec->local_storage;
3510 if (!local_storage)
return Qnil;
3511 rb_id_table_delete(local_storage,
id);
3515 if (local_storage == NULL) {
3516 th->ec->local_storage = local_storage = rb_id_table_create(0);
3518 rb_id_table_insert(local_storage,
id, val);
3531 return threadptr_local_aset(rb_thread_ptr(thread),
id, val);
3582rb_thread_variable_get(
VALUE thread,
VALUE key)
3586 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
3589 locals = rb_thread_local_storage(thread);
3611 locals = rb_thread_local_storage(thread);
3633 struct rb_id_table *local_storage = rb_thread_ptr(self)->ec->local_storage;
3635 if (!
id || local_storage == NULL) {
3638 return RBOOL(rb_id_table_lookup(local_storage,
id, &val));
3641static enum rb_id_table_iterator_result
3642thread_keys_i(
ID key,
VALUE value,
void *ary)
3645 return ID_TABLE_CONTINUE;
3652 return rb_ractor_living_thread_num(GET_RACTOR()) == 1;
3670rb_thread_keys(
VALUE self)
3672 struct rb_id_table *local_storage = rb_thread_ptr(self)->ec->local_storage;
3673 VALUE ary = rb_ary_new();
3675 if (local_storage) {
3676 rb_id_table_foreach(local_storage, thread_keys_i, (
void *)ary);
3684 rb_ary_push(ary, key);
3706rb_thread_variables(
VALUE thread)
3712 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
3715 locals = rb_thread_local_storage(thread);
3742 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
3745 locals = rb_thread_local_storage(thread);
3766rb_thread_priority(
VALUE thread)
3768 return INT2NUM(rb_thread_ptr(thread)->priority);
3799rb_thread_priority_set(
VALUE thread,
VALUE prio)
3804#if USE_NATIVE_THREAD_PRIORITY
3805 target_th->priority =
NUM2INT(prio);
3806 native_thread_apply_priority(th);
3809 if (priority > RUBY_THREAD_PRIORITY_MAX) {
3810 priority = RUBY_THREAD_PRIORITY_MAX;
3812 else if (priority < RUBY_THREAD_PRIORITY_MIN) {
3813 priority = RUBY_THREAD_PRIORITY_MIN;
3815 target_th->priority = (int8_t)priority;
3817 return INT2NUM(target_th->priority);
3822#if defined(NFDBITS) && defined(HAVE_RB_FD_INIT)
3858 FD_ZERO(fds->
fdset);
3864 size_t size = howmany(rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
3866 if (size <
sizeof(
fd_set))
3868 dst->maxfd = src->maxfd;
3870 memcpy(dst->fdset, src->fdset, size);
3891 size_t m = howmany(n + 1, NFDBITS) *
sizeof(fd_mask);
3892 size_t o = howmany(fds->
maxfd, NFDBITS) *
sizeof(fd_mask);
3899 memset((
char *)fds->
fdset + o, 0, m - o);
3908 FD_SET(n, fds->
fdset);
3914 if (n >= fds->
maxfd)
return;
3915 FD_CLR(n, fds->
fdset);
3921 if (n >= fds->
maxfd)
return 0;
3922 return FD_ISSET(n, fds->
fdset) != 0;
3928 size_t size = howmany(max, NFDBITS) *
sizeof(fd_mask);
3932 dst->fdset =
xrealloc(dst->fdset, size);
3933 memcpy(dst->fdset, src, size);
3939 size_t size = howmany(rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
3941 if (size <
sizeof(
fd_set))
3943 dst->maxfd = src->maxfd;
3944 dst->fdset =
xrealloc(dst->fdset, size);
3945 memcpy(dst->fdset, src->fdset, size);
3951 fd_set *r = NULL, *w = NULL, *e = NULL;
3954 r = rb_fd_ptr(readfds);
3958 w = rb_fd_ptr(writefds);
3962 e = rb_fd_ptr(exceptfds);
3964 return select(n, r, w, e, timeout);
3967#define rb_fd_no_init(fds) ((void)((fds)->fdset = 0), (void)((fds)->maxfd = 0))
3974#define FD_ZERO(f) rb_fd_zero(f)
3975#define FD_SET(i, f) rb_fd_set((i), (f))
3976#define FD_CLR(i, f) rb_fd_clr((i), (f))
3977#define FD_ISSET(i, f) rb_fd_isset((i), (f))
3979#elif defined(_WIN32)
3984 set->
capa = FD_SETSIZE;
3986 FD_ZERO(set->
fdset);
4008 SOCKET s = rb_w32_get_osfhandle(fd);
4010 for (i = 0; i < set->
fdset->fd_count; i++) {
4011 if (set->
fdset->fd_array[i] == s) {
4015 if (set->
fdset->fd_count >= (
unsigned)set->
capa) {
4016 set->
capa = (set->
fdset->fd_count / FD_SETSIZE + 1) * FD_SETSIZE;
4018 rb_xrealloc_mul_add(
4019 set->
fdset, set->
capa,
sizeof(SOCKET),
sizeof(
unsigned int));
4021 set->
fdset->fd_array[set->
fdset->fd_count++] = s;
4029#define FD_ZERO(f) rb_fd_zero(f)
4030#define FD_SET(i, f) rb_fd_set((i), (f))
4031#define FD_CLR(i, f) rb_fd_clr((i), (f))
4032#define FD_ISSET(i, f) rb_fd_isset((i), (f))
4034#define rb_fd_no_init(fds) (void)((fds)->fdset = 0)
4038#ifndef rb_fd_no_init
4039#define rb_fd_no_init(fds) (void)(fds)
4043wait_retryable(
int *result,
int errnum, rb_hrtime_t *rel, rb_hrtime_t end)
4052 if (rel && hrtime_update_expire(rel, end)) {
4059 else if (*result == 0) {
4062 return !hrtime_update_expire(rel, end);
4083select_set_free(
VALUE p)
4087 if (set->sigwait_fd >= 0) {
4088 rb_sigwait_fd_put(set->th, set->sigwait_fd);
4089 rb_sigwait_fd_migrate(set->th->vm);
4099static const rb_hrtime_t *
4100sigwait_timeout(
rb_thread_t *th,
int sigwait_fd,
const rb_hrtime_t *orig,
4103 static const rb_hrtime_t quantum = TIME_QUANTUM_USEC * 1000;
4105 if (sigwait_fd >= 0 && (!ubf_threads_empty() || BUSY_WAIT_SIGNALS)) {
4106 *drained_p = check_signals_nogvl(th, sigwait_fd);
4107 if (!orig || *orig > quantum)
4114#define sigwait_signals_fd(result, cond, sigwait_fd) \
4115 (result > 0 && (cond) ? (result--, (sigwait_fd)) : -1)
4123 rb_hrtime_t *to, rel, end = 0;
4125 timeout_prepare(&to, &rel, &end, set->timeout);
4126#define restore_fdset(dst, src) \
4127 ((dst) ? rb_fd_dup(dst, src) : (void)0)
4128#define do_select_update() \
4129 (restore_fdset(set->rset, &set->orig_rset), \
4130 restore_fdset(set->wset, &set->orig_wset), \
4131 restore_fdset(set->eset, &set->orig_eset), \
4138 BLOCKING_REGION(set->th, {
4139 const rb_hrtime_t *sto;
4142 sto = sigwait_timeout(set->th, set->sigwait_fd, to, &drained);
4143 if (!RUBY_VM_INTERRUPTED(set->th->ec)) {
4144 result = native_fd_select(set->max, set->rset, set->wset,
4146 rb_hrtime2timeval(&tv, sto), set->th);
4147 if (result < 0) lerrno = errno;
4149 }, set->sigwait_fd >= 0 ? ubf_sigwait : ubf_select, set->th, TRUE);
4151 if (set->sigwait_fd >= 0) {
4152 int fd = sigwait_signals_fd(result,
4155 (void)check_signals_nogvl(set->th, fd);
4158 RUBY_VM_CHECK_INTS_BLOCKING(set->th->ec);
4159 }
while (wait_retryable(&result, lerrno, to, end) && do_select_update());
4165 return (
VALUE)result;
4186 set.th = GET_THREAD();
4187 RUBY_VM_CHECK_INTS_BLOCKING(set.th->ec);
4192 set.timeout = timeout;
4194 if (!set.rset && !set.wset && !set.eset) {
4203 set.sigwait_fd = rb_sigwait_fd_get(set.th);
4204 if (set.sigwait_fd >= 0) {
4208 set.rset = init_set_fd(set.sigwait_fd, &set.orig_rset);
4209 if (set.sigwait_fd >= set.max) {
4210 set.max = set.sigwait_fd + 1;
4213#define fd_init_copy(f) do { \
4215 rb_fd_resize(set.max - 1, set.f); \
4216 if (&set.orig_##f != set.f) { \
4217 rb_fd_init_copy(&set.orig_##f, set.f); \
4221 rb_fd_no_init(&set.orig_##f); \
4235#define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
4236#define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
4237#define POLLEX_SET (POLLPRI)
4240# define POLLERR_SET (0)
4247rb_thread_wait_for_single_fd(
int fd,
int events,
struct timeval *timeout)
4249 struct pollfd fds[2];
4256 volatile int lerrno;
4258 wfd.th = GET_THREAD();
4263 ccan_list_add(&wfd.th->vm->waiting_fds, &wfd.wfd_node);
4267 EC_PUSH_TAG(wfd.th->ec);
4268 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
4269 rb_hrtime_t *to, rel, end = 0;
4270 RUBY_VM_CHECK_INTS_BLOCKING(wfd.th->ec);
4271 timeout_prepare(&to, &rel, &end, timeout);
4273 fds[0].events = (short)events;
4276 fds[1].fd = rb_sigwait_fd_get(wfd.th);
4278 if (fds[1].fd >= 0) {
4279 fds[1].events = POLLIN;
4290 BLOCKING_REGION(wfd.th, {
4291 const rb_hrtime_t *sto;
4294 sto = sigwait_timeout(wfd.th, fds[1].fd, to, &drained);
4295 if (!RUBY_VM_INTERRUPTED(wfd.th->ec)) {
4296 result = ppoll(fds, nfds, rb_hrtime2timespec(&ts, sto), 0);
4297 if (result < 0) lerrno = errno;
4299 }, ubf, wfd.th, TRUE);
4301 if (fds[1].fd >= 0) {
4302 int fd1 = sigwait_signals_fd(result, fds[1].revents, fds[1].fd);
4303 (void)check_signals_nogvl(wfd.th, fd1);
4304 rb_sigwait_fd_put(wfd.th, fds[1].fd);
4305 rb_sigwait_fd_migrate(wfd.th->vm);
4307 RUBY_VM_CHECK_INTS_BLOCKING(wfd.th->ec);
4308 }
while (wait_retryable(&result, lerrno, to, end));
4314 ccan_list_del(&wfd.wfd_node);
4319 EC_JUMP_TAG(wfd.th->ec, state);
4327 if (fds[0].revents & POLLNVAL) {
4337 if (fds[0].revents & POLLIN_SET)
4338 result |= RB_WAITFD_IN;
4339 if (fds[0].revents & POLLOUT_SET)
4340 result |= RB_WAITFD_OUT;
4341 if (fds[0].revents & POLLEX_SET)
4342 result |= RB_WAITFD_PRI;
4345 if (fds[0].revents & POLLERR_SET)
4364select_single(
VALUE ptr)
4370 args->read, args->write, args->except, args->tv);
4372 args->as.error = errno;
4375 if (args->read &&
rb_fd_isset(args->as.fd, args->read))
4377 if (args->write &&
rb_fd_isset(args->as.fd, args->write))
4379 if (args->except &&
rb_fd_isset(args->as.fd, args->except))
4386select_single_cleanup(
VALUE ptr)
4392 ccan_list_del(&args->wfd.wfd_node);
4403rb_thread_wait_for_single_fd(
int fd,
int events,
struct timeval *timeout)
4411 args.read = (events & RB_WAITFD_IN) ? init_set_fd(fd, &rfds) : NULL;
4412 args.write = (events & RB_WAITFD_OUT) ? init_set_fd(fd, &wfds) : NULL;
4413 args.except = (events & RB_WAITFD_PRI) ? init_set_fd(fd, &efds) : NULL;
4416 args.wfd.th = GET_THREAD();
4420 ccan_list_add(&args.wfd.th->vm->waiting_fds, &args.wfd.wfd_node);
4424 r = (int)
rb_ensure(select_single, ptr, select_single_cleanup, ptr);
4426 errno = args.as.error;
4436#ifdef USE_CONSERVATIVE_STACK_END
4438rb_gc_set_stack_end(
VALUE **stack_end_p)
4441 *stack_end_p = &stack_end;
4453 if (rb_signal_buff_size() > 0) {
4455 threadptr_trap_interrupt(mth);
4460async_bug_fd(
const char *mesg,
int errno_arg,
int fd)
4463 size_t n = strlcpy(buff, mesg,
sizeof(buff));
4464 if (n <
sizeof(buff)-3) {
4467 rb_async_bug_errno(buff, errno_arg);
4472consume_communication_pipe(
int fd)
4478 static char buff[1024];
4492 result = read(fd, buff,
sizeof(buff));
4495 if (USE_EVENTFD || result < (ssize_t)
sizeof(buff)) {
4499 else if (result == 0) {
4502 else if (result < 0) {
4508#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
4513 async_bug_fd(
"consume_communication_pipe: read", e, fd);
4520check_signals_nogvl(
rb_thread_t *th,
int sigwait_fd)
4523 int ret = sigwait_fd >= 0 ? consume_communication_pipe(sigwait_fd) : FALSE;
4524 ubf_wakeup_all_threads();
4525 ruby_sigchld_handler(vm);
4526 if (rb_signal_buff_size()) {
4527 if (th == vm->ractor.main_thread) {
4529 RUBY_VM_SET_TRAP_INTERRUPT(th->ec);
4532 threadptr_trap_interrupt(vm->ractor.main_thread);
4540rb_thread_stop_timer_thread(
void)
4542 if (TIMER_THREAD_CREATED_P() && native_stop_timer_thread()) {
4543 native_reset_timer_thread();
4548rb_thread_reset_timer_thread(
void)
4550 native_reset_timer_thread();
4554rb_thread_start_timer_thread(
void)
4557 rb_thread_create_timer_thread();
4561clear_coverage_i(st_data_t key, st_data_t val, st_data_t dummy)
4569 if (GET_VM()->coverage_mode & COVERAGE_TARGET_ONESHOT_LINES) {
4570 rb_ary_clear(lines);
4576 RARRAY_ASET(lines, i,
INT2FIX(0));
4583 RARRAY_ASET(counters, i,
INT2FIX(0));
4591rb_clear_coverages(
void)
4593 VALUE coverages = rb_get_coverages();
4594 if (
RTEST(coverages)) {
4599#if defined(HAVE_WORKING_FORK)
4607 vm->ractor.main_ractor = r;
4608 vm->ractor.main_thread = th;
4609 r->threads.main = th;
4610 r->status_ = ractor_created;
4612 thread_sched_atfork(TH_SCHED(th));
4616 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
4617 ccan_list_for_each(&r->threads.set, i, lt_node) {
4621 rb_vm_living_threads_init(vm);
4623 rb_ractor_atfork(vm, th);
4633 rb_ractor_sleeper_threads_clear(th->ractor);
4634 rb_clear_coverages();
4636 VM_ASSERT(vm->ractor.blocking_cnt == 0);
4637 VM_ASSERT(vm->ractor.cnt == 1);
4643 if (th != current_th) {
4644 rb_mutex_abandon_keeping_mutexes(th);
4645 rb_mutex_abandon_locking_mutex(th);
4646 thread_cleanup_func(th, TRUE);
4655 rb_thread_atfork_internal(th, terminate_atfork_i);
4656 th->join_list = NULL;
4657 rb_fiber_atfork(th);
4663 mjit_child_after_fork();
4669 if (th != current_th) {
4670 thread_cleanup_func_before_exec(th);
4678 rb_thread_atfork_internal(th, terminate_atfork_before_exec_i);
4698thgroup_memsize(
const void *ptr)
4700 return sizeof(
struct thgroup);
4706 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
4729thgroup_s_alloc(
VALUE klass)
4736 data->group = group;
4751thgroup_list(
VALUE group)
4753 VALUE ary = rb_ary_new();
4757 ccan_list_for_each(&r->threads.set, th, lt_node) {
4758 if (th->thgroup == group) {
4759 rb_ary_push(ary, th->self);
4783thgroup_enclose(
VALUE group)
4802thgroup_enclosed_p(
VALUE group)
4807 return RBOOL(data->enclosed);
4847 if (data->enclosed) {
4855 if (data->enclosed) {
4857 "can't move from the enclosed thread group");
4860 target_th->thgroup = group;
4868thread_shield_mark(
void *ptr)
4870 rb_gc_mark((
VALUE)ptr);
4875 {thread_shield_mark, 0, 0,},
4876 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
4880thread_shield_alloc(
VALUE klass)
4885#define GetThreadShieldPtr(obj) ((VALUE)rb_check_typeddata((obj), &thread_shield_data_type))
4886#define THREAD_SHIELD_WAITING_MASK (((FL_USER19-1)&~(FL_USER0-1))|FL_USER19)
4887#define THREAD_SHIELD_WAITING_SHIFT (FL_USHIFT)
4888#define THREAD_SHIELD_WAITING_MAX (THREAD_SHIELD_WAITING_MASK>>THREAD_SHIELD_WAITING_SHIFT)
4889STATIC_ASSERT(THREAD_SHIELD_WAITING_MAX, THREAD_SHIELD_WAITING_MAX <= UINT_MAX);
4890static inline unsigned int
4891rb_thread_shield_waiting(
VALUE b)
4893 return ((
RBASIC(b)->flags&THREAD_SHIELD_WAITING_MASK)>>THREAD_SHIELD_WAITING_SHIFT);
4897rb_thread_shield_waiting_inc(
VALUE b)
4899 unsigned int w = rb_thread_shield_waiting(b);
4901 if (w > THREAD_SHIELD_WAITING_MAX)
4903 RBASIC(b)->flags &= ~THREAD_SHIELD_WAITING_MASK;
4904 RBASIC(b)->flags |= ((
VALUE)w << THREAD_SHIELD_WAITING_SHIFT);
4908rb_thread_shield_waiting_dec(
VALUE b)
4910 unsigned int w = rb_thread_shield_waiting(b);
4913 RBASIC(b)->flags &= ~THREAD_SHIELD_WAITING_MASK;
4914 RBASIC(b)->flags |= ((
VALUE)w << THREAD_SHIELD_WAITING_SHIFT);
4918rb_thread_shield_new(
void)
4920 VALUE thread_shield = thread_shield_alloc(rb_cThreadShield);
4922 return thread_shield;
4926rb_thread_shield_owned(
VALUE self)
4928 VALUE mutex = GetThreadShieldPtr(self);
4929 if (!mutex)
return false;
4933 return m->fiber == GET_EC()->fiber_ptr;
4945rb_thread_shield_wait(
VALUE self)
4947 VALUE mutex = GetThreadShieldPtr(self);
4950 if (!mutex)
return Qfalse;
4951 m = mutex_ptr(mutex);
4952 if (m->fiber == GET_EC()->fiber_ptr)
return Qnil;
4953 rb_thread_shield_waiting_inc(self);
4955 rb_thread_shield_waiting_dec(self);
4958 return rb_thread_shield_waiting(self) > 0 ?
Qnil :
Qfalse;
4962thread_shield_get_mutex(
VALUE self)
4964 VALUE mutex = GetThreadShieldPtr(self);
4974rb_thread_shield_release(
VALUE self)
4976 VALUE mutex = thread_shield_get_mutex(self);
4978 return RBOOL(rb_thread_shield_waiting(self) > 0);
4985rb_thread_shield_destroy(
VALUE self)
4987 VALUE mutex = thread_shield_get_mutex(self);
4990 return RBOOL(rb_thread_shield_waiting(self) > 0);
4996 return th->ec->local_storage_recursive_hash;
5002 th->ec->local_storage_recursive_hash = hash;
5014recursive_list_access(
VALUE sym)
5017 VALUE hash = threadptr_recursive_hash(th);
5020 hash = rb_ident_hash_new();
5021 threadptr_recursive_hash_set(th, hash);
5025 list = rb_hash_aref(hash, sym);
5028 list = rb_ident_hash_new();
5029 rb_hash_aset(hash, sym, list);
5043#if SIZEOF_LONG == SIZEOF_VOIDP
5044 #define OBJ_ID_EQL(obj_id, other) ((obj_id) == (other))
5045#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
5046 #define OBJ_ID_EQL(obj_id, other) (RB_BIGNUM_TYPE_P((obj_id)) ? \
5047 rb_big_eql((obj_id), (other)) : ((obj_id) == (other)))
5050 VALUE pair_list = rb_hash_lookup2(list, obj,
Qundef);
5051 if (UNDEF_P(pair_list))
5053 if (paired_obj_id) {
5054 if (!RB_TYPE_P(pair_list,
T_HASH)) {
5055 if (!OBJ_ID_EQL(paired_obj_id, pair_list))
5059 if (
NIL_P(rb_hash_lookup(pair_list, paired_obj_id)))
5081 rb_hash_aset(list, obj,
Qtrue);
5083 else if (UNDEF_P(pair_list = rb_hash_lookup2(list, obj,
Qundef))) {
5084 rb_hash_aset(list, obj, paired_obj);
5087 if (!RB_TYPE_P(pair_list,
T_HASH)){
5088 VALUE other_paired_obj = pair_list;
5089 pair_list = rb_hash_new();
5090 rb_hash_aset(pair_list, other_paired_obj,
Qtrue);
5091 rb_hash_aset(list, obj, pair_list);
5093 rb_hash_aset(pair_list, paired_obj,
Qtrue);
5109 VALUE pair_list = rb_hash_lookup2(list, obj,
Qundef);
5110 if (UNDEF_P(pair_list)) {
5113 if (RB_TYPE_P(pair_list,
T_HASH)) {
5114 rb_hash_delete_entry(pair_list, paired_obj);
5120 rb_hash_delete_entry(list, obj);
5136 return (*p->func)(p->obj, p->arg, FALSE);
5157 p.list = recursive_list_access(sym);
5161 outermost = outer && !recursive_check(p.list,
ID2SYM(recursive_key), 0);
5163 if (recursive_check(p.list, p.obj, pairid)) {
5164 if (outer && !outermost) {
5167 return (*func)(obj, arg, TRUE);
5170 enum ruby_tag_type state;
5175 recursive_push(p.list,
ID2SYM(recursive_key), 0);
5176 recursive_push(p.list, p.obj, p.pairid);
5177 result = rb_catch_protect(p.list, exec_recursive_i, (
VALUE)&p, &state);
5178 if (!recursive_pop(p.list, p.obj, p.pairid))
goto invalid;
5179 if (!recursive_pop(p.list,
ID2SYM(recursive_key), 0))
goto invalid;
5180 if (state != TAG_NONE) EC_JUMP_TAG(GET_EC(), state);
5181 if (result == p.list) {
5182 result = (*func)(obj, arg, TRUE);
5187 recursive_push(p.list, p.obj, p.pairid);
5188 EC_PUSH_TAG(GET_EC());
5189 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
5190 ret = (*func)(obj, arg, FALSE);
5193 if (!recursive_pop(p.list, p.obj, p.pairid)) {
5196 if (state != TAG_NONE) EC_JUMP_TAG(GET_EC(), state);
5205 "for %+"PRIsVALUE
" in %+"PRIsVALUE,
5229 return exec_recursive(func, obj, rb_memory_id(paired_obj), arg, 0,
rb_frame_last_func());
5247 return exec_recursive(func, obj, 0, arg, 1, mid);
5259 return exec_recursive(func, obj, rb_memory_id(paired_obj), arg, 1,
rb_frame_last_func());
5271rb_thread_backtrace_m(
int argc,
VALUE *argv,
VALUE thval)
5273 return rb_vm_thread_backtrace(argc, argv, thval);
5288rb_thread_backtrace_locations_m(
int argc,
VALUE *argv,
VALUE thval)
5290 return rb_vm_thread_backtrace_locations(argc, argv, thval);
5294Init_Thread_Mutex(
void)
5324 sym_never =
ID2SYM(rb_intern_const(
"never"));
5325 sym_immediate =
ID2SYM(rb_intern_const(
"immediate"));
5326 sym_on_blocking =
ID2SYM(rb_intern_const(
"on_blocking"));
5385 rb_vm_register_special_exception(ruby_error_stream_closed,
rb_eIOError,
5386 "stream closed in another thread");
5396 th->thgroup = th->ractor->thgroup_default =
rb_obj_alloc(cThGroup);
5408 thread_sched_to_running(sched, th);
5410 th->pending_interrupt_queue = rb_ary_hidden_new(0);
5411 th->pending_interrupt_queue_checked = 0;
5412 th->pending_interrupt_mask_stack = rb_ary_hidden_new(0);
5416 rb_thread_create_timer_thread();
5429#ifdef NON_SCALAR_THREAD_ID
5430 #define thread_id_str(th) (NULL)
5432 #define thread_id_str(th) ((void *)(uintptr_t)(th)->nt->thread_id)
5441 rb_str_catf(msg,
"\n%d threads, %d sleeps current:%p main thread:%p\n",
5442 rb_ractor_living_thread_num(r), rb_ractor_sleeper_thread_num(r),
5443 (
void *)GET_THREAD(), (
void *)r->threads.main);
5445 ccan_list_for_each(&r->threads.set, th, lt_node) {
5446 rb_str_catf(msg,
"* %+"PRIsVALUE
"\n rb_thread_t:%p "
5448 th->self, (
void *)th, thread_id_str(th), th->ec->interrupt_flag);
5450 if (th->locking_mutex) {
5451 rb_mutex_t *mutex = mutex_ptr(th->locking_mutex);
5453 (
void *)mutex->fiber, rb_mutex_num_waiting(mutex));
5459 rb_str_catf(msg,
"\n depended by: tb_thread_id:%p", (
void *)list->thread);
5464 rb_str_concat(msg, rb_ary_join(rb_ec_backtrace_str_ary(th->ec, 0, 0), sep));
5472 if (GET_THREAD()->vm->thread_ignore_deadlock)
return;
5476 int sleeper_num = rb_ractor_sleeper_thread_num(r);
5477 int ltnum = rb_ractor_living_thread_num(r);
5479 if (ltnum > sleeper_num)
return;
5480 if (ltnum < sleeper_num)
rb_bug(
"sleeper must not be more than vm_living_thread_num(vm)");
5481 if (patrol_thread && patrol_thread != GET_THREAD())
return;
5483 ccan_list_for_each(&r->threads.set, th, lt_node) {
5484 if (th->status != THREAD_STOPPED_FOREVER || RUBY_VM_INTERRUPTED(th->ec)) {
5487 else if (th->locking_mutex) {
5488 rb_mutex_t *mutex = mutex_ptr(th->locking_mutex);
5489 if (mutex->fiber == th->ec->fiber_ptr || (!mutex->fiber && !ccan_list_empty(&mutex->waitq))) {
5500 argv[1] =
rb_str_new2(
"No live threads left. Deadlock?");
5501 debug_deadlock_check(r, argv[1]);
5502 rb_ractor_sleeper_threads_dec(GET_RACTOR());
5503 rb_threadptr_raise(r->threads.main, 2, argv);
5515 ccan_list_for_each(waiting_fds, waitfd, wfd_node) {
5526 VALUE coverage = rb_iseq_coverage(cfp->iseq);
5527 if (RB_TYPE_P(coverage,
T_ARRAY) && !RBASIC_CLASS(coverage)) {
5534 if (GET_VM()->coverage_mode & COVERAGE_TARGET_ONESHOT_LINES) {
5535 rb_iseq_clear_event_flags(cfp->iseq, cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded - 1, RUBY_EVENT_COVERAGE_LINE);
5536 rb_ary_push(lines,
LONG2FIX(line + 1));
5546 RARRAY_ASET(lines, line,
LONG2FIX(count));
5556 VALUE coverage = rb_iseq_coverage(cfp->iseq);
5557 if (RB_TYPE_P(coverage,
T_ARRAY) && !RBASIC_CLASS(coverage)) {
5560 long pc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded - 1;
5566 RARRAY_ASET(counters, idx,
LONG2FIX(count));
5575 VALUE path, beg_pos_lineno, beg_pos_column, end_pos_lineno, end_pos_column;
5577 if (!me->def)
return NULL;
5580 switch (me->def->type) {
5581 case VM_METHOD_TYPE_ISEQ: {
5584 path = rb_iseq_path(iseq);
5585 beg_pos_lineno =
INT2FIX(loc->code_location.beg_pos.lineno);
5586 beg_pos_column =
INT2FIX(loc->code_location.beg_pos.column);
5587 end_pos_lineno =
INT2FIX(loc->code_location.end_pos.lineno);
5588 end_pos_column =
INT2FIX(loc->code_location.end_pos.column);
5591 case VM_METHOD_TYPE_BMETHOD: {
5592 const rb_iseq_t *iseq = rb_proc_get_iseq(me->def->body.bmethod.proc, 0);
5595 rb_iseq_check(iseq);
5596 path = rb_iseq_path(iseq);
5597 loc = &ISEQ_BODY(iseq)->location;
5598 beg_pos_lineno =
INT2FIX(loc->code_location.beg_pos.lineno);
5599 beg_pos_column =
INT2FIX(loc->code_location.beg_pos.column);
5600 end_pos_lineno =
INT2FIX(loc->code_location.end_pos.lineno);
5601 end_pos_column =
INT2FIX(loc->code_location.end_pos.column);
5606 case VM_METHOD_TYPE_ALIAS:
5607 me = me->def->body.alias.original_me;
5609 case VM_METHOD_TYPE_REFINED:
5610 me = me->def->body.refined.orig_me;
5611 if (!me)
return NULL;
5618 if (RB_TYPE_P(path,
T_ARRAY)) {
5619 path = rb_ary_entry(path, 1);
5620 if (!RB_TYPE_P(path,
T_STRING))
return NULL;
5622 if (resolved_location) {
5623 resolved_location[0] = path;
5624 resolved_location[1] = beg_pos_lineno;
5625 resolved_location[2] = beg_pos_column;
5626 resolved_location[3] = end_pos_lineno;
5627 resolved_location[4] = end_pos_column;
5641 me = rb_resolve_me_location(me, 0);
5644 rcount = rb_hash_aref(me2counter, (
VALUE) me);
5652rb_get_coverages(
void)
5654 return GET_VM()->coverages;
5658rb_get_coverage_mode(
void)
5660 return GET_VM()->coverage_mode;
5664rb_set_coverages(
VALUE coverages,
int mode,
VALUE me2counter)
5666 GET_VM()->coverages = coverages;
5667 GET_VM()->me2counter = me2counter;
5668 GET_VM()->coverage_mode = mode;
5672rb_resume_coverages(
void)
5674 int mode = GET_VM()->coverage_mode;
5675 VALUE me2counter = GET_VM()->me2counter;
5676 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);
5677 if (mode & COVERAGE_TARGET_BRANCHES) {
5678 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);
5680 if (mode & COVERAGE_TARGET_METHODS) {
5686rb_suspend_coverages(
void)
5689 if (GET_VM()->coverage_mode & COVERAGE_TARGET_BRANCHES) {
5692 if (GET_VM()->coverage_mode & COVERAGE_TARGET_METHODS) {
5699rb_reset_coverages(
void)
5701 rb_clear_coverages();
5702 rb_iseq_remove_coverage_all();
5703 GET_VM()->coverages =
Qfalse;
5707rb_default_coverage(
int n)
5709 VALUE coverage = rb_ary_hidden_new_fill(3);
5711 int mode = GET_VM()->coverage_mode;
5713 if (mode & COVERAGE_TARGET_LINES) {
5714 lines = n > 0 ? rb_ary_hidden_new_fill(n) : rb_ary_hidden_new(0);
5716 RARRAY_ASET(coverage, COVERAGE_INDEX_LINES, lines);
5718 if (mode & COVERAGE_TARGET_BRANCHES) {
5719 branches = rb_ary_hidden_new_fill(2);
5741 VALUE structure = rb_hash_new();
5743 RARRAY_ASET(branches, 0, structure);
5745 RARRAY_ASET(branches, 1, rb_ary_hidden_new(0));
5747 RARRAY_ASET(coverage, COVERAGE_INDEX_BRANCHES, branches);
5753uninterruptible_exit(
VALUE v)
5756 rb_ary_pop(cur_th->pending_interrupt_mask_stack);
5758 cur_th->pending_interrupt_queue_checked = 0;
5759 if (!rb_threadptr_pending_interrupt_empty_p(cur_th)) {
5760 RUBY_VM_SET_INTERRUPT(cur_th->ec);
5768 VALUE interrupt_mask = rb_ident_hash_new();
5771 rb_hash_aset(interrupt_mask, rb_cObject, sym_never);
5773 rb_ary_push(cur_th->pending_interrupt_mask_stack, interrupt_mask);
5777 RUBY_VM_CHECK_INTS(cur_th->ec);
#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.
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 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.
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.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
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 always regardless of runtime -W flag.
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Creates an instance of the passed exception class.
VALUE rb_eArgError
ArgumentError exception.
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_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
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.
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_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
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_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_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__.
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.
ID rb_to_id(VALUE str)
Identical to rb_intern(), except it takes an instance of rb_cString.
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.
#define RB_NOGVL_INTR_FAIL
Passing this flag to rb_nogvl() prevents it from checking interrupts.
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.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
#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.
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.
#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.
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.
#define RARRAY_CONST_PTR_TRANSIENT
Just another name of rb_array_const_ptr_transient.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#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...
int ruby_native_thread_p(void)
Queries if the thread which calls this function is a ruby's thread.
int ruby_snprintf(char *str, size_t n, char const *fmt,...)
Our own locale-insensitive version of snprintf(3).
#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).
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
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 VALUE
Type that represents a Ruby object.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.