54 #undef _FORTIFY_SOURCE
55 #undef __USE_FORTIFY_LEVEL
56 #define __USE_FORTIFY_LEVEL 0
66 #ifndef USE_NATIVE_THREAD_PRIORITY
67 #define USE_NATIVE_THREAD_PRIORITY 0
68 #define RUBY_THREAD_PRIORITY_MAX 3
69 #define RUBY_THREAD_PRIORITY_MIN -3
73 #define THREAD_DEBUG 0
76 #define TIMET_MAX (~(time_t)0 <= 0 ? (time_t)((~(unsigned_time_t)0) >> 1) : (time_t)(~(unsigned_time_t)0))
77 #define TIMET_MIN (~(time_t)0 <= 0 ? (time_t)(((unsigned_time_t)1) << (sizeof(time_t) * CHAR_BIT - 1)) : (time_t)0)
94 #define eKillSignal INT2FIX(0)
95 #define eTerminateSignal INT2FIX(1)
98 #define closed_stream_error GET_VM()->special_exceptions[ruby_error_closed_stream]
108 #define THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
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) \
131 FLUSH_REGISTER_WINDOWS; \
132 RB_GC_SAVE_MACHINE_REGISTER_STACK(th); \
133 setjmp((th)->machine_regs); \
134 SET_MACHINE_STACK_END(&(th)->machine_stack_end); \
137 #define GVL_UNLOCK_BEGIN() do { \
138 rb_thread_t *_th_stored = GET_THREAD(); \
139 RB_GC_SAVE_MACHINE_CONTEXT(_th_stored); \
140 gvl_release(_th_stored->vm);
142 #define GVL_UNLOCK_END() \
143 gvl_acquire(_th_stored->vm, _th_stored); \
144 rb_thread_set_current(_th_stored); \
148 #define only_if_constant(expr, notconst) (__builtin_constant_p(expr) ? (expr) : (notconst))
150 #define only_if_constant(expr, notconst) notconst
152 #define BLOCKING_REGION(exec, ubf, ubfarg, fail_if_interrupted) do { \
153 rb_thread_t *__th = GET_THREAD(); \
154 struct rb_blocking_region_buffer __region; \
155 if (blocking_region_begin(__th, &__region, (ubf), (ubfarg), fail_if_interrupted) || \
157 !only_if_constant(fail_if_interrupted, TRUE)) { \
159 blocking_region_end(__th, &__region); \
164 #ifdef HAVE_VA_ARGS_MACRO
165 void rb_thread_debug(
const char *file,
int line,
const char *fmt, ...);
166 #define thread_debug(fmt, ...) rb_thread_debug(__FILE__, __LINE__, fmt, ##__VA_ARGS__)
167 #define POSITION_FORMAT "%s:%d:"
168 #define POSITION_ARGS ,file, line
170 void rb_thread_debug(
const char *fmt, ...);
171 #define thread_debug rb_thread_debug
172 #define POSITION_FORMAT
173 #define POSITION_ARGS
176 # if THREAD_DEBUG < 0
177 static int rb_thread_debug_enabled;
188 rb_thread_s_debug(
void)
190 return INT2NUM(rb_thread_debug_enabled);
204 rb_thread_debug_enabled =
RTEST(val) ?
NUM2INT(val) : 0;
208 # define rb_thread_debug_enabled THREAD_DEBUG
211 #define thread_debug if(0)printf
215 #define thread_start_func_2(th, st, rst) thread_start_func_2(th, st)
218 VALUE *register_stack_start));
224 #define DEBUG_OUT() \
225 WaitForSingleObject(&debug_mutex, INFINITE); \
226 printf(POSITION_FORMAT"%p - %s" POSITION_ARGS, GetCurrentThreadId(), buf); \
228 ReleaseMutex(&debug_mutex);
230 #elif defined(HAVE_PTHREAD_H)
233 #define DEBUG_OUT() \
234 pthread_mutex_lock(&debug_mutex); \
235 printf(POSITION_FORMAT"%#"PRIxVALUE" - %s" POSITION_ARGS, (VALUE)pthread_self(), buf); \
237 pthread_mutex_unlock(&debug_mutex);
240 #error "unsupported thread type"
244 static int debug_mutex_initialized = 1;
249 #ifdef HAVE_VA_ARGS_MACRO
250 const char *file,
int line,
252 const char *fmt, ...)
257 if (!rb_thread_debug_enabled)
return;
259 if (debug_mutex_initialized == 1) {
260 debug_mutex_initialized = 0;
261 native_mutex_initialize(&debug_mutex);
283 native_mutex_unlock(lock);
289 native_mutex_destroy(lock);
297 if (fail_if_interrupted) {
365 if (th != main_thread) {
371 thread_debug(
"terminate_i: main thread (%p)\n", (
void *)th);
402 if (err)
rb_bug(
"invalid keeping_mutexes: %s", err);
413 rb_bug(
"rb_thread_terminate_all: called by child thread (%p, %p)",
421 thread_debug(
"rb_thread_terminate_all (main thread: %p)\n", (
void *)th);
447 th->machine_register_stack_start = th->machine_register_stack_end = 0;
468 native_thread_destroy(th);
476 native_thread_init_stack(th);
488 # ifdef USE_SIGALTSTACK
491 rb_register_sigaltstack(th);
495 rb_bug(
"thread_start_func_2 must not used for main thread");
497 ruby_thread_set_native(th);
501 th->machine_register_stack_start = register_stack_start;
505 gvl_acquire(th->
vm, th);
507 thread_debug(
"thread start (get lock): %p\n", (
void *)th);
561 rb_bug(
"thread_start_func_2: locking_mutex must not be set (%p:%"PRIxVALUE")",
581 join_list = join_list->
next;
610 "can't start a new thread (frozen ThreadGroup)");
632 err = native_thread_create(th);
729 #define DELAY_INFTY 1E30
747 if ((*p)->th == th) {
790 if (th == target_th) {
793 if (
GET_VM()->main_thread == target_th) {
833 return target_th->
self;
929 time.
tv_usec = (int)((d - (
int)d) * 1e6);
945 while (th->
status == status) {
964 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
967 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
1000 native_sleep(th, &tv);
1013 if (!spurious_check)
1016 th->
status = prev_status;
1036 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
1039 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
1130 gvl_yield(th->
vm, th);
1158 thread_debug(
"enter blocking region (%p)\n", (
void *)th);
1160 gvl_release(th->
vm);
1171 gvl_acquire(th->
vm, th);
1173 thread_debug(
"leave blocking region (%p)\n", (
void *)th);
1174 remove_signal_thread_list(th);
1194 int saved_errno =
errno;
1199 errno = saved_errno;
1209 int saved_errno = 0;
1219 saved_errno =
errno;
1220 }, ubf, data2, fail_if_interrupted);
1222 if (!fail_if_interrupted) {
1226 errno = saved_errno;
1335 int saved_errno = 0;
1344 saved_errno =
errno;
1345 }, ubf_select, th,
FALSE);
1358 errno = saved_errno;
1368 void *(*f)(
void*) = (
void *(*)(
void*))
func;
1414 fprintf(stderr,
"[BUG] rb_thread_call_with_gvl() is called by non-ruby thread\n");
1422 rb_bug(
"rb_thread_call_with_gvl: called by a thread which has GVL.");
1519 for (i=0; i<mask_stack_len; i++) {
1520 mask = mask_stack[mask_stack_len-(i+1)];
1522 for (j=0; j<ancestors_len; j++) {
1523 VALUE klass = ancestors_ptr[j];
1577 switch (mask_timing) {
1899 int timer_interrupt;
1900 int pending_interrupt;
1901 int finalizer_interrupt;
1907 }
while (old != interrupt);
1950 if (finalizer_interrupt) {
1954 if (timer_interrupt) {
1955 unsigned long limits_us = TIME_QUANTUM_USEC;
2026 #if defined(POSIX_SIGNAL) && defined(SIGSEGV) && defined(HAVE_SIGALTSTACK)
2027 #define USE_SIGALTSTACK
2034 #ifdef USE_SIGALTSTACK
2113 if (th == target_th) {
2295 "stopping only thread\n\tnote: use sleep to stop forever");
2656 str =
rb_sprintf(
"#<%s:%p %s>", cname, (
void *)thread, status);
3001 if (!
RHASH(locals)->ntbl)
3070 #if USE_NATIVE_THREAD_PRIORITY
3072 native_thread_apply_priority(th);
3088 #if defined(NFDBITS) && defined(HAVE_RB_FD_INIT)
3125 fds->fdset =
ALLOC(fd_set);
3126 FD_ZERO(fds->fdset);
3132 size_t size = howmany(
rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
3134 if (size <
sizeof(fd_set))
3135 size =
sizeof(fd_set);
3136 dst->maxfd = src->maxfd;
3138 memcpy(dst->fdset, src->fdset, size);
3144 if (fds->fdset)
xfree(fds->fdset);
3153 MEMZERO(fds->fdset, fd_mask, howmany(fds->maxfd, NFDBITS));
3159 size_t m = howmany(n + 1, NFDBITS) *
sizeof(fd_mask);
3160 size_t o = howmany(fds->maxfd, NFDBITS) *
sizeof(fd_mask);
3162 if (m <
sizeof(fd_set)) m =
sizeof(fd_set);
3163 if (o <
sizeof(fd_set)) o =
sizeof(fd_set);
3166 fds->fdset =
xrealloc(fds->fdset, m);
3167 memset((
char *)fds->fdset + o, 0, m - o);
3169 if (n >= fds->maxfd) fds->maxfd = n + 1;
3182 if (n >= fds->maxfd)
return;
3189 if (n >= fds->maxfd)
return 0;
3190 return FD_ISSET(n, fds->fdset) != 0;
3196 size_t size = howmany(max, NFDBITS) *
sizeof(fd_mask);
3198 if (size <
sizeof(fd_set)) size =
sizeof(fd_set);
3200 dst->fdset =
xrealloc(dst->fdset, size);
3201 memcpy(dst->fdset, src, size);
3207 size_t size = howmany(
rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
3209 if (size >
sizeof(fd_set)) {
3212 memcpy(dst,
rb_fd_ptr(src),
sizeof(fd_set));
3218 size_t size = howmany(
rb_fd_max(src), NFDBITS) *
sizeof(fd_mask);
3220 if (size <
sizeof(fd_set))
3221 size =
sizeof(fd_set);
3222 dst->maxfd = src->maxfd;
3223 dst->fdset =
xrealloc(dst->fdset, size);
3224 memcpy(dst->fdset, src->fdset, size);
3227 #ifdef __native_client__
3228 int select(
int nfds, fd_set *readfds, fd_set *writefds,
3229 fd_set *exceptfds,
struct timeval *timeout);
3248 return select(n, r, w, e, timeout);
3256 #define FD_ZERO(f) rb_fd_zero(f)
3257 #define FD_SET(i, f) rb_fd_set((i), (f))
3258 #define FD_CLR(i, f) rb_fd_clr((i), (f))
3259 #define FD_ISSET(i, f) rb_fd_isset((i), (f))
3261 #elif defined(_WIN32)
3266 set->capa = FD_SETSIZE;
3267 set->fdset =
ALLOC(fd_set);
3268 FD_ZERO(set->fdset);
3285 if (max > FD_SETSIZE || (UINT)max > dst->fd_count) {
3289 memcpy(dst->fd_array, src->fdset->fd_array, max);
3290 dst->fd_count =
max;
3307 for (i = 0; i <
set->fdset->fd_count; i++) {
3308 if (set->fdset->fd_array[i] == s) {
3312 if (set->fdset->fd_count >= (
unsigned)set->capa) {
3313 set->capa = (
set->fdset->fd_count / FD_SETSIZE + 1) * FD_SETSIZE;
3314 set->fdset =
xrealloc(set->fdset,
sizeof(
unsigned int) +
sizeof(SOCKET) * set->capa);
3316 set->fdset->fd_array[
set->fdset->fd_count++] = s;
3324 #define FD_ZERO(f) rb_fd_zero(f)
3325 #define FD_SET(i, f) rb_fd_set((i), (f))
3326 #define FD_CLR(i, f) rb_fd_clr((i), (f))
3327 #define FD_ISSET(i, f) rb_fd_isset((i), (f))
3330 #define rb_fd_rcopy(d, s) (*(d) = *(s))
3348 limit += (double)timeout->
tv_sec+(
double)timeout->
tv_usec*1e-6;
3349 wait_rest = *timeout;
3350 timeout = &wait_rest;
3364 result = native_fd_select(n, read, write, except, timeout, th);
3366 }, ubf_select, th,
FALSE);
3388 wait_rest.
tv_sec = (time_t)d;
3389 wait_rest.
tv_usec = (int)((d-(
double)wait_rest.
tv_sec)*1e6);
3416 thread_debug(
"rb_thread_wait_fd_rw(%d, %s)\n", fd, read ?
"read" :
"write");
3427 thread_debug(
"rb_thread_wait_fd_rw(%d, %s): done\n", fd, read ?
"read" :
"write");
3491 if (!read && !write && !except) {
3509 return do_select(max, read, write, except, timeout);
3517 #if defined(HAVE_POLL) && defined(__linux__)
3524 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
3525 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
3526 #define POLLEX_SET (POLLPRI)
3531 ppoll(
struct pollfd *fds, nfds_t nfds,
3532 const struct timespec *ts,
const sigset_t *sigmask)
3543 tmp2 = ts->
tv_nsec / (1000 * 1000);
3547 timeout_ms = tmp + tmp2;
3553 return poll(fds, nfds, timeout_ms);
3579 fds.events = (short)events;
3584 result = ppoll(&fds, 1, timeout,
NULL);
3585 if (result < 0) lerrno =
errno;
3586 }, ubf_select, th,
FALSE);
3612 if (fds.revents & POLLNVAL) {
3622 if (fds.revents & POLLIN_SET)
3624 if (fds.revents & POLLOUT_SET)
3626 if (fds.revents & POLLEX_SET)
3712 #ifdef USE_CONSERVATIVE_STACK_END
3717 *stack_end_p = &stack_end;
3757 if (vm->prove_profile.enable) {
3760 if (vm->during_gc) {
3770 if (timer_thread_id && native_stop_timer_thread(close_anyway)) {
3771 native_reset_timer_thread();
3778 native_reset_timer_thread();
3785 rb_thread_create_timer_thread();
3806 if (
RTEST(coverages)) {
3881 return ptr ?
sizeof(
struct thgroup) : 0;
4066 "can't move from the enclosed thread group");
4099 #define GetMutexPtr(obj, tobj) \
4100 TypedData_Get_Struct((obj), rb_mutex_t, &mutex_data_type, (tobj))
4104 #define mutex_mark NULL
4114 if (err)
rb_bug(
"%s", err);
4116 native_mutex_destroy(&mutex->
lock);
4117 native_cond_destroy(&mutex->
cond);
4151 native_mutex_initialize(&mutex->
lock);
4152 native_cond_initialize(&mutex->
cond, RB_CONDATTR_CLOCK_MONOTONIC);
4214 native_mutex_lock(&mutex->
lock);
4215 if (mutex->
th == 0) {
4221 native_mutex_unlock(&mutex->
lock);
4229 int interrupted = 0;
4252 timeout_rel.
tv_nsec = timeout_ms * 1000 * 1000;
4253 timeout = native_cond_timeout(&mutex->
cond, timeout_rel);
4254 err = native_cond_timedwait(&mutex->
cond, &mutex->
lock, &timeout);
4257 native_cond_wait(&mutex->
cond, &mutex->
lock);
4270 native_mutex_lock(&mutex->
lock);
4272 native_cond_broadcast(&mutex->
cond);
4273 native_mutex_unlock(&mutex->
lock);
4307 while (mutex->
th != th) {
4310 volatile int timeout_ms = 0;
4317 native_mutex_lock(&mutex->
lock);
4331 interrupted =
lock_func(th, mutex, (
int)timeout_ms);
4332 native_mutex_unlock(&mutex->
lock);
4335 if (patrol_thread == th)
4336 patrol_thread =
NULL;
4341 if (mutex->
th && interrupted == 2) {
4345 th->
status = prev_status;
4375 if (mutex->
th == th)
4386 native_mutex_lock(&mutex->
lock);
4388 if (mutex->
th == 0) {
4389 err =
"Attempt to unlock a mutex which is not locked";
4391 else if (mutex->
th != th) {
4392 err =
"Attempt to unlock a mutex which is locked by another thread";
4397 native_cond_signal(&mutex->
cond);
4400 native_mutex_unlock(&mutex->
lock);
4404 while (*th_mutex != mutex) {
4473 if (!
NIL_P(timeout)) {
4478 if (
NIL_P(timeout)) {
4484 end = time(0) - beg;
4568 #define GetThreadShieldPtr(obj) ((VALUE)rb_check_typeddata((obj), &thread_shield_data_type))
4569 #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)
4570 #define THREAD_SHIELD_WAITING_SHIFT (FL_USHIFT)
4571 #define rb_thread_shield_waiting(b) (int)((RBASIC(b)->flags&THREAD_SHIELD_WAITING_MASK)>>THREAD_SHIELD_WAITING_SHIFT)
4599 return thread_shield;
4616 if (!mutex)
return Qfalse;
4691 #if SIZEOF_LONG == SIZEOF_VOIDP
4692 #define OBJ_ID_EQL(obj_id, other) ((obj_id) == (other))
4693 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
4694 #define OBJ_ID_EQL(obj_id, other) (RB_TYPE_P((obj_id), T_BIGNUM) ? \
4695 rb_big_eql((obj_id), (other)) : ((obj_id) == (other)))
4701 if (paired_obj_id) {
4736 VALUE other_paired_obj = pair_list;
4759 if (pair_list ==
Qundef) {
4827 if (outer && !outermost) {
4839 if (result == p.list) {
4943 #define rb_intern(str) rb_intern_const(str)
4964 #if THREAD_DEBUG < 0
5030 recursive_key =
rb_intern(
"__recursive_key__");
5039 gvl_acquire(th->
vm, th);
5051 rb_thread_create_timer_thread();
5054 (void)native_mutex_trylock;
5079 native_mutex_lock(&mutex->
lock);
5083 native_mutex_unlock(&mutex->
lock);
5089 #ifdef DEBUG_DEADLOCK_CHECK
5102 native_mutex_lock(&mutex->
lock);
5104 native_mutex_unlock(&mutex->
lock);
5120 if (patrol_thread && patrol_thread !=
GET_THREAD())
return;
5127 argv[1] =
rb_str_new2(
"No live threads left. Deadlock?");
5128 #ifdef DEBUG_DEADLOCK_CHECK
5141 if (coverage &&
RBASIC(coverage)->klass == 0) {
5157 return GET_VM()->coverages;
5163 GET_VM()->coverages = coverages;