61#undef __USE_FORTIFY_LEVEL
62#define __USE_FORTIFY_LEVEL 0
66#include "ruby/internal/config.h"
73#define TH_SCHED(th) (&(th)->ractor->threads.sched)
75#include "eval_intern.h"
78#include "internal/class.h"
79#include "internal/cont.h"
80#include "internal/error.h"
81#include "internal/eval.h"
82#include "internal/gc.h"
83#include "internal/hash.h"
84#include "internal/io.h"
85#include "internal/object.h"
86#include "internal/proc.h"
88#include "internal/signal.h"
89#include "internal/thread.h"
90#include "internal/time.h"
91#include "internal/warnings.h"
99#include "ractor_core.h"
103#include "ccan/list/list.h"
105#ifndef USE_NATIVE_THREAD_PRIORITY
106#define USE_NATIVE_THREAD_PRIORITY 0
107#define RUBY_THREAD_PRIORITY_MAX 3
108#define RUBY_THREAD_PRIORITY_MIN -3
111static VALUE rb_cThreadShield;
112static VALUE cThGroup;
114static VALUE sym_immediate;
115static VALUE sym_on_blocking;
116static VALUE sym_never;
118static uint32_t thread_default_quantum_ms = 100;
120#define THREAD_LOCAL_STORAGE_INITIALISED FL_USER13
121#define THREAD_LOCAL_STORAGE_INITIALISED_P(th) RB_FL_TEST_RAW((th), THREAD_LOCAL_STORAGE_INITIALISED)
124rb_thread_local_storage(
VALUE thread)
126 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
134 SLEEP_DEADLOCKABLE = 0x01,
135 SLEEP_SPURIOUS_CHECK = 0x02,
136 SLEEP_ALLOW_SPURIOUS = 0x04,
137 SLEEP_NO_CHECKINTS = 0x08,
140static void sleep_forever(rb_thread_t *th,
unsigned int fl);
141static int sleep_hrtime(rb_thread_t *, rb_hrtime_t,
unsigned int fl);
143static void rb_thread_sleep_deadly_allow_spurious_wakeup(
VALUE blocker,
VALUE timeout, rb_hrtime_t end);
144static int rb_threadptr_dead(rb_thread_t *th);
145static void rb_check_deadlock(rb_ractor_t *r);
146static int rb_threadptr_pending_interrupt_empty_p(
const rb_thread_t *th);
147static const char *thread_status_name(rb_thread_t *th,
int detail);
148static int hrtime_update_expire(rb_hrtime_t *,
const rb_hrtime_t);
149NORETURN(
static void async_bug_fd(
const char *mesg,
int errno_arg,
int fd));
150MAYBE_UNUSED(
static int consume_communication_pipe(
int fd));
153static rb_internal_thread_specific_key_t specific_key_count;
157#define THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
160 enum rb_thread_status prev_status;
163static int unblock_function_set(rb_thread_t *th,
rb_unblock_function_t *func,
void *arg,
int fail_if_interrupted);
164static void unblock_function_clear(rb_thread_t *th);
170#define THREAD_BLOCKING_BEGIN(th) do { \
171 struct rb_thread_sched * const sched = TH_SCHED(th); \
172 RB_VM_SAVE_MACHINE_CONTEXT(th); \
173 thread_sched_to_waiting((sched), (th), true);
175#define THREAD_BLOCKING_END(th) \
176 thread_sched_to_running((sched), (th)); \
177 rb_ractor_thread_switch(th->ractor, th, false); \
181#ifdef HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR_CONSTANT_P
182#define only_if_constant(expr, notconst) __builtin_choose_expr(__builtin_constant_p(expr), (expr), (notconst))
184#define only_if_constant(expr, notconst) (__builtin_constant_p(expr) ? (expr) : (notconst))
187#define only_if_constant(expr, notconst) notconst
189#define BLOCKING_REGION(th, exec, ubf, ubfarg, fail_if_interrupted) do { \
190 struct rb_blocking_region_buffer __region; \
191 if (blocking_region_begin(th, &__region, (ubf), (ubfarg), fail_if_interrupted) || \
193 !only_if_constant(fail_if_interrupted, TRUE)) { \
196 RB_VM_SAVE_MACHINE_CONTEXT(th); \
197 thread_sched_to_waiting(TH_SCHED(th), th, false); \
199 blocking_region_end(th, &__region); \
207#define RUBY_VM_CHECK_INTS_BLOCKING(ec) vm_check_ints_blocking(ec)
209vm_check_ints_blocking(rb_execution_context_t *ec)
211#ifdef RUBY_ASSERT_CRITICAL_SECTION
212 VM_ASSERT(ruby_assert_critical_section_entered == 0);
215 rb_thread_t *th = rb_ec_thread_ptr(ec);
217 if (LIKELY(rb_threadptr_pending_interrupt_empty_p(th))) {
218 if (LIKELY(!RUBY_VM_INTERRUPTED_ANY(ec)))
return FALSE;
221 th->pending_interrupt_queue_checked = 0;
222 RUBY_VM_SET_INTERRUPT(ec);
225 int result = rb_threadptr_execute_interrupts(th, 1);
228 if (result || RUBY_VM_INTERRUPTED(ec)) {
230 if (scheduler !=
Qnil) {
239rb_vm_check_ints_blocking(rb_execution_context_t *ec)
241 return vm_check_ints_blocking(ec);
249#if defined(HAVE_POLL)
250# if defined(__linux__)
253# if defined(__FreeBSD_version) && __FreeBSD_version >= 1100000
256# define POLLERR_SET (POLLHUP | POLLERR)
261timeout_prepare(rb_hrtime_t **to, rb_hrtime_t *rel, rb_hrtime_t *end,
265 *rel = rb_timeval2hrtime(timeout);
266 *end = rb_hrtime_add(rb_hrtime_now(), *rel);
274MAYBE_UNUSED(NOINLINE(
static int thread_start_func_2(rb_thread_t *th,
VALUE *stack_start)));
275MAYBE_UNUSED(
static bool th_has_dedicated_nt(
const rb_thread_t *th));
276MAYBE_UNUSED(
static int waitfd_to_waiting_flag(
int wfd_event));
278#include THREAD_IMPL_SRC
285#ifndef BUSY_WAIT_SIGNALS
286# define BUSY_WAIT_SIGNALS (0)
290# define USE_EVENTFD (0)
293#include "thread_sync.c"
320unblock_function_set(rb_thread_t *th,
rb_unblock_function_t *func,
void *arg,
int fail_if_interrupted)
323 if (fail_if_interrupted) {
324 if (RUBY_VM_INTERRUPTED_ANY(th->ec)) {
329 RUBY_VM_CHECK_INTS(th->ec);
333 }
while (!th->ec->raised_flag && RUBY_VM_INTERRUPTED_ANY(th->ec) &&
336 VM_ASSERT(th->unblock.func == NULL);
338 th->unblock.func = func;
339 th->unblock.arg = arg;
346unblock_function_clear(rb_thread_t *th)
349 th->unblock.func = 0;
354threadptr_set_interrupt_locked(rb_thread_t *th,
bool trap)
358 RUBY_DEBUG_LOG(
"th:%u trap:%d", rb_th_serial(th), trap);
361 RUBY_VM_SET_TRAP_INTERRUPT(th->ec);
364 RUBY_VM_SET_INTERRUPT(th->ec);
367 if (th->unblock.func != NULL) {
368 (th->unblock.func)(th->unblock.arg);
376threadptr_set_interrupt(rb_thread_t *th,
int trap)
380 threadptr_set_interrupt_locked(th, trap);
387rb_threadptr_interrupt(rb_thread_t *th)
389 RUBY_DEBUG_LOG(
"th:%u", rb_th_serial(th));
390 threadptr_set_interrupt(th,
false);
394threadptr_trap_interrupt(rb_thread_t *th)
396 threadptr_set_interrupt(th,
true);
400terminate_all(rb_ractor_t *r,
const rb_thread_t *main_thread)
404 ccan_list_for_each(&r->threads.set, th, lt_node) {
405 if (th != main_thread) {
406 RUBY_DEBUG_LOG(
"terminate start th:%u status:%s", rb_th_serial(th), thread_status_name(th, TRUE));
408 rb_threadptr_pending_interrupt_enque(th, RUBY_FATAL_THREAD_TERMINATED);
409 rb_threadptr_interrupt(th);
411 RUBY_DEBUG_LOG(
"terminate done th:%u status:%s", rb_th_serial(th), thread_status_name(th, TRUE));
414 RUBY_DEBUG_LOG(
"main thread th:%u", rb_th_serial(th));
420rb_threadptr_join_list_wakeup(rb_thread_t *thread)
422 while (thread->join_list) {
426 thread->join_list = join_list->next;
428 rb_thread_t *target_thread = join_list->thread;
430 if (target_thread->scheduler !=
Qnil && join_list->fiber) {
434 rb_threadptr_interrupt(target_thread);
436 switch (target_thread->status) {
438 case THREAD_STOPPED_FOREVER:
439 target_thread->status = THREAD_RUNNABLE;
449rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th)
451 while (th->keeping_mutexes) {
452 rb_mutex_t *mutex = th->keeping_mutexes;
453 th->keeping_mutexes = mutex->next_mutex;
456 VM_ASSERT(mutex->ec_serial);
457 const char *error_message = rb_mutex_unlock_th(mutex, th, 0);
458 if (error_message) rb_bug(
"invalid keeping_mutexes: %s", error_message);
463rb_thread_terminate_all(rb_thread_t *th)
465 rb_ractor_t *cr = th->ractor;
466 rb_execution_context_t *
volatile ec = th->ec;
467 volatile int sleeping = 0;
469 if (cr->threads.main != th) {
470 rb_bug(
"rb_thread_terminate_all: called by child thread (%p, %p)",
471 (
void *)cr->threads.main, (
void *)th);
475 rb_threadptr_unlock_all_locking_mutexes(th);
478 if (EC_EXEC_TAG() == TAG_NONE) {
480 RUBY_DEBUG_LOG(
"th:%u", rb_th_serial(th));
482 terminate_all(cr, th);
484 while (rb_ractor_living_thread_num(cr) > 1) {
485 rb_hrtime_t rel = RB_HRTIME_PER_SEC;
491 native_sleep(th, &rel);
492 RUBY_VM_CHECK_INTS_BLOCKING(ec);
510void rb_threadptr_root_fiber_terminate(rb_thread_t *th);
511static void threadptr_interrupt_exec_cleanup(rb_thread_t *th);
514thread_cleanup_func_before_exec(
void *th_ptr)
516 rb_thread_t *th = th_ptr;
517 th->status = THREAD_KILLED;
520 th->ec->machine.stack_start = th->ec->machine.stack_end = NULL;
522 threadptr_interrupt_exec_cleanup(th);
523 rb_threadptr_root_fiber_terminate(th);
527thread_cleanup_func(
void *th_ptr,
int atfork)
529 rb_thread_t *th = th_ptr;
531 th->locking_mutex =
Qfalse;
532 thread_cleanup_func_before_exec(th_ptr);
535 native_thread_destroy_atfork(th->nt);
544rb_thread_free_native_thread(
void *th_ptr)
546 rb_thread_t *th = th_ptr;
548 native_thread_destroy_atfork(th->nt);
552static VALUE rb_threadptr_raise(rb_thread_t *,
int,
VALUE *);
556ruby_thread_init_stack(rb_thread_t *th,
void *local_in_parent_frame)
558 native_thread_init_stack(th, local_in_parent_frame);
562rb_vm_proc_local_ep(
VALUE proc)
564 const VALUE *ep = vm_proc_ep(proc);
567 return rb_vm_ep_local_ep(ep);
576 int argc,
const VALUE *argv,
int kw_splat,
VALUE passed_block_handler);
579thread_do_start_proc(rb_thread_t *th)
581 VALUE args = th->invoke_arg.proc.args;
582 const VALUE *args_ptr;
584 VALUE procval = th->invoke_arg.proc.proc;
586 GetProcPtr(procval, proc);
588 th->ec->errinfo =
Qnil;
589 th->ec->root_lep = rb_vm_proc_local_ep(procval);
590 th->ec->root_svar =
Qfalse;
592 vm_check_ints_blocking(th->ec);
594 if (th->invoke_type == thread_invoke_type_ractor_proc) {
595 VALUE self = rb_ractor_self(th->ractor);
596 th->thgroup = th->ractor->thgroup_default =
rb_obj_alloc(cThGroup);
601 rb_ractor_receive_parameters(th->ec, th->ractor, args_len, (
VALUE *)args_ptr);
602 vm_check_ints_blocking(th->ec);
604 return rb_vm_invoke_proc_with_self(
607 th->invoke_arg.proc.kw_splat,
608 VM_BLOCK_HANDLER_NONE
617 th->invoke_arg.proc.args =
Qnil;
623 vm_check_ints_blocking(th->ec);
625 return rb_vm_invoke_proc(
628 th->invoke_arg.proc.kw_splat,
629 VM_BLOCK_HANDLER_NONE
635thread_do_start(rb_thread_t *th)
637 native_set_thread_name(th);
640 switch (th->invoke_type) {
641 case thread_invoke_type_proc:
642 result = thread_do_start_proc(th);
645 case thread_invoke_type_ractor_proc:
646 result = thread_do_start_proc(th);
647 rb_ractor_atexit(th->ec, result);
650 case thread_invoke_type_func:
651 result = (*th->invoke_arg.func.func)(th->invoke_arg.func.arg);
654 case thread_invoke_type_none:
655 rb_bug(
"unreachable");
661void rb_ec_clear_current_thread_trace_func(
const rb_execution_context_t *ec);
664thread_start_func_2(rb_thread_t *th,
VALUE *stack_start)
666 RUBY_DEBUG_LOG(
"th:%u", rb_th_serial(th));
667 VM_ASSERT(th != th->vm->ractor.main_thread);
669 enum ruby_tag_type state;
671 rb_thread_t *ractor_main_th = th->ractor->threads.main;
674 if (rb_ractor_status_p(th->ractor, ractor_blocking)) {
677 rb_vm_ractor_blocking_cnt_dec(th->vm, th->ractor, __FILE__, __LINE__);
678 rb_ractor_t *r = th->ractor;
679 r->r_stdin = rb_io_prep_stdin();
680 r->r_stdout = rb_io_prep_stdout();
681 r->r_stderr = rb_io_prep_stderr();
687 VM_ASSERT(UNDEF_P(th->value));
689 int fiber_scheduler_closed = 0, event_thread_end_hooked = 0;
694 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
697 result = thread_do_start(th);
700 if (!fiber_scheduler_closed) {
701 fiber_scheduler_closed = 1;
705 if (!event_thread_end_hooked) {
706 event_thread_end_hooked = 1;
710 if (state == TAG_NONE) {
715 errinfo = th->ec->errinfo;
717 VALUE exc = rb_vm_make_jump_tag_but_local_jump(state,
Qundef);
718 if (!
NIL_P(exc)) errinfo = exc;
720 if (state == TAG_FATAL) {
721 if (th->invoke_type == thread_invoke_type_ractor_proc) {
722 rb_ractor_atexit(th->ec,
Qnil);
727 if (th->invoke_type == thread_invoke_type_ractor_proc) {
728 rb_ractor_atexit_exception(th->ec);
734 if (th->report_on_exception) {
735 VALUE mesg = rb_thread_to_s(th->self);
736 rb_str_cat_cstr(mesg,
" terminated with exception (report_on_exception is true):\n");
737 rb_write_error_str(mesg);
738 rb_ec_error_print(th->ec, errinfo);
741 if (th->invoke_type == thread_invoke_type_ractor_proc) {
742 rb_ractor_atexit_exception(th->ec);
745 if (th->vm->thread_abort_on_exception ||
757 VM_ASSERT(!UNDEF_P(th->value));
759 rb_threadptr_join_list_wakeup(th);
760 rb_threadptr_unlock_all_locking_mutexes(th);
762 if (th->invoke_type == thread_invoke_type_ractor_proc) {
763 rb_thread_terminate_all(th);
764 rb_ractor_teardown(th->ec);
767 th->status = THREAD_KILLED;
768 RUBY_DEBUG_LOG(
"killed th:%u", rb_th_serial(th));
770 if (th->vm->ractor.main_thread == th) {
776 rb_threadptr_raise(ractor_main_th, 1, &errinfo);
781 rb_ec_clear_current_thread_trace_func(th->ec);
784 if (th->locking_mutex !=
Qfalse) {
785 rb_bug(
"thread_start_func_2: locking_mutex must not be set (%p:%"PRIxVALUE
")",
786 (
void *)th, th->locking_mutex);
789 if (ractor_main_th->status == THREAD_KILLED &&
790 th->ractor->threads.cnt <= 2 ) {
792 rb_threadptr_interrupt(ractor_main_th);
795 rb_check_deadlock(th->ractor);
797 rb_fiber_close(th->ec->fiber_ptr);
799 thread_cleanup_func(th, FALSE);
800 VM_ASSERT(th->ec->vm_stack == NULL);
802 if (th->invoke_type == thread_invoke_type_ractor_proc) {
806 thread_sched_to_dead(TH_SCHED(th), th);
807 rb_ractor_living_threads_remove(th->ractor, th);
810 rb_ractor_living_threads_remove(th->ractor, th);
811 thread_sched_to_dead(TH_SCHED(th), th);
818 enum thread_invoke_type type;
831static void thread_specific_storage_alloc(rb_thread_t *th);
836 rb_execution_context_t *ec = GET_EC();
837 rb_thread_t *th = rb_thread_ptr(thval), *current_th = rb_ec_thread_ptr(ec);
840 thread_specific_storage_alloc(th);
844 "can't start a new thread (frozen ThreadGroup)");
847 rb_fiber_inherit_storage(ec, th->ec->fiber_ptr);
849 switch (params->type) {
850 case thread_invoke_type_proc:
851 th->invoke_type = thread_invoke_type_proc;
852 th->invoke_arg.proc.args = params->args;
853 th->invoke_arg.proc.proc = params->proc;
857 case thread_invoke_type_ractor_proc:
858#if RACTOR_CHECK_MODE > 0
859 rb_ractor_setup_belonging_to(thval, rb_ractor_id(params->g));
861 th->invoke_type = thread_invoke_type_ractor_proc;
862 th->ractor = params->g;
863 th->ec->ractor_id = rb_ractor_id(th->ractor);
864 th->ractor->threads.main = th;
865 th->invoke_arg.proc.proc = rb_proc_isolate_bang(params->proc,
Qnil);
868 rb_ractor_send_parameters(ec, params->g, params->args);
871 case thread_invoke_type_func:
872 th->invoke_type = thread_invoke_type_func;
873 th->invoke_arg.func.func = params->fn;
874 th->invoke_arg.func.arg = (
void *)params->args;
878 rb_bug(
"unreachable");
881 th->priority = current_th->priority;
882 th->thgroup = current_th->thgroup;
885 th->pending_interrupt_queue_checked = 0;
886 th->pending_interrupt_mask_stack =
rb_ary_dup(current_th->pending_interrupt_mask_stack);
887 RBASIC_CLEAR_CLASS(th->pending_interrupt_mask_stack);
891 RUBY_DEBUG_LOG(
"r:%u th:%u", rb_ractor_id(th->ractor), rb_th_serial(th));
893 rb_ractor_living_threads_insert(th->ractor, th);
896 err = native_thread_create(th);
898 th->status = THREAD_KILLED;
899 rb_ractor_living_threads_remove(th->ractor, th);
905#define threadptr_initialized(th) ((th)->invoke_type != thread_invoke_type_none)
928thread_s_new(
int argc,
VALUE *argv,
VALUE klass)
931 VALUE thread = rb_thread_alloc(klass);
933 if (GET_RACTOR()->threads.main->status == THREAD_KILLED) {
938 th = rb_thread_ptr(thread);
939 if (!threadptr_initialized(th)) {
940 rb_raise(
rb_eThreadError,
"uninitialized thread - check '%"PRIsVALUE
"#initialize'",
960 .type = thread_invoke_type_proc,
964 return thread_create_core(rb_thread_alloc(klass), ¶ms);
968threadptr_invoke_proc_location(rb_thread_t *th)
970 if (th->invoke_type == thread_invoke_type_proc) {
971 return rb_proc_location(th->invoke_arg.proc.proc);
982 rb_thread_t *th = rb_thread_ptr(thread);
987 else if (th->invoke_type != thread_invoke_type_none) {
988 VALUE loc = threadptr_invoke_proc_location(th);
991 "already initialized thread - %"PRIsVALUE
":%"PRIsVALUE,
1000 .type = thread_invoke_type_proc,
1004 return thread_create_core(thread, ¶ms);
1012 .type = thread_invoke_type_func,
1016 return thread_create_core(rb_thread_alloc(
rb_cThread), ¶ms);
1020rb_thread_create_ractor(rb_ractor_t *r,
VALUE args,
VALUE proc)
1023 .type = thread_invoke_type_ractor_proc,
1028 return thread_create_core(rb_thread_alloc(
rb_cThread), ¶ms);
1034 rb_thread_t *target;
1040remove_from_join_list(
VALUE arg)
1043 rb_thread_t *target_thread = p->target;
1045 if (target_thread->status != THREAD_KILLED) {
1048 while (*join_list) {
1049 if (*join_list == p->waiter) {
1050 *join_list = (*join_list)->next;
1054 join_list = &(*join_list)->next;
1062thread_finished(rb_thread_t *th)
1064 return th->status == THREAD_KILLED || !UNDEF_P(th->value);
1068thread_join_sleep(
VALUE arg)
1071 rb_thread_t *target_th = p->target, *th = p->waiter->thread;
1072 rb_hrtime_t end = 0, *limit = p->limit;
1075 end = rb_hrtime_add(*limit, rb_hrtime_now());
1078 while (!thread_finished(target_th)) {
1082 if (scheduler !=
Qnil) {
1086 sleep_forever(th, SLEEP_DEADLOCKABLE | SLEEP_ALLOW_SPURIOUS | SLEEP_NO_CHECKINTS);
1090 if (hrtime_update_expire(limit, end)) {
1091 RUBY_DEBUG_LOG(
"timeout target_th:%u", rb_th_serial(target_th));
1095 if (scheduler !=
Qnil) {
1096 VALUE timeout = rb_float_new(hrtime2double(*limit));
1100 th->status = THREAD_STOPPED;
1101 native_sleep(th, limit);
1104 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1105 th->status = THREAD_RUNNABLE;
1107 RUBY_DEBUG_LOG(
"interrupted target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1114thread_join(rb_thread_t *target_th,
VALUE timeout, rb_hrtime_t *limit)
1116 rb_execution_context_t *ec = GET_EC();
1117 rb_thread_t *th = ec->thread_ptr;
1118 rb_fiber_t *fiber = ec->fiber_ptr;
1120 if (th == target_th) {
1121 rb_raise(
rb_eThreadError,
"Target thread must not be current thread");
1124 if (th->ractor->threads.main == target_th) {
1128 RUBY_DEBUG_LOG(
"target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1130 if (target_th->status != THREAD_KILLED) {
1132 waiter.next = target_th->join_list;
1134 waiter.fiber = rb_fiberptr_blocking(fiber) ? NULL : fiber;
1135 target_th->join_list = &waiter;
1138 arg.waiter = &waiter;
1139 arg.target = target_th;
1140 arg.timeout = timeout;
1148 RUBY_DEBUG_LOG(
"success target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1150 if (target_th->ec->errinfo !=
Qnil) {
1151 VALUE err = target_th->ec->errinfo;
1156 RUBY_DEBUG_LOG(
"terminated target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE));
1161 if (err == RUBY_FATAL_FIBER_KILLED) {
1165 rb_bug(
"thread_join: Fixnum (%d) should not reach here.",
FIX2INT(err));
1168 else if (THROW_DATA_P(target_th->ec->errinfo)) {
1169 rb_bug(
"thread_join: THROW_DATA should not reach here.");
1176 return target_th->self;
1219thread_join_m(
int argc,
VALUE *argv,
VALUE self)
1222 rb_hrtime_t rel = 0, *limit = 0;
1233 if (
NIL_P(timeout)) {
1237 rel = rb_sec2hrtime(NUM2TIMET(timeout));
1241 limit = double2hrtime(&rel,
rb_num2dbl(timeout));
1244 return thread_join(rb_thread_ptr(self), timeout, limit);
1262thread_value(
VALUE self)
1264 rb_thread_t *th = rb_thread_ptr(self);
1265 thread_join(th,
Qnil, 0);
1266 if (UNDEF_P(th->value)) {
1280#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
1281 if (clock_gettime(CLOCK_MONOTONIC, ts) == 0)
1291NOINLINE(rb_hrtime_t rb_hrtime_now(
void));
1298 return rb_timespec2hrtime(&ts);
1305COMPILER_WARNING_PUSH
1306#if defined(__GNUC__) && __GNUC__ == 7 && __GNUC_MINOR__ <= 3
1307COMPILER_WARNING_IGNORED(-Wmaybe-uninitialized)
1310#define PRIu64 PRI_64_PREFIX "u"
1318hrtime_update_expire(rb_hrtime_t *timeout,
const rb_hrtime_t end)
1320 rb_hrtime_t now = rb_hrtime_now();
1322 if (now > end)
return 1;
1324 RUBY_DEBUG_LOG(
"%"PRIu64
" > %"PRIu64
"", (uint64_t)end, (uint64_t)now);
1326 *timeout = end - now;
1332sleep_hrtime(rb_thread_t *th, rb_hrtime_t rel,
unsigned int fl)
1334 enum rb_thread_status prev_status = th->status;
1336 rb_hrtime_t end = rb_hrtime_add(rb_hrtime_now(), rel);
1338 th->status = THREAD_STOPPED;
1339 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1340 while (th->status == THREAD_STOPPED) {
1341 native_sleep(th, &rel);
1342 woke = vm_check_ints_blocking(th->ec);
1343 if (woke && !(fl & SLEEP_SPURIOUS_CHECK))
1345 if (hrtime_update_expire(&rel, end))
1349 th->status = prev_status;
1354sleep_hrtime_until(rb_thread_t *th, rb_hrtime_t end,
unsigned int fl)
1356 enum rb_thread_status prev_status = th->status;
1358 rb_hrtime_t rel = rb_hrtime_sub(end, rb_hrtime_now());
1360 th->status = THREAD_STOPPED;
1361 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1362 while (th->status == THREAD_STOPPED) {
1363 native_sleep(th, &rel);
1364 woke = vm_check_ints_blocking(th->ec);
1365 if (woke && !(fl & SLEEP_SPURIOUS_CHECK))
1367 if (hrtime_update_expire(&rel, end))
1371 th->status = prev_status;
1376sleep_forever(rb_thread_t *th,
unsigned int fl)
1378 enum rb_thread_status prev_status = th->status;
1379 enum rb_thread_status status;
1382 status = fl & SLEEP_DEADLOCKABLE ? THREAD_STOPPED_FOREVER : THREAD_STOPPED;
1383 th->status = status;
1385 if (!(fl & SLEEP_NO_CHECKINTS)) RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
1387 while (th->status == status) {
1388 if (fl & SLEEP_DEADLOCKABLE) {
1389 rb_ractor_sleeper_threads_inc(th->ractor);
1390 rb_check_deadlock(th->ractor);
1393 native_sleep(th, 0);
1395 if (fl & SLEEP_DEADLOCKABLE) {
1396 rb_ractor_sleeper_threads_dec(th->ractor);
1398 if (fl & SLEEP_ALLOW_SPURIOUS) {
1402 woke = vm_check_ints_blocking(th->ec);
1404 if (woke && !(fl & SLEEP_SPURIOUS_CHECK)) {
1408 th->status = prev_status;
1414 RUBY_DEBUG_LOG(
"forever");
1415 sleep_forever(GET_THREAD(), SLEEP_SPURIOUS_CHECK);
1421 RUBY_DEBUG_LOG(
"deadly");
1422 sleep_forever(GET_THREAD(), SLEEP_DEADLOCKABLE|SLEEP_SPURIOUS_CHECK);
1426rb_thread_sleep_deadly_allow_spurious_wakeup(
VALUE blocker,
VALUE timeout, rb_hrtime_t end)
1428 rb_thread_t *th = GET_THREAD();
1430 if (scheduler !=
Qnil) {
1434 RUBY_DEBUG_LOG(
"...");
1436 sleep_hrtime_until(th, end, SLEEP_SPURIOUS_CHECK);
1439 sleep_forever(th, SLEEP_DEADLOCKABLE);
1447 rb_thread_t *th = GET_THREAD();
1449 sleep_hrtime(th, rb_timeval2hrtime(&time), SLEEP_SPURIOUS_CHECK);
1453rb_ec_check_ints(rb_execution_context_t *ec)
1455 RUBY_VM_CHECK_INTS_BLOCKING(ec);
1468 rb_ec_check_ints(GET_EC());
1476rb_thread_check_trap_pending(
void)
1478 return rb_signal_buff_size() != 0;
1485 return (
int)RUBY_VM_INTERRUPTED(rb_thread_ptr(thval)->ec);
1495rb_thread_schedule_limits(uint32_t limits_us)
1498 rb_thread_t *th = GET_THREAD();
1499 RUBY_DEBUG_LOG(
"us:%u", (
unsigned int)limits_us);
1501 if (th->running_time_us >= limits_us) {
1502 RUBY_DEBUG_LOG(
"switch %s",
"start");
1504 RB_VM_SAVE_MACHINE_CONTEXT(th);
1505 thread_sched_yield(TH_SCHED(th), th);
1506 rb_ractor_thread_switch(th->ractor, th,
true);
1508 RUBY_DEBUG_LOG(
"switch %s",
"done");
1516 rb_thread_schedule_limits(0);
1517 RUBY_VM_CHECK_INTS(GET_EC());
1526#ifdef RUBY_ASSERT_CRITICAL_SECTION
1527 VM_ASSERT(ruby_assert_critical_section_entered == 0);
1529 VM_ASSERT(th == GET_THREAD());
1531 region->prev_status = th->status;
1532 if (unblock_function_set(th, ubf, arg, fail_if_interrupted)) {
1533 th->blocking_region_buffer = region;
1534 th->status = THREAD_STOPPED;
1535 rb_ractor_blocking_threads_inc(th->ractor, __FILE__, __LINE__);
1537 RUBY_DEBUG_LOG(
"thread_id:%p", (
void *)th->nt->thread_id);
1549 unblock_function_clear(th);
1551 unregister_ubf_list(th);
1553 thread_sched_to_running(TH_SCHED(th), th);
1554 rb_ractor_thread_switch(th->ractor, th,
false);
1556 th->blocking_region_buffer = 0;
1557 rb_ractor_blocking_threads_dec(th->ractor, __FILE__, __LINE__);
1558 if (th->status == THREAD_STOPPED) {
1559 th->status = region->prev_status;
1562 RUBY_DEBUG_LOG(
"end");
1566 VM_ASSERT(th == GET_THREAD());
1586 *unblock_function = ubf_select;
1594rb_nogvl(
void *(*func)(
void *),
void *data1,
1600 if (scheduler !=
Qnil) {
1605 if (!UNDEF_P(result)) {
1606 rb_errno_set(state.saved_errno);
1607 return state.result;
1613 rb_execution_context_t *ec = GET_EC();
1614 rb_thread_t *th = rb_ec_thread_ptr(ec);
1615 rb_vm_t *vm = rb_ec_vm_ptr(ec);
1616 bool is_main_thread = vm->ractor.main_thread == th;
1617 int saved_errno = 0;
1619 rb_thread_resolve_unblock_function(&ubf, &data2, th);
1621 if (ubf && rb_ractor_living_thread_num(th->ractor) == 1 && is_main_thread) {
1623 vm->ubf_async_safe = 1;
1627 rb_vm_t *
volatile saved_vm = vm;
1628 BLOCKING_REGION(th, {
1634 if (is_main_thread) vm->ubf_async_safe = 0;
1637 RUBY_VM_CHECK_INTS_BLOCKING(ec);
1640 rb_errno_set(saved_errno);
1741 return rb_nogvl(func, data1, ubf, data2, 0);
1745waitfd_to_waiting_flag(
int wfd_event)
1747 return wfd_event << 1;
1750static struct ccan_list_head *
1751rb_io_blocking_operations(
struct rb_io *io)
1753 rb_serial_t fork_generation = GET_VM()->fork_gen;
1757 if (io->fork_generation != fork_generation) {
1759 io->fork_generation = fork_generation;
1778 ccan_list_add(rb_io_blocking_operations(io), &blocking_operation->list);
1784 ccan_list_del(&blocking_operation->list);
1793io_blocking_operation_exit(
VALUE _arguments)
1798 rb_io_blocking_operation_pop(arguments->io, blocking_operation);
1800 rb_io_t *io = arguments->io;
1801 rb_thread_t *thread = io->closing_ec->thread_ptr;
1802 rb_fiber_t *fiber = io->closing_ec->fiber_ptr;
1804 if (thread->scheduler !=
Qnil) {
1828 VALUE wakeup_mutex = io->wakeup_mutex;
1831 blocking_operation->ec = NULL;
1836 .blocking_operation = blocking_operation
1843 rb_io_blocking_operation_pop(io, blocking_operation);
1848rb_thread_io_blocking_operation_ensure(
VALUE _argument)
1852 rb_io_blocking_operation_exit(arguments->io, arguments->blocking_operation);
1876 rb_execution_context_t *ec = GET_EC();
1880 rb_io_blocking_operation_enter(io, &blocking_operation);
1884 .blocking_operation = &blocking_operation
1891thread_io_mn_schedulable(rb_thread_t *th,
int events,
const struct timeval *timeout)
1893#if defined(USE_MN_THREADS) && USE_MN_THREADS
1894 return !th_has_dedicated_nt(th) && (events || timeout) && th->blocking;
1902thread_io_wait_events(rb_thread_t *th,
int fd,
int events,
const struct timeval *timeout)
1904#if defined(USE_MN_THREADS) && USE_MN_THREADS
1905 if (thread_io_mn_schedulable(th, events, timeout)) {
1906 rb_hrtime_t rel, *prel;
1909 rel = rb_timeval2hrtime(timeout);
1916 VM_ASSERT(prel || (events & (RB_WAITFD_IN | RB_WAITFD_OUT)));
1918 if (thread_sched_wait_events(TH_SCHED(th), th, fd, waitfd_to_waiting_flag(events), prel)) {
1932blocking_call_retryable_p(
int r,
int eno)
1934 if (r != -1)
return false;
1938#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
1948rb_thread_mn_schedulable(
VALUE thval)
1950 rb_thread_t *th = rb_thread_ptr(thval);
1951 return th->mn_schedulable;
1955rb_thread_io_blocking_call(
struct rb_io* io, rb_blocking_function_t *func,
void *data1,
int events)
1957 rb_execution_context_t *
volatile ec = GET_EC();
1958 rb_thread_t *
volatile th = rb_ec_thread_ptr(ec);
1960 RUBY_DEBUG_LOG(
"th:%u fd:%d ev:%d", rb_th_serial(th), io->
fd, events);
1963 volatile int saved_errno = 0;
1964 enum ruby_tag_type state;
1965 volatile bool prev_mn_schedulable = th->mn_schedulable;
1966 th->mn_schedulable = thread_io_mn_schedulable(th, events, NULL);
1979 rb_io_blocking_operation_enter(io, &blocking_operation);
1983 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1984 volatile enum ruby_tag_type saved_state = state;
1986 BLOCKING_REGION(th, {
1988 saved_errno =
errno;
1989 }, ubf_select, th, FALSE);
1993 blocking_call_retryable_p((
int)val, saved_errno) &&
1994 thread_io_wait_events(th, fd, events, NULL)) {
1995 RUBY_VM_CHECK_INTS_BLOCKING(ec);
1999 RUBY_VM_CHECK_INTS_BLOCKING(ec);
2001 state = saved_state;
2005 th = rb_ec_thread_ptr(ec);
2006 th->mn_schedulable = prev_mn_schedulable;
2009 rb_io_blocking_operation_exit(io, &blocking_operation);
2012 EC_JUMP_TAG(ec, state);
2016 if (saved_errno == ETIMEDOUT) {
2020 errno = saved_errno;
2026rb_thread_io_blocking_region(
struct rb_io *io, rb_blocking_function_t *func,
void *data1)
2028 return rb_thread_io_blocking_call(io, func, data1, 0);
2065 rb_thread_t *th = ruby_thread_from_native();
2076 fprintf(stderr,
"[BUG] rb_thread_call_with_gvl() is called by non-ruby thread\n");
2081 prev_unblock = th->unblock;
2085 return (*func)(data1);
2088 blocking_region_end(th, brb);
2092 int released = blocking_region_begin(th, brb, prev_unblock.func, prev_unblock.arg, FALSE);
2094 RB_VM_SAVE_MACHINE_CONTEXT(th);
2095 thread_sched_to_waiting(TH_SCHED(th), th,
true);
2106 rb_thread_t *th = ruby_thread_from_native();
2108 if (th && th->blocking_region_buffer == 0) {
2125thread_s_pass(
VALUE klass)
2150rb_threadptr_pending_interrupt_clear(rb_thread_t *th)
2156rb_threadptr_pending_interrupt_enque(rb_thread_t *th,
VALUE v)
2159 th->pending_interrupt_queue_checked = 0;
2163threadptr_check_pending_interrupt_queue(rb_thread_t *th)
2165 if (!th->pending_interrupt_queue) {
2170enum handle_interrupt_timing {
2172 INTERRUPT_IMMEDIATE,
2173 INTERRUPT_ON_BLOCKING,
2177static enum handle_interrupt_timing
2178rb_threadptr_pending_interrupt_from_symbol(rb_thread_t *th,
VALUE sym)
2180 if (sym == sym_immediate) {
2181 return INTERRUPT_IMMEDIATE;
2183 else if (sym == sym_on_blocking) {
2184 return INTERRUPT_ON_BLOCKING;
2186 else if (sym == sym_never) {
2187 return INTERRUPT_NEVER;
2194static enum handle_interrupt_timing
2195rb_threadptr_pending_interrupt_check_mask(rb_thread_t *th,
VALUE err)
2198 long mask_stack_len =
RARRAY_LEN(th->pending_interrupt_mask_stack);
2203 for (i=0; i<mask_stack_len; i++) {
2204 mask = mask_stack[mask_stack_len-(i+1)];
2209 return rb_threadptr_pending_interrupt_from_symbol(th, mask);
2221 klass =
RBASIC(mod)->klass;
2223 else if (mod != RCLASS_ORIGIN(mod)) {
2227 if ((sym = rb_hash_aref(mask, klass)) !=
Qnil) {
2228 return rb_threadptr_pending_interrupt_from_symbol(th, sym);
2233 return INTERRUPT_NONE;
2237rb_threadptr_pending_interrupt_empty_p(
const rb_thread_t *th)
2239 return RARRAY_LEN(th->pending_interrupt_queue) == 0;
2243rb_threadptr_pending_interrupt_include_p(rb_thread_t *th,
VALUE err)
2246 for (i=0; i<
RARRAY_LEN(th->pending_interrupt_queue); i++) {
2256rb_threadptr_pending_interrupt_deque(rb_thread_t *th,
enum handle_interrupt_timing timing)
2261 for (i=0; i<
RARRAY_LEN(th->pending_interrupt_queue); i++) {
2264 enum handle_interrupt_timing mask_timing = rb_threadptr_pending_interrupt_check_mask(th,
CLASS_OF(err));
2266 switch (mask_timing) {
2267 case INTERRUPT_ON_BLOCKING:
2268 if (timing != INTERRUPT_ON_BLOCKING) {
2272 case INTERRUPT_NONE:
2273 case INTERRUPT_IMMEDIATE:
2276 case INTERRUPT_NEVER:
2281 th->pending_interrupt_queue_checked = 1;
2285 if (rb_threadptr_pending_interrupt_empty_p(th)) {
2286 th->pending_interrupt_queue_checked = 1;
2293threadptr_pending_interrupt_active_p(rb_thread_t *th)
2300 if (th->pending_interrupt_queue_checked) {
2304 if (rb_threadptr_pending_interrupt_empty_p(th)) {
2316 if (val != sym_immediate && val != sym_on_blocking && val != sym_never) {
2317 rb_raise(rb_eArgError,
"unknown mask signature");
2325 if (
RTEST(*maskp)) {
2327 VALUE prev = *maskp;
2328 *maskp = rb_ident_hash_new();
2333 rb_hash_aset(*maskp, key, val);
2427rb_thread_s_handle_interrupt(
VALUE self,
VALUE mask_arg)
2430 rb_execution_context_t *
volatile ec = GET_EC();
2431 rb_thread_t *
volatile th = rb_ec_thread_ptr(ec);
2433 enum ruby_tag_type state;
2436 rb_raise(rb_eArgError,
"block is needed.");
2439 mask_arg = rb_to_hash_type(mask_arg);
2441 if (
OBJ_FROZEN(mask_arg) && rb_hash_compare_by_id_p(mask_arg)) {
2447 if (UNDEF_P(mask)) {
2458 rb_ary_push(th->pending_interrupt_mask_stack, mask);
2459 if (!rb_threadptr_pending_interrupt_empty_p(th)) {
2460 th->pending_interrupt_queue_checked = 0;
2461 RUBY_VM_SET_INTERRUPT(th->ec);
2464 EC_PUSH_TAG(th->ec);
2465 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2470 rb_ary_pop(th->pending_interrupt_mask_stack);
2471 if (!rb_threadptr_pending_interrupt_empty_p(th)) {
2472 th->pending_interrupt_queue_checked = 0;
2473 RUBY_VM_SET_INTERRUPT(th->ec);
2476 RUBY_VM_CHECK_INTS(th->ec);
2479 EC_JUMP_TAG(th->ec, state);
2496rb_thread_pending_interrupt_p(
int argc,
VALUE *argv,
VALUE target_thread)
2498 rb_thread_t *target_th = rb_thread_ptr(target_thread);
2500 if (!target_th->pending_interrupt_queue) {
2503 if (rb_threadptr_pending_interrupt_empty_p(target_th)) {
2507 VALUE err = argv[0];
2509 rb_raise(
rb_eTypeError,
"class or module required for rescue clause");
2511 return RBOOL(rb_threadptr_pending_interrupt_include_p(target_th, err));
2576rb_thread_s_pending_interrupt_p(
int argc,
VALUE *argv,
VALUE self)
2578 return rb_thread_pending_interrupt_p(argc, argv, GET_THREAD()->self);
2581NORETURN(
static void rb_threadptr_to_kill(rb_thread_t *th));
2584rb_threadptr_to_kill(rb_thread_t *th)
2586 VM_ASSERT(GET_THREAD() == th);
2587 rb_threadptr_pending_interrupt_clear(th);
2588 th->status = THREAD_RUNNABLE;
2590 th->ec->errinfo =
INT2FIX(TAG_FATAL);
2591 EC_JUMP_TAG(th->ec, TAG_FATAL);
2595threadptr_get_interrupts(rb_thread_t *th)
2597 rb_execution_context_t *ec = th->ec;
2601 old = ATOMIC_LOAD_RELAXED(ec->interrupt_flag);
2604 old = ATOMIC_CAS(ec->interrupt_flag, interrupt, interrupt & ec->interrupt_mask);
2605 }
while (old != interrupt);
2606 return interrupt & (
rb_atomic_t)~ec->interrupt_mask;
2609static void threadptr_interrupt_exec_exec(rb_thread_t *th);
2617rb_threadptr_execute_interrupts(rb_thread_t *th,
int blocking_timing)
2620 int postponed_job_interrupt = 0;
2623 VM_ASSERT(GET_THREAD() == th);
2625 if (th->ec->raised_flag)
return ret;
2627 while ((interrupt = threadptr_get_interrupts(th)) != 0) {
2629 int timer_interrupt;
2630 int pending_interrupt;
2632 int terminate_interrupt;
2634 timer_interrupt = interrupt & TIMER_INTERRUPT_MASK;
2635 pending_interrupt = interrupt & PENDING_INTERRUPT_MASK;
2636 postponed_job_interrupt = interrupt & POSTPONED_JOB_INTERRUPT_MASK;
2637 trap_interrupt = interrupt & TRAP_INTERRUPT_MASK;
2638 terminate_interrupt = interrupt & TERMINATE_INTERRUPT_MASK;
2640 if (interrupt & VM_BARRIER_INTERRUPT_MASK) {
2644 if (postponed_job_interrupt) {
2645 rb_postponed_job_flush(th->vm);
2648 if (trap_interrupt) {
2650 if (th == th->vm->ractor.main_thread) {
2651 enum rb_thread_status prev_status = th->status;
2653 th->status = THREAD_RUNNABLE;
2655 while ((sig = rb_get_next_signal()) != 0) {
2656 ret |= rb_signal_exec(th, sig);
2659 th->status = prev_status;
2662 if (!ccan_list_empty(&th->interrupt_exec_tasks)) {
2663 enum rb_thread_status prev_status = th->status;
2665 th->status = THREAD_RUNNABLE;
2667 threadptr_interrupt_exec_exec(th);
2669 th->status = prev_status;
2674 if (pending_interrupt && threadptr_pending_interrupt_active_p(th)) {
2675 VALUE err = rb_threadptr_pending_interrupt_deque(th, blocking_timing ? INTERRUPT_ON_BLOCKING : INTERRUPT_NONE);
2676 RUBY_DEBUG_LOG(
"err:%"PRIdVALUE, err);
2682 else if (err == RUBY_FATAL_THREAD_KILLED ||
2683 err == RUBY_FATAL_THREAD_TERMINATED ||
2685 terminate_interrupt = 1;
2688 if (err == th->vm->special_exceptions[ruby_error_stream_closed]) {
2690 err = ruby_vm_special_exception_copy(err);
2693 if (th->status == THREAD_STOPPED ||
2694 th->status == THREAD_STOPPED_FOREVER)
2695 th->status = THREAD_RUNNABLE;
2700 if (terminate_interrupt) {
2701 rb_threadptr_to_kill(th);
2704 if (timer_interrupt) {
2705 uint32_t limits_us = thread_default_quantum_ms * 1000;
2707 if (th->priority > 0)
2708 limits_us <<= th->priority;
2710 limits_us >>= -th->priority;
2712 if (th->status == THREAD_RUNNABLE)
2713 th->running_time_us += 10 * 1000;
2715 VM_ASSERT(th->ec->cfp);
2719 rb_thread_schedule_limits(limits_us);
2726rb_thread_execute_interrupts(
VALUE thval)
2728 rb_threadptr_execute_interrupts(rb_thread_ptr(thval), 1);
2732rb_threadptr_ready(rb_thread_t *th)
2734 rb_threadptr_interrupt(th);
2738rb_threadptr_raise(rb_thread_t *target_th,
int argc,
VALUE *argv)
2742 if (rb_threadptr_dead(target_th)) {
2750 exc = rb_make_exception(argc, argv);
2755 if (rb_threadptr_dead(target_th)) {
2759 rb_ec_setup_exception(GET_EC(), exc,
Qundef);
2760 rb_threadptr_pending_interrupt_enque(target_th, exc);
2761 rb_threadptr_interrupt(target_th);
2767rb_threadptr_signal_raise(rb_thread_t *th,
int sig)
2773 rb_threadptr_raise(th->vm->ractor.main_thread, 2, argv);
2777rb_threadptr_interrupt_raise(rb_thread_t *th)
2779 rb_thread_t *target_th = th->vm->ractor.main_thread;
2781 if (rb_threadptr_dead(target_th)) {
2790 if (rb_threadptr_dead(target_th)) {
2794 rb_ec_setup_exception(GET_EC(), exc,
Qundef);
2795 rb_threadptr_pending_interrupt_enque(target_th, exc);
2796 rb_threadptr_interrupt(target_th);
2800rb_threadptr_signal_exit(rb_thread_t *th)
2808 rb_threadptr_raise(th->vm->ractor.main_thread, 2, argv);
2812rb_ec_set_raised(rb_execution_context_t *ec)
2814 if (ec->raised_flag & RAISED_EXCEPTION) {
2817 ec->raised_flag |= RAISED_EXCEPTION;
2822rb_ec_reset_raised(rb_execution_context_t *ec)
2824 if (!(ec->raised_flag & RAISED_EXCEPTION)) {
2827 ec->raised_flag &= ~RAISED_EXCEPTION;
2852thread_io_close_notify_all(
VALUE _io)
2857 rb_vm_t *vm = io->closing_ec->thread_ptr->vm;
2858 VALUE error = vm->special_exceptions[ruby_error_stream_closed];
2861 ccan_list_for_each(rb_io_blocking_operations(io), blocking_operation, list) {
2862 rb_execution_context_t *ec = blocking_operation->ec;
2866 rb_thread_t *thread = ec->thread_ptr;
2869 if (thread->scheduler !=
Qnil) {
2875 rb_threadptr_pending_interrupt_enque(thread, error);
2876 rb_threadptr_interrupt(thread);
2883 return (
VALUE)count;
2887rb_thread_io_close_interrupt(
struct rb_io *io)
2890 if (io->closing_ec) {
2895 if (ccan_list_empty(rb_io_blocking_operations(io))) {
2900 rb_execution_context_t *ec = GET_EC();
2901 io->closing_ec = ec;
2905 rb_mutex_allow_trap(io->wakeup_mutex, 1);
2910 return (
size_t)result;
2914rb_thread_io_close_wait(
struct rb_io* io)
2916 VALUE wakeup_mutex = io->wakeup_mutex;
2924 while (!ccan_list_empty(rb_io_blocking_operations(io))) {
2930 io->wakeup_mutex =
Qnil;
2931 io->closing_ec = NULL;
2937 rb_warn(
"rb_thread_fd_close is deprecated (and is now a no-op).");
2961thread_raise_m(
int argc,
VALUE *argv,
VALUE self)
2963 rb_thread_t *target_th = rb_thread_ptr(self);
2964 const rb_thread_t *current_th = GET_THREAD();
2966 threadptr_check_pending_interrupt_queue(target_th);
2968 if (rb_threadptr_dead(target_th)) {
2972 VALUE exception = rb_exception_setup(argc, argv);
2973 rb_threadptr_pending_interrupt_enque(target_th, exception);
2974 rb_threadptr_interrupt(target_th);
2977 if (current_th == target_th) {
2978 RUBY_VM_CHECK_INTS(target_th->ec);
3001 rb_thread_t *target_th = rb_thread_ptr(thread);
3003 if (target_th->to_kill || target_th->status == THREAD_KILLED) {
3006 if (target_th == target_th->vm->ractor.main_thread) {
3010 RUBY_DEBUG_LOG(
"target_th:%u", rb_th_serial(target_th));
3012 if (target_th == GET_THREAD()) {
3014 rb_threadptr_to_kill(target_th);
3017 threadptr_check_pending_interrupt_queue(target_th);
3018 rb_threadptr_pending_interrupt_enque(target_th, RUBY_FATAL_THREAD_KILLED);
3019 rb_threadptr_interrupt(target_th);
3026rb_thread_to_be_killed(
VALUE thread)
3028 rb_thread_t *target_th = rb_thread_ptr(thread);
3030 if (target_th->to_kill || target_th->status == THREAD_KILLED) {
3072 rb_thread_t *th = GET_THREAD();
3105 rb_thread_t *target_th = rb_thread_ptr(thread);
3106 if (target_th->status == THREAD_KILLED)
return Qnil;
3108 rb_threadptr_ready(target_th);
3110 if (target_th->status == THREAD_STOPPED ||
3111 target_th->status == THREAD_STOPPED_FOREVER) {
3112 target_th->status = THREAD_RUNNABLE;
3154 "stopping only thread\n\tnote: use sleep to stop forever");
3187 return rb_ractor_thread_list();
3213 return rb_thread_list();
3219 return GET_THREAD()->self;
3232thread_s_current(
VALUE klass)
3240 return GET_RACTOR()->threads.main->self;
3251rb_thread_s_main(
VALUE klass)
3278rb_thread_s_abort_exc(
VALUE _)
3280 return RBOOL(GET_THREAD()->vm->thread_abort_on_exception);
3315rb_thread_s_abort_exc_set(
VALUE self,
VALUE val)
3317 GET_THREAD()->vm->thread_abort_on_exception =
RTEST(val);
3338rb_thread_abort_exc(
VALUE thread)
3340 return RBOOL(rb_thread_ptr(thread)->abort_on_exception);
3358rb_thread_abort_exc_set(
VALUE thread,
VALUE val)
3360 rb_thread_ptr(thread)->abort_on_exception =
RTEST(val);
3408rb_thread_s_report_exc(
VALUE _)
3410 return RBOOL(GET_THREAD()->vm->thread_report_on_exception);
3445rb_thread_s_report_exc_set(
VALUE self,
VALUE val)
3447 GET_THREAD()->vm->thread_report_on_exception =
RTEST(val);
3464rb_thread_s_ignore_deadlock(
VALUE _)
3466 return RBOOL(GET_THREAD()->vm->thread_ignore_deadlock);
3491rb_thread_s_ignore_deadlock_set(
VALUE self,
VALUE val)
3493 GET_THREAD()->vm->thread_ignore_deadlock =
RTEST(val);
3515rb_thread_report_exc(
VALUE thread)
3517 return RBOOL(rb_thread_ptr(thread)->report_on_exception);
3535rb_thread_report_exc_set(
VALUE thread,
VALUE val)
3537 rb_thread_ptr(thread)->report_on_exception =
RTEST(val);
3552rb_thread_group(
VALUE thread)
3554 return rb_thread_ptr(thread)->thgroup;
3558thread_status_name(rb_thread_t *th,
int detail)
3560 switch (th->status) {
3561 case THREAD_RUNNABLE:
3562 return th->to_kill ?
"aborting" :
"run";
3563 case THREAD_STOPPED_FOREVER:
3564 if (detail)
return "sleep_forever";
3565 case THREAD_STOPPED:
3575rb_threadptr_dead(rb_thread_t *th)
3577 return th->status == THREAD_KILLED;
3613rb_thread_status(
VALUE thread)
3615 rb_thread_t *target_th = rb_thread_ptr(thread);
3617 if (rb_threadptr_dead(target_th)) {
3618 if (!
NIL_P(target_th->ec->errinfo) &&
3619 !
FIXNUM_P(target_th->ec->errinfo)) {
3627 return rb_str_new2(thread_status_name(target_th, FALSE));
3647rb_thread_alive_p(
VALUE thread)
3649 return RBOOL(!thread_finished(rb_thread_ptr(thread)));
3667rb_thread_stop_p(
VALUE thread)
3669 rb_thread_t *th = rb_thread_ptr(thread);
3671 if (rb_threadptr_dead(th)) {
3674 return RBOOL(th->status == THREAD_STOPPED || th->status == THREAD_STOPPED_FOREVER);
3685rb_thread_getname(
VALUE thread)
3687 return rb_thread_ptr(thread)->name;
3701 rb_thread_t *target_th = rb_thread_ptr(thread);
3706 enc = rb_enc_get(name);
3707 if (!rb_enc_asciicompat(enc)) {
3708 rb_raise(rb_eArgError,
"ASCII incompatible encoding (%s)",
3713 target_th->name = name;
3714 if (threadptr_initialized(target_th) && target_th->has_dedicated_nt) {
3715 native_set_another_thread_name(target_th->nt->thread_id, name);
3720#if USE_NATIVE_THREAD_NATIVE_THREAD_ID
3744rb_thread_native_thread_id(
VALUE thread)
3746 rb_thread_t *target_th = rb_thread_ptr(thread);
3747 if (rb_threadptr_dead(target_th))
return Qnil;
3748 return native_thread_native_thread_id(target_th);
3751# define rb_thread_native_thread_id rb_f_notimplement
3762rb_thread_to_s(
VALUE thread)
3765 rb_thread_t *target_th = rb_thread_ptr(thread);
3769 status = thread_status_name(target_th, TRUE);
3770 str = rb_sprintf(
"#<%"PRIsVALUE
":%p", cname, (
void *)thread);
3771 if (!
NIL_P(target_th->name)) {
3772 rb_str_catf(str,
"@%"PRIsVALUE, target_th->name);
3774 if ((loc = threadptr_invoke_proc_location(target_th)) !=
Qnil) {
3775 rb_str_catf(str,
" %"PRIsVALUE
":%"PRIsVALUE,
3778 rb_str_catf(str,
" %s>", status);
3784#define recursive_key id__recursive_key__
3787threadptr_local_aref(rb_thread_t *th,
ID id)
3789 if (
id == recursive_key) {
3790 return th->ec->local_storage_recursive_hash;
3794 struct rb_id_table *local_storage = th->ec->local_storage;
3796 if (local_storage != NULL && rb_id_table_lookup(local_storage,
id, &val)) {
3808 return threadptr_local_aref(rb_thread_ptr(thread),
id);
3875 if (!
id)
return Qnil;
3893rb_thread_fetch(
int argc,
VALUE *argv,
VALUE self)
3897 rb_thread_t *target_th = rb_thread_ptr(self);
3904 if (block_given && argc == 2) {
3905 rb_warn(
"block supersedes default value argument");
3910 if (
id == recursive_key) {
3911 return target_th->ec->local_storage_recursive_hash;
3913 else if (
id && target_th->ec->local_storage &&
3914 rb_id_table_lookup(target_th->ec->local_storage,
id, &val)) {
3917 else if (block_given) {
3920 else if (argc == 1) {
3921 rb_key_err_raise(rb_sprintf(
"key not found: %+"PRIsVALUE, key), self, key);
3929threadptr_local_aset(rb_thread_t *th,
ID id,
VALUE val)
3931 if (
id == recursive_key) {
3932 th->ec->local_storage_recursive_hash = val;
3936 struct rb_id_table *local_storage = th->ec->local_storage;
3939 if (!local_storage)
return Qnil;
3940 rb_id_table_delete(local_storage,
id);
3944 if (local_storage == NULL) {
3945 th->ec->local_storage = local_storage = rb_id_table_create(0);
3947 rb_id_table_insert(local_storage,
id, val);
3960 return threadptr_local_aset(rb_thread_ptr(thread),
id, val);
4011rb_thread_variable_get(
VALUE thread,
VALUE key)
4016 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
4019 locals = rb_thread_local_storage(thread);
4020 return rb_hash_aref(locals, symbol);
4041 locals = rb_thread_local_storage(thread);
4063 struct rb_id_table *local_storage = rb_thread_ptr(self)->ec->local_storage;
4065 if (!
id || local_storage == NULL) {
4068 return RBOOL(rb_id_table_lookup(local_storage,
id, &val));
4071static enum rb_id_table_iterator_result
4072thread_keys_i(
ID key,
VALUE value,
void *ary)
4075 return ID_TABLE_CONTINUE;
4082 return rb_ractor_living_thread_num(GET_RACTOR()) == 1;
4100rb_thread_keys(
VALUE self)
4102 struct rb_id_table *local_storage = rb_thread_ptr(self)->ec->local_storage;
4105 if (local_storage) {
4106 rb_id_table_foreach(local_storage, thread_keys_i, (
void *)ary);
4136rb_thread_variables(
VALUE thread)
4142 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
4145 locals = rb_thread_local_storage(thread);
4173 if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
4176 locals = rb_thread_local_storage(thread);
4178 return RBOOL(rb_hash_lookup(locals, symbol) !=
Qnil);
4197rb_thread_priority(
VALUE thread)
4199 return INT2NUM(rb_thread_ptr(thread)->priority);
4230rb_thread_priority_set(
VALUE thread,
VALUE prio)
4232 rb_thread_t *target_th = rb_thread_ptr(thread);
4235#if USE_NATIVE_THREAD_PRIORITY
4236 target_th->priority =
NUM2INT(prio);
4237 native_thread_apply_priority(th);
4240 if (priority > RUBY_THREAD_PRIORITY_MAX) {
4241 priority = RUBY_THREAD_PRIORITY_MAX;
4243 else if (priority < RUBY_THREAD_PRIORITY_MIN) {
4244 priority = RUBY_THREAD_PRIORITY_MIN;
4246 target_th->priority = (int8_t)priority;
4248 return INT2NUM(target_th->priority);
4253#if defined(NFDBITS) && defined(HAVE_RB_FD_INIT)
4289 FD_ZERO(fds->
fdset);
4295 size_t size = howmany(
rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
4297 if (size <
sizeof(fd_set))
4298 size =
sizeof(fd_set);
4322 size_t m = howmany(n + 1, NFDBITS) *
sizeof(fd_mask);
4323 size_t o = howmany(fds->
maxfd, NFDBITS) *
sizeof(fd_mask);
4325 if (m <
sizeof(fd_set)) m =
sizeof(fd_set);
4326 if (o <
sizeof(fd_set)) o =
sizeof(fd_set);
4330 memset((
char *)fds->
fdset + o, 0, m - o);
4339 FD_SET(n, fds->
fdset);
4345 if (n >= fds->
maxfd)
return;
4346 FD_CLR(n, fds->
fdset);
4352 if (n >= fds->
maxfd)
return 0;
4353 return FD_ISSET(n, fds->
fdset) != 0;
4359 size_t size = howmany(max, NFDBITS) *
sizeof(fd_mask);
4361 if (size <
sizeof(fd_set)) size =
sizeof(fd_set);
4364 memcpy(dst->
fdset, src, size);
4370 size_t size = howmany(
rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
4372 if (size <
sizeof(fd_set))
4373 size =
sizeof(fd_set);
4382 fd_set *r = NULL, *w = NULL, *e = NULL;
4395 return select(n, r, w, e, timeout);
4398#define rb_fd_no_init(fds) ((void)((fds)->fdset = 0), (void)((fds)->maxfd = 0))
4405#define FD_ZERO(f) rb_fd_zero(f)
4406#define FD_SET(i, f) rb_fd_set((i), (f))
4407#define FD_CLR(i, f) rb_fd_clr((i), (f))
4408#define FD_ISSET(i, f) rb_fd_isset((i), (f))
4410#elif defined(_WIN32)
4415 set->
capa = FD_SETSIZE;
4417 FD_ZERO(set->
fdset);
4439 SOCKET s = rb_w32_get_osfhandle(fd);
4441 for (i = 0; i < set->
fdset->fd_count; i++) {
4442 if (set->
fdset->fd_array[i] == s) {
4446 if (set->
fdset->fd_count >= (
unsigned)set->
capa) {
4447 set->
capa = (set->
fdset->fd_count / FD_SETSIZE + 1) * FD_SETSIZE;
4449 rb_xrealloc_mul_add(
4450 set->
fdset, set->
capa,
sizeof(SOCKET),
sizeof(
unsigned int));
4452 set->
fdset->fd_array[set->
fdset->fd_count++] = s;
4460#define FD_ZERO(f) rb_fd_zero(f)
4461#define FD_SET(i, f) rb_fd_set((i), (f))
4462#define FD_CLR(i, f) rb_fd_clr((i), (f))
4463#define FD_ISSET(i, f) rb_fd_isset((i), (f))
4465#define rb_fd_no_init(fds) (void)((fds)->fdset = 0)
4469#ifndef rb_fd_no_init
4470#define rb_fd_no_init(fds) (void)(fds)
4474wait_retryable(
volatile int *result,
int errnum, rb_hrtime_t *rel, rb_hrtime_t end)
4484 if (rel && hrtime_update_expire(rel, end)) {
4494 return !hrtime_update_expire(rel, end);
4514select_set_free(
VALUE p)
4529 volatile int result = 0;
4531 rb_hrtime_t *to, rel, end = 0;
4533 timeout_prepare(&to, &rel, &end, set->timeout);
4534 volatile rb_hrtime_t endtime = end;
4535#define restore_fdset(dst, src) \
4536 ((dst) ? rb_fd_dup(dst, src) : (void)0)
4537#define do_select_update() \
4538 (restore_fdset(set->rset, &set->orig_rset), \
4539 restore_fdset(set->wset, &set->orig_wset), \
4540 restore_fdset(set->eset, &set->orig_eset), \
4546 BLOCKING_REGION(set->th, {
4549 if (!RUBY_VM_INTERRUPTED(set->th->ec)) {
4550 result = native_fd_select(set->max,
4551 set->rset, set->wset, set->eset,
4552 rb_hrtime2timeval(&tv, to), set->th);
4553 if (result < 0) lerrno = errno;
4555 }, ubf_select, set->th, TRUE);
4557 RUBY_VM_CHECK_INTS_BLOCKING(set->th->ec);
4558 }
while (wait_retryable(&result, lerrno, to, endtime) && do_select_update());
4560 RUBY_VM_CHECK_INTS_BLOCKING(set->th->ec);
4566 return (
VALUE)result;
4575 set.th = GET_THREAD();
4576 RUBY_VM_CHECK_INTS_BLOCKING(set.th->ec);
4581 set.timeout = timeout;
4583 if (!set.rset && !set.wset && !set.eset) {
4592#define fd_init_copy(f) do { \
4594 rb_fd_resize(set.max - 1, set.f); \
4595 if (&set.orig_##f != set.f) { \
4596 rb_fd_init_copy(&set.orig_##f, set.f); \
4600 rb_fd_no_init(&set.orig_##f); \
4614#define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
4615#define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
4616#define POLLEX_SET (POLLPRI)
4619# define POLLERR_SET (0)
4623wait_for_single_fd_blocking_region(rb_thread_t *th,
struct pollfd *fds, nfds_t nfds,
4624 rb_hrtime_t *
const to,
volatile int *lerrno)
4627 volatile int result = 0;
4630 BLOCKING_REGION(th, {
4631 if (!RUBY_VM_INTERRUPTED(th->ec)) {
4632 result = ppoll(fds, nfds, rb_hrtime2timespec(&ts, to), 0);
4633 if (result < 0) *lerrno =
errno;
4635 }, ubf_select, th, TRUE);
4643thread_io_wait(rb_thread_t *th,
struct rb_io *io,
int fd,
int events,
struct timeval *timeout)
4645 struct pollfd fds[1] = {{
4647 .events = (short)events,
4650 volatile int result = 0;
4653 enum ruby_tag_type state;
4654 volatile int lerrno;
4657 rb_execution_context_t *ec = th->ec;
4660 blocking_operation.ec = ec;
4661 rb_io_blocking_operation_enter(io, &blocking_operation);
4664 if (timeout == NULL && thread_io_wait_events(th, fd, events, NULL)) {
4667 fds[0].revents = events;
4672 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
4673 rb_hrtime_t *to, rel, end = 0;
4674 RUBY_VM_CHECK_INTS_BLOCKING(ec);
4675 timeout_prepare(&to, &rel, &end, timeout);
4677 nfds = numberof(fds);
4678 result = wait_for_single_fd_blocking_region(th, fds, nfds, to, &lerrno);
4680 RUBY_VM_CHECK_INTS_BLOCKING(ec);
4681 }
while (wait_retryable(&result, lerrno, to, end));
4683 RUBY_VM_CHECK_INTS_BLOCKING(ec);
4690 rb_io_blocking_operation_exit(io, &blocking_operation);
4694 EC_JUMP_TAG(ec, state);
4702 if (fds[0].revents & POLLNVAL) {
4712 if (fds[0].revents & POLLIN_SET)
4713 result |= RB_WAITFD_IN;
4714 if (fds[0].revents & POLLOUT_SET)
4715 result |= RB_WAITFD_OUT;
4716 if (fds[0].revents & POLLEX_SET)
4717 result |= RB_WAITFD_PRI;
4720 if (fds[0].revents & POLLERR_SET)
4728 struct rb_io_blocking_operation *blocking_operation;
4741select_single(
VALUE ptr)
4747 args->read, args->write, args->except, args->tv);
4749 args->as.error =
errno;
4752 if (args->read &&
rb_fd_isset(args->as.fd, args->read))
4754 if (args->write &&
rb_fd_isset(args->as.fd, args->write))
4756 if (args->except &&
rb_fd_isset(args->as.fd, args->except))
4763select_single_cleanup(
VALUE ptr)
4767 if (args->blocking_operation) {
4768 rb_io_blocking_operation_exit(args->io, args->blocking_operation);
4791thread_io_wait(rb_thread_t *th,
struct rb_io *io,
int fd,
int events,
struct timeval *timeout)
4800 blocking_operation.ec = th->ec;
4801 rb_io_blocking_operation_enter(io, &blocking_operation);
4802 args.blocking_operation = &blocking_operation;
4806 blocking_operation.ec = NULL;
4807 args.blocking_operation = NULL;
4811 args.read = (events & RB_WAITFD_IN) ? init_set_fd(fd, &rfds) : NULL;
4812 args.write = (events & RB_WAITFD_OUT) ? init_set_fd(fd, &wfds) : NULL;
4813 args.except = (events & RB_WAITFD_PRI) ? init_set_fd(fd, &efds) : NULL;
4816 int result = (int)
rb_ensure(select_single, ptr, select_single_cleanup, ptr);
4818 errno = args.as.error;
4825rb_thread_wait_for_single_fd(rb_thread_t *th,
int fd,
int events,
struct timeval *timeout)
4827 return thread_io_wait(th, NULL, fd, events, timeout);
4831rb_thread_io_wait(rb_thread_t *th,
struct rb_io *io,
int events,
struct timeval * timeout)
4833 return thread_io_wait(th, io, io->
fd, events, timeout);
4840#ifdef USE_CONSERVATIVE_STACK_END
4842rb_gc_set_stack_end(
VALUE **stack_end_p)
4845COMPILER_WARNING_PUSH
4846#if RBIMPL_COMPILER_IS(GCC)
4847COMPILER_WARNING_IGNORED(-Wdangling-pointer);
4849 *stack_end_p = &stack_end;
4859rb_threadptr_check_signal(rb_thread_t *mth)
4862 if (rb_signal_buff_size() > 0) {
4864 threadptr_trap_interrupt(mth);
4869async_bug_fd(
const char *mesg,
int errno_arg,
int fd)
4872 size_t n = strlcpy(buff, mesg,
sizeof(buff));
4873 if (n <
sizeof(buff)-3) {
4876 rb_async_bug_errno(buff, errno_arg);
4881consume_communication_pipe(
int fd)
4887 static char buff[1024];
4893 result = read(fd, buff,
sizeof(buff));
4895 RUBY_DEBUG_LOG(
"resultf:%d buff:%lu", (
int)result, (
unsigned long)buff[0]);
4897 RUBY_DEBUG_LOG(
"result:%d", (
int)result);
4901 if (USE_EVENTFD || result < (ssize_t)
sizeof(buff)) {
4905 else if (result == 0) {
4908 else if (result < 0) {
4914#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
4919 async_bug_fd(
"consume_communication_pipe: read", e, fd);
4926rb_thread_stop_timer_thread(
void)
4928 if (TIMER_THREAD_CREATED_P() && native_stop_timer_thread()) {
4929 native_reset_timer_thread();
4934rb_thread_reset_timer_thread(
void)
4936 native_reset_timer_thread();
4940rb_thread_start_timer_thread(
void)
4943 rb_thread_create_timer_thread();
4947clear_coverage_i(st_data_t key, st_data_t val, st_data_t dummy)
4955 if (GET_VM()->coverage_mode & COVERAGE_TARGET_ONESHOT_LINES) {
4977rb_clear_coverages(
void)
4979 VALUE coverages = rb_get_coverages();
4980 if (
RTEST(coverages)) {
4985#if defined(HAVE_WORKING_FORK)
4988rb_thread_atfork_internal(rb_thread_t *th,
void (*atfork)(rb_thread_t *,
const rb_thread_t *))
4991 rb_vm_t *vm = th->vm;
4992 rb_ractor_t *r = th->ractor;
4993 vm->ractor.main_ractor = r;
4994 vm->ractor.main_thread = th;
4995 r->threads.main = th;
4996 r->status_ = ractor_created;
4998 thread_sched_atfork(TH_SCHED(th));
5003 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
5004 if (r != vm->ractor.main_ractor) {
5005 rb_ractor_terminate_atfork(vm, r);
5007 ccan_list_for_each(&r->threads.set, i, lt_node) {
5011 rb_vm_living_threads_init(vm);
5013 rb_ractor_atfork(vm, th);
5014 rb_vm_postponed_job_atfork();
5018 ccan_list_head_init(&th->interrupt_exec_tasks);
5021 rb_ractor_sleeper_threads_clear(th->ractor);
5022 rb_clear_coverages();
5025 rb_thread_reset_timer_thread();
5026 rb_thread_start_timer_thread();
5028 VM_ASSERT(vm->ractor.blocking_cnt == 0);
5029 VM_ASSERT(vm->ractor.cnt == 1);
5033terminate_atfork_i(rb_thread_t *th,
const rb_thread_t *current_th)
5035 if (th != current_th) {
5037 th->scheduler =
Qnil;
5040 rb_mutex_abandon_keeping_mutexes(th);
5041 rb_mutex_abandon_locking_mutex(th);
5042 thread_cleanup_func(th, TRUE);
5046void rb_fiber_atfork(rb_thread_t *);
5050 rb_thread_t *th = GET_THREAD();
5051 rb_threadptr_pending_interrupt_clear(th);
5052 rb_thread_atfork_internal(th, terminate_atfork_i);
5053 th->join_list = NULL;
5054 th->scheduler =
Qnil;
5055 rb_fiber_atfork(th);
5062terminate_atfork_before_exec_i(rb_thread_t *th,
const rb_thread_t *current_th)
5064 if (th != current_th) {
5065 thread_cleanup_func_before_exec(th);
5072 rb_thread_t *th = GET_THREAD();
5073 rb_thread_atfork_internal(th, terminate_atfork_before_exec_i);
5098 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
5121thgroup_s_alloc(
VALUE klass)
5142thgroup_list(
VALUE group)
5145 rb_thread_t *th = 0;
5146 rb_ractor_t *r = GET_RACTOR();
5148 ccan_list_for_each(&r->threads.set, th, lt_node) {
5149 if (th->thgroup == group) {
5174thgroup_enclose(
VALUE group)
5193thgroup_enclosed_p(
VALUE group)
5198 return RBOOL(data->enclosed);
5231 rb_thread_t *target_th = rb_thread_ptr(thread);
5238 if (data->enclosed) {
5246 if (data->enclosed) {
5248 "can't move from the enclosed thread group");
5251 target_th->thgroup = group;
5259thread_shield_mark(
void *ptr)
5261 rb_gc_mark((
VALUE)ptr);
5266 {thread_shield_mark, 0, 0,},
5267 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
5271thread_shield_alloc(
VALUE klass)
5276#define GetThreadShieldPtr(obj) ((VALUE)rb_check_typeddata((obj), &thread_shield_data_type))
5277#define THREAD_SHIELD_WAITING_MASK (((FL_USER19-1)&~(FL_USER0-1))|FL_USER19)
5278#define THREAD_SHIELD_WAITING_SHIFT (FL_USHIFT)
5279#define THREAD_SHIELD_WAITING_MAX (THREAD_SHIELD_WAITING_MASK>>THREAD_SHIELD_WAITING_SHIFT)
5280STATIC_ASSERT(THREAD_SHIELD_WAITING_MAX, THREAD_SHIELD_WAITING_MAX <= UINT_MAX);
5281static inline unsigned int
5282rb_thread_shield_waiting(
VALUE b)
5284 return ((
RBASIC(b)->flags&THREAD_SHIELD_WAITING_MASK)>>THREAD_SHIELD_WAITING_SHIFT);
5288rb_thread_shield_waiting_inc(
VALUE b)
5290 unsigned int w = rb_thread_shield_waiting(b);
5292 if (w > THREAD_SHIELD_WAITING_MAX)
5294 RBASIC(b)->flags &= ~THREAD_SHIELD_WAITING_MASK;
5295 RBASIC(b)->flags |= ((
VALUE)w << THREAD_SHIELD_WAITING_SHIFT);
5299rb_thread_shield_waiting_dec(
VALUE b)
5301 unsigned int w = rb_thread_shield_waiting(b);
5304 RBASIC(b)->flags &= ~THREAD_SHIELD_WAITING_MASK;
5305 RBASIC(b)->flags |= ((
VALUE)w << THREAD_SHIELD_WAITING_SHIFT);
5309rb_thread_shield_new(
void)
5311 VALUE thread_shield = thread_shield_alloc(rb_cThreadShield);
5313 return thread_shield;
5317rb_thread_shield_owned(
VALUE self)
5319 VALUE mutex = GetThreadShieldPtr(self);
5320 if (!mutex)
return false;
5322 rb_mutex_t *m = mutex_ptr(mutex);
5324 return m->ec_serial == rb_ec_serial(GET_EC());
5336rb_thread_shield_wait(
VALUE self)
5338 VALUE mutex = GetThreadShieldPtr(self);
5341 if (!mutex)
return Qfalse;
5342 m = mutex_ptr(mutex);
5343 if (m->ec_serial == rb_ec_serial(GET_EC()))
return Qnil;
5344 rb_thread_shield_waiting_inc(self);
5346 rb_thread_shield_waiting_dec(self);
5349 return rb_thread_shield_waiting(self) > 0 ?
Qnil :
Qfalse;
5353thread_shield_get_mutex(
VALUE self)
5355 VALUE mutex = GetThreadShieldPtr(self);
5357 rb_raise(
rb_eThreadError,
"destroyed thread shield - %p", (
void *)self);
5365rb_thread_shield_release(
VALUE self)
5367 VALUE mutex = thread_shield_get_mutex(self);
5369 return RBOOL(rb_thread_shield_waiting(self) > 0);
5376rb_thread_shield_destroy(
VALUE self)
5378 VALUE mutex = thread_shield_get_mutex(self);
5381 return RBOOL(rb_thread_shield_waiting(self) > 0);
5385threadptr_recursive_hash(rb_thread_t *th)
5387 return th->ec->local_storage_recursive_hash;
5391threadptr_recursive_hash_set(rb_thread_t *th,
VALUE hash)
5393 th->ec->local_storage_recursive_hash = hash;
5405recursive_list_access(
VALUE sym)
5407 rb_thread_t *th = GET_THREAD();
5408 VALUE hash = threadptr_recursive_hash(th);
5411 hash = rb_ident_hash_new();
5412 threadptr_recursive_hash_set(th, hash);
5416 list = rb_hash_aref(hash, sym);
5419 list = rb_ident_hash_new();
5420 rb_hash_aset(hash, sym, list);
5434#if SIZEOF_LONG == SIZEOF_VOIDP
5435 #define OBJ_ID_EQL(obj_id, other) ((obj_id) == (other))
5436#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
5437 #define OBJ_ID_EQL(obj_id, other) (RB_BIGNUM_TYPE_P((obj_id)) ? \
5438 rb_big_eql((obj_id), (other)) : ((obj_id) == (other)))
5441 VALUE pair_list = rb_hash_lookup2(list, obj,
Qundef);
5442 if (UNDEF_P(pair_list))
5444 if (paired_obj_id) {
5446 if (!OBJ_ID_EQL(paired_obj_id, pair_list))
5450 if (
NIL_P(rb_hash_lookup(pair_list, paired_obj_id)))
5472 rb_hash_aset(list, obj,
Qtrue);
5474 else if (UNDEF_P(pair_list = rb_hash_lookup2(list, obj,
Qundef))) {
5475 rb_hash_aset(list, obj, paired_obj);
5479 VALUE other_paired_obj = pair_list;
5481 rb_hash_aset(pair_list, other_paired_obj,
Qtrue);
5482 rb_hash_aset(list, obj, pair_list);
5484 rb_hash_aset(pair_list, paired_obj,
Qtrue);
5500 VALUE pair_list = rb_hash_lookup2(list, obj,
Qundef);
5501 if (UNDEF_P(pair_list)) {
5505 rb_hash_delete_entry(pair_list, paired_obj);
5511 rb_hash_delete_entry(list, obj);
5527 return (*p->func)(p->obj, p->arg, FALSE);
5548 p.list = recursive_list_access(sym);
5552 outermost = outer && !recursive_check(p.list,
ID2SYM(recursive_key), 0);
5554 if (recursive_check(p.list, p.obj, pairid)) {
5555 if (outer && !outermost) {
5558 return (*func)(obj, arg, TRUE);
5561 enum ruby_tag_type state;
5566 recursive_push(p.list,
ID2SYM(recursive_key), 0);
5567 recursive_push(p.list, p.obj, p.pairid);
5568 result = rb_catch_protect(p.list, exec_recursive_i, (
VALUE)&p, &state);
5569 if (!recursive_pop(p.list, p.obj, p.pairid))
goto invalid;
5570 if (!recursive_pop(p.list,
ID2SYM(recursive_key), 0))
goto invalid;
5571 if (state != TAG_NONE) EC_JUMP_TAG(GET_EC(), state);
5572 if (result == p.list) {
5573 result = (*func)(obj, arg, TRUE);
5578 recursive_push(p.list, p.obj, p.pairid);
5579 EC_PUSH_TAG(GET_EC());
5580 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
5581 ret = (*func)(obj, arg, FALSE);
5584 if (!recursive_pop(p.list, p.obj, p.pairid)) {
5587 if (state != TAG_NONE) EC_JUMP_TAG(GET_EC(), state);
5596 "for %+"PRIsVALUE
" in %+"PRIsVALUE,
5620 return exec_recursive(func, obj, rb_memory_id(paired_obj), arg, 0,
rb_frame_last_func());
5638 return exec_recursive(func, obj, 0, arg, 1, mid);
5650 return exec_recursive(func, obj, rb_memory_id(paired_obj), arg, 1,
rb_frame_last_func());
5662rb_thread_backtrace_m(
int argc,
VALUE *argv,
VALUE thval)
5664 return rb_vm_thread_backtrace(argc, argv, thval);
5679rb_thread_backtrace_locations_m(
int argc,
VALUE *argv,
VALUE thval)
5681 return rb_vm_thread_backtrace_locations(argc, argv, thval);
5685Init_Thread_Mutex(
void)
5687 rb_thread_t *th = GET_THREAD();
5711 rb_thread_t *th = GET_THREAD();
5774 rb_vm_register_special_exception(ruby_error_stream_closed,
rb_eIOError,
5775 "stream closed in another thread");
5784 const char * ptr = getenv(
"RUBY_THREAD_TIMESLICE");
5787 long quantum = strtol(ptr, NULL, 0);
5788 if (quantum > 0 && !(SIZEOF_LONG > 4 && quantum > UINT32_MAX)) {
5789 thread_default_quantum_ms = (uint32_t)quantum;
5792 fprintf(stderr,
"Ignored RUBY_THREAD_TIMESLICE=%s\n", ptr);
5797 th->thgroup = th->ractor->thgroup_default =
rb_obj_alloc(cThGroup);
5798 rb_define_const(cThGroup,
"Default", th->thgroup);
5808#ifdef HAVE_PTHREAD_NP_H
5809 VM_ASSERT(TH_SCHED(th)->running == th);
5816 th->pending_interrupt_queue_checked = 0;
5821 rb_thread_create_timer_thread();
5832 rb_thread_t *th = ruby_thread_from_native();
5837#ifdef NON_SCALAR_THREAD_ID
5838 #define thread_id_str(th) (NULL)
5840 #define thread_id_str(th) ((void *)(uintptr_t)(th)->nt->thread_id)
5844debug_deadlock_check(rb_ractor_t *r,
VALUE msg)
5846 rb_thread_t *th = 0;
5849 rb_str_catf(msg,
"\n%d threads, %d sleeps current:%p main thread:%p\n",
5850 rb_ractor_living_thread_num(r), rb_ractor_sleeper_thread_num(r),
5851 (
void *)GET_THREAD(), (
void *)r->threads.main);
5853 ccan_list_for_each(&r->threads.set, th, lt_node) {
5854 rb_str_catf(msg,
"* %+"PRIsVALUE
"\n rb_thread_t:%p "
5856 th->self, (
void *)th, th->nt ? thread_id_str(th) :
"N/A", th->ec->interrupt_flag);
5858 if (th->locking_mutex) {
5859 rb_mutex_t *mutex = mutex_ptr(th->locking_mutex);
5860 rb_str_catf(msg,
" mutex:%llu cond:%"PRIuSIZE,
5861 (
unsigned long long)mutex->ec_serial, rb_mutex_num_waiting(mutex));
5867 rb_str_catf(msg,
"\n depended by: tb_thread_id:%p", (
void *)list->thread);
5871 rb_str_catf(msg,
"\n ");
5872 rb_str_concat(msg,
rb_ary_join(rb_ec_backtrace_str_ary(th->ec, RUBY_BACKTRACE_START, RUBY_ALL_BACKTRACE_LINES), sep));
5873 rb_str_catf(msg,
"\n");
5878rb_check_deadlock(rb_ractor_t *r)
5880 if (GET_THREAD()->vm->thread_ignore_deadlock)
return;
5882#ifdef RUBY_THREAD_PTHREAD_H
5883 if (r->threads.sched.readyq_cnt > 0)
return;
5886 int sleeper_num = rb_ractor_sleeper_thread_num(r);
5887 int ltnum = rb_ractor_living_thread_num(r);
5889 if (ltnum > sleeper_num)
return;
5890 if (ltnum < sleeper_num) rb_bug(
"sleeper must not be more than vm_living_thread_num(vm)");
5893 rb_thread_t *th = NULL;
5895 ccan_list_for_each(&r->threads.set, th, lt_node) {
5896 if (th->status != THREAD_STOPPED_FOREVER || RUBY_VM_INTERRUPTED(th->ec)) {
5899 else if (th->locking_mutex) {
5900 rb_mutex_t *mutex = mutex_ptr(th->locking_mutex);
5901 if (mutex->ec_serial == rb_ec_serial(th->ec) || (!mutex->ec_serial && !ccan_list_empty(&mutex->waitq))) {
5912 argv[1] =
rb_str_new2(
"No live threads left. Deadlock?");
5913 debug_deadlock_check(r, argv[1]);
5914 rb_ractor_sleeper_threads_dec(GET_RACTOR());
5915 rb_threadptr_raise(r->threads.main, 2, argv);
5922 const rb_control_frame_t *cfp = GET_EC()->cfp;
5923 VALUE coverage = rb_iseq_coverage(cfp->iseq);
5928 VM_ASSERT(line >= 0);
5931 void rb_iseq_clear_event_flags(
const rb_iseq_t *iseq,
size_t pos,
rb_event_flag_t reset);
5932 if (GET_VM()->coverage_mode & COVERAGE_TARGET_ONESHOT_LINES) {
5933 rb_iseq_clear_event_flags(cfp->iseq, cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded - 1, RUBY_EVENT_COVERAGE_LINE);
5953 const rb_control_frame_t *cfp = GET_EC()->cfp;
5954 VALUE coverage = rb_iseq_coverage(cfp->iseq);
5958 long pc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded - 1;
5970const rb_method_entry_t *
5971rb_resolve_me_location(
const rb_method_entry_t *me,
VALUE resolved_location[5])
5973 VALUE path, beg_pos_lineno, beg_pos_column, end_pos_lineno, end_pos_column;
5975 if (!me->def)
return NULL;
5978 switch (me->def->type) {
5979 case VM_METHOD_TYPE_ISEQ: {
5980 const rb_iseq_t *iseq = me->def->body.iseq.
iseqptr;
5981 rb_iseq_location_t *loc = &ISEQ_BODY(iseq)->location;
5982 path = rb_iseq_path(iseq);
5983 beg_pos_lineno =
INT2FIX(loc->code_location.beg_pos.lineno);
5984 beg_pos_column =
INT2FIX(loc->code_location.beg_pos.column);
5985 end_pos_lineno =
INT2FIX(loc->code_location.end_pos.lineno);
5986 end_pos_column =
INT2FIX(loc->code_location.end_pos.column);
5989 case VM_METHOD_TYPE_BMETHOD: {
5990 const rb_iseq_t *iseq = rb_proc_get_iseq(me->def->body.bmethod.proc, 0);
5992 rb_iseq_location_t *loc;
5993 rb_iseq_check(iseq);
5994 path = rb_iseq_path(iseq);
5995 loc = &ISEQ_BODY(iseq)->location;
5996 beg_pos_lineno =
INT2FIX(loc->code_location.beg_pos.lineno);
5997 beg_pos_column =
INT2FIX(loc->code_location.beg_pos.column);
5998 end_pos_lineno =
INT2FIX(loc->code_location.end_pos.lineno);
5999 end_pos_column =
INT2FIX(loc->code_location.end_pos.column);
6004 case VM_METHOD_TYPE_ALIAS:
6005 me = me->def->body.alias.original_me;
6007 case VM_METHOD_TYPE_REFINED:
6008 me = me->def->body.refined.orig_me;
6009 if (!me)
return NULL;
6020 if (resolved_location) {
6021 resolved_location[0] = path;
6022 resolved_location[1] = beg_pos_lineno;
6023 resolved_location[2] = beg_pos_column;
6024 resolved_location[3] = end_pos_lineno;
6025 resolved_location[4] = end_pos_column;
6033 const rb_control_frame_t *cfp = GET_EC()->cfp;
6034 const rb_callable_method_entry_t *cme = rb_vm_frame_method_entry(cfp);
6035 const rb_method_entry_t *me = (
const rb_method_entry_t *)cme;
6039 me = rb_resolve_me_location(me, 0);
6042 rcount = rb_hash_aref(me2counter, (
VALUE) me);
6050rb_get_coverages(
void)
6052 return GET_VM()->coverages;
6056rb_get_coverage_mode(
void)
6058 return GET_VM()->coverage_mode;
6062rb_set_coverages(
VALUE coverages,
int mode,
VALUE me2counter)
6064 GET_VM()->coverages = coverages;
6065 GET_VM()->me2counter = me2counter;
6066 GET_VM()->coverage_mode = mode;
6070rb_resume_coverages(
void)
6072 int mode = GET_VM()->coverage_mode;
6073 VALUE me2counter = GET_VM()->me2counter;
6074 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);
6075 if (mode & COVERAGE_TARGET_BRANCHES) {
6076 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);
6078 if (mode & COVERAGE_TARGET_METHODS) {
6084rb_suspend_coverages(
void)
6087 if (GET_VM()->coverage_mode & COVERAGE_TARGET_BRANCHES) {
6090 if (GET_VM()->coverage_mode & COVERAGE_TARGET_METHODS) {
6097rb_reset_coverages(
void)
6099 rb_clear_coverages();
6100 rb_iseq_remove_coverage_all();
6101 GET_VM()->coverages =
Qfalse;
6105rb_default_coverage(
int n)
6107 VALUE coverage = rb_ary_hidden_new_fill(3);
6109 int mode = GET_VM()->coverage_mode;
6111 if (mode & COVERAGE_TARGET_LINES) {
6114 RARRAY_ASET(coverage, COVERAGE_INDEX_LINES, lines);
6116 if (mode & COVERAGE_TARGET_BRANCHES) {
6117 branches = rb_ary_hidden_new_fill(2);
6145 RARRAY_ASET(coverage, COVERAGE_INDEX_BRANCHES, branches);
6151uninterruptible_exit(
VALUE v)
6153 rb_thread_t *cur_th = GET_THREAD();
6154 rb_ary_pop(cur_th->pending_interrupt_mask_stack);
6156 cur_th->pending_interrupt_queue_checked = 0;
6157 if (!rb_threadptr_pending_interrupt_empty_p(cur_th)) {
6158 RUBY_VM_SET_INTERRUPT(cur_th->ec);
6166 VALUE interrupt_mask = rb_ident_hash_new();
6167 rb_thread_t *cur_th = GET_THREAD();
6169 rb_hash_aset(interrupt_mask, rb_cObject, sym_never);
6171 rb_ary_push(cur_th->pending_interrupt_mask_stack, interrupt_mask);
6175 RUBY_VM_CHECK_INTS(cur_th->ec);
6180thread_specific_storage_alloc(rb_thread_t *th)
6182 VM_ASSERT(th->specific_storage == NULL);
6184 if (UNLIKELY(specific_key_count > 0)) {
6185 th->specific_storage =
ZALLOC_N(
void *, RB_INTERNAL_THREAD_SPECIFIC_KEY_MAX);
6189rb_internal_thread_specific_key_t
6192 rb_vm_t *vm = GET_VM();
6194 if (specific_key_count == 0 && vm->ractor.cnt > 1) {
6195 rb_raise(
rb_eThreadError,
"The first rb_internal_thread_specific_key_create() is called with multiple ractors");
6197 else if (specific_key_count > RB_INTERNAL_THREAD_SPECIFIC_KEY_MAX) {
6198 rb_raise(
rb_eThreadError,
"rb_internal_thread_specific_key_create() is called more than %d times", RB_INTERNAL_THREAD_SPECIFIC_KEY_MAX);
6201 rb_internal_thread_specific_key_t key = specific_key_count++;
6205 rb_ractor_t *cr = GET_RACTOR();
6208 ccan_list_for_each(&cr->threads.set, th, lt_node) {
6209 thread_specific_storage_alloc(th);
6220 rb_thread_t *th =
DATA_PTR(thread_val);
6222 VM_ASSERT(rb_thread_ptr(thread_val) == th);
6223 VM_ASSERT(key < RB_INTERNAL_THREAD_SPECIFIC_KEY_MAX);
6224 VM_ASSERT(th->specific_storage);
6226 return th->specific_storage[key];
6233 rb_thread_t *th =
DATA_PTR(thread_val);
6235 VM_ASSERT(rb_thread_ptr(thread_val) == th);
6236 VM_ASSERT(key < RB_INTERNAL_THREAD_SPECIFIC_KEY_MAX);
6237 VM_ASSERT(th->specific_storage);
6239 th->specific_storage[key] = data;
6245 struct ccan_list_node node;
6247 rb_interrupt_exec_func_t *func;
6249 enum rb_interrupt_exec_flag flags;
6253rb_threadptr_interrupt_exec_task_mark(rb_thread_t *th)
6257 ccan_list_for_each(&th->interrupt_exec_tasks, task, node) {
6258 if (task->flags & rb_interrupt_exec_flag_value_data) {
6259 rb_gc_mark((
VALUE)task->data);
6267rb_threadptr_interrupt_exec(rb_thread_t *th, rb_interrupt_exec_func_t *func,
void *data,
enum rb_interrupt_exec_flag flags)
6279 ccan_list_add_tail(&th->interrupt_exec_tasks, &task->node);
6280 threadptr_set_interrupt_locked(th,
true);
6286threadptr_interrupt_exec_exec(rb_thread_t *th)
6297 RUBY_DEBUG_LOG(
"task:%p", task);
6300 if (task->flags & rb_interrupt_exec_flag_new_thread) {
6304 (*task->func)(task->data);
6315threadptr_interrupt_exec_cleanup(rb_thread_t *th)
6332 rb_interrupt_exec_func_t *func,
void *data,
enum rb_interrupt_exec_flag flags)
6334 RUBY_DEBUG_LOG(
"flags:%d", (
int)flags);
6336 rb_thread_t *main_th = target_r->threads.main;
6337 rb_threadptr_interrupt_exec(main_th, func, data, flags | rb_interrupt_exec_flag_new_thread);
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
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.
struct rb_trace_arg_struct rb_trace_arg_t
Type that represents a specific trace event.
#define RUBY_INTERNAL_EVENT_SWITCH
Thread switched.
int rb_remove_event_hook(rb_event_hook_func_t func)
Removes the passed function from the list of event hooks.
#define RUBY_EVENT_THREAD_BEGIN
Encountered a new thread.
void(* rb_event_hook_func_t)(rb_event_flag_t evflag, VALUE data, VALUE self, ID mid, VALUE klass)
Type of event hooks.
uint32_t rb_event_flag_t
Represents event(s).
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
#define RUBY_EVENT_THREAD_END
Encountered an end of a thread.
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
ID rb_frame_last_func(void)
Returns the ID of the last method in the call stack.
int rb_keyword_given_p(void)
Determines if the current method is given a keyword argument.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define ALLOC
Old name of RB_ALLOC.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#define xrealloc
Old name of ruby_xrealloc.
#define ID2SYM
Old name of RB_ID2SYM.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define CLASS_OF
Old name of rb_class_of.
#define xmalloc
Old name of ruby_xmalloc.
#define LONG2FIX
Old name of RB_INT2FIX.
#define FIX2INT
Old name of RB_FIX2INT.
#define ZALLOC_N
Old name of RB_ZALLOC_N.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define T_HASH
Old name of RUBY_T_HASH.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
#define NIL_P
Old name of RB_NIL_P.
#define POSFIXABLE
Old name of RB_POSFIXABLE.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void ruby_stop(int ex)
Calls ruby_cleanup() and exits the process.
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
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_eInterrupt
Interrupt exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Creates an instance of the passed exception class.
VALUE rb_eException
Mother of all exceptions.
VALUE rb_eThreadError
ThreadError exception.
void rb_exit(int status)
Terminates the current execution context.
VALUE rb_eSignal
SignalException exception.
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
VALUE rb_cInteger
Module class.
VALUE rb_obj_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_ary_shift(VALUE ary)
Destructively deletes an element from the beginning of the passed array and returns what was deleted.
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_ary_delete_at(VALUE ary, long pos)
Destructively removes an element which resides at the specific index of the passed array.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_pop(VALUE ary)
Destructively deletes an element from the end of the passed array and returns what was deleted.
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_clear(VALUE ary)
Destructively removes everything form an array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
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_mutex_new(void)
Creates a mutex.
VALUE rb_thread_kill(VALUE thread)
Terminates the given thread.
#define RUBY_UBF_IO
A special UBF for blocking IO operations.
VALUE rb_thread_main(void)
Obtains the "main" thread.
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
void rb_thread_sleep_forever(void)
Blocks indefinitely.
void rb_thread_fd_close(int fd)
This funciton is now a no-op.
void rb_thread_wait_for(struct timeval time)
Identical to rb_thread_sleep(), except it takes struct timeval instead.
VALUE rb_mutex_synchronize(VALUE mutex, VALUE(*func)(VALUE arg), VALUE arg)
Obtains the lock, runs the passed function, and releases the lock when it completes.
VALUE rb_thread_stop(void)
Stops the current thread.
VALUE rb_mutex_sleep(VALUE self, VALUE timeout)
Releases the lock held in the mutex and waits for the period of time; reacquires the lock on wakeup.
VALUE rb_exec_recursive_paired(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h)
Identical to rb_exec_recursive(), except it checks for the recursion on the ordered pair of { g,...
void rb_unblock_function_t(void *)
This is the type of UBFs.
void rb_thread_atfork_before_exec(void)
:FIXME: situation of this function is unclear.
void rb_thread_check_ints(void)
Checks for interrupts.
VALUE rb_thread_run(VALUE thread)
This is a rb_thread_wakeup() + rb_thread_schedule() combo.
VALUE rb_thread_wakeup(VALUE thread)
Marks a given thread as eligible for scheduling.
VALUE rb_mutex_unlock(VALUE mutex)
Releases the mutex.
VALUE rb_exec_recursive_paired_outer(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h)
Identical to rb_exec_recursive_outer(), except it checks for the recursion on the ordered pair of { g...
void rb_thread_sleep_deadly(void)
Identical to rb_thread_sleep_forever(), except the thread calling this function is considered "dead" ...
void rb_thread_atfork(void)
A pthread_atfork(3posix)-like API.
VALUE rb_thread_current(void)
Obtains the "current" thread.
int rb_thread_alone(void)
Checks if the thread this function is running is the only thread that is currently alive.
VALUE rb_thread_local_aset(VALUE thread, ID key, VALUE val)
This badly named function writes to a Fiber local storage.
void rb_thread_schedule(void)
Tries to switch to another thread.
#define RUBY_UBF_PROCESS
A special UBF for blocking process operations.
VALUE rb_exec_recursive_outer(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
Identical to rb_exec_recursive(), except it calls f for outermost recursion only.
VALUE rb_thread_wakeup_alive(VALUE thread)
Identical to rb_thread_wakeup(), except it doesn't raise on an already killed thread.
VALUE rb_mutex_lock(VALUE mutex)
Attempts to lock the mutex.
void rb_thread_sleep(int sec)
Blocks for the given period of time.
void rb_timespec_now(struct timespec *ts)
Fills the current time into the given struct.
struct timeval rb_time_timeval(VALUE time)
Converts an instance of rb_cTime to a struct timeval that represents the identical point of time.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
int rb_sourceline(void)
Resembles __LINE__.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
ID rb_to_id(VALUE str)
Identical to rb_intern_str(), except it tries to convert the parameter object to an instance of rb_cS...
#define RB_IO_POINTER(obj, fp)
Queries the underlying IO pointer.
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.
int ruby_thread_has_gvl_p(void)
Whether the current thread is holding the GVL.
void * rb_internal_thread_specific_get(VALUE thread_val, rb_internal_thread_specific_key_t key)
Get thread and tool specific data.
#define RB_NOGVL_INTR_FAIL
Passing this flag to rb_nogvl() prevents it from checking interrupts.
void rb_internal_thread_specific_set(VALUE thread_val, rb_internal_thread_specific_key_t key, void *data)
Set thread and tool specific data.
rb_internal_thread_specific_key_t rb_internal_thread_specific_key_create(void)
Create a key to store thread specific data.
void * rb_nogvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2, int flags)
Identical to rb_thread_call_without_gvl(), except it additionally takes "flags" that change the behav...
void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
(Re-)acquires the GVL.
#define RB_NOGVL_OFFLOAD_SAFE
Passing this flag to rb_nogvl() indicates that the passed function is safe to offload to a background...
void * rb_thread_call_without_gvl2(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
Identical to rb_thread_call_without_gvl(), except it does not interface with signals etc.
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
Allows the passed function to run in parallel with other Ruby threads.
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
VALUE rb_yield(VALUE val)
Yields the block.
void rb_throw_obj(VALUE tag, VALUE val)
Identical to rb_throw(), except it allows arbitrary Ruby object to become a tag.
static int rb_fd_max(const rb_fdset_t *f)
It seems this function has no use.
void rb_fd_copy(rb_fdset_t *dst, const fd_set *src, int max)
Destructively overwrites an fdset with another.
void rb_fd_dup(rb_fdset_t *dst, const rb_fdset_t *src)
Identical to rb_fd_copy(), except it copies unlimited number of file descriptors.
void rb_fd_term(rb_fdset_t *f)
Destroys the rb_fdset_t, releasing any memory and resources it used.
static fd_set * rb_fd_ptr(const rb_fdset_t *f)
Raw pointer to fd_set.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define ALLOCA_N(type, n)
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
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.
fd_set rb_fdset_t
The data structure which wraps the fd_set bitmap used by select(2).
#define rb_fd_zero
Clears the given rb_fdset_t.
#define rb_fd_clr
Unsets the given fd from the rb_fdset_t.
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RBASIC(obj)
Convenient casting macro.
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
#define DATA_PTR(obj)
Convenient getter macro.
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#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 rb_errno(void)
Identical to system errno.
#define errno
Ractor-aware version of errno.
int ruby_native_thread_p(void)
Queries if the thread which calls this function is a ruby's thread.
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_blocking_operation_wait(VALUE scheduler, void *(*function)(void *), void *data, rb_unblock_function_t *unblock_function, void *data2, int flags, struct rb_fiber_scheduler_blocking_operation_state *state)
Defer the execution of the passed function to the scheduler.
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_fiber_interrupt(VALUE scheduler, VALUE fiber, VALUE exception)
Interrupt a fiber by raising an exception.
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_yield(VALUE scheduler)
Yield to the scheduler, to be resumed on the next scheduling cycle.
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_current_for_threadptr(struct rb_thread_struct *thread)
Identical to rb_fiber_scheduler_current_for_thread(), except it expects a threadptr instead of a thre...
VALUE rb_fiber_scheduler_unblock(VALUE scheduler, VALUE blocker, VALUE fiber)
Wakes up a fiber previously blocked using rb_fiber_scheduler_block().
int rb_thread_fd_select(int nfds, rb_fdset_t *rfds, rb_fdset_t *wfds, rb_fdset_t *efds, struct timeval *timeout)
Waits for multiple file descriptors at once.
#define rb_fd_resize(n, f)
Does nothing (defined for compatibility).
static bool RB_TEST(VALUE obj)
Emulates Ruby's "if" statement.
@ RUBY_Qundef
Represents so-called undef.
#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.
Ruby's IO, metadata and buffers.
VALUE self
The IO's Ruby level counterpart.
struct ccan_list_head blocking_operations
Threads that are performing a blocking operation without the GVL using this IO.
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.