Lines Matching refs:th

86 static void sleep_timeval(rb_thread_t *th, struct timeval time, int spurious_check);
87 static void sleep_wait_for_interrupt(rb_thread_t *th, double sleepsec, int spurious_check);
88 static void sleep_forever(rb_thread_t *th, int nodeadlock, int spurious_check);
90 static int rb_threadptr_dead(rb_thread_t *th);
92 static int rb_threadptr_pending_interrupt_empty_p(rb_thread_t *th);
115 static int set_unblock_function(rb_thread_t *th, rb_unblock_function_t *func, void *arg,
117 static void reset_unblock_function(rb_thread_t *th, const struct rb_unblock_callback *old);
119 static inline int blocking_region_begin(rb_thread_t *th, struct rb_blocking_region_buffer *region,
121 static inline void blocking_region_end(rb_thread_t *th, struct rb_blocking_region_buffer *region);
124 #define RB_GC_SAVE_MACHINE_REGISTER_STACK(th) \
125 do{(th)->machine_register_stack_end = rb_ia64_bsp();}while(0)
127 #define RB_GC_SAVE_MACHINE_REGISTER_STACK(th)
129 #define RB_GC_SAVE_MACHINE_CONTEXT(th) \
132 RB_GC_SAVE_MACHINE_REGISTER_STACK(th); \
133 setjmp((th)->machine_regs); \
134 SET_MACHINE_STACK_END(&(th)->machine_stack_end); \
215 #define thread_start_func_2(th, st, rst) thread_start_func_2(th, st)
217 NOINLINE(static int thread_start_func_2(rb_thread_t *th, VALUE *stack_start,
293 set_unblock_function(rb_thread_t *th, rb_unblock_function_t *func, void *arg,
298 if (RUBY_VM_INTERRUPTED_ANY(th)) {
303 RUBY_VM_CHECK_INTS(th);
306 native_mutex_lock(&th->interrupt_lock);
307 if (RUBY_VM_INTERRUPTED_ANY(th)) {
308 native_mutex_unlock(&th->interrupt_lock);
312 if (old) *old = th->unblock;
313 th->unblock.func = func;
314 th->unblock.arg = arg;
316 native_mutex_unlock(&th->interrupt_lock);
322 reset_unblock_function(rb_thread_t *th, const struct rb_unblock_callback *old)
324 native_mutex_lock(&th->interrupt_lock);
325 th->unblock = *old;
326 native_mutex_unlock(&th->interrupt_lock);
330 rb_threadptr_interrupt_common(rb_thread_t *th, int trap)
332 native_mutex_lock(&th->interrupt_lock);
334 RUBY_VM_SET_TRAP_INTERRUPT(th);
336 RUBY_VM_SET_INTERRUPT(th);
337 if (th->unblock.func) {
338 (th->unblock.func)(th->unblock.arg);
343 native_mutex_unlock(&th->interrupt_lock);
347 rb_threadptr_interrupt(rb_thread_t *th)
349 rb_threadptr_interrupt_common(th, 0);
353 rb_threadptr_trap_interrupt(rb_thread_t *th)
355 rb_threadptr_interrupt_common(th, 1);
362 rb_thread_t *th;
363 GetThreadPtr(thval, th);
365 if (th != main_thread) {
366 thread_debug("terminate_i: %p\n", (void *)th);
367 rb_threadptr_pending_interrupt_enque(th, eTerminateSignal);
368 rb_threadptr_interrupt(th);
371 thread_debug("terminate_i: main thread (%p)\n", (void *)th);
380 struct rb_thread_struct volatile *th;
387 static void rb_mutex_abandon_keeping_mutexes(rb_thread_t *th);
388 static void rb_mutex_abandon_locking_mutex(rb_thread_t *th);
389 static const char* rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t volatile *th);
392 rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th)
396 rb_mutex_t *mutexes = th->keeping_mutexes;
403 err = rb_mutex_unlock_th(mutex, th);
411 rb_thread_t *th = GET_THREAD(); /* main thread */
412 rb_vm_t *vm = th->vm;
414 if (vm->main_thread != th) {
416 (void *)vm->main_thread, (void *)th);
420 rb_threadptr_unlock_all_locking_mutexes(th);
423 thread_debug("rb_thread_terminate_all (main thread: %p)\n", (void *)th);
424 st_foreach(vm->living_threads, terminate_i, (st_data_t)th);
429 TH_PUSH_TAG(th);
431 native_sleep(th, 0);
432 RUBY_VM_CHECK_INTS_BLOCKING(th);
445 rb_thread_t *th = th_ptr;
446 th->status = THREAD_KILLED;
447 th->machine_stack_start = th->machine_stack_end = 0;
449 th->machine_register_stack_start = th->machine_register_stack_end = 0;
456 rb_thread_t *th = th_ptr;
458 th->locking_mutex = Qfalse;
469 native_mutex_destroy(&th->interrupt_lock);
470 native_thread_destroy(th);
476 ruby_thread_init_stack(rb_thread_t *th)
478 native_thread_init_stack(th);
482 thread_start_func_2(rb_thread_t *th, VALUE *stack_start, VALUE *register_stack_start)
485 VALUE args = th->first_args;
491 void rb_register_sigaltstack(rb_thread_t *th);
493 rb_register_sigaltstack(th);
496 if (th == th->vm->main_thread)
499 ruby_thread_set_native(th);
501 th->machine_stack_start = stack_start;
503 th->machine_register_stack_start = register_stack_start;
505 thread_debug("thread start: %p\n", (void *)th);
507 gvl_acquire(th->vm, th);
509 thread_debug("thread start (get lock): %p\n", (void *)th);
510 rb_thread_set_current(th);
512 TH_PUSH_TAG(th);
514 SAVE_ROOT_JMPBUF(th, {
515 if (!th->first_func) {
516 GetProcPtr(th->first_proc, proc);
517 th->errinfo = Qnil;
518 th->root_lep = rb_vm_ep_local_ep(proc->block.ep);
519 th->root_svar = Qnil;
520 EXEC_EVENT_HOOK(th, RUBY_EVENT_THREAD_BEGIN, th->self, 0, 0, Qundef);
521 th->value = rb_vm_invoke_proc(th, proc, (int)RARRAY_LEN(args), RARRAY_PTR(args), 0);
522 EXEC_EVENT_HOOK(th, RUBY_EVENT_THREAD_END, th->self, 0, 0, Qundef);
525 th->value = (*th->first_func)((void *)args);
530 errinfo = th->errinfo;
534 else if (th->safe_level >= 4) {
541 else if (th->vm->thread_abort_on_exception ||
542 th->abort_on_exception || RTEST(ruby_debug)) {
548 th->value = Qnil;
551 th->status = THREAD_KILLED;
552 thread_debug("thread end: %p\n", (void *)th);
554 main_th = th->vm->main_thread;
562 if (th->locking_mutex != Qfalse) {
564 (void *)th, th->locking_mutex);
568 st_delete_wrap(th->vm->living_threads, th->self);
575 join_list = th->join_list;
577 rb_threadptr_interrupt(join_list->th);
578 switch (join_list->th->status) {
580 join_list->th->status = THREAD_RUNNABLE;
586 rb_threadptr_unlock_all_locking_mutexes(th);
587 rb_check_deadlock(th->vm);
589 if (!th->root_fiber) {
590 rb_thread_recycle_stack_release(th->stack);
591 th->stack = 0;
594 native_mutex_lock(&th->vm->thread_destruct_lock);
596 th->vm->running_thread = NULL;
597 native_mutex_unlock(&th->vm->thread_destruct_lock);
598 thread_cleanup_func(th, FALSE);
599 gvl_release(th->vm);
607 rb_thread_t *th, *current_th = GET_THREAD();
614 GetThreadPtr(thval, th);
617 th->first_func = fn;
618 th->first_proc = fn ? Qfalse : rb_block_proc();
619 th->first_args = args; /* GC: shouldn't put before above line */
621 th->priority = current_th->priority;
622 th->thgroup = current_th->thgroup;
624 th->pending_interrupt_queue = rb_ary_tmp_new(0);
625 th->pending_interrupt_queue_checked = 0;
626 th->pending_interrupt_mask_stack = rb_ary_dup(current_th->pending_interrupt_mask_stack);
627 RBASIC(th->pending_interrupt_mask_stack)->klass = 0;
629 th->interrupt_mask = 0;
631 native_mutex_initialize(&th->interrupt_lock);
634 err = native_thread_create(th);
636 th->status = THREAD_KILLED;
639 st_insert(th->vm->living_threads, thval, (st_data_t) th->thread_id);
666 rb_thread_t *th;
673 GetThreadPtr(thread, th);
674 if (!th->first_args) {
701 rb_thread_t *th;
705 GetThreadPtr(thread, th);
706 if (th->first_args) {
707 VALUE proc = th->first_proc, line, loc;
743 rb_thread_t *target_th = p->target, *th = p->waiting;
749 if ((*p)->th == th) {
764 rb_thread_t *target_th = p->target, *th = p->waiting;
769 sleep_forever(th, 1, 0);
778 sleep_wait_for_interrupt(th, limit - now, 0);
789 rb_thread_t *th = GET_THREAD();
792 if (th == target_th) {
800 arg.waiting = th;
809 list.th = th;
909 rb_thread_t *th;
910 GetThreadPtr(self, th);
911 thread_join(th, DELAY_INFTY);
912 return th->value;
940 sleep_forever(rb_thread_t *th, int deadlockable, int spurious_check)
942 enum rb_thread_status prev_status = th->status;
945 th->status = status;
946 RUBY_VM_CHECK_INTS_BLOCKING(th);
947 while (th->status == status) {
949 th->vm->sleeper++;
950 rb_check_deadlock(th->vm);
952 native_sleep(th, 0);
954 th->vm->sleeper--;
956 RUBY_VM_CHECK_INTS_BLOCKING(th);
960 th->status = prev_status;
980 sleep_timeval(rb_thread_t *th, struct timeval tv, int spurious_check)
983 enum rb_thread_status prev_status = th->status;
999 th->status = THREAD_STOPPED;
1000 RUBY_VM_CHECK_INTS_BLOCKING(th);
1001 while (th->status == THREAD_STOPPED) {
1002 native_sleep(th, &tv);
1003 RUBY_VM_CHECK_INTS_BLOCKING(th);
1018 th->status = prev_status;
1053 sleep_wait_for_interrupt(rb_thread_t *th, double sleepsec, int spurious_check)
1055 sleep_timeval(th, double2timeval(sleepsec), spurious_check);
1059 sleep_for_polling(rb_thread_t *th)
1064 sleep_timeval(th, time, 1);
1070 rb_thread_t *th = GET_THREAD();
1071 sleep_timeval(th, time, 1);
1078 rb_thread_t *th = GET_THREAD();
1079 RUBY_VM_CHECK_INTS_BLOCKING(th);
1080 sleep_for_polling(th);
1111 rb_thread_t *th;
1112 GetThreadPtr(thval, th);
1113 return (int)RUBY_VM_INTERRUPTED(th);
1127 rb_thread_t *th = GET_THREAD();
1129 if (th->running_time_us >= limits_us) {
1131 RB_GC_SAVE_MACHINE_CONTEXT(th);
1132 gvl_yield(th->vm, th);
1133 rb_thread_set_current(th);
1153 blocking_region_begin(rb_thread_t *th, struct rb_blocking_region_buffer *region,
1156 region->prev_status = th->status;
1157 if (set_unblock_function(th, ubf, arg, &region->oldubf, fail_if_interrupted)) {
1158 th->blocking_region_buffer = region;
1159 th->status = THREAD_STOPPED;
1160 thread_debug("enter blocking region (%p)\n", (void *)th);
1161 RB_GC_SAVE_MACHINE_CONTEXT(th);
1162 gvl_release(th->vm);
1171 blocking_region_end(rb_thread_t *th, struct rb_blocking_region_buffer *region)
1173 gvl_acquire(th->vm, th);
1174 rb_thread_set_current(th);
1175 thread_debug("leave blocking region (%p)\n", (void *)th);
1176 remove_signal_thread_list(th);
1177 th->blocking_region_buffer = 0;
1178 reset_unblock_function(th, &region->oldubf);
1179 if (th->status == THREAD_STOPPED) {
1180 th->status = region->prev_status;
1187 rb_thread_t *th = GET_THREAD();
1189 blocking_region_begin(th, region, ubf_select, th, FALSE);
1197 rb_thread_t *th = ruby_thread_from_native();
1198 blocking_region_end(th, region);
1200 RUBY_VM_CHECK_INTS_BLOCKING(th);
1210 rb_thread_t *th = GET_THREAD();
1213 th->waiting_fd = -1;
1216 data2 = th;
1225 RUBY_VM_CHECK_INTS_BLOCKING(th);
1336 rb_thread_t *th = GET_THREAD();
1340 th->waiting_fd = fd;
1342 TH_PUSH_TAG(th);
1347 }, ubf_select, th, FALSE);
1352 th->waiting_fd = -1;
1358 RUBY_VM_CHECK_INTS_BLOCKING(th);
1405 rb_thread_t *th = ruby_thread_from_native();
1410 if (th == 0) {
1420 brb = (struct rb_blocking_region_buffer *)th->blocking_region_buffer;
1421 prev_unblock = th->unblock;
1427 blocking_region_end(th, brb);
1431 blocking_region_begin(th, brb, prev_unblock.func, prev_unblock.arg, FALSE);
1447 rb_thread_t *th = ruby_thread_from_native();
1449 if (th && th->blocking_region_buffer == 0) {
1479 * will be queued to th->pending_interrupt_queue.
1491 rb_threadptr_pending_interrupt_clear(rb_thread_t *th)
1493 rb_ary_clear(th->pending_interrupt_queue);
1497 rb_threadptr_pending_interrupt_enque(rb_thread_t *th, VALUE v)
1499 rb_ary_push(th->pending_interrupt_queue, v);
1500 th->pending_interrupt_queue_checked = 0;
1511 rb_threadptr_pending_interrupt_check_mask(rb_thread_t *th, VALUE err)
1514 long mask_stack_len = RARRAY_LEN(th->pending_interrupt_mask_stack);
1515 VALUE *mask_stack = RARRAY_PTR(th->pending_interrupt_mask_stack);
1550 rb_threadptr_pending_interrupt_empty_p(rb_thread_t *th)
1552 return RARRAY_LEN(th->pending_interrupt_queue) == 0;
1556 rb_threadptr_pending_interrupt_include_p(rb_thread_t *th, VALUE err)
1559 for (i=0; i<RARRAY_LEN(th->pending_interrupt_queue); i++) {
1560 VALUE e = RARRAY_PTR(th->pending_interrupt_queue)[i];
1569 rb_threadptr_pending_interrupt_deque(rb_thread_t *th, enum handle_interrupt_timing timing)
1574 for (i=0; i<RARRAY_LEN(th->pending_interrupt_queue); i++) {
1575 VALUE err = RARRAY_PTR(th->pending_interrupt_queue)[i];
1577 enum handle_interrupt_timing mask_timing = rb_threadptr_pending_interrupt_check_mask(th, CLASS_OF(err));
1587 rb_ary_delete_at(th->pending_interrupt_queue, i);
1594 th->pending_interrupt_queue_checked = 1;
1597 VALUE err = rb_ary_shift(th->pending_interrupt_queue);
1598 if (rb_threadptr_pending_interrupt_empty_p(th)) {
1599 th->pending_interrupt_queue_checked = 1;
1606 rb_threadptr_pending_interrupt_active_p(rb_thread_t *th)
1613 if (th->pending_interrupt_queue_checked) {
1617 if (rb_threadptr_pending_interrupt_empty_p(th)) {
1676 * th = Thread.new do
1690 * th.raise "stop"
1744 rb_thread_t *th = GET_THREAD();
1754 rb_ary_push(th->pending_interrupt_mask_stack, mask);
1755 if (!rb_threadptr_pending_interrupt_empty_p(th)) {
1756 th->pending_interrupt_queue_checked = 0;
1757 RUBY_VM_SET_INTERRUPT(th);
1760 TH_PUSH_TAG(th);
1766 rb_ary_pop(th->pending_interrupt_mask_stack);
1767 if (!rb_threadptr_pending_interrupt_empty_p(th)) {
1768 th->pending_interrupt_queue_checked = 0;
1769 RUBY_VM_SET_INTERRUPT(th);
1772 RUBY_VM_CHECK_INTS(th);
1843 * th = Thread.new{
1856 * th.raise # stop thread
1862 * th = Thread.new{
1883 rb_threadptr_to_kill(rb_thread_t *th)
1885 rb_threadptr_pending_interrupt_clear(th);
1886 th->status = THREAD_RUNNABLE;
1887 th->to_kill = 1;
1888 th->errinfo = INT2FIX(TAG_FATAL);
1889 TH_JUMP_TAG(th, TAG_FATAL);
1893 rb_threadptr_execute_interrupts(rb_thread_t *th, int blocking_timing)
1895 if (th->raised_flag) return;
1907 interrupt = th->interrupt_flag;
1908 old = ATOMIC_CAS(th->interrupt_flag, interrupt, interrupt & th->interrupt_mask);
1911 interrupt &= (rb_atomic_t)~th->interrupt_mask;
1921 if (trap_interrupt && (th == th->vm->main_thread)) {
1922 enum rb_thread_status prev_status = th->status;
1923 th->status = THREAD_RUNNABLE;
1925 rb_signal_exec(th, sig);
1927 th->status = prev_status;
1931 if (pending_interrupt && rb_threadptr_pending_interrupt_active_p(th)) {
1932 VALUE err = rb_threadptr_pending_interrupt_deque(th, blocking_timing ? INTERRUPT_ON_BLOCKING : INTERRUPT_NONE);
1941 rb_threadptr_to_kill(th);
1944 /* set runnable if th was slept. */
1945 if (th->status == THREAD_STOPPED ||
1946 th->status == THREAD_STOPPED_FOREVER)
1947 th->status = THREAD_RUNNABLE;
1959 if (th->priority > 0)
1960 limits_us <<= th->priority;
1962 limits_us >>= -th->priority;
1964 if (th->status == THREAD_RUNNABLE)
1965 th->running_time_us += TIME_QUANTUM_USEC;
1967 EXEC_EVENT_HOOK(th, RUBY_EVENT_SWITCH, th->cfp->self, 0, 0, Qundef);
1977 rb_thread_t *th;
1978 GetThreadPtr(thval, th);
1979 rb_threadptr_execute_interrupts(th, 1);
1983 rb_threadptr_ready(rb_thread_t *th)
1985 rb_threadptr_interrupt(th);
1989 rb_threadptr_raise(rb_thread_t *th, int argc, VALUE *argv)
1993 if (rb_threadptr_dead(th)) {
2003 rb_threadptr_pending_interrupt_enque(th, exc);
2004 rb_threadptr_interrupt(th);
2009 rb_threadptr_signal_raise(rb_thread_t *th, int sig)
2015 rb_threadptr_raise(th->vm->main_thread, 2, argv);
2019 rb_threadptr_signal_exit(rb_thread_t *th)
2025 rb_threadptr_raise(th->vm->main_thread, 2, argv);
2033 ruby_thread_stack_overflow(rb_thread_t *th)
2035 th->raised_flag = 0;
2039 th->errinfo = sysstack_error;
2040 TH_JUMP_TAG(th, TAG_RAISE);
2045 rb_threadptr_set_raised(rb_thread_t *th)
2047 if (th->raised_flag & RAISED_EXCEPTION) {
2050 th->raised_flag |= RAISED_EXCEPTION;
2055 rb_threadptr_reset_raised(rb_thread_t *th)
2057 if (!(th->raised_flag & RAISED_EXCEPTION)) {
2060 th->raised_flag &= ~RAISED_EXCEPTION;
2068 rb_thread_t *th;
2069 GetThreadPtr((VALUE)key, th);
2071 if (th->waiting_fd == fd) {
2072 VALUE err = th->vm->special_exceptions[ruby_error_closed_stream];
2073 rb_threadptr_pending_interrupt_enque(th, err);
2074 rb_threadptr_interrupt(th);
2110 rb_thread_t *th = GET_THREAD();
2115 if (th == target_th) {
2116 RUBY_VM_CHECK_INTS(th);
2137 rb_thread_t *th;
2139 GetThreadPtr(thread, th);
2141 if (th != GET_THREAD() && th->safe_level < 4) {
2144 if (th->to_kill || th->status == THREAD_KILLED) {
2147 if (th == th->vm->main_thread) {
2151 thread_debug("rb_thread_kill: %p (%p)\n", (void *)th, (void *)th->thread_id);
2153 if (th == GET_THREAD()) {
2155 rb_threadptr_to_kill(th);
2158 rb_threadptr_pending_interrupt_enque(th, eKillSignal);
2159 rb_threadptr_interrupt(th);
2180 rb_thread_s_kill(VALUE obj, VALUE th)
2182 return rb_thread_kill(th);
2199 rb_thread_t *th = GET_THREAD();
2200 return rb_thread_kill(th->self);
2233 rb_thread_t *th;
2234 GetThreadPtr(thread, th);
2236 if (th->status == THREAD_KILLED) {
2239 rb_threadptr_ready(th);
2240 if (th->status == THREAD_STOPPED || th->status == THREAD_STOPPED_FOREVER)
2241 th->status = THREAD_RUNNABLE;
2307 rb_thread_t *th;
2308 GetThreadPtr((VALUE)key, th);
2310 switch (th->status) {
2314 rb_ary_push(ary, th->self);
2456 rb_thread_t *th;
2457 GetThreadPtr(thread, th);
2458 return th->abort_on_exception ? Qtrue : Qfalse;
2474 rb_thread_t *th;
2477 GetThreadPtr(thread, th);
2478 th->abort_on_exception = RTEST(val);
2496 rb_thread_t *th;
2498 GetThreadPtr(thread, th);
2499 group = th->thgroup;
2508 thread_status_name(rb_thread_t *th)
2510 switch (th->status) {
2512 if (th->to_kill)
2527 rb_threadptr_dead(rb_thread_t *th)
2529 return th->status == THREAD_KILLED;
2558 rb_thread_t *th;
2559 GetThreadPtr(thread, th);
2561 if (rb_threadptr_dead(th)) {
2562 if (!NIL_P(th->errinfo) && !FIXNUM_P(th->errinfo)
2568 return rb_str_new2(thread_status_name(th));
2587 rb_thread_t *th;
2588 GetThreadPtr(thread, th);
2590 if (rb_threadptr_dead(th))
2610 rb_thread_t *th;
2611 GetThreadPtr(thread, th);
2613 if (rb_threadptr_dead(th))
2615 if (th->status == THREAD_STOPPED || th->status == THREAD_STOPPED_FOREVER)
2635 rb_thread_t *th;
2636 GetThreadPtr(thread, th);
2638 return INT2NUM(th->safe_level);
2652 rb_thread_t *th;
2656 GetThreadPtr(thread, th);
2657 status = thread_status_name(th);
2667 rb_thread_t *th;
2670 GetThreadPtr(thread, th);
2671 if (rb_safe_level() >= 4 && th != GET_THREAD()) {
2674 if (!th->local_storage) {
2677 if (st_lookup(th->local_storage, id, &val)) {
2695 * ].each do |th|
2696 * th.join
2697 * puts "#{th.inspect}: #{th[:name]}"
2752 rb_thread_t *th;
2753 GetThreadPtr(thread, th);
2755 if (rb_safe_level() >= 4 && th != GET_THREAD()) {
2761 if (!th->local_storage) {
2762 th->local_storage = st_init_numtable();
2765 st_delete_wrap(th->local_storage, id);
2768 st_insert(th->local_storage, id, val);
2822 rb_thread_t *th;
2824 GetThreadPtr(thread, th);
2826 if (rb_safe_level() >= 4 && th != GET_THREAD()) {
2847 rb_thread_t *th;
2849 GetThreadPtr(thread, th);
2851 if (rb_safe_level() >= 4 && th != GET_THREAD()) {
2878 rb_thread_t *th;
2881 GetThreadPtr(self, th);
2883 if (!th->local_storage) {
2886 if (st_lookup(th->local_storage, id, 0)) {
2933 rb_thread_t *th;
2935 GetThreadPtr(self, th);
2937 if (th->local_storage) {
2938 st_foreach(th->local_storage, thread_keys_i, ary);
3031 rb_thread_t *th;
3032 GetThreadPtr(thread, th);
3033 return INT2NUM(th->priority);
3066 rb_thread_t *th;
3068 GetThreadPtr(thread, th);
3073 th->priority = NUM2INT(prio);
3074 native_thread_apply_priority(th);
3083 th->priority = priority;
3085 return INT2NUM(th->priority);
3346 rb_thread_t *th = GET_THREAD();
3366 result = native_fd_select(n, read, write, except, timeout, th);
3368 }, ubf_select, th, FALSE);
3370 RUBY_VM_CHECK_INTS_BLOCKING(th);
3570 rb_thread_t *th = GET_THREAD();
3588 }, ubf_select, th, FALSE);
3590 RUBY_VM_CHECK_INTS_BLOCKING(th);
3745 * vm->running_thread switch. however it guarantee th->running_thread
3760 rb_thread_t *th = vm->running_thread;
3816 rb_thread_t *th = GET_THREAD();
3817 rb_vm_t *vm = th->vm;
3818 VALUE thval = th->self;
3819 vm->main_thread = th;
3821 gvl_atfork(th->vm);
3822 st_foreach(vm->living_threads, atfork, (st_data_t)th);
3824 st_insert(vm->living_threads, thval, (st_data_t)th->thread_id);
3833 rb_thread_t *th;
3834 GetThreadPtr(thval, th);
3836 if (th != (rb_thread_t *)current_th) {
3837 rb_mutex_abandon_keeping_mutexes(th);
3838 rb_mutex_abandon_locking_mutex(th);
3839 thread_cleanup_func(th, TRUE);
3858 rb_thread_t *th;
3859 GetThreadPtr(thval, th);
3861 if (th != (rb_thread_t *)current_th) {
3862 thread_cleanup_func_before_exec(th);
3931 rb_thread_t *th;
3932 GetThreadPtr(thread, th);
3934 if (th->thgroup == group) {
4042 rb_thread_t *th;
4046 GetThreadPtr(thread, th);
4056 if (!th->thgroup) {
4060 if (OBJ_FROZEN(th->thgroup)) {
4063 TypedData_Get_Struct(th->thgroup, struct thgroup, &thgroup_data_type, data);
4069 th->thgroup = group;
4109 if (mutex->th) {
4111 const char *err = rb_mutex_unlock_th(mutex, mutex->th);
4183 return mutex->th ? Qtrue : Qfalse;
4187 mutex_locked(rb_thread_t *th, VALUE self)
4192 if (th->keeping_mutexes) {
4193 mutex->next_mutex = th->keeping_mutexes;
4195 th->keeping_mutexes = mutex;
4213 if (mutex->th == 0) {
4214 mutex->th = GET_THREAD();
4225 lock_func(rb_thread_t *th, rb_mutex_t *mutex, int timeout_ms)
4232 if (!mutex->th) {
4233 mutex->th = th;
4236 if (RUBY_VM_INTERRUPTED(th)) {
4291 rb_thread_t *th = GET_THREAD();
4296 if (!mutex->allow_trap && th->interrupt_mask & TRAP_INTERRUPT_MASK) {
4301 if (mutex->th == GET_THREAD()) {
4305 while (mutex->th != th) {
4307 enum rb_thread_status prev_status = th->status;
4311 set_unblock_function(th, lock_interrupt, mutex, &oldubf, FALSE);
4312 th->status = THREAD_STOPPED_FOREVER;
4313 th->locking_mutex = self;
4316 th->vm->sleeper++;
4322 if ((vm_living_thread_num(th->vm) == th->vm->sleeper) &&
4325 patrol_thread = th;
4329 interrupted = lock_func(th, mutex, (int)timeout_ms);
4333 if (patrol_thread == th)
4336 reset_unblock_function(th, &oldubf);
4338 th->locking_mutex = Qfalse;
4339 if (mutex->th && interrupted == 2) {
4340 rb_check_deadlock(th->vm);
4342 if (th->status == THREAD_STOPPED_FOREVER) {
4343 th->status = prev_status;
4345 th->vm->sleeper--;
4347 if (mutex->th == th) mutex_locked(th, self);
4350 RUBY_VM_CHECK_INTS_BLOCKING(th);
4368 rb_thread_t *th = GET_THREAD();
4373 if (mutex->th == th)
4380 rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t volatile *th)
4386 if (mutex->th == 0) {
4389 else if (mutex->th != th) {
4393 mutex->th = 0;
4401 rb_mutex_t *volatile *th_mutex = &th->keeping_mutexes;
4433 rb_mutex_abandon_keeping_mutexes(rb_thread_t *th)
4435 if (th->keeping_mutexes) {
4436 rb_mutex_abandon_all(th->keeping_mutexes);
4438 th->keeping_mutexes = NULL;
4442 rb_mutex_abandon_locking_mutex(rb_thread_t *th)
4446 if (!th->locking_mutex) return;
4448 GetMutexPtr(th->locking_mutex, mutex);
4449 if (mutex->th == th)
4451 th->locking_mutex = Qfalse;
4462 mutex->th = 0;
4633 if (m->th == GET_THREAD()) return Qnil;
4973 rb_thread_t *th = GET_THREAD();
5042 th->thgroup = th->vm->thgroup_default = rb_obj_alloc(cThGroup);
5043 rb_define_const(cThGroup, "Default", th->thgroup);
5065 gvl_init(th->vm);
5066 gvl_acquire(th->vm, th);
5067 native_mutex_initialize(&th->vm->thread_destruct_lock);
5068 native_mutex_initialize(&th->interrupt_lock);
5070 th->pending_interrupt_queue = rb_ary_tmp_new(0);
5071 th->pending_interrupt_queue_checked = 0;
5072 th->pending_interrupt_mask_stack = rb_ary_tmp_new(0);
5074 th->interrupt_mask = 0;
5087 rb_thread_t *th = ruby_thread_from_native();
5089 return th != 0;
5096 rb_thread_t *th;
5097 GetThreadPtr(thval, th);
5099 if (th->status != THREAD_STOPPED_FOREVER || RUBY_VM_INTERRUPTED(th)) {
5102 else if (th->locking_mutex) {
5104 GetMutexPtr(th->locking_mutex, mutex);
5107 if (mutex->th == th || (!mutex->th && mutex->cond_waiting)) {
5121 rb_thread_t *th;
5122 GetThreadPtr(thval, th);
5124 printf("th:%p %d %d", th, th->status, th->interrupt_flag);
5125 if (th->locking_mutex) {
5127 GetMutexPtr(th->locking_mutex, mutex);
5130 printf(" %p %d\n", mutex->th, mutex->cond_waiting);