Ruby  2.1.10p492(2016-04-01revision54464)
thread_pthread.c
Go to the documentation of this file.
1 /* -*-c-*- */
2 /**********************************************************************
3 
4  thread_pthread.c -
5 
6  $Author: usa $
7 
8  Copyright (C) 2004-2007 Koichi Sasada
9 
10 **********************************************************************/
11 
12 #ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
13 
14 #include "gc.h"
15 
16 #ifdef HAVE_SYS_RESOURCE_H
17 #include <sys/resource.h>
18 #endif
19 #ifdef HAVE_THR_STKSEGMENT
20 #include <thread.h>
21 #endif
22 #if HAVE_FCNTL_H
23 #include <fcntl.h>
24 #elif HAVE_SYS_FCNTL_H
25 #include <sys/fcntl.h>
26 #endif
27 #ifdef HAVE_SYS_PRCTL_H
28 #include <sys/prctl.h>
29 #endif
30 #if defined(__native_client__) && defined(NACL_NEWLIB)
31 # include "nacl/select.h"
32 #endif
33 #if defined(HAVE_SYS_TIME_H)
34 #include <sys/time.h>
35 #endif
36 
37 static void native_mutex_lock(pthread_mutex_t *lock);
38 static void native_mutex_unlock(pthread_mutex_t *lock);
39 static int native_mutex_trylock(pthread_mutex_t *lock);
40 static void native_mutex_initialize(pthread_mutex_t *lock);
41 static void native_mutex_destroy(pthread_mutex_t *lock);
42 static void native_cond_signal(rb_nativethread_cond_t *cond);
43 static void native_cond_broadcast(rb_nativethread_cond_t *cond);
44 static void native_cond_wait(rb_nativethread_cond_t *cond, pthread_mutex_t *mutex);
45 static void native_cond_initialize(rb_nativethread_cond_t *cond, int flags);
46 static void native_cond_destroy(rb_nativethread_cond_t *cond);
47 static void rb_thread_wakeup_timer_thread_low(void);
48 static pthread_t timer_thread_id;
49 
50 #define RB_CONDATTR_CLOCK_MONOTONIC 1
51 
52 #if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(HAVE_CLOCKID_T) && \
53  defined(CLOCK_REALTIME) && defined(CLOCK_MONOTONIC) && \
54  defined(HAVE_CLOCK_GETTIME) && defined(HAVE_PTHREAD_CONDATTR_INIT)
55 #define USE_MONOTONIC_COND 1
56 #else
57 #define USE_MONOTONIC_COND 0
58 #endif
59 
60 #if defined(HAVE_POLL) && defined(HAVE_FCNTL) && defined(F_GETFL) && defined(F_SETFL) && defined(O_NONBLOCK) && !defined(__native_client__)
61 /* The timer thread sleeps while only one Ruby thread is running. */
62 # define USE_SLEEPY_TIMER_THREAD 1
63 #else
64 # define USE_SLEEPY_TIMER_THREAD 0
65 #endif
66 
67 static void
68 gvl_acquire_common(rb_vm_t *vm)
69 {
70  if (vm->gvl.acquired) {
71 
72  vm->gvl.waiting++;
73  if (vm->gvl.waiting == 1) {
74  /*
75  * Wake up timer thread iff timer thread is slept.
76  * When timer thread is polling mode, we don't want to
77  * make confusing timer thread interval time.
78  */
79  rb_thread_wakeup_timer_thread_low();
80  }
81 
82  while (vm->gvl.acquired) {
83  native_cond_wait(&vm->gvl.cond, &vm->gvl.lock);
84  }
85 
86  vm->gvl.waiting--;
87 
88  if (vm->gvl.need_yield) {
89  vm->gvl.need_yield = 0;
90  native_cond_signal(&vm->gvl.switch_cond);
91  }
92  }
93 
94  vm->gvl.acquired = 1;
95 }
96 
97 static void
98 gvl_acquire(rb_vm_t *vm, rb_thread_t *th)
99 {
100  native_mutex_lock(&vm->gvl.lock);
101  gvl_acquire_common(vm);
102  native_mutex_unlock(&vm->gvl.lock);
103 }
104 
105 static void
106 gvl_release_common(rb_vm_t *vm)
107 {
108  vm->gvl.acquired = 0;
109  if (vm->gvl.waiting > 0)
110  native_cond_signal(&vm->gvl.cond);
111 }
112 
113 static void
114 gvl_release(rb_vm_t *vm)
115 {
116  native_mutex_lock(&vm->gvl.lock);
117  gvl_release_common(vm);
118  native_mutex_unlock(&vm->gvl.lock);
119 }
120 
121 static void
122 gvl_yield(rb_vm_t *vm, rb_thread_t *th)
123 {
124  native_mutex_lock(&vm->gvl.lock);
125 
126  gvl_release_common(vm);
127 
128  /* An another thread is processing GVL yield. */
129  if (UNLIKELY(vm->gvl.wait_yield)) {
130  while (vm->gvl.wait_yield)
131  native_cond_wait(&vm->gvl.switch_wait_cond, &vm->gvl.lock);
132  goto acquire;
133  }
134 
135  if (vm->gvl.waiting > 0) {
136  /* Wait until another thread task take GVL. */
137  vm->gvl.need_yield = 1;
138  vm->gvl.wait_yield = 1;
139  while (vm->gvl.need_yield)
140  native_cond_wait(&vm->gvl.switch_cond, &vm->gvl.lock);
141  vm->gvl.wait_yield = 0;
142  }
143  else {
144  native_mutex_unlock(&vm->gvl.lock);
145  sched_yield();
146  native_mutex_lock(&vm->gvl.lock);
147  }
148 
149  native_cond_broadcast(&vm->gvl.switch_wait_cond);
150  acquire:
151  gvl_acquire_common(vm);
152  native_mutex_unlock(&vm->gvl.lock);
153 }
154 
155 static void
156 gvl_init(rb_vm_t *vm)
157 {
158  native_mutex_initialize(&vm->gvl.lock);
159  native_cond_initialize(&vm->gvl.cond, RB_CONDATTR_CLOCK_MONOTONIC);
160  native_cond_initialize(&vm->gvl.switch_cond, RB_CONDATTR_CLOCK_MONOTONIC);
161  native_cond_initialize(&vm->gvl.switch_wait_cond, RB_CONDATTR_CLOCK_MONOTONIC);
162  vm->gvl.acquired = 0;
163  vm->gvl.waiting = 0;
164  vm->gvl.need_yield = 0;
165  vm->gvl.wait_yield = 0;
166 }
167 
168 static void
169 gvl_destroy(rb_vm_t *vm)
170 {
171  native_cond_destroy(&vm->gvl.switch_wait_cond);
172  native_cond_destroy(&vm->gvl.switch_cond);
173  native_cond_destroy(&vm->gvl.cond);
174  native_mutex_destroy(&vm->gvl.lock);
175 }
176 
177 static void
178 gvl_atfork(rb_vm_t *vm)
179 {
180  gvl_init(vm);
181  gvl_acquire(vm, GET_THREAD());
182 }
183 
184 #define NATIVE_MUTEX_LOCK_DEBUG 0
185 
186 static void
187 mutex_debug(const char *msg, pthread_mutex_t *lock)
188 {
189  if (NATIVE_MUTEX_LOCK_DEBUG) {
190  int r;
191  static pthread_mutex_t dbglock = PTHREAD_MUTEX_INITIALIZER;
192 
193  if ((r = pthread_mutex_lock(&dbglock)) != 0) {exit(EXIT_FAILURE);}
194  fprintf(stdout, "%s: %p\n", msg, (void *)lock);
195  if ((r = pthread_mutex_unlock(&dbglock)) != 0) {exit(EXIT_FAILURE);}
196  }
197 }
198 
199 static void
200 native_mutex_lock(pthread_mutex_t *lock)
201 {
202  int r;
203  mutex_debug("lock", lock);
204  if ((r = pthread_mutex_lock(lock)) != 0) {
205  rb_bug_errno("pthread_mutex_lock", r);
206  }
207 }
208 
209 static void
210 native_mutex_unlock(pthread_mutex_t *lock)
211 {
212  int r;
213  mutex_debug("unlock", lock);
214  if ((r = pthread_mutex_unlock(lock)) != 0) {
215  rb_bug_errno("pthread_mutex_unlock", r);
216  }
217 }
218 
219 static inline int
220 native_mutex_trylock(pthread_mutex_t *lock)
221 {
222  int r;
223  mutex_debug("trylock", lock);
224  if ((r = pthread_mutex_trylock(lock)) != 0) {
225  if (r == EBUSY) {
226  return EBUSY;
227  }
228  else {
229  rb_bug_errno("pthread_mutex_trylock", r);
230  }
231  }
232  return 0;
233 }
234 
235 static void
236 native_mutex_initialize(pthread_mutex_t *lock)
237 {
238  int r = pthread_mutex_init(lock, 0);
239  mutex_debug("init", lock);
240  if (r != 0) {
241  rb_bug_errno("pthread_mutex_init", r);
242  }
243 }
244 
245 static void
246 native_mutex_destroy(pthread_mutex_t *lock)
247 {
248  int r = pthread_mutex_destroy(lock);
249  mutex_debug("destroy", lock);
250  if (r != 0) {
251  rb_bug_errno("pthread_mutex_destroy", r);
252  }
253 }
254 
255 static void
256 native_cond_initialize(rb_nativethread_cond_t *cond, int flags)
257 {
258 #ifdef HAVE_PTHREAD_COND_INIT
259  int r;
260 # if USE_MONOTONIC_COND
261  pthread_condattr_t attr;
262 
263  pthread_condattr_init(&attr);
264 
265  cond->clockid = CLOCK_REALTIME;
266  if (flags & RB_CONDATTR_CLOCK_MONOTONIC) {
267  r = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
268  if (r == 0) {
269  cond->clockid = CLOCK_MONOTONIC;
270  }
271  }
272 
273  r = pthread_cond_init(&cond->cond, &attr);
274  pthread_condattr_destroy(&attr);
275 # else
276  r = pthread_cond_init(&cond->cond, NULL);
277 # endif
278  if (r != 0) {
279  rb_bug_errno("pthread_cond_init", r);
280  }
281 
282  return;
283 #endif
284 }
285 
286 static void
287 native_cond_destroy(rb_nativethread_cond_t *cond)
288 {
289 #ifdef HAVE_PTHREAD_COND_INIT
290  int r = pthread_cond_destroy(&cond->cond);
291  if (r != 0) {
292  rb_bug_errno("pthread_cond_destroy", r);
293  }
294 #endif
295 }
296 
297 /*
298  * In OS X 10.7 (Lion), pthread_cond_signal and pthread_cond_broadcast return
299  * EAGAIN after retrying 8192 times. You can see them in the following page:
300  *
301  * http://www.opensource.apple.com/source/Libc/Libc-763.11/pthreads/pthread_cond.c
302  *
303  * The following native_cond_signal and native_cond_broadcast functions
304  * need to retrying until pthread functions don't return EAGAIN.
305  */
306 
307 static void
308 native_cond_signal(rb_nativethread_cond_t *cond)
309 {
310  int r;
311  do {
312  r = pthread_cond_signal(&cond->cond);
313  } while (r == EAGAIN);
314  if (r != 0) {
315  rb_bug_errno("pthread_cond_signal", r);
316  }
317 }
318 
319 static void
320 native_cond_broadcast(rb_nativethread_cond_t *cond)
321 {
322  int r;
323  do {
324  r = pthread_cond_broadcast(&cond->cond);
325  } while (r == EAGAIN);
326  if (r != 0) {
327  rb_bug_errno("native_cond_broadcast", r);
328  }
329 }
330 
331 static void
332 native_cond_wait(rb_nativethread_cond_t *cond, pthread_mutex_t *mutex)
333 {
334  int r = pthread_cond_wait(&cond->cond, mutex);
335  if (r != 0) {
336  rb_bug_errno("pthread_cond_wait", r);
337  }
338 }
339 
340 static int
341 native_cond_timedwait(rb_nativethread_cond_t *cond, pthread_mutex_t *mutex, struct timespec *ts)
342 {
343  int r;
344 
345  /*
346  * An old Linux may return EINTR. Even though POSIX says
347  * "These functions shall not return an error code of [EINTR]".
348  * http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_cond_timedwait.html
349  * Let's hide it from arch generic code.
350  */
351  do {
352  r = pthread_cond_timedwait(&cond->cond, mutex, ts);
353  } while (r == EINTR);
354 
355  if (r != 0 && r != ETIMEDOUT) {
356  rb_bug_errno("pthread_cond_timedwait", r);
357  }
358 
359  return r;
360 }
361 
362 static struct timespec
363 native_cond_timeout(rb_nativethread_cond_t *cond, struct timespec timeout_rel)
364 {
365  int ret;
366  struct timeval tv;
367  struct timespec timeout;
368  struct timespec now;
369 
370 #if USE_MONOTONIC_COND
371  if (cond->clockid == CLOCK_MONOTONIC) {
372  ret = clock_gettime(cond->clockid, &now);
373  if (ret != 0)
374  rb_sys_fail("clock_gettime()");
375  goto out;
376  }
377 
378  if (cond->clockid != CLOCK_REALTIME)
379  rb_bug("unsupported clockid %"PRIdVALUE, (SIGNED_VALUE)cond->clockid);
380 #endif
381 
382  ret = gettimeofday(&tv, 0);
383  if (ret != 0)
384  rb_sys_fail(0);
385  now.tv_sec = tv.tv_sec;
386  now.tv_nsec = tv.tv_usec * 1000;
387 
388 #if USE_MONOTONIC_COND
389  out:
390 #endif
391  timeout.tv_sec = now.tv_sec;
392  timeout.tv_nsec = now.tv_nsec;
393  timeout.tv_sec += timeout_rel.tv_sec;
394  timeout.tv_nsec += timeout_rel.tv_nsec;
395 
396  if (timeout.tv_nsec >= 1000*1000*1000) {
397  timeout.tv_sec++;
398  timeout.tv_nsec -= 1000*1000*1000;
399  }
400 
401  if (timeout.tv_sec < now.tv_sec)
402  timeout.tv_sec = TIMET_MAX;
403 
404  return timeout;
405 }
406 
407 #define native_cleanup_push pthread_cleanup_push
408 #define native_cleanup_pop pthread_cleanup_pop
409 #ifdef HAVE_SCHED_YIELD
410 #define native_thread_yield() (void)sched_yield()
411 #else
412 #define native_thread_yield() ((void)0)
413 #endif
414 
415 #if defined(SIGVTALRM) && !defined(__CYGWIN__) && !defined(__SYMBIAN32__)
416 #define USE_SIGNAL_THREAD_LIST 1
417 #endif
418 #ifdef USE_SIGNAL_THREAD_LIST
419 static void add_signal_thread_list(rb_thread_t *th);
420 static void remove_signal_thread_list(rb_thread_t *th);
421 static rb_nativethread_lock_t signal_thread_list_lock;
422 #endif
423 
424 static pthread_key_t ruby_native_thread_key;
425 
426 static void
427 null_func(int i)
428 {
429  /* null */
430 }
431 
432 static rb_thread_t *
433 ruby_thread_from_native(void)
434 {
435  return pthread_getspecific(ruby_native_thread_key);
436 }
437 
438 static int
439 ruby_thread_set_native(rb_thread_t *th)
440 {
441  return pthread_setspecific(ruby_native_thread_key, th) == 0;
442 }
443 
444 static void native_thread_init(rb_thread_t *th);
445 
446 void
447 Init_native_thread(void)
448 {
449  rb_thread_t *th = GET_THREAD();
450 
451  pthread_key_create(&ruby_native_thread_key, NULL);
452  th->thread_id = pthread_self();
453  native_thread_init(th);
454 #ifdef USE_SIGNAL_THREAD_LIST
455  native_mutex_initialize(&signal_thread_list_lock);
456 #endif
457 #ifndef __native_client__
458  posix_signal(SIGVTALRM, null_func);
459 #endif
460 }
461 
462 static void
463 native_thread_init(rb_thread_t *th)
464 {
465  native_cond_initialize(&th->native_thread_data.sleep_cond, RB_CONDATTR_CLOCK_MONOTONIC);
466  ruby_thread_set_native(th);
467 }
468 
469 static void
470 native_thread_destroy(rb_thread_t *th)
471 {
472  native_cond_destroy(&th->native_thread_data.sleep_cond);
473 }
474 
475 #ifndef USE_THREAD_CACHE
476 #define USE_THREAD_CACHE 0
477 #endif
478 
479 #if USE_THREAD_CACHE
480 static rb_thread_t *register_cached_thread_and_wait(void);
481 #endif
482 
483 #if defined HAVE_PTHREAD_GETATTR_NP || defined HAVE_PTHREAD_ATTR_GET_NP
484 #define STACKADDR_AVAILABLE 1
485 #elif defined HAVE_PTHREAD_GET_STACKADDR_NP && defined HAVE_PTHREAD_GET_STACKSIZE_NP
486 #define STACKADDR_AVAILABLE 1
487 #undef MAINSTACKADDR_AVAILABLE
488 #define MAINSTACKADDR_AVAILABLE 1
489 void *pthread_get_stackaddr_np(pthread_t);
490 size_t pthread_get_stacksize_np(pthread_t);
491 #elif defined HAVE_THR_STKSEGMENT || defined HAVE_PTHREAD_STACKSEG_NP
492 #define STACKADDR_AVAILABLE 1
493 #elif defined HAVE_PTHREAD_GETTHRDS_NP
494 #define STACKADDR_AVAILABLE 1
495 #elif defined __ia64 && defined _HPUX_SOURCE
496 #include <sys/dyntune.h>
497 
498 #define STACKADDR_AVAILABLE 1
499 
500 /*
501  * Do not lower the thread's stack to PTHREAD_STACK_MIN,
502  * otherwise one would receive a 'sendsig: useracc failed.'
503  * and a coredump.
504  */
505 #undef PTHREAD_STACK_MIN
506 
507 #define HAVE_PTHREAD_ATTR_GET_NP 1
508 #undef HAVE_PTHREAD_ATTR_GETSTACK
509 
510 /*
511  * As the PTHREAD_STACK_MIN is undefined and
512  * noone touches the default stacksize,
513  * it is just fine to use the default.
514  */
515 #define pthread_attr_get_np(thid, attr) 0
516 
517 /*
518  * Using value of sp is very rough... To make it more real,
519  * addr would need to be aligned to vps_pagesize.
520  * The vps_pagesize is 'Default user page size (kBytes)'
521  * and could be retrieved by gettune().
522  */
523 static int
524 hpux_attr_getstackaddr(const pthread_attr_t *attr, void **addr)
525 {
526  static uint64_t pagesize;
527  size_t size;
528 
529  if (!pagesize) {
530  if (gettune("vps_pagesize", &pagesize)) {
531  pagesize = 16;
532  }
533  pagesize *= 1024;
534  }
535  pthread_attr_getstacksize(attr, &size);
536  *addr = (void *)((size_t)((char *)_Asm_get_sp() - size) & ~(pagesize - 1));
537  return 0;
538 }
539 #define pthread_attr_getstackaddr(attr, addr) hpux_attr_getstackaddr(attr, addr)
540 #endif
541 
542 #ifndef MAINSTACKADDR_AVAILABLE
543 # ifdef STACKADDR_AVAILABLE
544 # define MAINSTACKADDR_AVAILABLE 1
545 # else
546 # define MAINSTACKADDR_AVAILABLE 0
547 # endif
548 #endif
549 #if MAINSTACKADDR_AVAILABLE && !defined(get_main_stack)
550 # define get_main_stack(addr, size) get_stack(addr, size)
551 #endif
552 
553 #ifdef STACKADDR_AVAILABLE
554 /*
555  * Get the initial address and size of current thread's stack
556  */
557 static int
558 get_stack(void **addr, size_t *size)
559 {
560 #define CHECK_ERR(expr) \
561  {int err = (expr); if (err) return err;}
562 #ifdef HAVE_PTHREAD_GETATTR_NP /* Linux */
563  pthread_attr_t attr;
564  size_t guard = 0;
566  CHECK_ERR(pthread_getattr_np(pthread_self(), &attr));
567 # ifdef HAVE_PTHREAD_ATTR_GETSTACK
568  CHECK_ERR(pthread_attr_getstack(&attr, addr, size));
569  STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
570 # else
571  CHECK_ERR(pthread_attr_getstackaddr(&attr, addr));
572  CHECK_ERR(pthread_attr_getstacksize(&attr, size));
573 # endif
574  CHECK_ERR(pthread_attr_getguardsize(&attr, &guard));
575  *size -= guard;
576  pthread_attr_destroy(&attr);
577 #elif defined HAVE_PTHREAD_ATTR_GET_NP /* FreeBSD, DragonFly BSD, NetBSD */
578  pthread_attr_t attr;
579  CHECK_ERR(pthread_attr_init(&attr));
580  CHECK_ERR(pthread_attr_get_np(pthread_self(), &attr));
581 # ifdef HAVE_PTHREAD_ATTR_GETSTACK
582  CHECK_ERR(pthread_attr_getstack(&attr, addr, size));
583 # else
584  CHECK_ERR(pthread_attr_getstackaddr(&attr, addr));
585  CHECK_ERR(pthread_attr_getstacksize(&attr, size));
586 # endif
587  STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
588  pthread_attr_destroy(&attr);
589 #elif (defined HAVE_PTHREAD_GET_STACKADDR_NP && defined HAVE_PTHREAD_GET_STACKSIZE_NP) /* MacOS X */
590  pthread_t th = pthread_self();
591  *addr = pthread_get_stackaddr_np(th);
592  *size = pthread_get_stacksize_np(th);
593 #elif defined HAVE_THR_STKSEGMENT || defined HAVE_PTHREAD_STACKSEG_NP
594  stack_t stk;
595 # if defined HAVE_THR_STKSEGMENT /* Solaris */
596  CHECK_ERR(thr_stksegment(&stk));
597 # else /* OpenBSD */
598  CHECK_ERR(pthread_stackseg_np(pthread_self(), &stk));
599 # endif
600  *addr = stk.ss_sp;
601  *size = stk.ss_size;
602 #elif defined HAVE_PTHREAD_GETTHRDS_NP /* AIX */
603  pthread_t th = pthread_self();
604  struct __pthrdsinfo thinfo;
605  char reg[256];
606  int regsiz=sizeof(reg);
607  CHECK_ERR(pthread_getthrds_np(&th, PTHRDSINFO_QUERY_ALL,
608  &thinfo, sizeof(thinfo),
609  &reg, &regsiz));
610  *addr = thinfo.__pi_stackaddr;
611  /* Must not use thinfo.__pi_stacksize for size.
612  It is around 3KB smaller than the correct size
613  calculated by thinfo.__pi_stackend - thinfo.__pi_stackaddr. */
614  *size = thinfo.__pi_stackend - thinfo.__pi_stackaddr;
615  STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
616 #else
617 #error STACKADDR_AVAILABLE is defined but not implemented.
618 #endif
619  return 0;
620 #undef CHECK_ERR
621 }
622 #endif
623 
624 static struct {
626  size_t stack_maxsize;
627  VALUE *stack_start;
628 #ifdef __ia64
629  VALUE *register_stack_start;
630 #endif
631 } native_main_thread;
632 
633 #ifdef STACK_END_ADDRESS
634 extern void *STACK_END_ADDRESS;
635 #endif
636 
637 enum {
638  RUBY_STACK_SPACE_LIMIT = 1024 * 1024, /* 1024KB */
639  RUBY_STACK_SPACE_RATIO = 5
640 };
641 
642 static size_t
643 space_size(size_t stack_size)
644 {
645  size_t space_size = stack_size / RUBY_STACK_SPACE_RATIO;
646  if (space_size > RUBY_STACK_SPACE_LIMIT) {
647  return RUBY_STACK_SPACE_LIMIT;
648  }
649  else {
650  return space_size;
651  }
652 }
653 
654 #ifdef __linux__
655 static __attribute__((noinline)) void
656 reserve_stack(volatile char *limit, size_t size)
657 {
658 # ifdef C_ALLOCA
659 # error needs alloca()
660 # endif
661  struct rlimit rl;
662  volatile char buf[0x100];
663  enum {stack_check_margin = 0x1000}; /* for -fstack-check */
664 
666 
667  if (!getrlimit(RLIMIT_STACK, &rl) && rl.rlim_cur == RLIM_INFINITY)
668  return;
669 
670  if (size < stack_check_margin) return;
671  size -= stack_check_margin;
672 
673  size -= sizeof(buf); /* margin */
674  if (IS_STACK_DIR_UPPER()) {
675  const volatile char *end = buf + sizeof(buf);
676  limit += size;
677  if (limit > end) {
678  /* |<-bottom (=limit(a)) top->|
679  * | .. |<-buf 256B |<-end | stack check |
680  * | 256B | =size= | margin (4KB)|
681  * | =size= limit(b)->| 256B | |
682  * | | alloca(sz) | | |
683  * | .. |<-buf |<-limit(c) [sz-1]->0> | |
684  */
685  size_t sz = limit - end;
686  limit = alloca(sz);
687  limit[sz-1] = 0;
688  }
689  }
690  else {
691  limit -= size;
692  if (buf > limit) {
693  /* |<-top (=limit(a)) bottom->|
694  * | .. | 256B buf->| | stack check |
695  * | 256B | =size= | margin (4KB)|
696  * | =size= limit(b)->| 256B | |
697  * | | alloca(sz) | | |
698  * | .. | buf->| limit(c)-><0> | |
699  */
700  size_t sz = buf - limit;
701  limit = alloca(sz);
702  limit[0] = 0;
703  }
704  }
705 }
706 #else
707 # define reserve_stack(limit, size) ((void)(limit), (void)(size))
708 #endif
709 
710 #undef ruby_init_stack
711 /* Set stack bottom of Ruby implementation.
712  *
713  * You must call this function before any heap allocation by Ruby implementation.
714  * Or GC will break living objects */
715 void
716 ruby_init_stack(volatile VALUE *addr
717 #ifdef __ia64
718  , void *bsp
719 #endif
720  )
721 {
722  native_main_thread.id = pthread_self();
723 #if MAINSTACKADDR_AVAILABLE
724  if (native_main_thread.stack_maxsize) return;
725  {
726  void* stackaddr;
727  size_t size;
728  if (get_main_stack(&stackaddr, &size) == 0) {
729  native_main_thread.stack_maxsize = size;
730  native_main_thread.stack_start = stackaddr;
731  reserve_stack(stackaddr, size);
732  return;
733  }
734  }
735 #endif
736 #ifdef STACK_END_ADDRESS
737  native_main_thread.stack_start = STACK_END_ADDRESS;
738 #else
739  if (!native_main_thread.stack_start ||
740  STACK_UPPER((VALUE *)(void *)&addr,
741  native_main_thread.stack_start > addr,
742  native_main_thread.stack_start < addr)) {
743  native_main_thread.stack_start = (VALUE *)addr;
744  }
745 #endif
746 #ifdef __ia64
747  if (!native_main_thread.register_stack_start ||
748  (VALUE*)bsp < native_main_thread.register_stack_start) {
749  native_main_thread.register_stack_start = (VALUE*)bsp;
750  }
751 #endif
752  {
753 #if defined(HAVE_GETRLIMIT)
754 #if defined(PTHREAD_STACK_DEFAULT)
755 # if PTHREAD_STACK_DEFAULT < RUBY_STACK_SPACE*5
756 # error "PTHREAD_STACK_DEFAULT is too small"
757 # endif
758  size_t size = PTHREAD_STACK_DEFAULT;
759 #else
761 #endif
762  size_t space;
763  int pagesize = getpagesize();
764  struct rlimit rlim;
766  if (getrlimit(RLIMIT_STACK, &rlim) == 0) {
767  size = (size_t)rlim.rlim_cur;
768  }
769  addr = native_main_thread.stack_start;
770  if (IS_STACK_DIR_UPPER()) {
771  space = ((size_t)((char *)addr + size) / pagesize) * pagesize - (size_t)addr;
772  }
773  else {
774  space = (size_t)addr - ((size_t)((char *)addr - size) / pagesize + 1) * pagesize;
775  }
776  native_main_thread.stack_maxsize = space;
777 #endif
778  }
779 
780  /* If addr is out of range of main-thread stack range estimation, */
781  /* it should be on co-routine (alternative stack). [Feature #2294] */
782  {
783  void *start, *end;
785 
786  if (IS_STACK_DIR_UPPER()) {
787  start = native_main_thread.stack_start;
788  end = (char *)native_main_thread.stack_start + native_main_thread.stack_maxsize;
789  }
790  else {
791  start = (char *)native_main_thread.stack_start - native_main_thread.stack_maxsize;
792  end = native_main_thread.stack_start;
793  }
794 
795  if ((void *)addr < start || (void *)addr > end) {
796  /* out of range */
797  native_main_thread.stack_start = (VALUE *)addr;
798  native_main_thread.stack_maxsize = 0; /* unknown */
799  }
800  }
801 }
802 
803 #define CHECK_ERR(expr) \
804  {int err = (expr); if (err) {rb_bug_errno(#expr, err);}}
805 
806 static int
807 native_thread_init_stack(rb_thread_t *th)
808 {
809  rb_nativethread_id_t curr = pthread_self();
810 
811  if (pthread_equal(curr, native_main_thread.id)) {
812  th->machine.stack_start = native_main_thread.stack_start;
813  th->machine.stack_maxsize = native_main_thread.stack_maxsize;
814  }
815  else {
816 #ifdef STACKADDR_AVAILABLE
817  void *start;
818  size_t size;
819 
820  if (get_stack(&start, &size) == 0) {
821  th->machine.stack_start = start;
822  th->machine.stack_maxsize = size;
823  }
824 #elif defined get_stack_of
825  if (!th->machine.stack_maxsize) {
826  native_mutex_lock(&th->interrupt_lock);
827  native_mutex_unlock(&th->interrupt_lock);
828  }
829 #else
830  rb_raise(rb_eNotImpError, "ruby engine can initialize only in the main thread");
831 #endif
832  }
833 #ifdef __ia64
834  th->machine.register_stack_start = native_main_thread.register_stack_start;
835  th->machine.stack_maxsize /= 2;
836  th->machine.register_stack_maxsize = th->machine.stack_maxsize;
837 #endif
838  return 0;
839 }
840 
841 #ifndef __CYGWIN__
842 #define USE_NATIVE_THREAD_INIT 1
843 #endif
844 
845 static void *
846 thread_start_func_1(void *th_ptr)
847 {
848 #if USE_THREAD_CACHE
849  thread_start:
850 #endif
851  {
852  rb_thread_t *th = th_ptr;
853 #if !defined USE_NATIVE_THREAD_INIT
854  VALUE stack_start;
855 #endif
856 
857 #if defined USE_NATIVE_THREAD_INIT
858  native_thread_init_stack(th);
859 #endif
860  native_thread_init(th);
861  /* run */
862 #if defined USE_NATIVE_THREAD_INIT
863  thread_start_func_2(th, th->machine.stack_start, rb_ia64_bsp());
864 #else
865  thread_start_func_2(th, &stack_start, rb_ia64_bsp());
866 #endif
867  }
868 #if USE_THREAD_CACHE
869  if (1) {
870  /* cache thread */
871  rb_thread_t *th;
872  if ((th = register_cached_thread_and_wait()) != 0) {
873  th_ptr = (void *)th;
874  th->thread_id = pthread_self();
875  goto thread_start;
876  }
877  }
878 #endif
879  return 0;
880 }
881 
882 struct cached_thread_entry {
883  volatile rb_thread_t **th_area;
885  struct cached_thread_entry *next;
886 };
887 
888 
889 #if USE_THREAD_CACHE
890 static pthread_mutex_t thread_cache_lock = PTHREAD_MUTEX_INITIALIZER;
891 struct cached_thread_entry *cached_thread_root;
892 
893 static rb_thread_t *
894 register_cached_thread_and_wait(void)
895 {
896  rb_nativethread_cond_t cond = { PTHREAD_COND_INITIALIZER, };
897  volatile rb_thread_t *th_area = 0;
898  struct timeval tv;
899  struct timespec ts;
900  struct cached_thread_entry *entry =
901  (struct cached_thread_entry *)malloc(sizeof(struct cached_thread_entry));
902 
903  if (entry == 0) {
904  return 0; /* failed -> terminate thread immediately */
905  }
906 
907  gettimeofday(&tv, 0);
908  ts.tv_sec = tv.tv_sec + 60;
909  ts.tv_nsec = tv.tv_usec * 1000;
910 
911  pthread_mutex_lock(&thread_cache_lock);
912  {
913  entry->th_area = &th_area;
914  entry->cond = &cond;
915  entry->next = cached_thread_root;
916  cached_thread_root = entry;
917 
918  native_cond_timedwait(&cond, &thread_cache_lock, &ts);
919 
920  {
921  struct cached_thread_entry *e, **prev = &cached_thread_root;
922 
923  while ((e = *prev) != 0) {
924  if (e == entry) {
925  *prev = e->next;
926  break;
927  }
928  prev = &e->next;
929  }
930  }
931 
932  free(entry); /* ok */
933  native_cond_destroy(&cond);
934  }
935  pthread_mutex_unlock(&thread_cache_lock);
936 
937  return (rb_thread_t *)th_area;
938 }
939 #endif
940 
941 static int
942 use_cached_thread(rb_thread_t *th)
943 {
944  int result = 0;
945 #if USE_THREAD_CACHE
946  struct cached_thread_entry *entry;
947 
948  if (cached_thread_root) {
949  pthread_mutex_lock(&thread_cache_lock);
950  entry = cached_thread_root;
951  {
952  if (cached_thread_root) {
953  cached_thread_root = entry->next;
954  *entry->th_area = th;
955  result = 1;
956  }
957  }
958  if (result) {
959  native_cond_signal(entry->cond);
960  }
961  pthread_mutex_unlock(&thread_cache_lock);
962  }
963 #endif
964  return result;
965 }
966 
967 static int
968 native_thread_create(rb_thread_t *th)
969 {
970  int err = 0;
971 
972  if (use_cached_thread(th)) {
973  thread_debug("create (use cached thread): %p\n", (void *)th);
974  }
975  else {
976 #ifdef HAVE_PTHREAD_ATTR_INIT
977  pthread_attr_t attr;
978  pthread_attr_t *const attrp = &attr;
979 #else
980  pthread_attr_t *const attrp = NULL;
981 #endif
982  const size_t stack_size = th->vm->default_params.thread_machine_stack_size;
983  const size_t space = space_size(stack_size);
984 
985  th->machine.stack_maxsize = stack_size - space;
986 #ifdef __ia64
987  th->machine.stack_maxsize /= 2;
988  th->machine.register_stack_maxsize = th->machine.stack_maxsize;
989 #endif
990 
991 #ifdef HAVE_PTHREAD_ATTR_INIT
992  CHECK_ERR(pthread_attr_init(&attr));
993 
994 # ifdef PTHREAD_STACK_MIN
995  thread_debug("create - stack size: %lu\n", (unsigned long)stack_size);
996  CHECK_ERR(pthread_attr_setstacksize(&attr, stack_size));
997 # endif
998 
999 # ifdef HAVE_PTHREAD_ATTR_SETINHERITSCHED
1000  CHECK_ERR(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
1001 # endif
1002  CHECK_ERR(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
1003 #endif
1004 #ifdef get_stack_of
1005  native_mutex_lock(&th->interrupt_lock);
1006 #endif
1007  err = pthread_create(&th->thread_id, attrp, thread_start_func_1, th);
1008 #ifdef get_stack_of
1009  if (!err) {
1010  get_stack_of(th->thread_id,
1011  &th->machine.stack_start,
1012  &th->machine.stack_maxsize);
1013  }
1014  native_mutex_unlock(&th->interrupt_lock);
1015 #endif
1016  thread_debug("create: %p (%d)\n", (void *)th, err);
1017 #ifdef HAVE_PTHREAD_ATTR_INIT
1018  CHECK_ERR(pthread_attr_destroy(&attr));
1019 #endif
1020  }
1021  return err;
1022 }
1023 
1024 static void
1025 native_thread_join(pthread_t th)
1026 {
1027  int err = pthread_join(th, 0);
1028  if (err) {
1029  rb_raise(rb_eThreadError, "native_thread_join() failed (%d)", err);
1030  }
1031 }
1032 
1033 
1034 #if USE_NATIVE_THREAD_PRIORITY
1035 
1036 static void
1037 native_thread_apply_priority(rb_thread_t *th)
1038 {
1039 #if defined(_POSIX_PRIORITY_SCHEDULING) && (_POSIX_PRIORITY_SCHEDULING > 0)
1040  struct sched_param sp;
1041  int policy;
1042  int priority = 0 - th->priority;
1043  int max, min;
1044  pthread_getschedparam(th->thread_id, &policy, &sp);
1045  max = sched_get_priority_max(policy);
1046  min = sched_get_priority_min(policy);
1047 
1048  if (min > priority) {
1049  priority = min;
1050  }
1051  else if (max < priority) {
1052  priority = max;
1053  }
1054 
1055  sp.sched_priority = priority;
1056  pthread_setschedparam(th->thread_id, policy, &sp);
1057 #else
1058  /* not touched */
1059 #endif
1060 }
1061 
1062 #endif /* USE_NATIVE_THREAD_PRIORITY */
1063 
1064 static int
1065 native_fd_select(int n, rb_fdset_t *readfds, rb_fdset_t *writefds, rb_fdset_t *exceptfds, struct timeval *timeout, rb_thread_t *th)
1066 {
1067  return rb_fd_select(n, readfds, writefds, exceptfds, timeout);
1068 }
1069 
1070 static void
1071 ubf_pthread_cond_signal(void *ptr)
1072 {
1073  rb_thread_t *th = (rb_thread_t *)ptr;
1074  thread_debug("ubf_pthread_cond_signal (%p)\n", (void *)th);
1075  native_cond_signal(&th->native_thread_data.sleep_cond);
1076 }
1077 
1078 static void
1079 native_sleep(rb_thread_t *th, struct timeval *timeout_tv)
1080 {
1081  struct timespec timeout;
1082  pthread_mutex_t *lock = &th->interrupt_lock;
1084 
1085  if (timeout_tv) {
1086  struct timespec timeout_rel;
1087 
1088  timeout_rel.tv_sec = timeout_tv->tv_sec;
1089  timeout_rel.tv_nsec = timeout_tv->tv_usec * 1000;
1090 
1091  /* Solaris cond_timedwait() return EINVAL if an argument is greater than
1092  * current_time + 100,000,000. So cut up to 100,000,000. This is
1093  * considered as a kind of spurious wakeup. The caller to native_sleep
1094  * should care about spurious wakeup.
1095  *
1096  * See also [Bug #1341] [ruby-core:29702]
1097  * http://download.oracle.com/docs/cd/E19683-01/816-0216/6m6ngupgv/index.html
1098  */
1099  if (timeout_rel.tv_sec > 100000000) {
1100  timeout_rel.tv_sec = 100000000;
1101  timeout_rel.tv_nsec = 0;
1102  }
1103 
1104  timeout = native_cond_timeout(cond, timeout_rel);
1105  }
1106 
1107  GVL_UNLOCK_BEGIN();
1108  {
1109  pthread_mutex_lock(lock);
1110  th->unblock.func = ubf_pthread_cond_signal;
1111  th->unblock.arg = th;
1112 
1113  if (RUBY_VM_INTERRUPTED(th)) {
1114  /* interrupted. return immediate */
1115  thread_debug("native_sleep: interrupted before sleep\n");
1116  }
1117  else {
1118  if (!timeout_tv)
1119  native_cond_wait(cond, lock);
1120  else
1121  native_cond_timedwait(cond, lock, &timeout);
1122  }
1123  th->unblock.func = 0;
1124  th->unblock.arg = 0;
1125 
1126  pthread_mutex_unlock(lock);
1127  }
1128  GVL_UNLOCK_END();
1129 
1130  thread_debug("native_sleep done\n");
1131 }
1132 
1133 #ifdef USE_SIGNAL_THREAD_LIST
1134 struct signal_thread_list {
1135  rb_thread_t *th;
1136  struct signal_thread_list *prev;
1137  struct signal_thread_list *next;
1138 };
1139 
1140 static struct signal_thread_list signal_thread_list_anchor = {
1141  0, 0, 0,
1142 };
1143 
1144 #define FGLOCK(lock, body) do { \
1145  native_mutex_lock(lock); \
1146  { \
1147  body; \
1148  } \
1149  native_mutex_unlock(lock); \
1150 } while (0)
1151 
1152 #if 0 /* for debug */
1153 static void
1154 print_signal_list(char *str)
1155 {
1156  struct signal_thread_list *list =
1157  signal_thread_list_anchor.next;
1158  thread_debug("list (%s)> ", str);
1159  while (list) {
1160  thread_debug("%p (%p), ", list->th, list->th->thread_id);
1161  list = list->next;
1162  }
1163  thread_debug("\n");
1164 }
1165 #endif
1166 
1167 static void
1168 add_signal_thread_list(rb_thread_t *th)
1169 {
1171  FGLOCK(&signal_thread_list_lock, {
1172  struct signal_thread_list *list =
1173  malloc(sizeof(struct signal_thread_list));
1174 
1175  if (list == 0) {
1176  fprintf(stderr, "[FATAL] failed to allocate memory\n");
1177  exit(EXIT_FAILURE);
1178  }
1179 
1180  list->th = th;
1181 
1182  list->prev = &signal_thread_list_anchor;
1183  list->next = signal_thread_list_anchor.next;
1184  if (list->next) {
1185  list->next->prev = list;
1186  }
1187  signal_thread_list_anchor.next = list;
1189  });
1190  }
1191 }
1192 
1193 static void
1194 remove_signal_thread_list(rb_thread_t *th)
1195 {
1197  FGLOCK(&signal_thread_list_lock, {
1198  struct signal_thread_list *list =
1199  (struct signal_thread_list *)
1201 
1202  list->prev->next = list->next;
1203  if (list->next) {
1204  list->next->prev = list->prev;
1205  }
1207  list->th = 0;
1208  free(list); /* ok */
1209  });
1210  }
1211 }
1212 
1213 static void
1214 ubf_select_each(rb_thread_t *th)
1215 {
1216  thread_debug("ubf_select_each (%p)\n", (void *)th->thread_id);
1217  if (th) {
1218  pthread_kill(th->thread_id, SIGVTALRM);
1219  }
1220 }
1221 
1222 static void
1223 ubf_select(void *ptr)
1224 {
1225  rb_thread_t *th = (rb_thread_t *)ptr;
1226  add_signal_thread_list(th);
1227 
1228  /*
1229  * ubf_select_each() doesn't guarantee to wake up the target thread.
1230  * Therefore, we need to activate timer thread when called from
1231  * Thread#kill etc.
1232  * In the other hands, we shouldn't call rb_thread_wakeup_timer_thread()
1233  * if running on timer thread because it may make endless wakeups.
1234  */
1235  if (pthread_self() != timer_thread_id)
1237  ubf_select_each(th);
1238 }
1239 
1240 static void
1241 ping_signal_thread_list(void)
1242 {
1243  if (signal_thread_list_anchor.next) {
1244  FGLOCK(&signal_thread_list_lock, {
1245  struct signal_thread_list *list;
1246 
1247  list = signal_thread_list_anchor.next;
1248  while (list) {
1249  ubf_select_each(list->th);
1250  list = list->next;
1251  }
1252  });
1253  }
1254 }
1255 
1256 static int
1257 check_signal_thread_list(void)
1258 {
1259  if (signal_thread_list_anchor.next)
1260  return 1;
1261  else
1262  return 0;
1263 }
1264 #else /* USE_SIGNAL_THREAD_LIST */
1265 #define add_signal_thread_list(th) (void)(th)
1266 #define remove_signal_thread_list(th) (void)(th)
1267 #define ubf_select 0
1268 static void ping_signal_thread_list(void) { return; }
1269 static int check_signal_thread_list(void) { return 0; }
1270 #endif /* USE_SIGNAL_THREAD_LIST */
1271 
1272 #define TT_DEBUG 0
1273 #define WRITE_CONST(fd, str) (void)(write((fd),(str),sizeof(str)-1)<0)
1274 
1275 /* 100ms. 10ms is too small for user level thread scheduling
1276  * on recent Linux (tested on 2.6.35)
1277  */
1278 #define TIME_QUANTUM_USEC (100 * 1000)
1279 
1280 #if USE_SLEEPY_TIMER_THREAD
1281 static int timer_thread_pipe[2] = {-1, -1};
1282 static int timer_thread_pipe_low[2] = {-1, -1}; /* low priority */
1283 static int timer_thread_pipe_owner_process;
1284 
1285 /* only use signal-safe system calls here */
1286 static void
1287 rb_thread_wakeup_timer_thread_fd(int fd)
1288 {
1289  ssize_t result;
1290 
1291  /* already opened */
1292  if (timer_thread_pipe_owner_process == getpid()) {
1293  const char *buff = "!";
1294  retry:
1295  if ((result = write(fd, buff, 1)) <= 0) {
1296  switch (errno) {
1297  case EINTR: goto retry;
1298  case EAGAIN:
1299 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
1300  case EWOULDBLOCK:
1301 #endif
1302  break;
1303  default:
1304  rb_async_bug_errno("rb_thread_wakeup_timer_thread - write", errno);
1305  }
1306  }
1307  if (TT_DEBUG) WRITE_CONST(2, "rb_thread_wakeup_timer_thread: write\n");
1308  }
1309  else {
1310  /* ignore wakeup */
1311  }
1312 }
1313 
1314 void
1316 {
1317  rb_thread_wakeup_timer_thread_fd(timer_thread_pipe[1]);
1318 }
1319 
1320 static void
1321 rb_thread_wakeup_timer_thread_low(void)
1322 {
1323  rb_thread_wakeup_timer_thread_fd(timer_thread_pipe_low[1]);
1324 }
1325 
1326 /* VM-dependent API is not available for this function */
1327 static void
1328 consume_communication_pipe(int fd)
1329 {
1330 #define CCP_READ_BUFF_SIZE 1024
1331  /* buffer can be shared because no one refers to them. */
1332  static char buff[CCP_READ_BUFF_SIZE];
1333  ssize_t result;
1334 
1335  while (1) {
1336  result = read(fd, buff, sizeof(buff));
1337  if (result == 0) {
1338  return;
1339  }
1340  else if (result < 0) {
1341  switch (errno) {
1342  case EINTR:
1343  continue; /* retry */
1344  case EAGAIN:
1345  return;
1346  default:
1347  rb_async_bug_errno("consume_communication_pipe: read\n", errno);
1348  }
1349  }
1350  }
1351 }
1352 
1353 static void
1354 close_communication_pipe(int pipes[2])
1355 {
1356  if (close(pipes[0]) < 0) {
1357  rb_bug_errno("native_stop_timer_thread - close(ttp[0])", errno);
1358  }
1359  if (close(pipes[1]) < 0) {
1360  rb_bug_errno("native_stop_timer_thread - close(ttp[1])", errno);
1361  }
1362  pipes[0] = pipes[1] = -1;
1363 }
1364 
1365 static void
1366 set_nonblock(int fd)
1367 {
1368  int oflags;
1369  int err;
1370 
1371  oflags = fcntl(fd, F_GETFL);
1372  if (oflags == -1)
1373  rb_sys_fail(0);
1374  oflags |= O_NONBLOCK;
1375  err = fcntl(fd, F_SETFL, oflags);
1376  if (err == -1)
1377  rb_sys_fail(0);
1378 }
1379 
1380 static void
1381 setup_communication_pipe_internal(int pipes[2])
1382 {
1383  int err;
1384 
1385  if (pipes[0] != -1) {
1386  /* close pipe of parent process */
1387  close_communication_pipe(pipes);
1388  }
1389 
1390  err = rb_cloexec_pipe(pipes);
1391  if (err != 0) {
1392  rb_bug_errno("setup_communication_pipe: Failed to create communication pipe for timer thread", errno);
1393  }
1394  rb_update_max_fd(pipes[0]);
1395  rb_update_max_fd(pipes[1]);
1396  set_nonblock(pipes[0]);
1397  set_nonblock(pipes[1]);
1398 }
1399 
1400 /* communication pipe with timer thread and signal handler */
1401 static void
1402 setup_communication_pipe(void)
1403 {
1404  if (timer_thread_pipe_owner_process == getpid()) {
1405  /* already set up. */
1406  return;
1407  }
1408  setup_communication_pipe_internal(timer_thread_pipe);
1409  setup_communication_pipe_internal(timer_thread_pipe_low);
1410 
1411  /* validate pipe on this process */
1412  timer_thread_pipe_owner_process = getpid();
1413 }
1414 
1421 static inline void
1422 timer_thread_sleep(rb_global_vm_lock_t* gvl)
1423 {
1424  int result;
1425  int need_polling;
1426  struct pollfd pollfds[2];
1427 
1428  pollfds[0].fd = timer_thread_pipe[0];
1429  pollfds[0].events = POLLIN;
1430  pollfds[1].fd = timer_thread_pipe_low[0];
1431  pollfds[1].events = POLLIN;
1432 
1433  need_polling = check_signal_thread_list();
1434 
1435  if (gvl->waiting > 0 || need_polling) {
1436  /* polling (TIME_QUANTUM_USEC usec) */
1437  result = poll(pollfds, 1, TIME_QUANTUM_USEC/1000);
1438  }
1439  else {
1440  /* wait (infinite) */
1441  result = poll(pollfds, numberof(pollfds), -1);
1442  }
1443 
1444  if (result == 0) {
1445  /* maybe timeout */
1446  }
1447  else if (result > 0) {
1448  consume_communication_pipe(timer_thread_pipe[0]);
1449  consume_communication_pipe(timer_thread_pipe_low[0]);
1450  }
1451  else { /* result < 0 */
1452  switch (errno) {
1453  case EBADF:
1454  case EINVAL:
1455  case ENOMEM: /* from Linux man */
1456  case EFAULT: /* from FreeBSD man */
1457  rb_async_bug_errno("thread_timer: select", errno);
1458  default:
1459  /* ignore */;
1460  }
1461  }
1462 }
1463 
1464 #else /* USE_SLEEPY_TIMER_THREAD */
1465 # define PER_NANO 1000000000
1466 void rb_thread_wakeup_timer_thread(void) {}
1467 static void rb_thread_wakeup_timer_thread_low(void) {}
1468 
1469 static pthread_mutex_t timer_thread_lock;
1470 static rb_nativethread_cond_t timer_thread_cond;
1471 
1472 static inline void
1473 timer_thread_sleep(rb_global_vm_lock_t* unused)
1474 {
1475  struct timespec ts;
1476  ts.tv_sec = 0;
1477  ts.tv_nsec = TIME_QUANTUM_USEC * 1000;
1478  ts = native_cond_timeout(&timer_thread_cond, ts);
1479 
1480  native_cond_timedwait(&timer_thread_cond, &timer_thread_lock, &ts);
1481 }
1482 #endif /* USE_SLEEPY_TIMER_THREAD */
1483 
1484 #if defined(__linux__) && defined(PR_SET_NAME)
1485 # undef SET_THREAD_NAME
1486 # define SET_THREAD_NAME(name) prctl(PR_SET_NAME, name)
1487 #elif !defined(SET_THREAD_NAME)
1488 # define SET_THREAD_NAME(name) (void)0
1489 #endif
1490 
1491 static void *
1492 thread_timer(void *p)
1493 {
1495 
1496  if (TT_DEBUG) WRITE_CONST(2, "start timer thread\n");
1497 
1498  SET_THREAD_NAME("ruby-timer-thr");
1499 
1500 #if !USE_SLEEPY_TIMER_THREAD
1501  native_mutex_initialize(&timer_thread_lock);
1502  native_cond_initialize(&timer_thread_cond, RB_CONDATTR_CLOCK_MONOTONIC);
1503  native_mutex_lock(&timer_thread_lock);
1504 #endif
1505  while (system_working > 0) {
1506 
1507  /* timer function */
1508  ping_signal_thread_list();
1510 
1511  if (TT_DEBUG) WRITE_CONST(2, "tick\n");
1512 
1513  /* wait */
1514  timer_thread_sleep(gvl);
1515  }
1516 #if !USE_SLEEPY_TIMER_THREAD
1517  native_mutex_unlock(&timer_thread_lock);
1518  native_cond_destroy(&timer_thread_cond);
1519  native_mutex_destroy(&timer_thread_lock);
1520 #endif
1521 
1522  if (TT_DEBUG) WRITE_CONST(2, "finish timer thread\n");
1523  return NULL;
1524 }
1525 
1526 static void
1527 rb_thread_create_timer_thread(void)
1528 {
1529  if (!timer_thread_id) {
1530  int err;
1531 #ifdef HAVE_PTHREAD_ATTR_INIT
1532  pthread_attr_t attr;
1533 
1534  err = pthread_attr_init(&attr);
1535  if (err != 0) {
1536  fprintf(stderr, "[FATAL] Failed to initialize pthread attr: %s\n", strerror(err));
1537  exit(EXIT_FAILURE);
1538  }
1539 # ifdef PTHREAD_STACK_MIN
1540  {
1541  const size_t min_size = (4096 * 4);
1542  /* Allocate the machine stack for the timer thread
1543  * at least 16KB (4 pages). FreeBSD 8.2 AMD64 causes
1544  * machine stack overflow only with PTHREAD_STACK_MIN.
1545  */
1546  size_t stack_size = PTHREAD_STACK_MIN; /* may be dynamic, get only once */
1547  if (stack_size < min_size) stack_size = min_size;
1548  if (THREAD_DEBUG) stack_size += BUFSIZ;
1549  pthread_attr_setstacksize(&attr, stack_size);
1550  }
1551 # endif
1552 #endif
1553 
1554 #if USE_SLEEPY_TIMER_THREAD
1555  setup_communication_pipe();
1556 #endif /* USE_SLEEPY_TIMER_THREAD */
1557 
1558  /* create timer thread */
1559  if (timer_thread_id) {
1560  rb_bug("rb_thread_create_timer_thread: Timer thread was already created\n");
1561  }
1562 #ifdef HAVE_PTHREAD_ATTR_INIT
1563  err = pthread_create(&timer_thread_id, &attr, thread_timer, &GET_VM()->gvl);
1564 #else
1565  err = pthread_create(&timer_thread_id, NULL, thread_timer, &GET_VM()->gvl);
1566 #endif
1567  if (err != 0) {
1568  fprintf(stderr, "[FATAL] Failed to create timer thread: %s\n", strerror(err));
1569  exit(EXIT_FAILURE);
1570  }
1571 #ifdef HAVE_PTHREAD_ATTR_INIT
1572  pthread_attr_destroy(&attr);
1573 #endif
1574  }
1575 }
1576 
1577 static int
1578 native_stop_timer_thread(int close_anyway)
1579 {
1580  int stopped;
1581  stopped = --system_working <= 0;
1582 
1583  if (TT_DEBUG) fprintf(stderr, "stop timer thread\n");
1584  if (stopped) {
1585  /* join */
1587  native_thread_join(timer_thread_id);
1588  if (TT_DEBUG) fprintf(stderr, "joined timer thread\n");
1589  timer_thread_id = 0;
1590 
1591  /* close communication pipe */
1592  if (close_anyway) {
1593  /* TODO: Uninstall all signal handlers or mask all signals.
1594  * This pass is cleaning phase (terminate ruby process).
1595  * To avoid such race, we skip to close communication
1596  * pipe. OS will close it at process termination.
1597  * It may not good practice, but pragmatic.
1598  * We remain it is TODO.
1599  */
1600  /* close_communication_pipe(); */
1601  }
1602  }
1603  return stopped;
1604 }
1605 
1606 static void
1607 native_reset_timer_thread(void)
1608 {
1609  if (TT_DEBUG) fprintf(stderr, "reset timer thread\n");
1610 }
1611 
1612 #ifdef HAVE_SIGALTSTACK
1613 int
1614 ruby_stack_overflowed_p(const rb_thread_t *th, const void *addr)
1615 {
1616  void *base;
1617  size_t size;
1618  const size_t water_mark = 1024 * 1024;
1620 
1621 #ifdef STACKADDR_AVAILABLE
1622  if (get_stack(&base, &size) == 0) {
1623 # ifdef __APPLE__
1624  if (pthread_equal(th->thread_id, native_main_thread.id)) {
1625  struct rlimit rlim;
1626  if (getrlimit(RLIMIT_STACK, &rlim) == 0 && rlim.rlim_cur > size) {
1627  size = (size_t)rlim.rlim_cur;
1628  }
1629  }
1630 # endif
1631  base = (char *)base + STACK_DIR_UPPER(+size, -size);
1632  }
1633  else
1634 #endif
1635  if (th) {
1636  size = th->machine.stack_maxsize;
1637  base = (char *)th->machine.stack_start - STACK_DIR_UPPER(0, size);
1638  }
1639  else {
1640  return 0;
1641  }
1642  size /= RUBY_STACK_SPACE_RATIO;
1643  if (size > water_mark) size = water_mark;
1644  if (IS_STACK_DIR_UPPER()) {
1645  if (size > ~(size_t)base+1) size = ~(size_t)base+1;
1646  if (addr > base && addr <= (void *)((char *)base + size)) return 1;
1647  }
1648  else {
1649  if (size > (size_t)base) size = (size_t)base;
1650  if (addr > (void *)((char *)base - size) && addr <= base) return 1;
1651  }
1652  return 0;
1653 }
1654 #endif
1655 
1656 int
1657 rb_reserved_fd_p(int fd)
1658 {
1659 #if USE_SLEEPY_TIMER_THREAD
1660  if (fd == timer_thread_pipe[0] ||
1661  fd == timer_thread_pipe[1] ||
1662  fd == timer_thread_pipe_low[0] ||
1663  fd == timer_thread_pipe_low[1]) {
1664  return 1;
1665  }
1666  else {
1667  return 0;
1668  }
1669 #else
1670  return 0;
1671 #endif
1672 }
1673 
1676 {
1677  return pthread_self();
1678 }
1679 
1680 #endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
#define cond(node)
Definition: ripper.c:427
rb_nativethread_cond_t sleep_cond
RUBY_SYMBOL_EXPORT_BEGIN rb_nativethread_id_t rb_nativethread_self()
rb_vm_t * vm
Definition: vm_core.h:526
void rb_bug(const char *fmt,...)
Definition: error.c:327
int gettimeofday(struct timeval *, struct timezone *)
Definition: win32.c:4313
void rb_update_max_fd(int fd)
Definition: io.c:183
volatile unsigned long waiting
static int max(int a, int b)
Definition: strftime.c:141
rb_unblock_function_t * func
Definition: vm_core.h:500
#define THREAD_DEBUG
Definition: thread.c:74
rb_nativethread_cond_t switch_cond
#define CLOCK_MONOTONIC
Definition: win32.h:129
const int id
Definition: nkf.c:209
long tv_sec
Definition: ossl_asn1.c:17
int fcntl(int, int,...)
Definition: win32.c:4089
void rb_async_bug_errno(const char *mesg, int errno_arg)
Definition: error.c:371
#define STACK_UPPER(x, a, b)
Definition: gc.h:74
if((ID)(DISPID) nameid !=nameid)
Definition: win32ole.c:770
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1857
pthread_mutex_t rb_nativethread_lock_t
static volatile int system_working
Definition: thread.c:95
struct rb_thread_struct::@169 machine
time_t tv_sec
Definition: missing.h:51
sighandler_t posix_signal(int signum, sighandler_t handler)
Definition: missing-pips.c:43
unsigned long long uint64_t
Definition: sha2.h:102
fd_set rb_fdset_t
Definition: intern.h:348
#define RUBY_VM_THREAD_VM_STACK_SIZE
Definition: vm_core.h:431
void rb_thread_wakeup_timer_thread(void)
int getrlimit(int resource, struct rlimit *rlp)
Definition: missing-pips.c:48
#define F_SETFL
Definition: win32.h:622
long tv_usec
Definition: ossl_asn1.c:18
#define PRIdVALUE
Definition: ruby.h:132
long tv_nsec
Definition: missing.h:52
#define UNLIKELY(x)
Definition: vm_core.h:109
static char msg[50]
Definition: strerror.c:8
#define thread_debug
Definition: thread.c:210
int err
Definition: win32.c:114
#define EXIT_FAILURE
Definition: eval_intern.h:24
#define GVL_UNLOCK_BEGIN()
Definition: thread.c:136
rb_nativethread_cond_t cond
#define numberof(array)
Definition: etc.c:602
int errno
#define STACK_DIR_UPPER(a, b)
Definition: gc.h:82
#define CLOCK_REALTIME
Definition: win32.h:128
pthread_t rb_nativethread_id_t
#define malloc
Definition: ripper.c:96
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308
int pthread_kill(pthread_t thread, int sig)
Definition: missing-pips.c:37
#define GVL_UNLOCK_END()
Definition: thread.c:141
unsigned long VALUE
Definition: ruby.h:88
#define STACK_GROW_DIR_DETECTION
Definition: gc.h:81
static VALUE result
Definition: nkf.c:40
void Init_native_thread(void)
void rb_bug_errno(const char *mesg, int errno_arg)
Definition: error.c:350
int clock_gettime(clockid_t, struct timespec *)
Definition: win32.c:4325
void ruby_init_stack(volatile VALUE *)
rb_nativethread_cond_t switch_wait_cond
static void timer_thread_function(void *)
Definition: thread.c:3829
void rb_sys_fail(const char *mesg)
Definition: error.c:1976
int rb_reserved_fd_p(int fd)
#define WRITE_CONST(fd, str)
Definition: error.c:368
int rb_cloexec_pipe(int fildes[2])
Definition: io.c:286
#define thread_start_func_2(th, st, rst)
Definition: thread.c:214
int size
Definition: encoding.c:49
struct rb_vm_struct::@168 default_params
VALUE * stack_start
Definition: vm_core.h:621
struct rb_unblock_callback unblock
Definition: vm_core.h:589
#define rb_fd_select(n, rfds, wfds, efds, timeout)
Definition: intern.h:361
rb_nativethread_id_t thread_id
Definition: vm_core.h:561
RUBY_EXTERN char * strerror(int)
Definition: strerror.c:11
#define O_NONBLOCK
Definition: win32.h:626
struct rb_encoding_entry * list
Definition: encoding.c:47
#define ETIMEDOUT
Definition: win32.h:584
native_thread_data_t native_thread_data
Definition: vm_core.h:566
#define EWOULDBLOCK
Definition: rubysocket.h:126
size_t thread_machine_stack_size
Definition: vm_core.h:421
static VALUE thread_start(VALUE klass, VALUE args)
Definition: thread.c:713
VALUE rb_eNotImpError
Definition: error.c:558
rb_global_vm_lock_t gvl
Definition: vm_core.h:351
RUBY_SYMBOL_EXPORT_BEGIN void * alloca()
#define RUBY_VM_INTERRUPTED(th)
Definition: vm_core.h:965
rb_nativethread_lock_t interrupt_lock
Definition: vm_core.h:587
size_t stack_maxsize
Definition: vm_core.h:623
#define NULL
Definition: _sdbm.c:102
static rb_thread_t * GET_THREAD(void)
Definition: vm_core.h:929
free(psz)
VALUE rb_eThreadError
Definition: eval.c:730
#define IS_STACK_DIR_UPPER()
Definition: gc.h:84
#define SIGNED_VALUE
Definition: ruby.h:90
#define GET_VM()
Definition: vm_core.h:922