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))) {
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;
155 struct ccan_list_node wfd_node;
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)) { \
201 RB_GC_SAVE_MACHINE_CONTEXT(th); \
202 thread_sched_to_waiting(TH_SCHED(th)); \
204 blocking_region_end(th, &__region); \
212#define RUBY_VM_CHECK_INTS_BLOCKING(ec) vm_check_ints_blocking(ec)
218 if (LIKELY(rb_threadptr_pending_interrupt_empty_p(th))) {
219 if (LIKELY(!RUBY_VM_INTERRUPTED_ANY(ec)))
return FALSE;
222 th->pending_interrupt_queue_checked = 0;
223 RUBY_VM_SET_INTERRUPT(ec);
225 return rb_threadptr_execute_interrupts(th, 1);
231 return vm_check_ints_blocking(ec);
239#if defined(HAVE_POLL)
240# if defined(__linux__)
243# if defined(__FreeBSD_version) && __FreeBSD_version >= 1100000
246# define POLLERR_SET (POLLHUP | POLLERR)
251timeout_prepare(rb_hrtime_t **to, rb_hrtime_t *rel, rb_hrtime_t *end,
255 *rel = rb_timeval2hrtime(timeout);
256 *end = rb_hrtime_add(rb_hrtime_now(), *rel);
264MAYBE_UNUSED(NOINLINE(
static int thread_start_func_2(
rb_thread_t *th,
VALUE *stack_start)));
265void ruby_sigchld_handler(
rb_vm_t *);
268ubf_sigwait(
void *ignore)
270 rb_thread_wakeup_timer_thread(0);
273#include THREAD_IMPL_SRC
280#ifndef BUSY_WAIT_SIGNALS
281# define BUSY_WAIT_SIGNALS (0)
285# define USE_EVENTFD (0)
288#include "thread_sync.c"
318 if (fail_if_interrupted) {
319 if (RUBY_VM_INTERRUPTED_ANY(th->ec)) {
324 RUBY_VM_CHECK_INTS(th->ec);
328 }
while (!th->ec->raised_flag && RUBY_VM_INTERRUPTED_ANY(th->ec) &&
331 VM_ASSERT(th->unblock.func == NULL);
333 th->unblock.func = func;
334 th->unblock.arg = arg;
344 th->unblock.func = 0;
349rb_threadptr_interrupt_common(
rb_thread_t *th,
int trap)
354 RUBY_VM_SET_TRAP_INTERRUPT(th->ec);
357 RUBY_VM_SET_INTERRUPT(th->ec);
359 if (th->unblock.func != NULL) {
360 (th->unblock.func)(th->unblock.arg);
371 rb_threadptr_interrupt_common(th, 0);
377 rb_threadptr_interrupt_common(th, 1);
385 ccan_list_for_each(&r->threads.set, th, lt_node) {
386 if (th != main_thread) {
387 RUBY_DEBUG_LOG(
"terminate start th:%u status:%s", rb_th_serial(th), thread_status_name(th, TRUE));
389 rb_threadptr_pending_interrupt_enque(th, eTerminateSignal);
390 rb_threadptr_interrupt(th);
392 RUBY_DEBUG_LOG(
"terminate done th:%u status:%s", rb_th_serial(th), thread_status_name(th, TRUE));
395 RUBY_DEBUG_LOG(
"main thread th:%u", rb_th_serial(th));
403 while (thread->join_list) {
407 thread->join_list = join_list->next;
411 if (target_thread->scheduler !=
Qnil && join_list->fiber) {
415 rb_threadptr_interrupt(target_thread);
417 switch (target_thread->status) {
419 case THREAD_STOPPED_FOREVER:
420 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);
525 native_thread_destroy(th);
534 native_thread_init_stack(th);
538rb_vm_proc_local_ep(
VALUE proc)
540 const VALUE *ep = vm_proc_ep(proc);
543 return rb_vm_ep_local_ep(ep);
552 int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler);
557 VALUE args = th->invoke_arg.proc.args;
558 const VALUE *args_ptr;
560 VALUE procval = th->invoke_arg.proc.proc;
562 GetProcPtr(procval, proc);
564 th->ec->errinfo =
Qnil;
565 th->ec->root_lep = rb_vm_proc_local_ep(procval);
566 th->ec->root_svar =
Qfalse;
568 vm_check_ints_blocking(th->ec);
570 if (th->invoke_type == thread_invoke_type_ractor_proc) {
571 VALUE self = rb_ractor_self(th->ractor);
575 rb_ractor_receive_parameters(th->ec, th->ractor, args_len, (
VALUE *)args_ptr);
576 vm_check_ints_blocking(th->ec);
578 return rb_vm_invoke_proc_with_self(
581 th->invoke_arg.proc.kw_splat,
582 VM_BLOCK_HANDLER_NONE
591 th->invoke_arg.proc.args =
Qnil;
597 vm_check_ints_blocking(th->ec);
599 return rb_vm_invoke_proc(
602 th->invoke_arg.proc.kw_splat,
603 VM_BLOCK_HANDLER_NONE
611 native_set_thread_name(th);
614 switch (th->invoke_type) {
615 case thread_invoke_type_proc:
616 result = thread_do_start_proc(th);
619 case thread_invoke_type_ractor_proc:
620 result = thread_do_start_proc(th);
621 rb_ractor_atexit(th->ec, result);
624 case thread_invoke_type_func:
625 result = (*th->invoke_arg.func.func)(th->invoke_arg.func.arg);
628 case thread_invoke_type_none:
636#define thread_sched_to_dead thread_sched_to_waiting
641 STACK_GROW_DIR_DETECTION;
642 enum ruby_tag_type state;
644 size_t size = th->vm->default_params.thread_vm_stack_size /
sizeof(
VALUE);
645 rb_thread_t *ractor_main_th = th->ractor->threads.main;
646 VALUE * vm_stack = NULL;
648 VM_ASSERT(th != th->vm->ractor.main_thread);
649 RUBY_DEBUG_LOG(
"th:%u", rb_th_serial(th));
652 thread_sched_to_running(TH_SCHED(th), th);
653 ruby_thread_set_native(th);
655 RUBY_DEBUG_LOG(
"got lock. th:%u", rb_th_serial(th));
658 if (rb_ractor_status_p(th->ractor, ractor_blocking)) {
661 rb_vm_ractor_blocking_cnt_dec(th->vm, th->ractor, __FILE__, __LINE__);
663 r->r_stdin = rb_io_prep_stdin();
664 r->r_stdout = rb_io_prep_stdout();
665 r->r_stderr = rb_io_prep_stderr();
674 vm_stack = alloca(size *
sizeof(
VALUE));
677 rb_ec_initialize_vm_stack(th->ec, vm_stack, size);
678 th->ec->machine.stack_start = STACK_DIR_UPPER(vm_stack + size, vm_stack);
679 th->ec->machine.stack_maxsize -= size *
sizeof(
VALUE);
682 VM_ASSERT(UNDEF_P(th->value));
684 int fiber_scheduler_closed = 0, event_thread_end_hooked = 0;
689 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
692 SAVE_ROOT_JMPBUF(th, result = thread_do_start(th));
695 if (!fiber_scheduler_closed) {
696 fiber_scheduler_closed = 1;
700 if (!event_thread_end_hooked) {
701 event_thread_end_hooked = 1;
705 if (state == TAG_NONE) {
709 errinfo = th->ec->errinfo;
711 VALUE exc = rb_vm_make_jump_tag_but_local_jump(state,
Qundef);
712 if (!
NIL_P(exc)) errinfo = exc;
714 if (state == TAG_FATAL) {
715 if (th->invoke_type == thread_invoke_type_ractor_proc) {
716 rb_ractor_atexit(th->ec,
Qnil);
724 if (th->report_on_exception) {
725 VALUE mesg = rb_thread_to_s(th->self);
726 rb_str_cat_cstr(mesg,
" terminated with exception (report_on_exception is true):\n");
727 rb_write_error_str(mesg);
728 rb_ec_error_print(th->ec, errinfo);
731 if (th->invoke_type == thread_invoke_type_ractor_proc) {
732 rb_ractor_atexit_exception(th->ec);
735 if (th->vm->thread_abort_on_exception ||
747 VM_ASSERT(!UNDEF_P(th->value));
749 rb_threadptr_join_list_wakeup(th);
750 rb_threadptr_unlock_all_locking_mutexes(th);
752 if (th->invoke_type == thread_invoke_type_ractor_proc) {
753 rb_thread_terminate_all(th);
754 rb_ractor_teardown(th->ec);
757 th->status = THREAD_KILLED;
758 RUBY_DEBUG_LOG(
"killed th:%u", rb_th_serial(th));
760 if (th->vm->ractor.main_thread == th) {
766 rb_threadptr_raise(ractor_main_th, 1, &errinfo);
771 rb_ec_clear_current_thread_trace_func(th->ec);
774 if (th->locking_mutex !=
Qfalse) {
775 rb_bug(
"thread_start_func_2: locking_mutex must not be set (%p:%"PRIxVALUE
")",
776 (
void *)th, th->locking_mutex);
779 if (ractor_main_th->status == THREAD_KILLED &&
780 th->ractor->threads.cnt <= 2 ) {
782 rb_threadptr_interrupt(ractor_main_th);
785 rb_check_deadlock(th->ractor);
787 rb_fiber_close(th->ec->fiber_ptr);
789 thread_cleanup_func(th, FALSE);
790 VM_ASSERT(th->ec->vm_stack == NULL);
792 if (th->invoke_type == thread_invoke_type_ractor_proc) {
796 thread_sched_to_dead(TH_SCHED(th));
797 rb_ractor_living_threads_remove(th->ractor, th);
800 rb_ractor_living_threads_remove(th->ractor, th);
801 thread_sched_to_dead(TH_SCHED(th));
808 enum thread_invoke_type type;
825 rb_thread_t *th = rb_thread_ptr(thval), *current_th = rb_ec_thread_ptr(ec);
830 "can't start a new thread (frozen ThreadGroup)");
833 rb_fiber_inherit_storage(ec, th->ec->fiber_ptr);
835 switch (params->type) {
836 case thread_invoke_type_proc:
837 th->invoke_type = thread_invoke_type_proc;
838 th->invoke_arg.proc.args = params->args;
839 th->invoke_arg.proc.proc = params->proc;
843 case thread_invoke_type_ractor_proc:
844#if RACTOR_CHECK_MODE > 0
845 rb_ractor_setup_belonging_to(thval, rb_ractor_id(params->g));
847 th->invoke_type = thread_invoke_type_ractor_proc;
848 th->ractor = params->g;
849 th->ractor->threads.main = th;
850 th->invoke_arg.proc.proc = rb_proc_isolate_bang(params->proc);
853 rb_ractor_send_parameters(ec, params->g, params->args);
856 case thread_invoke_type_func:
857 th->invoke_type = thread_invoke_type_func;
858 th->invoke_arg.func.func = params->fn;
859 th->invoke_arg.func.arg = (
void *)params->args;
866 th->priority = current_th->priority;
867 th->thgroup = current_th->thgroup;
869 th->pending_interrupt_queue = rb_ary_hidden_new(0);
870 th->pending_interrupt_queue_checked = 0;
871 th->pending_interrupt_mask_stack = rb_ary_dup(current_th->pending_interrupt_mask_stack);
872 RBASIC_CLEAR_CLASS(th->pending_interrupt_mask_stack);
876 RUBY_DEBUG_LOG(
"r:%u th:%u", rb_ractor_id(th->ractor), rb_th_serial(th));
878 rb_ractor_living_threads_insert(th->ractor, th);
881 err = native_thread_create(th);
883 th->status = THREAD_KILLED;
884 rb_ractor_living_threads_remove(th->ractor, th);
890#define threadptr_initialized(th) ((th)->invoke_type != thread_invoke_type_none)
913thread_s_new(
int argc,
VALUE *argv,
VALUE klass)
916 VALUE thread = rb_thread_alloc(klass);
918 if (GET_RACTOR()->threads.main->status == THREAD_KILLED) {
923 th = rb_thread_ptr(thread);
924 if (!threadptr_initialized(th)) {
945 .type = thread_invoke_type_proc,
949 return thread_create_core(rb_thread_alloc(klass), ¶ms);
955 if (th->invoke_type == thread_invoke_type_proc) {
956 return rb_proc_location(th->invoke_arg.proc.proc);
972 else if (th->invoke_type != thread_invoke_type_none) {
973 VALUE loc = threadptr_invoke_proc_location(th);
976 "already initialized thread - %"PRIsVALUE
":%"PRIsVALUE,
985 .type = thread_invoke_type_proc,
989 return thread_create_core(thread, ¶ms);
997 .type = thread_invoke_type_func,
1001 return thread_create_core(rb_thread_alloc(
rb_cThread), ¶ms);
1008 .type = thread_invoke_type_ractor_proc,
1013 return thread_create_core(rb_thread_alloc(
rb_cThread), ¶ms);
1025remove_from_join_list(
VALUE arg)
1030 if (target_thread->status != THREAD_KILLED) {
1033 while (*join_list) {
1034 if (*join_list == p->waiter) {
1035 *join_list = (*join_list)->next;
1039 join_list = &(*join_list)->next;
1049 return th->status == THREAD_KILLED || !UNDEF_P(th->value);
1053thread_join_sleep(
VALUE arg)
1056 rb_thread_t *target_th = p->target, *th = p->waiter->thread;
1057 rb_hrtime_t end = 0, *limit = p->limit;
1060 end = rb_hrtime_add(*limit, rb_hrtime_now());
1063 while (!thread_finished(target_th)) {
1066 if (scheduler !=
Qnil) {
1069 if (thread_finished(target_th))
break;
1074 th->status = THREAD_STOPPED_FOREVER;
1075 rb_ractor_sleeper_threads_inc(th->ractor);
1076 rb_check_deadlock(th->ractor);
1077 native_sleep(th, 0);
1078 rb_ractor_sleeper_threads_dec(th->ractor);
1081 if (hrtime_update_expire(limit, end)) {
1082 RUBY_DEBUG_LOG(
"timeout target_th:%u", rb_th_serial(target_th));
1085 th->status = THREAD_STOPPED;
1086 native_sleep(th, limit);
1088 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1089 th->status = THREAD_RUNNABLE;
1091 RUBY_DEBUG_LOG(
"interrupted target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1104 if (th == target_th) {
1108 if (th->ractor->threads.main == target_th) {
1112 RUBY_DEBUG_LOG(
"target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1114 if (target_th->status != THREAD_KILLED) {
1116 waiter.next = target_th->join_list;
1118 waiter.fiber = rb_fiberptr_blocking(fiber) ? NULL : fiber;
1119 target_th->join_list = &waiter;
1122 arg.waiter = &waiter;
1123 arg.target = target_th;
1124 arg.timeout = timeout;
1132 RUBY_DEBUG_LOG(
"success target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1134 if (target_th->ec->errinfo !=
Qnil) {
1135 VALUE err = target_th->ec->errinfo;
1140 RUBY_DEBUG_LOG(
"terminated target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1145 rb_bug(
"thread_join: Fixnum (%d) should not reach here.",
FIX2INT(err));
1148 else if (THROW_DATA_P(target_th->ec->errinfo)) {
1149 rb_bug(
"thread_join: THROW_DATA should not reach here.");
1156 return target_th->self;
1199thread_join_m(
int argc,
VALUE *argv,
VALUE self)
1202 rb_hrtime_t rel = 0, *limit = 0;
1213 if (
NIL_P(timeout)) {
1217 rel = rb_sec2hrtime(NUM2TIMET(timeout));
1221 limit = double2hrtime(&rel,
rb_num2dbl(timeout));
1224 return thread_join(rb_thread_ptr(self), timeout, limit);
1242thread_value(
VALUE self)
1245 thread_join(th,
Qnil, 0);
1246 if (UNDEF_P(th->value)) {
1260#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
1261 if (clock_gettime(CLOCK_MONOTONIC, ts) == 0)
1271NOINLINE(rb_hrtime_t rb_hrtime_now(
void));
1278 return rb_timespec2hrtime(&ts);
1284 enum rb_thread_status prev_status = th->status;
1285 enum rb_thread_status status;
1288 status = fl & SLEEP_DEADLOCKABLE ? THREAD_STOPPED_FOREVER : THREAD_STOPPED;
1289 th->status = status;
1290 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1291 while (th->status == status) {
1292 if (fl & SLEEP_DEADLOCKABLE) {
1293 rb_ractor_sleeper_threads_inc(th->ractor);
1294 rb_check_deadlock(th->ractor);
1296 native_sleep(th, 0);
1297 if (fl & SLEEP_DEADLOCKABLE) {
1298 rb_ractor_sleeper_threads_dec(th->ractor);
1300 woke = vm_check_ints_blocking(th->ec);
1301 if (woke && !(fl & SLEEP_SPURIOUS_CHECK))
1304 th->status = prev_status;
1311COMPILER_WARNING_PUSH
1312#if defined(__GNUC__) && __GNUC__ == 7 && __GNUC_MINOR__ <= 3
1313COMPILER_WARNING_IGNORED(-Wmaybe-uninitialized)
1316#define PRIu64 PRI_64_PREFIX "u"
1324hrtime_update_expire(rb_hrtime_t *timeout,
const rb_hrtime_t end)
1326 rb_hrtime_t now = rb_hrtime_now();
1328 if (now > end)
return 1;
1330 RUBY_DEBUG_LOG(
"%"PRIu64
" > %"PRIu64
"", (uint64_t)end, (uint64_t)now);
1332 *timeout = end - now;
1338sleep_hrtime(
rb_thread_t *th, rb_hrtime_t rel,
unsigned int fl)
1340 enum rb_thread_status prev_status = th->status;
1342 rb_hrtime_t end = rb_hrtime_add(rb_hrtime_now(), rel);
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;
1360sleep_hrtime_until(
rb_thread_t *th, rb_hrtime_t end,
unsigned int fl)
1362 enum rb_thread_status prev_status = th->status;
1364 rb_hrtime_t rel = rb_hrtime_sub(end, rb_hrtime_now());
1366 th->status = THREAD_STOPPED;
1367 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1368 while (th->status == THREAD_STOPPED) {
1369 native_sleep(th, &rel);
1370 woke = vm_check_ints_blocking(th->ec);
1371 if (woke && !(fl & SLEEP_SPURIOUS_CHECK))
1373 if (hrtime_update_expire(&rel, end))
1377 th->status = prev_status;
1385 sleep_forever(GET_THREAD(), SLEEP_SPURIOUS_CHECK);
1392 sleep_forever(GET_THREAD(), SLEEP_DEADLOCKABLE|SLEEP_SPURIOUS_CHECK);
1396rb_thread_sleep_interruptible(
void)
1399 enum rb_thread_status prev_status = th->status;
1401 th->status = THREAD_STOPPED;
1402 native_sleep(th, 0);
1403 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1404 th->status = prev_status;
1408rb_thread_sleep_deadly_allow_spurious_wakeup(
VALUE blocker,
VALUE timeout, rb_hrtime_t end)
1411 if (scheduler !=
Qnil) {
1417 sleep_hrtime_until(GET_THREAD(), end, SLEEP_SPURIOUS_CHECK);
1420 sleep_forever(GET_THREAD(), SLEEP_DEADLOCKABLE);
1430 sleep_hrtime(th, rb_timeval2hrtime(&time), SLEEP_SPURIOUS_CHECK);
1443 RUBY_VM_CHECK_INTS_BLOCKING(GET_EC());
1451rb_thread_check_trap_pending(
void)
1453 return rb_signal_buff_size() != 0;
1460 return (
int)RUBY_VM_INTERRUPTED(rb_thread_ptr(thval)->ec);
1470rb_thread_schedule_limits(uint32_t limits_us)
1474 RUBY_DEBUG_LOG(
"us:%u", (
unsigned int)limits_us);
1476 if (th->running_time_us >= limits_us) {
1477 RUBY_DEBUG_LOG(
"switch %s",
"start");
1479 RB_GC_SAVE_MACHINE_CONTEXT(th);
1480 thread_sched_yield(TH_SCHED(th), th);
1481 rb_ractor_thread_switch(th->ractor, th);
1483 RUBY_DEBUG_LOG(
"switch %s",
"done");
1491 rb_thread_schedule_limits(0);
1492 RUBY_VM_CHECK_INTS(GET_EC());
1501#ifdef RUBY_VM_CRITICAL_SECTION
1502 VM_ASSERT(ruby_assert_critical_section_entered == 0);
1504 VM_ASSERT(th == GET_THREAD());
1506 region->prev_status = th->status;
1507 if (unblock_function_set(th, ubf, arg, fail_if_interrupted)) {
1508 th->blocking_region_buffer = region;
1509 th->status = THREAD_STOPPED;
1510 rb_ractor_blocking_threads_inc(th->ractor, __FILE__, __LINE__);
1524 unblock_function_clear(th);
1526 unregister_ubf_list(th);
1528 thread_sched_to_running(TH_SCHED(th), th);
1529 rb_ractor_thread_switch(th->ractor, th);
1531 th->blocking_region_buffer = 0;
1532 rb_ractor_blocking_threads_dec(th->ractor, __FILE__, __LINE__);
1533 if (th->status == THREAD_STOPPED) {
1534 th->status = region->prev_status;
1538 VM_ASSERT(th == GET_THREAD());
1542rb_nogvl(
void *(*func)(
void *),
void *data1,
1549 rb_vm_t *vm = rb_ec_vm_ptr(ec);
1550 bool is_main_thread = vm->ractor.main_thread == th;
1551 int saved_errno = 0;
1558 else if (ubf && rb_ractor_living_thread_num(th->ractor) == 1 && is_main_thread) {
1560 vm->ubf_async_safe = 1;
1563 ubf_th = rb_thread_start_unblock_thread();
1567 BLOCKING_REGION(th, {
1569 saved_errno = errno;
1572 if (is_main_thread) vm->ubf_async_safe = 0;
1575 RUBY_VM_CHECK_INTS_BLOCKING(ec);
1582 errno = saved_errno;
1683 return rb_nogvl(func, data1, ubf, data2, 0);
1687rb_thread_io_blocking_region(rb_blocking_function_t *func,
void *data1,
int fd)
1691 volatile int saved_errno = 0;
1692 enum ruby_tag_type state;
1696 .th = rb_ec_thread_ptr(ec)
1707 ccan_list_add(&rb_ec_vm_ptr(ec)->waiting_fds, &
waiting_fd.wfd_node);
1712 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1715 saved_errno = errno;
1731 EC_JUMP_TAG(ec, state);
1734 RUBY_VM_CHECK_INTS_BLOCKING(ec);
1737 if (saved_errno == ETIMEDOUT) {
1741 errno = saved_errno;
1788 fprintf(stderr,
"[BUG] rb_thread_call_with_gvl() is called by non-ruby thread\n");
1793 prev_unblock = th->unblock;
1796 rb_bug(
"rb_thread_call_with_gvl: called by a thread which has GVL.");
1799 blocking_region_end(th, brb);
1803 int released = blocking_region_begin(th, brb, prev_unblock.func, prev_unblock.arg, FALSE);
1805 RB_GC_SAVE_MACHINE_CONTEXT(th);
1806 thread_sched_to_waiting(TH_SCHED(th));
1820ruby_thread_has_gvl_p(
void)
1824 if (th && th->blocking_region_buffer == 0) {
1841thread_s_pass(
VALUE klass)
1866rb_threadptr_pending_interrupt_clear(
rb_thread_t *th)
1868 rb_ary_clear(th->pending_interrupt_queue);
1874 rb_ary_push(th->pending_interrupt_queue, v);
1875 th->pending_interrupt_queue_checked = 0;
1879threadptr_check_pending_interrupt_queue(
rb_thread_t *th)
1881 if (!th->pending_interrupt_queue) {
1886enum handle_interrupt_timing {
1888 INTERRUPT_IMMEDIATE,
1889 INTERRUPT_ON_BLOCKING,
1893static enum handle_interrupt_timing
1897 long mask_stack_len =
RARRAY_LEN(th->pending_interrupt_mask_stack);
1902 for (i=0; i<mask_stack_len; i++) {
1903 mask = mask_stack[mask_stack_len-(i+1)];
1910 klass =
RBASIC(mod)->klass;
1912 else if (mod != RCLASS_ORIGIN(mod)) {
1916 if ((sym = rb_hash_aref(mask, klass)) !=
Qnil) {
1917 if (sym == sym_immediate) {
1918 return INTERRUPT_IMMEDIATE;
1920 else if (sym == sym_on_blocking) {
1921 return INTERRUPT_ON_BLOCKING;
1923 else if (sym == sym_never) {
1924 return INTERRUPT_NEVER;
1933 return INTERRUPT_NONE;
1937rb_threadptr_pending_interrupt_empty_p(
const rb_thread_t *th)
1939 return RARRAY_LEN(th->pending_interrupt_queue) == 0;
1946 for (i=0; i<
RARRAY_LEN(th->pending_interrupt_queue); i++) {
1956rb_threadptr_pending_interrupt_deque(
rb_thread_t *th,
enum handle_interrupt_timing timing)
1961 for (i=0; i<
RARRAY_LEN(th->pending_interrupt_queue); i++) {
1964 enum handle_interrupt_timing mask_timing = rb_threadptr_pending_interrupt_check_mask(th,
CLASS_OF(err));
1966 switch (mask_timing) {
1967 case INTERRUPT_ON_BLOCKING:
1968 if (timing != INTERRUPT_ON_BLOCKING) {
1972 case INTERRUPT_NONE:
1973 case INTERRUPT_IMMEDIATE:
1974 rb_ary_delete_at(th->pending_interrupt_queue, i);
1976 case INTERRUPT_NEVER:
1981 th->pending_interrupt_queue_checked = 1;
1984 VALUE err = rb_ary_shift(th->pending_interrupt_queue);
1985 if (rb_threadptr_pending_interrupt_empty_p(th)) {
1986 th->pending_interrupt_queue_checked = 1;
1993threadptr_pending_interrupt_active_p(
rb_thread_t *th)
2000 if (th->pending_interrupt_queue_checked) {
2004 if (rb_threadptr_pending_interrupt_empty_p(th)) {
2016 if (val != sym_immediate && val != sym_on_blocking && val != sym_never) {
2021 *maskp = rb_ident_hash_new();
2023 rb_hash_aset(*maskp, key, val);
2137rb_thread_s_handle_interrupt(
VALUE self,
VALUE mask_arg)
2143 enum ruby_tag_type state;
2150 mask_arg = rb_to_hash_type(mask_arg);
2156 rb_ary_push(th->pending_interrupt_mask_stack, mask);
2157 if (!rb_threadptr_pending_interrupt_empty_p(th)) {
2158 th->pending_interrupt_queue_checked = 0;
2159 RUBY_VM_SET_INTERRUPT(th->ec);
2162 EC_PUSH_TAG(th->ec);
2163 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2168 rb_ary_pop(th->pending_interrupt_mask_stack);
2169 if (!rb_threadptr_pending_interrupt_empty_p(th)) {
2170 th->pending_interrupt_queue_checked = 0;
2171 RUBY_VM_SET_INTERRUPT(th->ec);
2174 RUBY_VM_CHECK_INTS(th->ec);
2177 EC_JUMP_TAG(th->ec, state);
2194rb_thread_pending_interrupt_p(
int argc,
VALUE *argv,
VALUE target_thread)
2196 rb_thread_t *target_th = rb_thread_ptr(target_thread);
2198 if (!target_th->pending_interrupt_queue) {
2201 if (rb_threadptr_pending_interrupt_empty_p(target_th)) {
2205 VALUE err = argv[0];
2209 return RBOOL(rb_threadptr_pending_interrupt_include_p(target_th, err));
2274rb_thread_s_pending_interrupt_p(
int argc,
VALUE *argv,
VALUE self)
2276 return rb_thread_pending_interrupt_p(argc, argv, GET_THREAD()->self);
2279NORETURN(
static void rb_threadptr_to_kill(
rb_thread_t *th));
2284 rb_threadptr_pending_interrupt_clear(th);
2285 th->status = THREAD_RUNNABLE;
2287 th->ec->errinfo =
INT2FIX(TAG_FATAL);
2288 EC_JUMP_TAG(th->ec, TAG_FATAL);
2299 interrupt = ec->interrupt_flag;
2300 old = ATOMIC_CAS(ec->interrupt_flag, interrupt, interrupt & ec->interrupt_mask);
2301 }
while (old != interrupt);
2302 return interrupt & (
rb_atomic_t)~ec->interrupt_mask;
2307extern bool mjit_waitpid_finished;
2308extern int mjit_waitpid_status;
2311MJIT_FUNC_EXPORTED
int
2312rb_threadptr_execute_interrupts(
rb_thread_t *th,
int blocking_timing)
2315 int postponed_job_interrupt = 0;
2318 if (th->ec->raised_flag)
return ret;
2320 while ((interrupt = threadptr_get_interrupts(th)) != 0) {
2322 int timer_interrupt;
2323 int pending_interrupt;
2325 int terminate_interrupt;
2327 timer_interrupt = interrupt & TIMER_INTERRUPT_MASK;
2328 pending_interrupt = interrupt & PENDING_INTERRUPT_MASK;
2329 postponed_job_interrupt = interrupt & POSTPONED_JOB_INTERRUPT_MASK;
2330 trap_interrupt = interrupt & TRAP_INTERRUPT_MASK;
2331 terminate_interrupt = interrupt & TERMINATE_INTERRUPT_MASK;
2333 if (interrupt & VM_BARRIER_INTERRUPT_MASK) {
2338 if (postponed_job_interrupt) {
2339 rb_postponed_job_flush(th->vm);
2343 if (trap_interrupt && (th == th->vm->ractor.main_thread)) {
2344 enum rb_thread_status prev_status = th->status;
2345 int sigwait_fd = rb_sigwait_fd_get(th);
2347 if (sigwait_fd >= 0) {
2348 (void)consume_communication_pipe(sigwait_fd);
2349 ruby_sigchld_handler(th->vm);
2350 rb_sigwait_fd_put(th, sigwait_fd);
2351 rb_sigwait_fd_migrate(th->vm);
2353 th->status = THREAD_RUNNABLE;
2354 while ((sig = rb_get_next_signal()) != 0) {
2355 ret |= rb_signal_exec(th, sig);
2357 th->status = prev_status;
2363 if (mjit_waitpid_finished && th == th->vm->ractor.main_thread) {
2364 mjit_waitpid_finished =
false;
2365 mjit_notify_waitpid(WIFEXITED(mjit_waitpid_status) ? WEXITSTATUS(mjit_waitpid_status) : -1);
2370 if (pending_interrupt && threadptr_pending_interrupt_active_p(th)) {
2371 VALUE err = rb_threadptr_pending_interrupt_deque(th, blocking_timing ? INTERRUPT_ON_BLOCKING : INTERRUPT_NONE);
2372 RUBY_DEBUG_LOG(
"err:%"PRIdVALUE
"\n", err);
2378 else if (err == eKillSignal ||
2379 err == eTerminateSignal ||
2381 terminate_interrupt = 1;
2384 if (err == th->vm->special_exceptions[ruby_error_stream_closed]) {
2386 err = ruby_vm_special_exception_copy(err);
2389 if (th->status == THREAD_STOPPED ||
2390 th->status == THREAD_STOPPED_FOREVER)
2391 th->status = THREAD_RUNNABLE;
2396 if (terminate_interrupt) {
2397 rb_threadptr_to_kill(th);
2400 if (timer_interrupt) {
2401 uint32_t limits_us = TIME_QUANTUM_USEC;
2403 if (th->priority > 0)
2404 limits_us <<= th->priority;
2406 limits_us >>= -th->priority;
2408 if (th->status == THREAD_RUNNABLE)
2409 th->running_time_us += TIME_QUANTUM_USEC;
2411 VM_ASSERT(th->ec->cfp);
2415 rb_thread_schedule_limits(limits_us);
2422rb_thread_execute_interrupts(
VALUE thval)
2424 rb_threadptr_execute_interrupts(rb_thread_ptr(thval), 1);
2430 rb_threadptr_interrupt(th);
2438 if (rb_threadptr_dead(target_th)) {
2446 exc = rb_make_exception(argc, argv);
2451 if (rb_threadptr_dead(target_th)) {
2455 rb_ec_setup_exception(GET_EC(), exc,
Qundef);
2456 rb_threadptr_pending_interrupt_enque(target_th, exc);
2457 rb_threadptr_interrupt(target_th);
2462rb_threadptr_signal_raise(
rb_thread_t *th,
int sig)
2468 rb_threadptr_raise(th->vm->ractor.main_thread, 2, argv);
2480 rb_threadptr_raise(th->vm->ractor.main_thread, 2, argv);
2486 if (ec->raised_flag & RAISED_EXCEPTION) {
2489 ec->raised_flag |= RAISED_EXCEPTION;
2496 if (!(ec->raised_flag & RAISED_EXCEPTION)) {
2499 ec->raised_flag &= ~RAISED_EXCEPTION;
2504rb_notify_fd_close(
int fd,
struct ccan_list_head *busy)
2506 rb_vm_t *vm = GET_THREAD()->vm;
2511 ccan_list_for_each_safe(&vm->waiting_fds, wfd, next, wfd_node) {
2512 if (wfd->fd == fd) {
2516 ccan_list_del(&wfd->wfd_node);
2517 ccan_list_add(busy, &wfd->wfd_node);
2519 err = th->vm->special_exceptions[ruby_error_stream_closed];
2520 rb_threadptr_pending_interrupt_enque(th, err);
2521 rb_threadptr_interrupt(th);
2527 return !ccan_list_empty(busy);
2533 struct ccan_list_head busy;
2535 ccan_list_head_init(&busy);
2536 if (rb_notify_fd_close(fd, &busy)) {
2563thread_raise_m(
int argc,
VALUE *argv,
VALUE self)
2568 threadptr_check_pending_interrupt_queue(target_th);
2569 rb_threadptr_raise(target_th, argc, argv);
2572 if (current_th == target_th) {
2573 RUBY_VM_CHECK_INTS(target_th->ec);
2595 if (target_th->to_kill || target_th->status == THREAD_KILLED) {
2598 if (target_th == target_th->vm->ractor.main_thread) {
2602 RUBY_DEBUG_LOG(
"target_th:%u", rb_th_serial(target_th));
2604 if (target_th == GET_THREAD()) {
2606 rb_threadptr_to_kill(target_th);
2609 threadptr_check_pending_interrupt_queue(target_th);
2610 rb_threadptr_pending_interrupt_enque(target_th, eKillSignal);
2611 rb_threadptr_interrupt(target_th);
2618rb_thread_to_be_killed(
VALUE thread)
2622 if (target_th->to_kill || target_th->status == THREAD_KILLED) {
2698 if (target_th->status == THREAD_KILLED)
return Qnil;
2700 rb_threadptr_ready(target_th);
2702 if (target_th->status == THREAD_STOPPED ||
2703 target_th->status == THREAD_STOPPED_FOREVER) {
2704 target_th->status = THREAD_RUNNABLE;
2746 "stopping only thread\n\tnote: use sleep to stop forever");
2779 return rb_ractor_thread_list(GET_RACTOR());
2805 return rb_thread_list();
2811 return GET_THREAD()->self;
2824thread_s_current(
VALUE klass)
2832 return GET_RACTOR()->threads.main->self;
2843rb_thread_s_main(
VALUE klass)
2870rb_thread_s_abort_exc(
VALUE _)
2872 return RBOOL(GET_THREAD()->vm->thread_abort_on_exception);
2907rb_thread_s_abort_exc_set(
VALUE self,
VALUE val)
2909 GET_THREAD()->vm->thread_abort_on_exception =
RTEST(val);
2930rb_thread_abort_exc(
VALUE thread)
2932 return RBOOL(rb_thread_ptr(thread)->abort_on_exception);
2950rb_thread_abort_exc_set(
VALUE thread,
VALUE val)
2952 rb_thread_ptr(thread)->abort_on_exception =
RTEST(val);
3000rb_thread_s_report_exc(
VALUE _)
3002 return RBOOL(GET_THREAD()->vm->thread_report_on_exception);
3037rb_thread_s_report_exc_set(
VALUE self,
VALUE val)
3039 GET_THREAD()->vm->thread_report_on_exception =
RTEST(val);
3056rb_thread_s_ignore_deadlock(
VALUE _)
3058 return RBOOL(GET_THREAD()->vm->thread_ignore_deadlock);
3083rb_thread_s_ignore_deadlock_set(
VALUE self,
VALUE val)
3085 GET_THREAD()->vm->thread_ignore_deadlock =
RTEST(val);
3107rb_thread_report_exc(
VALUE thread)
3109 return RBOOL(rb_thread_ptr(thread)->report_on_exception);
3127rb_thread_report_exc_set(
VALUE thread,
VALUE val)
3129 rb_thread_ptr(thread)->report_on_exception =
RTEST(val);
3144rb_thread_group(
VALUE thread)
3146 return rb_thread_ptr(thread)->thgroup;
3152 switch (th->status) {
3153 case THREAD_RUNNABLE:
3154 return th->to_kill ?
"aborting" :
"run";
3155 case THREAD_STOPPED_FOREVER:
3156 if (detail)
return "sleep_forever";
3157 case THREAD_STOPPED:
3169 return th->status == THREAD_KILLED;
3205rb_thread_status(
VALUE thread)
3209 if (rb_threadptr_dead(target_th)) {
3210 if (!
NIL_P(target_th->ec->errinfo) &&
3211 !
FIXNUM_P(target_th->ec->errinfo)) {
3219 return rb_str_new2(thread_status_name(target_th, FALSE));
3239rb_thread_alive_p(
VALUE thread)
3241 return RBOOL(!thread_finished(rb_thread_ptr(thread)));
3259rb_thread_stop_p(
VALUE thread)
3263 if (rb_threadptr_dead(th)) {
3266 return RBOOL(th->status == THREAD_STOPPED || th->status == THREAD_STOPPED_FOREVER);
3277rb_thread_getname(
VALUE thread)
3279 return rb_thread_ptr(thread)->name;
3298 enc = rb_enc_get(name);
3305 target_th->name = name;
3306 if (threadptr_initialized(target_th)) {
3307 native_set_another_thread_name(target_th->nt->thread_id, name);
3312#if USE_NATIVE_THREAD_NATIVE_THREAD_ID
3336rb_thread_native_thread_id(
VALUE thread)
3339 if (rb_threadptr_dead(target_th))
return Qnil;
3340 return native_thread_native_thread_id(target_th);
3343# define rb_thread_native_thread_id rb_f_notimplement
3354rb_thread_to_s(
VALUE thread)
3361 status = thread_status_name(target_th, TRUE);
3362 str =
rb_sprintf(
"#<%"PRIsVALUE
":%p", cname, (
void *)thread);
3363 if (!
NIL_P(target_th->name)) {
3366 if ((loc = threadptr_invoke_proc_location(target_th)) !=
Qnil) {
3376#define recursive_key id__recursive_key__
3381 if (
id == recursive_key) {
3382 return th->ec->local_storage_recursive_hash;
3386 struct rb_id_table *local_storage = th->ec->local_storage;
3388 if (local_storage != NULL && rb_id_table_lookup(local_storage,
id, &val)) {
3400 return threadptr_local_aref(rb_thread_ptr(thread),
id);
3467 if (!
id)
return Qnil;
3485rb_thread_fetch(
int argc,
VALUE *argv,
VALUE self)
3496 if (block_given && argc == 2) {
3497 rb_warn(
"block supersedes default value argument");
3502 if (
id == recursive_key) {
3503 return target_th->ec->local_storage_recursive_hash;
3505 else if (
id && target_th->ec->local_storage &&
3506 rb_id_table_lookup(target_th->ec->local_storage,
id, &val)) {
3509 else if (block_given) {
3512 else if (argc == 1) {
3513 rb_key_err_raise(
rb_sprintf(
"key not found: %+"PRIsVALUE, key), self, key);
3523 if (
id == recursive_key) {
3524 th->ec->local_storage_recursive_hash = val;
3528 struct rb_id_table *local_storage = th->ec->local_storage;
3531 if (!local_storage)
return Qnil;
3532 rb_id_table_delete(local_storage,
id);
3536 if (local_storage == NULL) {
3537 th->ec->local_storage = local_storage = rb_id_table_create(0);
3539 rb_id_table_insert(local_storage,
id, val);
3552 return threadptr_local_aset(rb_thread_ptr(thread),
id, val);
3603rb_thread_variable_get(
VALUE thread,
VALUE key)
3607 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
3610 locals = rb_thread_local_storage(thread);
3632 locals = rb_thread_local_storage(thread);
3654 struct rb_id_table *local_storage = rb_thread_ptr(self)->ec->local_storage;
3656 if (!
id || local_storage == NULL) {
3659 return RBOOL(rb_id_table_lookup(local_storage,
id, &val));
3662static enum rb_id_table_iterator_result
3663thread_keys_i(
ID key,
VALUE value,
void *ary)
3666 return ID_TABLE_CONTINUE;
3673 return rb_ractor_living_thread_num(GET_RACTOR()) == 1;
3691rb_thread_keys(
VALUE self)
3693 struct rb_id_table *local_storage = rb_thread_ptr(self)->ec->local_storage;
3694 VALUE ary = rb_ary_new();
3696 if (local_storage) {
3697 rb_id_table_foreach(local_storage, thread_keys_i, (
void *)ary);
3705 rb_ary_push(ary, key);
3727rb_thread_variables(
VALUE thread)
3733 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
3736 locals = rb_thread_local_storage(thread);
3763 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
3766 locals = rb_thread_local_storage(thread);
3787rb_thread_priority(
VALUE thread)
3789 return INT2NUM(rb_thread_ptr(thread)->priority);
3820rb_thread_priority_set(
VALUE thread,
VALUE prio)
3825#if USE_NATIVE_THREAD_PRIORITY
3826 target_th->priority =
NUM2INT(prio);
3827 native_thread_apply_priority(th);
3830 if (priority > RUBY_THREAD_PRIORITY_MAX) {
3831 priority = RUBY_THREAD_PRIORITY_MAX;
3833 else if (priority < RUBY_THREAD_PRIORITY_MIN) {
3834 priority = RUBY_THREAD_PRIORITY_MIN;
3836 target_th->priority = (int8_t)priority;
3838 return INT2NUM(target_th->priority);
3843#if defined(NFDBITS) && defined(HAVE_RB_FD_INIT)
3879 FD_ZERO(fds->
fdset);
3885 size_t size = howmany(
rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
3887 if (size <
sizeof(fd_set))
3888 size =
sizeof(fd_set);
3912 size_t m = howmany(n + 1, NFDBITS) *
sizeof(fd_mask);
3913 size_t o = howmany(fds->
maxfd, NFDBITS) *
sizeof(fd_mask);
3915 if (m <
sizeof(fd_set)) m =
sizeof(fd_set);
3916 if (o <
sizeof(fd_set)) o =
sizeof(fd_set);
3920 memset((
char *)fds->
fdset + o, 0, m - o);
3929 FD_SET(n, fds->
fdset);
3935 if (n >= fds->
maxfd)
return;
3936 FD_CLR(n, fds->
fdset);
3942 if (n >= fds->
maxfd)
return 0;
3943 return FD_ISSET(n, fds->
fdset) != 0;
3949 size_t size = howmany(max, NFDBITS) *
sizeof(fd_mask);
3951 if (size <
sizeof(fd_set)) size =
sizeof(fd_set);
3954 memcpy(dst->
fdset, src, size);
3960 size_t size = howmany(
rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
3962 if (size <
sizeof(fd_set))
3963 size =
sizeof(fd_set);
3972 fd_set *r = NULL, *w = NULL, *e = NULL;
3985 return select(n, r, w, e, timeout);
3988#define rb_fd_no_init(fds) ((void)((fds)->fdset = 0), (void)((fds)->maxfd = 0))
3995#define FD_ZERO(f) rb_fd_zero(f)
3996#define FD_SET(i, f) rb_fd_set((i), (f))
3997#define FD_CLR(i, f) rb_fd_clr((i), (f))
3998#define FD_ISSET(i, f) rb_fd_isset((i), (f))
4000#elif defined(_WIN32)
4005 set->
capa = FD_SETSIZE;
4007 FD_ZERO(set->
fdset);
4029 SOCKET s = rb_w32_get_osfhandle(fd);
4031 for (i = 0; i < set->
fdset->fd_count; i++) {
4032 if (set->
fdset->fd_array[i] == s) {
4036 if (set->
fdset->fd_count >= (
unsigned)set->
capa) {
4037 set->
capa = (set->
fdset->fd_count / FD_SETSIZE + 1) * FD_SETSIZE;
4039 rb_xrealloc_mul_add(
4040 set->
fdset, set->
capa,
sizeof(SOCKET),
sizeof(
unsigned int));
4042 set->
fdset->fd_array[set->
fdset->fd_count++] = s;
4050#define FD_ZERO(f) rb_fd_zero(f)
4051#define FD_SET(i, f) rb_fd_set((i), (f))
4052#define FD_CLR(i, f) rb_fd_clr((i), (f))
4053#define FD_ISSET(i, f) rb_fd_isset((i), (f))
4055#define rb_fd_no_init(fds) (void)((fds)->fdset = 0)
4059#ifndef rb_fd_no_init
4060#define rb_fd_no_init(fds) (void)(fds)
4064wait_retryable(
int *result,
int errnum, rb_hrtime_t *rel, rb_hrtime_t end)
4073 if (rel && hrtime_update_expire(rel, end)) {
4080 else if (*result == 0) {
4083 return !hrtime_update_expire(rel, end);
4104select_set_free(
VALUE p)
4108 if (set->sigwait_fd >= 0) {
4109 rb_sigwait_fd_put(set->th, set->sigwait_fd);
4110 rb_sigwait_fd_migrate(set->th->vm);
4120static const rb_hrtime_t *
4121sigwait_timeout(
rb_thread_t *th,
int sigwait_fd,
const rb_hrtime_t *orig,
4124 static const rb_hrtime_t quantum = TIME_QUANTUM_USEC * 1000;
4126 if (sigwait_fd >= 0 && (!ubf_threads_empty() || BUSY_WAIT_SIGNALS)) {
4127 *drained_p = check_signals_nogvl(th, sigwait_fd);
4128 if (!orig || *orig > quantum)
4135#define sigwait_signals_fd(result, cond, sigwait_fd) \
4136 (result > 0 && (cond) ? (result--, (sigwait_fd)) : -1)
4144 rb_hrtime_t *to, rel, end = 0;
4146 timeout_prepare(&to, &rel, &end, set->timeout);
4147#define restore_fdset(dst, src) \
4148 ((dst) ? rb_fd_dup(dst, src) : (void)0)
4149#define do_select_update() \
4150 (restore_fdset(set->rset, &set->orig_rset), \
4151 restore_fdset(set->wset, &set->orig_wset), \
4152 restore_fdset(set->eset, &set->orig_eset), \
4159 BLOCKING_REGION(set->th, {
4160 const rb_hrtime_t *sto;
4163 sto = sigwait_timeout(set->th, set->sigwait_fd, to, &drained);
4164 if (!RUBY_VM_INTERRUPTED(set->th->ec)) {
4165 result = native_fd_select(set->max, set->rset, set->wset,
4167 rb_hrtime2timeval(&tv, sto), set->th);
4168 if (result < 0) lerrno = errno;
4170 }, set->sigwait_fd >= 0 ? ubf_sigwait : ubf_select, set->th, TRUE);
4172 if (set->sigwait_fd >= 0) {
4173 int fd = sigwait_signals_fd(result,
4176 (void)check_signals_nogvl(set->th, fd);
4179 RUBY_VM_CHECK_INTS_BLOCKING(set->th->ec);
4180 }
while (wait_retryable(&result, lerrno, to, end) && do_select_update());
4186 return (
VALUE)result;
4207 set.th = GET_THREAD();
4208 RUBY_VM_CHECK_INTS_BLOCKING(set.th->ec);
4213 set.timeout = timeout;
4215 if (!set.rset && !set.wset && !set.eset) {
4224 set.sigwait_fd = rb_sigwait_fd_get(set.th);
4225 if (set.sigwait_fd >= 0) {
4229 set.rset = init_set_fd(set.sigwait_fd, &set.orig_rset);
4230 if (set.sigwait_fd >= set.max) {
4231 set.max = set.sigwait_fd + 1;
4234#define fd_init_copy(f) do { \
4236 rb_fd_resize(set.max - 1, set.f); \
4237 if (&set.orig_##f != set.f) { \
4238 rb_fd_init_copy(&set.orig_##f, set.f); \
4242 rb_fd_no_init(&set.orig_##f); \
4256#define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
4257#define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
4258#define POLLEX_SET (POLLPRI)
4261# define POLLERR_SET (0)
4268rb_thread_wait_for_single_fd(
int fd,
int events,
struct timeval *timeout)
4270 struct pollfd fds[2];
4277 volatile int lerrno;
4279 wfd.th = GET_THREAD();
4284 ccan_list_add(&wfd.th->vm->waiting_fds, &wfd.wfd_node);
4288 EC_PUSH_TAG(wfd.th->ec);
4289 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
4290 rb_hrtime_t *to, rel, end = 0;
4291 RUBY_VM_CHECK_INTS_BLOCKING(wfd.th->ec);
4292 timeout_prepare(&to, &rel, &end, timeout);
4294 fds[0].events = (short)events;
4297 fds[1].fd = rb_sigwait_fd_get(wfd.th);
4299 if (fds[1].fd >= 0) {
4300 fds[1].events = POLLIN;
4311 BLOCKING_REGION(wfd.th, {
4312 const rb_hrtime_t *sto;
4315 sto = sigwait_timeout(wfd.th, fds[1].fd, to, &drained);
4316 if (!RUBY_VM_INTERRUPTED(wfd.th->ec)) {
4317 result = ppoll(fds, nfds, rb_hrtime2timespec(&ts, sto), 0);
4318 if (result < 0) lerrno = errno;
4320 }, ubf, wfd.th, TRUE);
4322 if (fds[1].fd >= 0) {
4323 int fd1 = sigwait_signals_fd(result, fds[1].revents, fds[1].fd);
4324 (void)check_signals_nogvl(wfd.th, fd1);
4325 rb_sigwait_fd_put(wfd.th, fds[1].fd);
4326 rb_sigwait_fd_migrate(wfd.th->vm);
4328 RUBY_VM_CHECK_INTS_BLOCKING(wfd.th->ec);
4329 }
while (wait_retryable(&result, lerrno, to, end));
4335 ccan_list_del(&wfd.wfd_node);
4340 EC_JUMP_TAG(wfd.th->ec, state);
4348 if (fds[0].revents & POLLNVAL) {
4358 if (fds[0].revents & POLLIN_SET)
4359 result |= RB_WAITFD_IN;
4360 if (fds[0].revents & POLLOUT_SET)
4361 result |= RB_WAITFD_OUT;
4362 if (fds[0].revents & POLLEX_SET)
4363 result |= RB_WAITFD_PRI;
4366 if (fds[0].revents & POLLERR_SET)
4385select_single(
VALUE ptr)
4391 args->read, args->write, args->except, args->tv);
4393 args->as.error = errno;
4396 if (args->read &&
rb_fd_isset(args->as.fd, args->read))
4398 if (args->write &&
rb_fd_isset(args->as.fd, args->write))
4400 if (args->except &&
rb_fd_isset(args->as.fd, args->except))
4407select_single_cleanup(
VALUE ptr)
4413 ccan_list_del(&args->wfd.wfd_node);
4424rb_thread_wait_for_single_fd(
int fd,
int events,
struct timeval *timeout)
4432 args.read = (events & RB_WAITFD_IN) ? init_set_fd(fd, &rfds) : NULL;
4433 args.write = (events & RB_WAITFD_OUT) ? init_set_fd(fd, &wfds) : NULL;
4434 args.except = (events & RB_WAITFD_PRI) ? init_set_fd(fd, &efds) : NULL;
4437 args.wfd.th = GET_THREAD();
4441 ccan_list_add(&args.wfd.th->vm->waiting_fds, &args.wfd.wfd_node);
4445 r = (int)
rb_ensure(select_single, ptr, select_single_cleanup, ptr);
4447 errno = args.as.error;
4457#ifdef USE_CONSERVATIVE_STACK_END
4459rb_gc_set_stack_end(
VALUE **stack_end_p)
4462 *stack_end_p = &stack_end;
4474 if (rb_signal_buff_size() > 0) {
4476 threadptr_trap_interrupt(mth);
4481async_bug_fd(
const char *mesg,
int errno_arg,
int fd)
4484 size_t n = strlcpy(buff, mesg,
sizeof(buff));
4485 if (n <
sizeof(buff)-3) {
4488 rb_async_bug_errno(buff, errno_arg);
4493consume_communication_pipe(
int fd)
4499 static char buff[1024];
4513 result = read(fd, buff,
sizeof(buff));
4516 if (USE_EVENTFD || result < (ssize_t)
sizeof(buff)) {
4520 else if (result == 0) {
4523 else if (result < 0) {
4529#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
4534 async_bug_fd(
"consume_communication_pipe: read", e, fd);
4541check_signals_nogvl(
rb_thread_t *th,
int sigwait_fd)
4544 int ret = sigwait_fd >= 0 ? consume_communication_pipe(sigwait_fd) : FALSE;
4545 ubf_wakeup_all_threads();
4546 ruby_sigchld_handler(vm);
4547 if (rb_signal_buff_size()) {
4548 if (th == vm->ractor.main_thread) {
4550 RUBY_VM_SET_TRAP_INTERRUPT(th->ec);
4553 threadptr_trap_interrupt(vm->ractor.main_thread);
4561rb_thread_stop_timer_thread(
void)
4563 if (TIMER_THREAD_CREATED_P() && native_stop_timer_thread()) {
4564 native_reset_timer_thread();
4569rb_thread_reset_timer_thread(
void)
4571 native_reset_timer_thread();
4575rb_thread_start_timer_thread(
void)
4578 rb_thread_create_timer_thread();
4582clear_coverage_i(st_data_t key, st_data_t val, st_data_t dummy)
4590 if (GET_VM()->coverage_mode & COVERAGE_TARGET_ONESHOT_LINES) {
4591 rb_ary_clear(lines);
4612rb_clear_coverages(
void)
4614 VALUE coverages = rb_get_coverages();
4615 if (
RTEST(coverages)) {
4620#if defined(HAVE_WORKING_FORK)
4628 vm->ractor.main_ractor = r;
4629 vm->ractor.main_thread = th;
4630 r->threads.main = th;
4631 r->status_ = ractor_created;
4633 thread_sched_atfork(TH_SCHED(th));
4637 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
4638 ccan_list_for_each(&r->threads.set, i, lt_node) {
4642 rb_vm_living_threads_init(vm);
4644 rb_ractor_atfork(vm, th);
4654 rb_ractor_sleeper_threads_clear(th->ractor);
4655 rb_clear_coverages();
4657 VM_ASSERT(vm->ractor.blocking_cnt == 0);
4658 VM_ASSERT(vm->ractor.cnt == 1);
4664 if (th != current_th) {
4665 rb_mutex_abandon_keeping_mutexes(th);
4666 rb_mutex_abandon_locking_mutex(th);
4667 thread_cleanup_func(th, TRUE);
4676 rb_threadptr_pending_interrupt_clear(th);
4677 rb_thread_atfork_internal(th, terminate_atfork_i);
4678 th->join_list = NULL;
4679 rb_fiber_atfork(th);
4685 mjit_child_after_fork();
4691 if (th != current_th) {
4692 thread_cleanup_func_before_exec(th);
4700 rb_thread_atfork_internal(th, terminate_atfork_before_exec_i);
4720thgroup_memsize(
const void *ptr)
4722 return sizeof(
struct thgroup);
4728 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
4751thgroup_s_alloc(
VALUE klass)
4758 data->group = group;
4773thgroup_list(
VALUE group)
4775 VALUE ary = rb_ary_new();
4779 ccan_list_for_each(&r->threads.set, th, lt_node) {
4780 if (th->thgroup == group) {
4781 rb_ary_push(ary, th->self);
4805thgroup_enclose(
VALUE group)
4824thgroup_enclosed_p(
VALUE group)
4829 return RBOOL(data->enclosed);
4869 if (data->enclosed) {
4877 if (data->enclosed) {
4879 "can't move from the enclosed thread group");
4882 target_th->thgroup = group;
4890thread_shield_mark(
void *ptr)
4892 rb_gc_mark((
VALUE)ptr);
4897 {thread_shield_mark, 0, 0,},
4898 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
4902thread_shield_alloc(
VALUE klass)
4907#define GetThreadShieldPtr(obj) ((VALUE)rb_check_typeddata((obj), &thread_shield_data_type))
4908#define THREAD_SHIELD_WAITING_MASK (((FL_USER19-1)&~(FL_USER0-1))|FL_USER19)
4909#define THREAD_SHIELD_WAITING_SHIFT (FL_USHIFT)
4910#define THREAD_SHIELD_WAITING_MAX (THREAD_SHIELD_WAITING_MASK>>THREAD_SHIELD_WAITING_SHIFT)
4911STATIC_ASSERT(THREAD_SHIELD_WAITING_MAX, THREAD_SHIELD_WAITING_MAX <= UINT_MAX);
4912static inline unsigned int
4913rb_thread_shield_waiting(
VALUE b)
4915 return ((
RBASIC(b)->flags&THREAD_SHIELD_WAITING_MASK)>>THREAD_SHIELD_WAITING_SHIFT);
4919rb_thread_shield_waiting_inc(
VALUE b)
4921 unsigned int w = rb_thread_shield_waiting(b);
4923 if (w > THREAD_SHIELD_WAITING_MAX)
4925 RBASIC(b)->flags &= ~THREAD_SHIELD_WAITING_MASK;
4926 RBASIC(b)->flags |= ((
VALUE)w << THREAD_SHIELD_WAITING_SHIFT);
4930rb_thread_shield_waiting_dec(
VALUE b)
4932 unsigned int w = rb_thread_shield_waiting(b);
4935 RBASIC(b)->flags &= ~THREAD_SHIELD_WAITING_MASK;
4936 RBASIC(b)->flags |= ((
VALUE)w << THREAD_SHIELD_WAITING_SHIFT);
4940rb_thread_shield_new(
void)
4942 VALUE thread_shield = thread_shield_alloc(rb_cThreadShield);
4944 return thread_shield;
4948rb_thread_shield_owned(
VALUE self)
4950 VALUE mutex = GetThreadShieldPtr(self);
4951 if (!mutex)
return false;
4955 return m->fiber == GET_EC()->fiber_ptr;
4967rb_thread_shield_wait(
VALUE self)
4969 VALUE mutex = GetThreadShieldPtr(self);
4972 if (!mutex)
return Qfalse;
4973 m = mutex_ptr(mutex);
4974 if (m->fiber == GET_EC()->fiber_ptr)
return Qnil;
4975 rb_thread_shield_waiting_inc(self);
4977 rb_thread_shield_waiting_dec(self);
4980 return rb_thread_shield_waiting(self) > 0 ?
Qnil :
Qfalse;
4984thread_shield_get_mutex(
VALUE self)
4986 VALUE mutex = GetThreadShieldPtr(self);
4996rb_thread_shield_release(
VALUE self)
4998 VALUE mutex = thread_shield_get_mutex(self);
5000 return RBOOL(rb_thread_shield_waiting(self) > 0);
5007rb_thread_shield_destroy(
VALUE self)
5009 VALUE mutex = thread_shield_get_mutex(self);
5012 return RBOOL(rb_thread_shield_waiting(self) > 0);
5018 return th->ec->local_storage_recursive_hash;
5024 th->ec->local_storage_recursive_hash = hash;
5036recursive_list_access(
VALUE sym)
5039 VALUE hash = threadptr_recursive_hash(th);
5042 hash = rb_ident_hash_new();
5043 threadptr_recursive_hash_set(th, hash);
5047 list = rb_hash_aref(hash, sym);
5050 list = rb_ident_hash_new();
5051 rb_hash_aset(hash, sym, list);
5065#if SIZEOF_LONG == SIZEOF_VOIDP
5066 #define OBJ_ID_EQL(obj_id, other) ((obj_id) == (other))
5067#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
5068 #define OBJ_ID_EQL(obj_id, other) (RB_BIGNUM_TYPE_P((obj_id)) ? \
5069 rb_big_eql((obj_id), (other)) : ((obj_id) == (other)))
5072 VALUE pair_list = rb_hash_lookup2(list, obj,
Qundef);
5073 if (UNDEF_P(pair_list))
5075 if (paired_obj_id) {
5077 if (!OBJ_ID_EQL(paired_obj_id, pair_list))
5081 if (
NIL_P(rb_hash_lookup(pair_list, paired_obj_id)))
5103 rb_hash_aset(list, obj,
Qtrue);
5105 else if (UNDEF_P(pair_list = rb_hash_lookup2(list, obj,
Qundef))) {
5106 rb_hash_aset(list, obj, paired_obj);
5110 VALUE other_paired_obj = pair_list;
5111 pair_list = rb_hash_new();
5112 rb_hash_aset(pair_list, other_paired_obj,
Qtrue);
5113 rb_hash_aset(list, obj, pair_list);
5115 rb_hash_aset(pair_list, paired_obj,
Qtrue);
5131 VALUE pair_list = rb_hash_lookup2(list, obj,
Qundef);
5132 if (UNDEF_P(pair_list)) {
5136 rb_hash_delete_entry(pair_list, paired_obj);
5142 rb_hash_delete_entry(list, obj);
5158 return (*p->func)(p->obj, p->arg, FALSE);
5179 p.list = recursive_list_access(sym);
5183 outermost = outer && !recursive_check(p.list,
ID2SYM(recursive_key), 0);
5185 if (recursive_check(p.list, p.obj, pairid)) {
5186 if (outer && !outermost) {
5189 return (*func)(obj, arg, TRUE);
5192 enum ruby_tag_type state;
5197 recursive_push(p.list,
ID2SYM(recursive_key), 0);
5198 recursive_push(p.list, p.obj, p.pairid);
5199 result = rb_catch_protect(p.list, exec_recursive_i, (
VALUE)&p, &state);
5200 if (!recursive_pop(p.list, p.obj, p.pairid))
goto invalid;
5201 if (!recursive_pop(p.list,
ID2SYM(recursive_key), 0))
goto invalid;
5202 if (state != TAG_NONE) EC_JUMP_TAG(GET_EC(), state);
5203 if (result == p.list) {
5204 result = (*func)(obj, arg, TRUE);
5209 recursive_push(p.list, p.obj, p.pairid);
5210 EC_PUSH_TAG(GET_EC());
5211 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
5212 ret = (*func)(obj, arg, FALSE);
5215 if (!recursive_pop(p.list, p.obj, p.pairid)) {
5218 if (state != TAG_NONE) EC_JUMP_TAG(GET_EC(), state);
5227 "for %+"PRIsVALUE
" in %+"PRIsVALUE,
5251 return exec_recursive(func, obj, rb_memory_id(paired_obj), arg, 0,
rb_frame_last_func());
5269 return exec_recursive(func, obj, 0, arg, 1, mid);
5281 return exec_recursive(func, obj, rb_memory_id(paired_obj), arg, 1,
rb_frame_last_func());
5293rb_thread_backtrace_m(
int argc,
VALUE *argv,
VALUE thval)
5295 return rb_vm_thread_backtrace(argc, argv, thval);
5310rb_thread_backtrace_locations_m(
int argc,
VALUE *argv,
VALUE thval)
5312 return rb_vm_thread_backtrace_locations(argc, argv, thval);
5316Init_Thread_Mutex(
void)
5407 rb_vm_register_special_exception(ruby_error_stream_closed,
rb_eIOError,
5408 "stream closed in another thread");
5418 th->thgroup = th->ractor->thgroup_default =
rb_obj_alloc(cThGroup);
5430 thread_sched_to_running(sched, th);
5432 th->pending_interrupt_queue = rb_ary_hidden_new(0);
5433 th->pending_interrupt_queue_checked = 0;
5434 th->pending_interrupt_mask_stack = rb_ary_hidden_new(0);
5438 rb_thread_create_timer_thread();
5451#ifdef NON_SCALAR_THREAD_ID
5452 #define thread_id_str(th) (NULL)
5454 #define thread_id_str(th) ((void *)(uintptr_t)(th)->nt->thread_id)
5463 rb_str_catf(msg,
"\n%d threads, %d sleeps current:%p main thread:%p\n",
5464 rb_ractor_living_thread_num(r), rb_ractor_sleeper_thread_num(r),
5465 (
void *)GET_THREAD(), (
void *)r->threads.main);
5467 ccan_list_for_each(&r->threads.set, th, lt_node) {
5468 rb_str_catf(msg,
"* %+"PRIsVALUE
"\n rb_thread_t:%p "
5470 th->self, (
void *)th, thread_id_str(th), th->ec->interrupt_flag);
5472 if (th->locking_mutex) {
5473 rb_mutex_t *mutex = mutex_ptr(th->locking_mutex);
5475 (
void *)mutex->fiber, rb_mutex_num_waiting(mutex));
5481 rb_str_catf(msg,
"\n depended by: tb_thread_id:%p", (
void *)list->thread);
5486 rb_str_concat(msg, rb_ary_join(rb_ec_backtrace_str_ary(th->ec, 0, 0), sep));
5494 if (GET_THREAD()->vm->thread_ignore_deadlock)
return;
5498 int sleeper_num = rb_ractor_sleeper_thread_num(r);
5499 int ltnum = rb_ractor_living_thread_num(r);
5501 if (ltnum > sleeper_num)
return;
5502 if (ltnum < sleeper_num)
rb_bug(
"sleeper must not be more than vm_living_thread_num(vm)");
5503 if (patrol_thread && patrol_thread != GET_THREAD())
return;
5505 ccan_list_for_each(&r->threads.set, th, lt_node) {
5506 if (th->status != THREAD_STOPPED_FOREVER || RUBY_VM_INTERRUPTED(th->ec)) {
5509 else if (th->locking_mutex) {
5510 rb_mutex_t *mutex = mutex_ptr(th->locking_mutex);
5511 if (mutex->fiber == th->ec->fiber_ptr || (!mutex->fiber && !ccan_list_empty(&mutex->waitq))) {
5522 argv[1] =
rb_str_new2(
"No live threads left. Deadlock?");
5523 debug_deadlock_check(r, argv[1]);
5524 rb_ractor_sleeper_threads_dec(GET_RACTOR());
5525 rb_threadptr_raise(r->threads.main, 2, argv);
5532rb_vm_memsize_waiting_fds(
struct ccan_list_head *waiting_fds)
5537 ccan_list_for_each(waiting_fds, waitfd, wfd_node) {
5548 VALUE coverage = rb_iseq_coverage(cfp->iseq);
5556 if (GET_VM()->coverage_mode & COVERAGE_TARGET_ONESHOT_LINES) {
5557 rb_iseq_clear_event_flags(cfp->iseq, cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded - 1, RUBY_EVENT_COVERAGE_LINE);
5558 rb_ary_push(lines,
LONG2FIX(line + 1));
5578 VALUE coverage = rb_iseq_coverage(cfp->iseq);
5582 long pc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded - 1;
5597 VALUE path, beg_pos_lineno, beg_pos_column, end_pos_lineno, end_pos_column;
5599 if (!me->def)
return NULL;
5602 switch (me->def->type) {
5603 case VM_METHOD_TYPE_ISEQ: {
5606 path = rb_iseq_path(iseq);
5607 beg_pos_lineno =
INT2FIX(loc->code_location.beg_pos.lineno);
5608 beg_pos_column =
INT2FIX(loc->code_location.beg_pos.column);
5609 end_pos_lineno =
INT2FIX(loc->code_location.end_pos.lineno);
5610 end_pos_column =
INT2FIX(loc->code_location.end_pos.column);
5613 case VM_METHOD_TYPE_BMETHOD: {
5614 const rb_iseq_t *iseq = rb_proc_get_iseq(me->def->body.bmethod.proc, 0);
5617 rb_iseq_check(iseq);
5618 path = rb_iseq_path(iseq);
5619 loc = &ISEQ_BODY(iseq)->location;
5620 beg_pos_lineno =
INT2FIX(loc->code_location.beg_pos.lineno);
5621 beg_pos_column =
INT2FIX(loc->code_location.beg_pos.column);
5622 end_pos_lineno =
INT2FIX(loc->code_location.end_pos.lineno);
5623 end_pos_column =
INT2FIX(loc->code_location.end_pos.column);
5628 case VM_METHOD_TYPE_ALIAS:
5629 me = me->def->body.alias.original_me;
5631 case VM_METHOD_TYPE_REFINED:
5632 me = me->def->body.refined.orig_me;
5633 if (!me)
return NULL;
5641 path = rb_ary_entry(path, 1);
5644 if (resolved_location) {
5645 resolved_location[0] = path;
5646 resolved_location[1] = beg_pos_lineno;
5647 resolved_location[2] = beg_pos_column;
5648 resolved_location[3] = end_pos_lineno;
5649 resolved_location[4] = end_pos_column;
5663 me = rb_resolve_me_location(me, 0);
5666 rcount = rb_hash_aref(me2counter, (
VALUE) me);
5674rb_get_coverages(
void)
5676 return GET_VM()->coverages;
5680rb_get_coverage_mode(
void)
5682 return GET_VM()->coverage_mode;
5686rb_set_coverages(
VALUE coverages,
int mode,
VALUE me2counter)
5688 GET_VM()->coverages = coverages;
5689 GET_VM()->me2counter = me2counter;
5690 GET_VM()->coverage_mode = mode;
5694rb_resume_coverages(
void)
5696 int mode = GET_VM()->coverage_mode;
5697 VALUE me2counter = GET_VM()->me2counter;
5698 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);
5699 if (mode & COVERAGE_TARGET_BRANCHES) {
5700 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);
5702 if (mode & COVERAGE_TARGET_METHODS) {
5708rb_suspend_coverages(
void)
5711 if (GET_VM()->coverage_mode & COVERAGE_TARGET_BRANCHES) {
5714 if (GET_VM()->coverage_mode & COVERAGE_TARGET_METHODS) {
5721rb_reset_coverages(
void)
5723 rb_clear_coverages();
5724 rb_iseq_remove_coverage_all();
5725 GET_VM()->coverages =
Qfalse;
5729rb_default_coverage(
int n)
5731 VALUE coverage = rb_ary_hidden_new_fill(3);
5733 int mode = GET_VM()->coverage_mode;
5735 if (mode & COVERAGE_TARGET_LINES) {
5736 lines = n > 0 ? rb_ary_hidden_new_fill(n) : rb_ary_hidden_new(0);
5738 RARRAY_ASET(coverage, COVERAGE_INDEX_LINES, lines);
5740 if (mode & COVERAGE_TARGET_BRANCHES) {
5741 branches = rb_ary_hidden_new_fill(2);
5763 VALUE structure = rb_hash_new();
5769 RARRAY_ASET(coverage, COVERAGE_INDEX_BRANCHES, branches);
5775uninterruptible_exit(
VALUE v)
5778 rb_ary_pop(cur_th->pending_interrupt_mask_stack);
5780 cur_th->pending_interrupt_queue_checked = 0;
5781 if (!rb_threadptr_pending_interrupt_empty_p(cur_th)) {
5782 RUBY_VM_SET_INTERRUPT(cur_th->ec);
5790 VALUE interrupt_mask = rb_ident_hash_new();
5793 rb_hash_aset(interrupt_mask, rb_cObject, sym_never);
5795 rb_ary_push(cur_th->pending_interrupt_mask_stack, interrupt_mask);
5799 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.
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implenentation 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 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.
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
static bool rb_enc_asciicompat(rb_encoding *enc)
Queries if the passed encoding is in some sense compatible with ASCII.
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_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__.
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.
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.
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.
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.
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...
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.
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.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.