54 #undef _FORTIFY_SOURCE
55 #undef __USE_FORTIFY_LEVEL
56 #define __USE_FORTIFY_LEVEL 0
67 #ifndef USE_NATIVE_THREAD_PRIORITY
68 #define USE_NATIVE_THREAD_PRIORITY 0
69 #define RUBY_THREAD_PRIORITY_MAX 3
70 #define RUBY_THREAD_PRIORITY_MIN -3
74 #define THREAD_DEBUG 0
93 #define eKillSignal INT2FIX(0)
94 #define eTerminateSignal INT2FIX(1)
97 #define closed_stream_error GET_VM()->special_exceptions[ruby_error_closed_stream]
107 #define THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
123 #define RB_GC_SAVE_MACHINE_REGISTER_STACK(th) \
124 do{(th)->machine.register_stack_end = rb_ia64_bsp();}while(0)
126 #define RB_GC_SAVE_MACHINE_REGISTER_STACK(th)
128 #define RB_GC_SAVE_MACHINE_CONTEXT(th) \
130 FLUSH_REGISTER_WINDOWS; \
131 RB_GC_SAVE_MACHINE_REGISTER_STACK(th); \
132 setjmp((th)->machine.regs); \
133 SET_MACHINE_STACK_END(&(th)->machine.stack_end); \
136 #define GVL_UNLOCK_BEGIN() do { \
137 rb_thread_t *_th_stored = GET_THREAD(); \
138 RB_GC_SAVE_MACHINE_CONTEXT(_th_stored); \
139 gvl_release(_th_stored->vm);
141 #define GVL_UNLOCK_END() \
142 gvl_acquire(_th_stored->vm, _th_stored); \
143 rb_thread_set_current(_th_stored); \
147 #define only_if_constant(expr, notconst) (__builtin_constant_p(expr) ? (expr) : (notconst))
149 #define only_if_constant(expr, notconst) notconst
151 #define BLOCKING_REGION(exec, ubf, ubfarg, fail_if_interrupted) do { \
152 rb_thread_t *__th = GET_THREAD(); \
153 struct rb_blocking_region_buffer __region; \
154 if (blocking_region_begin(__th, &__region, (ubf), (ubfarg), fail_if_interrupted) || \
156 !only_if_constant(fail_if_interrupted, TRUE)) { \
158 blocking_region_end(__th, &__region); \
163 #ifdef HAVE_VA_ARGS_MACRO
164 void rb_thread_debug(
const char *file,
int line,
const char *fmt, ...);
165 #define thread_debug(fmt, ...) rb_thread_debug(__FILE__, __LINE__, fmt, ##__VA_ARGS__)
166 #define POSITION_FORMAT "%s:%d:"
167 #define POSITION_ARGS ,file, line
169 void rb_thread_debug(
const char *fmt, ...);
170 #define thread_debug rb_thread_debug
171 #define POSITION_FORMAT
172 #define POSITION_ARGS
175 # if THREAD_DEBUG < 0
176 static int rb_thread_debug_enabled;
187 rb_thread_s_debug(
void)
189 return INT2NUM(rb_thread_debug_enabled);
203 rb_thread_debug_enabled =
RTEST(val) ?
NUM2INT(val) : 0;
207 # define rb_thread_debug_enabled THREAD_DEBUG
210 #define thread_debug if(0)printf
214 #define thread_start_func_2(th, st, rst) thread_start_func_2(th, st)
217 VALUE *register_stack_start));
223 #define DEBUG_OUT() \
224 WaitForSingleObject(&debug_mutex, INFINITE); \
225 printf(POSITION_FORMAT"%p - %s" POSITION_ARGS, GetCurrentThreadId(), buf); \
227 ReleaseMutex(&debug_mutex);
229 #elif defined(HAVE_PTHREAD_H)
232 #define DEBUG_OUT() \
233 pthread_mutex_lock(&debug_mutex); \
234 printf(POSITION_FORMAT"%#"PRIxVALUE" - %s" POSITION_ARGS, (VALUE)pthread_self(), buf); \
236 pthread_mutex_unlock(&debug_mutex);
239 #error "unsupported thread type"
243 static int debug_mutex_initialized = 1;
248 #ifdef HAVE_VA_ARGS_MACRO
249 const char *file,
int line,
251 const char *fmt, ...)
256 if (!rb_thread_debug_enabled)
return;
258 if (debug_mutex_initialized == 1) {
259 debug_mutex_initialized = 0;
260 native_mutex_initialize(&debug_mutex);
282 native_mutex_initialize(lock);
288 native_mutex_destroy(lock);
294 native_mutex_lock(lock);
300 native_mutex_unlock(lock);
308 if (fail_if_interrupted) {
377 if (th != main_thread) {
383 thread_debug(
"terminate_i: main thread (%p)\n", (
void *)th);
416 if (err)
rb_bug(
"invalid keeping_mutexes: %s", err);
427 rb_bug(
"rb_thread_terminate_all: called by child thread (%p, %p)",
435 thread_debug(
"rb_thread_terminate_all (main thread: %p)\n", (
void *)th);
470 th->
machine.register_stack_start = th->
machine.register_stack_end = 0;
491 native_thread_destroy(th);
499 native_thread_init_stack(th);
511 # ifdef USE_SIGALTSTACK
514 rb_register_sigaltstack(th);
518 rb_bug(
"thread_start_func_2 must not be used for main thread");
520 ruby_thread_set_native(th);
524 th->
machine.register_stack_start = register_stack_start;
528 gvl_acquire(th->
vm, th);
530 thread_debug(
"thread start (get lock): %p\n", (
void *)th);
537 GetProcPtr(th->first_proc, proc);
539 th->root_lep = rb_vm_ep_local_ep(proc->block.ep);
540 th->root_svar = Qnil;
541 EXEC_EVENT_HOOK(th, RUBY_EVENT_THREAD_BEGIN, th->self, 0, 0, Qundef);
542 th->value = rb_vm_invoke_proc(th, proc, (int)RARRAY_LEN(args), RARRAY_CONST_PTR(args), 0);
543 EXEC_EVENT_HOOK(th, RUBY_EVENT_THREAD_END, th->self, 0, 0, Qundef);
546 th->value = (*th->first_func)((void *)args);
583 rb_bug(
"thread_start_func_2: locking_mutex must not be set (%p:%"PRIxVALUE")",
603 join_list = join_list->
next;
632 "can't start a new thread (frozen ThreadGroup)");
652 native_cond_initialize(&th->
interrupt_cond, RB_CONDATTR_CLOCK_MONOTONIC);
655 err = native_thread_create(th);
752 #define DELAY_INFTY 1E30
770 if ((*p)->th == th) {
813 if (th == target_th) {
816 if (
GET_VM()->main_thread == target_th) {
856 return target_th->
self;
951 #if SIGNEDNESS_OF_TIME_T < 0
952 # define TIMEVAL_SEC_MAX SIGNED_INTEGER_MAX(TYPEOF_TIMEVAL_TV_SEC)
953 # define TIMEVAL_SEC_MIN SIGNED_INTEGER_MIN(TYPEOF_TIMEVAL_TV_SEC)
954 #elif SIGNEDNESS_OF_TIME_T > 0
955 # define TIMEVAL_SEC_MAX ((TYPEOF_TIMEVAL_TV_SEC)(~(unsigned_time_t)0))
956 # define TIMEVAL_SEC_MIN ((TYPEOF_TIMEVAL_TV_SEC)0)
963 const double TIMEVAL_SEC_MAX_PLUS_ONE = (2*(double)(TIMEVAL_SEC_MAX/2+1));
967 if (TIMEVAL_SEC_MAX_PLUS_ONE <= d) {
968 time.
tv_sec = TIMEVAL_SEC_MAX;
971 else if (d <= TIMEVAL_SEC_MIN) {
972 time.
tv_sec = TIMEVAL_SEC_MIN;
977 time.
tv_usec = (int)((d - (time_t)d) * 1e6);
994 while (th->
status == status) {
1004 if (!spurious_check)
1007 th->
status = prev_status;
1013 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
1034 to.
tv_sec = TIMEVAL_SEC_MAX;
1038 if (to.
tv_sec == TIMEVAL_SEC_MAX)
1049 native_sleep(th, &tv);
1062 if (!spurious_check)
1065 th->
status = prev_status;
1085 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
1179 gvl_yield(th->
vm, th);
1207 thread_debug(
"enter blocking region (%p)\n", (
void *)th);
1209 gvl_release(th->
vm);
1220 gvl_acquire(th->
vm, th);
1222 thread_debug(
"leave blocking region (%p)\n", (
void *)th);
1223 remove_signal_thread_list(th);
1243 int saved_errno =
errno;
1248 errno = saved_errno;
1258 int saved_errno = 0;
1268 saved_errno =
errno;
1269 }, ubf, data2, fail_if_interrupted);
1271 if (!fail_if_interrupted) {
1275 errno = saved_errno;
1384 int saved_errno = 0;
1393 saved_errno =
errno;
1394 }, ubf_select, th,
FALSE);
1407 errno = saved_errno;
1417 void *(*f)(
void*) = (
void *(*)(
void*))
func;
1463 fprintf(stderr,
"[BUG] rb_thread_call_with_gvl() is called by non-ruby thread\n");
1471 rb_bug(
"rb_thread_call_with_gvl: called by a thread which has GVL.");
1568 for (i=0; i<mask_stack_len; i++) {
1569 mask = mask_stack[mask_stack_len-(i+1)];
1571 for (j=0; j<ancestors_len; j++) {
1572 VALUE klass = ancestors_ptr[j];
1626 switch (mask_timing) {
1948 }
while (old != interrupt);
1956 int postponed_job_interrupt = 0;
1962 int timer_interrupt;
1963 int pending_interrupt;
1971 if (postponed_job_interrupt) {
2007 if (timer_interrupt) {
2008 unsigned long limits_us = TIME_QUANTUM_USEC;
2079 #if defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK)
2080 #define USE_SIGALTSTACK
2087 #ifdef USE_SIGALTSTACK
2166 if (th == target_th) {
2346 "stopping only thread\n\tnote: use sleep to stop forever");
2743 str =
rb_sprintf(
"#<%s:%p %s>", cname, (
void *)thread, status);
2832 if (!
id)
return Qnil;
2919 if (!
id)
return Qnil;
3090 if (!
RHASH(locals)->ntbl)
3158 #if USE_NATIVE_THREAD_PRIORITY
3160 native_thread_apply_priority(th);
3176 #if defined(NFDBITS) && defined(HAVE_RB_FD_INIT)
3213 fds->fdset =
ALLOC(fd_set);
3214 FD_ZERO(fds->fdset);
3220 size_t size = howmany(
rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
3222 if (size <
sizeof(fd_set))
3223 size =
sizeof(fd_set);
3224 dst->maxfd = src->maxfd;
3226 memcpy(dst->fdset, src->fdset, size);
3232 if (fds->fdset)
xfree(fds->fdset);
3241 MEMZERO(fds->fdset, fd_mask, howmany(fds->maxfd, NFDBITS));
3247 size_t m = howmany(n + 1, NFDBITS) *
sizeof(fd_mask);
3248 size_t o = howmany(fds->maxfd, NFDBITS) *
sizeof(fd_mask);
3250 if (m <
sizeof(fd_set)) m =
sizeof(fd_set);
3251 if (o <
sizeof(fd_set)) o =
sizeof(fd_set);
3254 fds->fdset =
xrealloc(fds->fdset, m);
3255 memset((
char *)fds->fdset + o, 0, m - o);
3257 if (n >= fds->maxfd) fds->maxfd = n + 1;
3270 if (n >= fds->maxfd)
return;
3277 if (n >= fds->maxfd)
return 0;
3278 return FD_ISSET(n, fds->fdset) != 0;
3284 size_t size = howmany(max, NFDBITS) *
sizeof(fd_mask);
3286 if (size <
sizeof(fd_set)) size =
sizeof(fd_set);
3288 dst->fdset =
xrealloc(dst->fdset, size);
3289 memcpy(dst->fdset, src, size);
3295 size_t size = howmany(
rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
3297 if (size >
sizeof(fd_set)) {
3300 memcpy(dst,
rb_fd_ptr(src),
sizeof(fd_set));
3306 size_t size = howmany(
rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
3308 if (size <
sizeof(fd_set))
3309 size =
sizeof(fd_set);
3310 dst->maxfd = src->maxfd;
3311 dst->fdset =
xrealloc(dst->fdset, size);
3312 memcpy(dst->fdset, src->fdset, size);
3315 #ifdef __native_client__
3316 int select(
int nfds, fd_set *readfds, fd_set *writefds,
3317 fd_set *exceptfds,
struct timeval *timeout);
3336 return select(n, r, w, e, timeout);
3344 #define FD_ZERO(f) rb_fd_zero(f)
3345 #define FD_SET(i, f) rb_fd_set((i), (f))
3346 #define FD_CLR(i, f) rb_fd_clr((i), (f))
3347 #define FD_ISSET(i, f) rb_fd_isset((i), (f))
3349 #elif defined(_WIN32)
3354 set->capa = FD_SETSIZE;
3355 set->fdset =
ALLOC(fd_set);
3356 FD_ZERO(set->fdset);
3373 if (max > FD_SETSIZE || (UINT)max > dst->fd_count) {
3377 memcpy(dst->fd_array, src->fdset->fd_array, max);
3378 dst->fd_count =
max;
3395 for (i = 0; i < set->fdset->fd_count; i++) {
3396 if (set->fdset->fd_array[i] == s) {
3400 if (set->fdset->fd_count >= (
unsigned)set->capa) {
3401 set->capa = (set->fdset->fd_count / FD_SETSIZE + 1) * FD_SETSIZE;
3402 set->fdset =
xrealloc(set->fdset,
sizeof(
unsigned int) +
sizeof(SOCKET) * set->capa);
3404 set->fdset->fd_array[set->fdset->fd_count++] = s;
3412 #define FD_ZERO(f) rb_fd_zero(f)
3413 #define FD_SET(i, f) rb_fd_set((i), (f))
3414 #define FD_CLR(i, f) rb_fd_clr((i), (f))
3415 #define FD_ISSET(i, f) rb_fd_isset((i), (f))
3418 #define rb_fd_rcopy(d, s) (*(d) = *(s))
3436 limit += (double)timeout->
tv_sec+(
double)timeout->
tv_usec*1e-6;
3437 wait_rest = *timeout;
3438 timeout = &wait_rest;
3452 result = native_fd_select(n, read, write, except, timeout, th);
3454 }, ubf_select, th,
FALSE);
3476 wait_rest.
tv_sec = (time_t)d;
3477 wait_rest.
tv_usec = (int)((d-(
double)wait_rest.
tv_sec)*1e6);
3504 thread_debug(
"rb_thread_wait_fd_rw(%d, %s)\n", fd, read ?
"read" :
"write");
3515 thread_debug(
"rb_thread_wait_fd_rw(%d, %s): done\n", fd, read ?
"read" :
"write");
3579 if (!read && !write && !except) {
3597 return do_select(max, read, write, except, timeout);
3605 #if defined(HAVE_POLL) && defined(__linux__)
3612 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
3613 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
3614 #define POLLEX_SET (POLLPRI)
3619 ppoll(
struct pollfd *fds, nfds_t nfds,
3620 const struct timespec *ts,
const sigset_t *sigmask)
3627 if (ts->
tv_sec > TIMET_MAX/1000)
3631 tmp2 = ts->
tv_nsec / (1000 * 1000);
3632 if (TIMET_MAX - tmp < tmp2)
3635 timeout_ms = tmp + tmp2;
3641 return poll(fds, nfds, timeout_ms);
3667 fds.events = (short)events;
3672 result = ppoll(&fds, 1, timeout,
NULL);
3673 if (result < 0) lerrno =
errno;
3674 }, ubf_select, th,
FALSE);
3700 if (fds.revents & POLLNVAL) {
3710 if (fds.revents & POLLIN_SET)
3712 if (fds.revents & POLLOUT_SET)
3714 if (fds.revents & POLLEX_SET)
3800 #ifdef USE_CONSERVATIVE_STACK_END
3805 *stack_end_p = &stack_end;
3845 if (vm->prove_profile.enable) {
3848 if (vm->during_gc) {
3858 if (timer_thread_id && native_stop_timer_thread(close_anyway)) {
3859 native_reset_timer_thread();
3866 native_reset_timer_thread();
3873 rb_thread_create_timer_thread();
3894 if (
RTEST(coverages)) {
3967 return ptr ?
sizeof(
struct thgroup) : 0;
4149 "can't move from the enclosed thread group");
4182 #define GetMutexPtr(obj, tobj) \
4183 TypedData_Get_Struct((obj), rb_mutex_t, &mutex_data_type, (tobj))
4185 #define mutex_mark NULL
4195 if (err)
rb_bug(
"%s", err);
4197 native_mutex_destroy(&mutex->
lock);
4198 native_cond_destroy(&mutex->
cond);
4233 native_mutex_initialize(&mutex->
lock);
4234 native_cond_initialize(&mutex->
cond, RB_CONDATTR_CLOCK_MONOTONIC);
4296 native_mutex_lock(&mutex->
lock);
4297 if (mutex->
th == 0) {
4303 native_mutex_unlock(&mutex->
lock);
4311 int interrupted = 0;
4334 timeout_rel.
tv_nsec = timeout_ms * 1000 * 1000;
4335 timeout = native_cond_timeout(&mutex->
cond, timeout_rel);
4336 err = native_cond_timedwait(&mutex->
cond, &mutex->
lock, &timeout);
4339 native_cond_wait(&mutex->
cond, &mutex->
lock);
4352 native_mutex_lock(&mutex->
lock);
4354 native_cond_broadcast(&mutex->
cond);
4355 native_mutex_unlock(&mutex->
lock);
4389 while (mutex->
th != th) {
4392 volatile int timeout_ms = 0;
4399 native_mutex_lock(&mutex->
lock);
4413 interrupted =
lock_func(th, mutex, (
int)timeout_ms);
4414 native_mutex_unlock(&mutex->
lock);
4417 if (patrol_thread == th)
4418 patrol_thread =
NULL;
4423 if (mutex->
th && interrupted == 2) {
4427 th->
status = prev_status;
4457 if (mutex->
th == th)
4468 native_mutex_lock(&mutex->
lock);
4470 if (mutex->
th == 0) {
4471 err =
"Attempt to unlock a mutex which is not locked";
4473 else if (mutex->
th != th) {
4474 err =
"Attempt to unlock a mutex which is locked by another thread";
4479 native_cond_signal(&mutex->
cond);
4482 native_mutex_unlock(&mutex->
lock);
4486 while (*th_mutex != mutex) {
4533 if (mutex->
th == th)
4572 if (!
NIL_P(timeout)) {
4577 if (
NIL_P(timeout)) {
4583 end = time(0) - beg;
4671 #define GetThreadShieldPtr(obj) ((VALUE)rb_check_typeddata((obj), &thread_shield_data_type))
4672 #define THREAD_SHIELD_WAITING_MASK (FL_USER0|FL_USER1|FL_USER2|FL_USER3|FL_USER4|FL_USER5|FL_USER6|FL_USER7|FL_USER8|FL_USER9|FL_USER10|FL_USER11|FL_USER12|FL_USER13|FL_USER14|FL_USER15|FL_USER16|FL_USER17|FL_USER18|FL_USER19)
4673 #define THREAD_SHIELD_WAITING_SHIFT (FL_USHIFT)
4674 #define rb_thread_shield_waiting(b) (int)((RBASIC(b)->flags&THREAD_SHIELD_WAITING_MASK)>>THREAD_SHIELD_WAITING_SHIFT)
4702 return thread_shield;
4719 if (!mutex)
return Qfalse;
4816 #if SIZEOF_LONG == SIZEOF_VOIDP
4817 #define OBJ_ID_EQL(obj_id, other) ((obj_id) == (other))
4818 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
4819 #define OBJ_ID_EQL(obj_id, other) (RB_TYPE_P((obj_id), T_BIGNUM) ? \
4820 rb_big_eql((obj_id), (other)) : ((obj_id) == (other)))
4826 if (paired_obj_id) {
4861 VALUE other_paired_obj = pair_list;
4883 if (pair_list ==
Qundef) {
4940 if (outer && !outermost) {
4957 if (result == p.list) {
5072 #define rb_intern(str) rb_intern_const(str)
5094 #if THREAD_DEBUG < 0
5160 recursive_key =
rb_intern(
"__recursive_key__");
5169 gvl_acquire(th->
vm, th);
5173 RB_CONDATTR_CLOCK_MONOTONIC);
5183 rb_thread_create_timer_thread();
5186 (void)native_mutex_trylock;
5211 native_mutex_lock(&mutex->
lock);
5215 native_mutex_unlock(&mutex->
lock);
5221 #ifdef DEBUG_DEADLOCK_CHECK
5234 native_mutex_lock(&mutex->
lock);
5236 native_mutex_unlock(&mutex->
lock);
5252 if (patrol_thread && patrol_thread !=
GET_THREAD())
return;
5259 argv[1] =
rb_str_new2(
"No live threads left. Deadlock?");
5260 #ifdef DEBUG_DEADLOCK_CHECK
5273 if (coverage &&
RBASIC(coverage)->klass == 0) {
5289 return GET_VM()->coverages;
5295 GET_VM()->coverages = coverages;
5331 err =
kill(pid, sig);
#define RBASIC_CLEAR_CLASS(obj)
static int vm_living_thread_num(rb_vm_t *vm)
struct timeval rb_time_interval(VALUE num)
void rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th)
rb_thread_list_t * join_list
static VALUE thgroup_enclose(VALUE group)
rb_nativethread_cond_t cond
static VALUE rb_thread_variable_p(VALUE thread, VALUE key)
#define RUBY_VM_CHECK_INTS(th)
unsigned long running_time_us
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
static VALUE thgroup_add(VALUE group, VALUE thread)
void ruby_kill(rb_pid_t pid, int sig)
static int check_deadlock_i(st_data_t key, st_data_t val, int *found)
VALUE rb_mutex_sleep(VALUE self, VALUE timeout)
int ruby_thread_has_gvl_p(void)
VALUE rb_ary_pop(VALUE ary)
static VALUE rb_thread_s_handle_interrupt(VALUE self, VALUE mask_arg)
static const rb_thread_t * patrol_thread
struct rb_mutex_struct * next_mutex
void ruby_thread_stack_overflow(rb_thread_t *th)
void rb_bug(const char *fmt,...)
static VALUE rb_thread_priority(VALUE thread)
int gettimeofday(struct timeval *, struct timezone *)
void rb_postponed_job_flush(rb_vm_t *vm)
#define RUBY_TYPED_FREE_IMMEDIATELY
static int lock_func(rb_thread_t *th, rb_mutex_t *mutex, int timeout_ms)
static const char * rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t volatile *th)
VALUE rb_obj_id(VALUE obj)
static void thread_cleanup_func_before_exec(void *th_ptr)
static VALUE trap(int sig, sighandler_t func, VALUE command)
struct rb_thread_struct * running_thread
VALUE rb_make_exception(int argc, VALUE *argv)
void rb_mutex_allow_trap(VALUE self, int val)
void rb_nativethread_lock_unlock(rb_nativethread_lock_t *lock)
#define RUBY_VM_SET_INTERRUPT(th)
static VALUE rb_thread_abort_exc_set(VALUE thread, VALUE val)
VALUE rb_mutex_owned_p(VALUE self)
int pending_interrupt_queue_checked
static void rb_mutex_abandon_all(rb_mutex_t *mutexes)
struct rb_blocking_region_buffer * rb_thread_blocking_region_begin(void)
VALUE rb_exec_recursive_outer(VALUE(*func)(VALUE, VALUE, int), VALUE obj, VALUE arg)
static int max(int a, int b)
VALUE(* func)(VALUE, VALUE, int)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
static VALUE thgroup_enclosed_p(VALUE group)
int rb_thread_check_trap_pending(void)
void rb_threadptr_execute_interrupts(rb_thread_t *th, int blocking_timing)
VALUE rb_thread_list(void)
static VALUE thread_join_sleep(VALUE arg)
VALUE rb_exec_recursive(VALUE(*func)(VALUE, VALUE, int), VALUE obj, VALUE arg)
static int terminate_atfork_before_exec_i(st_data_t key, st_data_t val, st_data_t current_th)
void rb_thread_blocking_region_end(struct rb_blocking_region_buffer *region)
static VALUE rb_thread_variables(VALUE thread)
struct rb_thread_struct * th
void rb_unblock_function_t(void *)
VALUE rb_ary_delete_at(VALUE ary, long pos)
static VALUE recursive_list_access(void)
rb_unblock_function_t * func
int rb_remove_event_hook(rb_event_hook_func_t func)
int st_insert(st_table *, st_data_t, st_data_t)
static void update_coverage(rb_event_flag_t event, VALUE proc, VALUE self, ID id, VALUE klass)
static VALUE thread_s_new(int argc, VALUE *argv, VALUE klass)
void rb_error_frozen(const char *what)
#define TypedData_Wrap_Struct(klass, data_type, sval)
VALUE pending_interrupt_mask_stack
VALUE rb_ary_shift(VALUE ary)
static VALUE threadptr_local_aref(rb_thread_t *th, ID id)
#define TypedData_Get_Struct(obj, type, data_type, sval)
VALUE rb_mod_ancestors(VALUE mod)
static VALUE mutex_initialize(VALUE self)
static void rb_mutex_abandon_keeping_mutexes(rb_thread_t *th)
void rb_threadptr_signal_raise(rb_thread_t *th, int sig)
struct rb_thread_struct volatile * th
static struct timeval double2timeval(double d)
ID rb_frame_this_func(void)
SOCKET rb_w32_get_osfhandle(int)
VALUE rb_thread_stop(void)
#define TH_JUMP_TAG(th, st)
static VALUE mutex_alloc(VALUE klass)
VALUE rb_mutex_synchronize(VALUE mutex, VALUE(*func)(VALUE arg), VALUE arg)
static const rb_data_type_t mutex_data_type
VALUE rb_ary_push(VALUE ary, VALUE item)
void rb_thread_wait_for(struct timeval time)
st_table * living_threads
void rb_signal_exec(rb_thread_t *th, int sig)
static int handle_interrupt_arg_check_i(VALUE key, VALUE val)
struct st_table * rb_hash_tbl_raw(VALUE hash)
VALUE rb_ary_tmp_new(long capa)
static VALUE rb_thread_safe_level(VALUE thread)
static VALUE rb_thread_aset(VALUE self, VALUE id, VALUE val)
VALUE rb_thread_current(void)
void rb_threadptr_pending_interrupt_enque(rb_thread_t *th, VALUE v)
#define OBJ_ID_EQL(obj_id, other)
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rb_thread_alloc(VALUE klass)
static VALUE rb_mutex_sleep_forever(VALUE time)
static VALUE rb_thread_abort_exc(VALUE thread)
static void blocking_region_end(rb_thread_t *th, struct rb_blocking_region_buffer *region)
VALUE rb_ivar_get(VALUE, ID)
void rb_nativethread_lock_initialize(rb_nativethread_lock_t *lock)
void * rb_thread_call_without_gvl(void *(*func)(void *data), void *data1, rb_unblock_function_t *ubf, void *data2)
VALUE rb_ary_clear(VALUE ary)
pthread_mutex_t rb_nativethread_lock_t
static void clear_coverage(void)
int rb_thread_alone(void)
VALUE rb_convert_type(VALUE, int, const char *, const char *)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
static void sleep_forever(rb_thread_t *th, int nodeadlock, int spurious_check)
VALUE rb_thread_local_aref(VALUE thread, ID id)
rb_nativethread_lock_t lock
VALUE rb_catch_protect(VALUE t, rb_block_call_func *func, VALUE data, int *stateptr)
#define RUBY_VM_SET_TRAP_INTERRUPT(th)
static size_t thgroup_memsize(const void *ptr)
static void rb_mutex_abandon_locking_mutex(rb_thread_t *th)
static VALUE sym_immediate
static int blocking_region_begin(rb_thread_t *th, struct rb_blocking_region_buffer *region, rb_unblock_function_t *ubf, void *arg, int fail_if_interrupted)
void rb_gc_mark(VALUE ptr)
static void thread_shield_mark(void *ptr)
static size_t mutex_memsize(const void *ptr)
static volatile int system_working
static VALUE thread_join(rb_thread_t *target_th, double delay)
static VALUE remove_from_join_list(VALUE arg)
union select_args::@154 as
VALUE rb_thread_kill(VALUE thread)
VALUE rb_mutex_locked_p(VALUE self)
static int rb_threadptr_dead(rb_thread_t *th)
ID rb_check_id(volatile VALUE *namep)
Returns ID for the given name if it is interned already, or 0.
static VALUE rb_thread_alive_p(VALUE thread)
VALUE rb_exec_recursive_paired(VALUE(*func)(VALUE, VALUE, int), VALUE obj, VALUE paired_obj, VALUE arg)
#define rb_fd_rcopy(d, s)
void rb_thread_start_timer_thread(void)
struct rb_thread_struct::@169 machine
static rb_fdset_t * init_set_fd(int fd, rb_fdset_t *fds)
const char * rb_obj_classname(VALUE)
static VALUE rb_thread_stop_p(VALUE thread)
static void thread_cleanup_func(void *th_ptr, int atfork)
static double timeofday(void)
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
int ruby_native_thread_p(void)
static VALUE rb_thread_s_abort_exc_set(VALUE self, VALUE val)
static rb_atomic_t threadptr_get_interrupts(rb_thread_t *th)
#define rb_fd_isset(n, f)
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
VALUE(* first_func)(ANYARGS)
void rb_hash_foreach(VALUE hash, int(*func)(ANYARGS), VALUE farg)
VALUE rb_thread_wakeup(VALUE thread)
static VALUE rb_thread_s_main(VALUE klass)
void rb_exc_raise(VALUE mesg)
static void rb_thread_wait_fd_rw(int fd, int read)
static VALUE sym_on_blocking
static void rb_thread_schedule_limits(unsigned long limits_us)
#define RB_TYPE_P(obj, type)
void rb_reset_random_seed(void)
int rb_thread_fd_writable(int fd)
static void rb_threadptr_interrupt_common(rb_thread_t *th, int trap)
static VALUE thgroup_s_alloc(VALUE klass)
#define RUBY_VM_INTERRUPTED_ANY(th)
int st_lookup(st_table *, st_data_t, st_data_t *)
static int thread_list_i(st_data_t key, st_data_t val, void *data)
#define MEMZERO(p, type, n)
static VALUE coverage(VALUE fname, int n)
#define closed_stream_error
VALUE rb_vm_thread_backtrace(int argc, VALUE *argv, VALUE thval)
static const char * thread_status_name(rb_thread_t *th)
#define RUBY_THREAD_PRIORITY_MAX
static VALUE rb_thread_priority_set(VALUE thread, VALUE prio)
static int do_select(int n, rb_fdset_t *read, rb_fdset_t *write, rb_fdset_t *except, struct timeval *timeout)
static void sleep_for_polling(rb_thread_t *th)
int rb_block_given_p(void)
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
static const rb_data_type_t thread_shield_data_type
RUBY_EXTERN VALUE rb_cObject
static VALUE rb_thread_inspect(VALUE thread)
static VALUE rb_threadptr_raise(rb_thread_t *, int, VALUE *)
VALUE rb_mutex_trylock(VALUE self)
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
void * blocking_region_buffer
static VALUE exec_recursive(VALUE(*func)(VALUE, VALUE, int), VALUE obj, VALUE pairid, VALUE arg, int outer)
static VALUE thread_create_core(VALUE thval, VALUE args, VALUE(*fn)(ANYARGS))
static int rb_threadptr_pending_interrupt_include_p(rb_thread_t *th, VALUE err)
void rb_thread_stop_timer_thread(int close_anyway)
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
static void rb_threadptr_ready(rb_thread_t *th)
int st_delete(st_table *, st_data_t *, st_data_t *)
void rb_define_const(VALUE, const char *, VALUE)
void rb_thread_atfork_before_exec(void)
static int rb_threadptr_pending_interrupt_empty_p(rb_thread_t *th)
void rb_threadptr_check_signal(rb_thread_t *mth)
VALUE rb_class_inherited_p(VALUE, VALUE)
int thread_abort_on_exception
static VALUE rb_thread_variable_get(VALUE thread, VALUE key)
void ruby_thread_init_stack(rb_thread_t *th)
VALUE rb_proc_location(VALUE self)
static VALUE rb_thread_exit(void)
RUBY_EXTERN VALUE rb_cModule
void rb_thread_check_ints(void)
void rb_thread_fd_close(int fd)
if((ID)(DISPID) nameid!=nameid)
VALUE rb_thread_shield_new(void)
void ruby_stop(int ex)
Calls ruby_cleanup() and exits the process.
VALUE rb_obj_alloc(VALUE)
VALUE rb_thread_shield_release(VALUE self)
void rb_thread_atfork(void)
#define GVL_UNLOCK_BEGIN()
static const rb_data_type_t thgroup_data_type
VALUE rb_thread_create(VALUE(*fn)(ANYARGS), void *arg)
void rb_throw_obj(VALUE tag, VALUE value)
static VALUE thread_s_current(VALUE klass)
static void sleep_wait_for_interrupt(rb_thread_t *th, double sleepsec, int spurious_check)
#define ATOMIC_CAS(var, oldval, newval)
void rb_thread_polling(void)
#define GetMutexPtr(obj, tobj)
static VALUE rb_thread_backtrace_locations_m(int argc, VALUE *argv, VALUE thval)
int rb_thread_select(int max, fd_set *read, fd_set *write, fd_set *except, struct timeval *timeout)
#define RARRAY_CONST_PTR(a)
void * rb_thread_call_without_gvl2(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
struct rb_unblock_callback oldubf
SSL_METHOD *(* func)(void)
#define rb_thread_set_current(th)
VALUE rb_uninterruptible(VALUE(*b_proc)(ANYARGS), VALUE data)
static int thgroup_list_i(st_data_t key, st_data_t val, st_data_t data)
VALUE special_exceptions[ruby_special_error_count]
struct rb_mutex_struct * keeping_mutexes
VALUE rb_thread_shield_wait(VALUE self)
VALUE rb_sprintf(const char *format,...)
int rb_get_next_signal(void)
VALUE rb_hash_delete(VALUE hash, VALUE key)
int rb_thread_fd_select(int max, rb_fdset_t *read, rb_fdset_t *write, rb_fdset_t *except, struct timeval *timeout)
#define rb_fd_copy(d, s, n)
static int set_unblock_function(rb_thread_t *th, rb_unblock_function_t *func, void *arg, struct rb_unblock_callback *old, int fail_if_interrupted)
static int thread_fd_close_i(st_data_t key, st_data_t val, st_data_t data)
int rb_threadptr_reset_raised(rb_thread_t *th)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
unsigned char buf[MIME_BUF_SIZE]
#define RUBY_VM_SET_TIMER_INTERRUPT(th)
void rb_add_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data)
static VALUE thread_initialize(VALUE thread, VALUE args)
static void rb_check_deadlock(rb_vm_t *vm)
static VALUE rb_mutex_synchronize_m(VALUE self, VALUE args)
void rb_thread_sleep_forever(void)
static VALUE thread_shield_alloc(VALUE klass)
#define THREAD_SHIELD_WAITING_MASK
#define SAVE_ROOT_JMPBUF(th, stmt)
RUBY_EXTERN VALUE rb_cThread
int rb_wait_for_single_fd(int fd, int events, struct timeval *tv)
static int keys_i(VALUE key, VALUE value, VALUE ary)
#define UNINITIALIZED_VAR(x)
const char * rb_class2name(VALUE)
struct rb_thread_struct * main_thread
static int clear_coverage_i(st_data_t key, st_data_t val, st_data_t dummy)
static VALUE rb_thread_s_pending_interrupt_p(int argc, VALUE *argv, VALUE self)
void rb_threadptr_pending_interrupt_clear(rb_thread_t *th)
void rb_gc_set_stack_end(VALUE **stack_end_p)
static void rb_thread_shield_waiting_dec(VALUE b)
int clock_gettime(clockid_t, struct timespec *)
void rb_thread_schedule(void)
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
VALUE rb_exec_recursive_paired_outer(VALUE(*func)(VALUE, VALUE, int), VALUE obj, VALUE paired_obj, VALUE arg)
static VALUE thread_value(VALUE self)
static void reset_unblock_function(rb_thread_t *th, const struct rb_unblock_callback *old)
rb_atomic_t interrupt_flag
rb_nativethread_cond_t interrupt_cond
static void timer_thread_function(void *)
void rb_thread_wait_fd(int fd)
st_table * st_init_numtable(void)
VALUE rb_blocking_function_t(void *)
void rb_sys_fail(const char *mesg)
VALUE rb_thread_main(void)
void rb_threadptr_restore_recursive_data(rb_thread_t *th, VALUE old)
static VALUE rb_thread_backtrace_m(int argc, VALUE *argv, VALUE thval)
static VALUE rb_thread_pending_interrupt_p(int argc, VALUE *argv, VALUE target_thread)
enum rb_thread_status status
static void st_delete_wrap(st_table *table, st_data_t key)
void rb_thread_sleep(int sec)
static VALUE thread_s_pass(VALUE klass)
VALUE rb_threadptr_reset_recursive_data(rb_thread_t *th)
static VALUE thread_join_m(int argc, VALUE *argv, VALUE self)
#define thread_start_func_2(th, st, rst)
void rb_thread_sleep_deadly(void)
enum rb_thread_status prev_status
static VALUE mutex_sleep(int argc, VALUE *argv, VALUE self)
#define RARRAY_ASET(a, i, v)
void rb_thread_recycle_stack_release(VALUE *)
void rb_thread_terminate_all(void)
#define THREAD_SHIELD_WAITING_SHIFT
static void rb_threadptr_to_kill(rb_thread_t *th)
void rb_reset_coverages(void)
VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
void rb_thread_execute_interrupts(VALUE thval)
static VALUE thgroup_list(VALUE group)
#define RARRAY_AREF(a, i)
#define RUBY_INTERNAL_EVENT_SWITCH
unsigned long interrupt_mask
VALUE rb_block_proc(void)
#define RUBY_THREAD_PRIORITY_MIN
VALUE rb_thread_group(VALUE thread)
struct rb_unblock_callback unblock
unsigned long rb_event_flag_t
#define BLOCKING_REGION(exec, ubf, ubfarg, fail_if_interrupted)
VALUE rb_hash_aref(VALUE hash, VALUE key)
#define rb_fd_select(n, rfds, wfds, efds, timeout)
#define RB_GC_SAVE_MACHINE_CONTEXT(th)
void rb_thread_reset_timer_thread(void)
rb_nativethread_id_t thread_id
static VALUE rb_thread_status(VALUE thread)
rb_nativethread_lock_t thread_destruct_lock
int rb_signal_buff_size(void)
static void rb_thread_shield_waiting_inc(VALUE b)
RUBY_EXTERN char * strerror(int)
struct rb_encoding_entry * list
VALUE rb_thread_shield_destroy(VALUE self)
static VALUE rb_threadptr_pending_interrupt_deque(rb_thread_t *th, enum handle_interrupt_timing timing)
static void recursive_push(VALUE list, VALUE obj, VALUE paired_obj)
static VALUE thread_start(VALUE klass, VALUE args)
static VALUE threadptr_local_aset(rb_thread_t *th, ID id, VALUE val)
#define TypedData_Make_Struct(klass, type, data_type, sval)
void rb_nativethread_lock_lock(rb_nativethread_lock_t *lock)
static VALUE rb_mutex_wait_for(VALUE time)
VALUE rb_ary_dup(VALUE ary)
const struct st_hash_type * type
#define GetThreadPtr(obj, ptr)
static unsigned int hash(const char *str, unsigned int len)
static VALUE thread_raise_m(int argc, VALUE *argv, VALUE self)
#define rb_fd_resize(n, f)
#define rb_thread_shield_waiting(b)
#define EXEC_EVENT_HOOK(th_, flag_, self_, id_, klass_, data_)
static void sleep_timeval(rb_thread_t *th, struct timeval time, int spurious_check)
#define RUBY_EVENT_COVERAGE
RUBY_EXTERN VALUE rb_eIOError
VALUE rb_thread_io_blocking_region(rb_blocking_function_t *func, void *data1, int fd)
void rb_threadptr_trap_interrupt(rb_thread_t *th)
static VALUE exec_recursive_i(RB_BLOCK_CALL_FUNC_ARGLIST(tag, data))
#define StringValuePtr(v)
#define rb_fd_init_copy(d, s)
static int terminate_i(st_data_t key, st_data_t val, rb_thread_t *main_thread)
struct rb_thread_list_struct * next
#define RUBY_VM_INTERRUPTED(th)
const struct st_hash_type st_hashtype_num
static VALUE rb_thread_s_abort_exc(void)
VALUE rb_thread_local_aset(VALUE thread, ID id, VALUE val)
static void recursive_pop(VALUE list, VALUE obj, VALUE paired_obj)
void rb_vm_gvl_destroy(rb_vm_t *vm)
int rb_threadptr_pending_interrupt_active_p(rb_thread_t *th)
static VALUE rb_thread_aref(VALUE thread, VALUE key)
void st_clear(st_table *)
static void mutex_locked(rb_thread_t *th, VALUE self)
static VALUE ident_hash_new(void)
#define RUBY_TYPED_DEFAULT_FREE
NOINLINE(static int thread_start_func_2(rb_thread_t *th, VALUE *stack_start, VALUE *register_stack_start))
static VALUE rb_thread_keys(VALUE self)
#define GetThreadShieldPtr(obj)
rb_nativethread_lock_t interrupt_lock
VALUE pending_interrupt_queue
void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
static void getclockofday(struct timeval *tp)
static VALUE select_single_cleanup(VALUE ptr)
VALUE rb_vm_make_jump_tag_but_local_jump(int state, VALUE val)
static VALUE select_single(VALUE ptr)
struct rb_mutex_struct rb_mutex_t
VALUE rb_get_coverages(void)
VALUE rb_vm_thread_backtrace_locations(int argc, VALUE *argv, VALUE thval)
static int thread_keys_i(ID key, VALUE value, VALUE ary)
static void * call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2, int fail_if_interrupted)
#define GET_THROWOBJ_STATE(obj)
static VALUE rb_thread_variable_set(VALUE thread, VALUE id, VALUE val)
void rb_threadptr_interrupt(rb_thread_t *th)
VALUE rb_thread_wakeup_alive(VALUE thread)
VALUE rb_thread_blocking_region(rb_blocking_function_t *func, void *data1, rb_unblock_function_t *ubf, void *data2)
void rb_obj_call_init(VALUE obj, int argc, VALUE *argv)
static void mutex_free(void *ptr)
VALUE rb_mutex_unlock(VALUE self)
static rb_thread_t * GET_THREAD(void)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
int st_foreach(st_table *, int(*)(ANYARGS), st_data_t)
#define GET_THROWOBJ_VAL(obj)
void rb_set_coverages(VALUE coverages)
int select(int num_fds, fd_set *in_fds, fd_set *out_fds, fd_set *ex_fds, struct timeval *timeout)
static VALUE rb_thread_key_p(VALUE self, VALUE key)
#define RUBY_VM_CHECK_INTS_BLOCKING(th)
VALUE rb_obj_is_mutex(VALUE obj)
static VALUE rb_thread_s_kill(VALUE obj, VALUE th)
static VALUE recursive_check(VALUE list, VALUE obj_id, VALUE paired_obj_id)
VALUE rb_thread_run(VALUE thread)
void rb_threadptr_signal_exit(rb_thread_t *th)
static void lock_interrupt(void *ptr)
static void rb_thread_atfork_internal(int(*atfork)(st_data_t, st_data_t, st_data_t))
#define TYPEOF_TIMEVAL_TV_SEC
int rb_thread_interrupted(VALUE thval)
struct timeval rb_time_timeval(VALUE)
VALUE rb_mutex_lock(VALUE self)
int rb_threadptr_set_raised(rb_thread_t *th)
static enum handle_interrupt_timing rb_threadptr_pending_interrupt_check_mask(rb_thread_t *th, VALUE err)
static int terminate_atfork_i(st_data_t key, st_data_t val, st_data_t current_th)
void rb_nativethread_lock_destroy(rb_nativethread_lock_t *lock)