Ruby  1.9.3p547(2014-05-14revision45962)
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 #if defined(HAVE_SYS_TIME_H)
28 #include <sys/time.h>
29 #endif
30 
31 static void native_mutex_lock(pthread_mutex_t *lock);
32 static void native_mutex_unlock(pthread_mutex_t *lock);
33 static int native_mutex_trylock(pthread_mutex_t *lock);
34 static void native_mutex_initialize(pthread_mutex_t *lock);
35 static void native_mutex_destroy(pthread_mutex_t *lock);
36 static void native_cond_signal(rb_thread_cond_t *cond);
37 static void native_cond_broadcast(rb_thread_cond_t *cond);
38 static void native_cond_wait(rb_thread_cond_t *cond, pthread_mutex_t *mutex);
39 static void native_cond_initialize(rb_thread_cond_t *cond, int flags);
40 static void native_cond_destroy(rb_thread_cond_t *cond);
41 static pthread_t timer_thread_id;
42 
43 #define RB_CONDATTR_CLOCK_MONOTONIC 1
44 
45 #if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(HAVE_CLOCKID_T) && \
46  defined(CLOCK_REALTIME) && defined(CLOCK_MONOTONIC) && defined(HAVE_CLOCK_GETTIME)
47 #define USE_MONOTONIC_COND 1
48 #else
49 #define USE_MONOTONIC_COND 0
50 #endif
51 
52 static void
53 gvl_acquire_common(rb_vm_t *vm)
54 {
55  if (vm->gvl.acquired) {
56 
57  vm->gvl.waiting++;
58  if (vm->gvl.waiting == 1) {
59  /* transit to polling mode */
61  }
62 
63  while (vm->gvl.acquired) {
64  native_cond_wait(&vm->gvl.cond, &vm->gvl.lock);
65  }
66 
67  vm->gvl.waiting--;
68 
69  if (vm->gvl.need_yield) {
70  vm->gvl.need_yield = 0;
71  native_cond_signal(&vm->gvl.switch_cond);
72  }
73  }
74 
75  vm->gvl.acquired = 1;
76 }
77 
78 static void
79 gvl_acquire(rb_vm_t *vm, rb_thread_t *th)
80 {
81  native_mutex_lock(&vm->gvl.lock);
82  gvl_acquire_common(vm);
83  native_mutex_unlock(&vm->gvl.lock);
84 }
85 
86 static void
87 gvl_release_common(rb_vm_t *vm)
88 {
89  vm->gvl.acquired = 0;
90  if (vm->gvl.waiting > 0)
91  native_cond_signal(&vm->gvl.cond);
92 }
93 
94 static void
95 gvl_release(rb_vm_t *vm)
96 {
97  native_mutex_lock(&vm->gvl.lock);
98  gvl_release_common(vm);
99  native_mutex_unlock(&vm->gvl.lock);
100 }
101 
102 static void
103 gvl_yield(rb_vm_t *vm, rb_thread_t *th)
104 {
105  native_mutex_lock(&vm->gvl.lock);
106 
107  gvl_release_common(vm);
108 
109  /* An another thread is processing GVL yield. */
110  if (UNLIKELY(vm->gvl.wait_yield)) {
111  while (vm->gvl.wait_yield)
112  native_cond_wait(&vm->gvl.switch_wait_cond, &vm->gvl.lock);
113  goto acquire;
114  }
115 
116  if (vm->gvl.waiting > 0) {
117  /* Wait until another thread task take GVL. */
118  vm->gvl.need_yield = 1;
119  vm->gvl.wait_yield = 1;
120  while (vm->gvl.need_yield)
121  native_cond_wait(&vm->gvl.switch_cond, &vm->gvl.lock);
122  vm->gvl.wait_yield = 0;
123  }
124  else {
125  native_mutex_unlock(&vm->gvl.lock);
126  sched_yield();
127  native_mutex_lock(&vm->gvl.lock);
128  }
129 
130  native_cond_broadcast(&vm->gvl.switch_wait_cond);
131  acquire:
132  gvl_acquire_common(vm);
133  native_mutex_unlock(&vm->gvl.lock);
134 }
135 
136 static void
137 gvl_init(rb_vm_t *vm)
138 {
139  native_mutex_initialize(&vm->gvl.lock);
140  native_cond_initialize(&vm->gvl.cond, RB_CONDATTR_CLOCK_MONOTONIC);
141  native_cond_initialize(&vm->gvl.switch_cond, RB_CONDATTR_CLOCK_MONOTONIC);
142  native_cond_initialize(&vm->gvl.switch_wait_cond, RB_CONDATTR_CLOCK_MONOTONIC);
143  vm->gvl.acquired = 0;
144  vm->gvl.waiting = 0;
145  vm->gvl.need_yield = 0;
146  vm->gvl.wait_yield = 0;
147 }
148 
149 static void
150 gvl_destroy(rb_vm_t *vm)
151 {
152  native_cond_destroy(&vm->gvl.switch_wait_cond);
153  native_cond_destroy(&vm->gvl.switch_cond);
154  native_cond_destroy(&vm->gvl.cond);
155  native_mutex_destroy(&vm->gvl.lock);
156 }
157 
158 static void
159 gvl_atfork(rb_vm_t *vm)
160 {
161  gvl_init(vm);
162  gvl_acquire(vm, GET_THREAD());
163 }
164 
165 #define NATIVE_MUTEX_LOCK_DEBUG 0
166 
167 static void
168 mutex_debug(const char *msg, pthread_mutex_t *lock)
169 {
170  if (NATIVE_MUTEX_LOCK_DEBUG) {
171  int r;
172  static pthread_mutex_t dbglock = PTHREAD_MUTEX_INITIALIZER;
173 
174  if ((r = pthread_mutex_lock(&dbglock)) != 0) {exit(EXIT_FAILURE);}
175  fprintf(stdout, "%s: %p\n", msg, (void *)lock);
176  if ((r = pthread_mutex_unlock(&dbglock)) != 0) {exit(EXIT_FAILURE);}
177  }
178 }
179 
180 static void
181 native_mutex_lock(pthread_mutex_t *lock)
182 {
183  int r;
184  mutex_debug("lock", lock);
185  if ((r = pthread_mutex_lock(lock)) != 0) {
186  rb_bug_errno("pthread_mutex_lock", r);
187  }
188 }
189 
190 static void
191 native_mutex_unlock(pthread_mutex_t *lock)
192 {
193  int r;
194  mutex_debug("unlock", lock);
195  if ((r = pthread_mutex_unlock(lock)) != 0) {
196  rb_bug_errno("pthread_mutex_unlock", r);
197  }
198 }
199 
200 static inline int
201 native_mutex_trylock(pthread_mutex_t *lock)
202 {
203  int r;
204  mutex_debug("trylock", lock);
205  if ((r = pthread_mutex_trylock(lock)) != 0) {
206  if (r == EBUSY) {
207  return EBUSY;
208  }
209  else {
210  rb_bug_errno("pthread_mutex_trylock", r);
211  }
212  }
213  return 0;
214 }
215 
216 static void
217 native_mutex_initialize(pthread_mutex_t *lock)
218 {
219  int r = pthread_mutex_init(lock, 0);
220  mutex_debug("init", lock);
221  if (r != 0) {
222  rb_bug_errno("pthread_mutex_init", r);
223  }
224 }
225 
226 static void
227 native_mutex_destroy(pthread_mutex_t *lock)
228 {
229  int r = pthread_mutex_destroy(lock);
230  mutex_debug("destroy", lock);
231  if (r != 0) {
232  rb_bug_errno("pthread_mutex_destroy", r);
233  }
234 }
235 
236 static void
237 native_cond_initialize(rb_thread_cond_t *cond, int flags)
238 {
239  int r;
240  pthread_condattr_t attr;
241 
242  pthread_condattr_init(&attr);
243 
244 #if USE_MONOTONIC_COND
245  cond->clockid = CLOCK_REALTIME;
246  if (flags & RB_CONDATTR_CLOCK_MONOTONIC) {
247  r = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
248  if (r == 0) {
249  cond->clockid = CLOCK_MONOTONIC;
250  }
251  }
252 #endif
253 
254  r = pthread_cond_init(&cond->cond, &attr);
255  pthread_condattr_destroy(&attr);
256  if (r != 0) {
257  rb_bug_errno("pthread_cond_init", r);
258  }
259 
260  return;
261  }
262 
263 static void
264 native_cond_destroy(rb_thread_cond_t *cond)
265 {
266  int r = pthread_cond_destroy(&cond->cond);
267  if (r != 0) {
268  rb_bug_errno("pthread_cond_destroy", r);
269  }
270 }
271 
272 /*
273  * In OS X 10.7 (Lion), pthread_cond_signal and pthread_cond_broadcast return
274  * EAGAIN after retrying 8192 times. You can see them in the following page:
275  *
276  * http://www.opensource.apple.com/source/Libc/Libc-763.11/pthreads/pthread_cond.c
277  *
278  * The following native_cond_signal and native_cond_broadcast functions
279  * need to retrying until pthread functions don't return EAGAIN.
280  */
281 
282 static void
283 native_cond_signal(rb_thread_cond_t *cond)
284 {
285  int r;
286  do {
287  r = pthread_cond_signal(&cond->cond);
288  } while (r == EAGAIN);
289  if (r != 0) {
290  rb_bug_errno("pthread_cond_signal", r);
291  }
292 }
293 
294 static void
295 native_cond_broadcast(rb_thread_cond_t *cond)
296 {
297  int r;
298  do {
299  r = pthread_cond_broadcast(&cond->cond);
300  } while (r == EAGAIN);
301  if (r != 0) {
302  rb_bug_errno("native_cond_broadcast", r);
303  }
304 }
305 
306 static void
307 native_cond_wait(rb_thread_cond_t *cond, pthread_mutex_t *mutex)
308 {
309  int r = pthread_cond_wait(&cond->cond, mutex);
310  if (r != 0) {
311  rb_bug_errno("pthread_cond_wait", r);
312  }
313 }
314 
315 static int
316 native_cond_timedwait(rb_thread_cond_t *cond, pthread_mutex_t *mutex, struct timespec *ts)
317 {
318  int r;
319 
320  /*
321  * An old Linux may return EINTR. Even though POSIX says
322  * "These functions shall not return an error code of [EINTR]".
323  * http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_cond_timedwait.html
324  * Let's hide it from arch generic code.
325  */
326  do {
327  r = pthread_cond_timedwait(&cond->cond, mutex, ts);
328  } while (r == EINTR);
329 
330  if (r != 0 && r != ETIMEDOUT) {
331  rb_bug_errno("pthread_cond_timedwait", r);
332  }
333 
334  return r;
335 }
336 
337 #if SIZEOF_TIME_T == SIZEOF_LONG
338 typedef unsigned long unsigned_time_t;
339 #elif SIZEOF_TIME_T == SIZEOF_INT
340 typedef unsigned int unsigned_time_t;
341 #elif SIZEOF_TIME_T == SIZEOF_LONG_LONG
342 typedef unsigned LONG_LONG unsigned_time_t;
343 #else
344 # error cannot find integer type which size is same as time_t.
345 #endif
346 
347 #define TIMET_MAX (~(time_t)0 <= 0 ? (time_t)((~(unsigned_time_t)0) >> 1) : (time_t)(~(unsigned_time_t)0))
348 
349 static struct timespec
350 native_cond_timeout(rb_thread_cond_t *cond, struct timespec timeout_rel)
351 {
352  int ret;
353  struct timeval tv;
354  struct timespec timeout;
355  struct timespec now;
356 
357 #if USE_MONOTONIC_COND
358  if (cond->clockid == CLOCK_MONOTONIC) {
359  ret = clock_gettime(cond->clockid, &now);
360  if (ret != 0)
361  rb_sys_fail("clock_gettime()");
362  goto out;
363  }
364 
365  if (cond->clockid != CLOCK_REALTIME)
366  rb_bug("unsupported clockid %d", cond->clockid);
367 #endif
368 
369  ret = gettimeofday(&tv, 0);
370  if (ret != 0)
371  rb_sys_fail(0);
372  now.tv_sec = tv.tv_sec;
373  now.tv_nsec = tv.tv_usec * 1000;
374 
375 #if USE_MONOTONIC_COND
376  out:
377 #endif
378  timeout.tv_sec = now.tv_sec;
379  timeout.tv_nsec = now.tv_nsec;
380  timeout.tv_sec += timeout_rel.tv_sec;
381  timeout.tv_nsec += timeout_rel.tv_nsec;
382 
383  if (timeout.tv_nsec >= 1000*1000*1000) {
384  timeout.tv_sec++;
385  timeout.tv_nsec -= 1000*1000*1000;
386  }
387 
388  if (timeout.tv_sec < now.tv_sec)
389  timeout.tv_sec = TIMET_MAX;
390 
391  return timeout;
392 }
393 
394 #define native_cleanup_push pthread_cleanup_push
395 #define native_cleanup_pop pthread_cleanup_pop
396 #ifdef HAVE_SCHED_YIELD
397 #define native_thread_yield() (void)sched_yield()
398 #else
399 #define native_thread_yield() ((void)0)
400 #endif
401 
402 #if defined(SIGVTALRM) && !defined(__CYGWIN__) && !defined(__SYMBIAN32__)
403 #define USE_SIGNAL_THREAD_LIST 1
404 #endif
405 #ifdef USE_SIGNAL_THREAD_LIST
406 static void add_signal_thread_list(rb_thread_t *th);
407 static void remove_signal_thread_list(rb_thread_t *th);
408 static rb_thread_lock_t signal_thread_list_lock;
409 #endif
410 
411 static pthread_key_t ruby_native_thread_key;
412 
413 static void
414 null_func(int i)
415 {
416  /* null */
417 }
418 
419 static rb_thread_t *
420 ruby_thread_from_native(void)
421 {
422  return pthread_getspecific(ruby_native_thread_key);
423 }
424 
425 static int
426 ruby_thread_set_native(rb_thread_t *th)
427 {
428  return pthread_setspecific(ruby_native_thread_key, th) == 0;
429 }
430 
431 static void native_thread_init(rb_thread_t *th);
432 
433 void
434 Init_native_thread(void)
435 {
436  rb_thread_t *th = GET_THREAD();
437 
438  pthread_key_create(&ruby_native_thread_key, NULL);
439  th->thread_id = pthread_self();
440  native_thread_init(th);
441 #ifdef USE_SIGNAL_THREAD_LIST
442  native_mutex_initialize(&signal_thread_list_lock);
443 #endif
444  posix_signal(SIGVTALRM, null_func);
445 }
446 
447 static void
448 native_thread_init(rb_thread_t *th)
449 {
450  native_cond_initialize(&th->native_thread_data.sleep_cond, RB_CONDATTR_CLOCK_MONOTONIC);
451  ruby_thread_set_native(th);
452 }
453 
454 static void
455 native_thread_destroy(rb_thread_t *th)
456 {
457  native_cond_destroy(&th->native_thread_data.sleep_cond);
458 }
459 
460 #define USE_THREAD_CACHE 0
461 
462 #if USE_THREAD_CACHE
463 static rb_thread_t *register_cached_thread_and_wait(void);
464 #endif
465 
466 #if defined HAVE_PTHREAD_GETATTR_NP || defined HAVE_PTHREAD_ATTR_GET_NP
467 #define STACKADDR_AVAILABLE 1
468 #elif defined HAVE_PTHREAD_GET_STACKADDR_NP && defined HAVE_PTHREAD_GET_STACKSIZE_NP
469 #define STACKADDR_AVAILABLE 1
470 #elif defined HAVE_THR_STKSEGMENT || defined HAVE_PTHREAD_STACKSEG_NP
471 #define STACKADDR_AVAILABLE 1
472 #elif defined HAVE_PTHREAD_GETTHRDS_NP
473 #define STACKADDR_AVAILABLE 1
474 #endif
475 
476 #ifdef STACKADDR_AVAILABLE
477 /*
478  * Get the initial address and size of current thread's stack
479  */
480 static int
481 get_stack(void **addr, size_t *size)
482 {
483 #define CHECK_ERR(expr) \
484  {int err = (expr); if (err) return err;}
485 #ifdef HAVE_PTHREAD_GETATTR_NP /* Linux */
486  pthread_attr_t attr;
487  size_t guard = 0;
489  CHECK_ERR(pthread_getattr_np(pthread_self(), &attr));
490 # ifdef HAVE_PTHREAD_ATTR_GETSTACK
491  CHECK_ERR(pthread_attr_getstack(&attr, addr, size));
492  STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
493 # else
494  CHECK_ERR(pthread_attr_getstackaddr(&attr, addr));
495  CHECK_ERR(pthread_attr_getstacksize(&attr, size));
496 # endif
497  CHECK_ERR(pthread_attr_getguardsize(&attr, &guard));
498  *size -= guard;
499  pthread_attr_destroy(&attr);
500 #elif defined HAVE_PTHREAD_ATTR_GET_NP /* FreeBSD, DragonFly BSD, NetBSD */
501  pthread_attr_t attr;
502  CHECK_ERR(pthread_attr_init(&attr));
503  CHECK_ERR(pthread_attr_get_np(pthread_self(), &attr));
504 # ifdef HAVE_PTHREAD_ATTR_GETSTACK
505  CHECK_ERR(pthread_attr_getstack(&attr, addr, size));
506  STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
507 # else
508  CHECK_ERR(pthread_attr_getstackaddr(&attr, addr));
509  CHECK_ERR(pthread_attr_getstacksize(&attr, size));
510  STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
511 # endif
512  pthread_attr_destroy(&attr);
513 #elif (defined HAVE_PTHREAD_GET_STACKADDR_NP && defined HAVE_PTHREAD_GET_STACKSIZE_NP) /* MacOS X */
514  pthread_t th = pthread_self();
515  *addr = pthread_get_stackaddr_np(th);
516  *size = pthread_get_stacksize_np(th);
517 #elif defined HAVE_THR_STKSEGMENT || defined HAVE_PTHREAD_STACKSEG_NP
518  stack_t stk;
519 # if defined HAVE_THR_STKSEGMENT /* Solaris */
520  CHECK_ERR(thr_stksegment(&stk));
521 # else /* OpenBSD */
522  CHECK_ERR(pthread_stackseg_np(pthread_self(), &stk));
523 # endif
524  *addr = stk.ss_sp;
525  *size = stk.ss_size;
526 #elif defined HAVE_PTHREAD_GETTHRDS_NP /* AIX */
527  pthread_t th = pthread_self();
528  struct __pthrdsinfo thinfo;
529  char reg[256];
530  int regsiz=sizeof(reg);
531  CHECK_ERR(pthread_getthrds_np(&th, PTHRDSINFO_QUERY_ALL,
532  &thinfo, sizeof(thinfo),
533  &reg, &regsiz));
534  *addr = thinfo.__pi_stackaddr;
535  *size = thinfo.__pi_stacksize;
536  STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + *size));
537 #else
538 #error STACKADDR_AVAILABLE is defined but not implemented.
539 #endif
540  return 0;
541 #undef CHECK_ERR
542 }
543 #endif
544 
545 static struct {
547  size_t stack_maxsize;
548  VALUE *stack_start;
549 #ifdef __ia64
550  VALUE *register_stack_start;
551 #endif
552 } native_main_thread;
553 
554 #ifdef STACK_END_ADDRESS
555 extern void *STACK_END_ADDRESS;
556 #endif
557 
558 #undef ruby_init_stack
559 void
560 ruby_init_stack(volatile VALUE *addr
561 #ifdef __ia64
562  , void *bsp
563 #endif
564  )
565 {
566  native_main_thread.id = pthread_self();
567 #ifdef STACK_END_ADDRESS
568  native_main_thread.stack_start = STACK_END_ADDRESS;
569 #else
570  if (!native_main_thread.stack_start ||
571  STACK_UPPER((VALUE *)(void *)&addr,
572  native_main_thread.stack_start > addr,
573  native_main_thread.stack_start < addr)) {
574  native_main_thread.stack_start = (VALUE *)addr;
575  }
576 #endif
577 #ifdef __ia64
578  if (!native_main_thread.register_stack_start ||
579  (VALUE*)bsp < native_main_thread.register_stack_start) {
580  native_main_thread.register_stack_start = (VALUE*)bsp;
581  }
582 #endif
583  {
584  size_t size = 0;
585  size_t space = 0;
586 #if defined(STACKADDR_AVAILABLE)
587  void* stackaddr;
589  get_stack(&stackaddr, &size);
590  space = STACK_DIR_UPPER((char *)addr - (char *)stackaddr, (char *)stackaddr - (char *)addr);
591 #elif defined(HAVE_GETRLIMIT)
592  struct rlimit rlim;
593  if (getrlimit(RLIMIT_STACK, &rlim) == 0) {
594  size = (size_t)rlim.rlim_cur;
595  }
596  space = size > 5 * 1024 * 1024 ? 1024 * 1024 : size / 5;
597 #endif
598  native_main_thread.stack_maxsize = size - space;
599  }
600 }
601 
602 #define CHECK_ERR(expr) \
603  {int err = (expr); if (err) {rb_bug_errno(#expr, err);}}
604 
605 static int
606 native_thread_init_stack(rb_thread_t *th)
607 {
608  rb_thread_id_t curr = pthread_self();
609 
610  if (pthread_equal(curr, native_main_thread.id)) {
611  th->machine_stack_start = native_main_thread.stack_start;
612  th->machine_stack_maxsize = native_main_thread.stack_maxsize;
613  }
614  else {
615 #ifdef STACKADDR_AVAILABLE
616  void *start;
617  size_t size;
618 
619  if (get_stack(&start, &size) == 0) {
620  th->machine_stack_start = start;
622  }
623 #else
624  rb_raise(rb_eNotImpError, "ruby engine can initialize only in the main thread");
625 #endif
626  }
627 #ifdef __ia64
628  th->machine_register_stack_start = native_main_thread.register_stack_start;
629  th->machine_stack_maxsize /= 2;
630  th->machine_register_stack_maxsize = th->machine_stack_maxsize;
631 #endif
632  return 0;
633 }
634 
635 #ifndef __CYGWIN__
636 #define USE_NATIVE_THREAD_INIT 1
637 #endif
638 
639 static void *
640 thread_start_func_1(void *th_ptr)
641 {
642 #if USE_THREAD_CACHE
643  thread_start:
644 #endif
645  {
646  rb_thread_t *th = th_ptr;
647 #if !defined USE_NATIVE_THREAD_INIT
648  VALUE stack_start;
649 #endif
650 
651 #if defined USE_NATIVE_THREAD_INIT
652  native_thread_init_stack(th);
653 #endif
654  native_thread_init(th);
655  /* run */
656 #if defined USE_NATIVE_THREAD_INIT
657  thread_start_func_2(th, th->machine_stack_start, rb_ia64_bsp());
658 #else
659  thread_start_func_2(th, &stack_start, rb_ia64_bsp());
660 #endif
661  }
662 #if USE_THREAD_CACHE
663  if (1) {
664  /* cache thread */
665  rb_thread_t *th;
666  if ((th = register_cached_thread_and_wait()) != 0) {
667  th_ptr = (void *)th;
668  th->thread_id = pthread_self();
669  goto thread_start;
670  }
671  }
672 #endif
673  return 0;
674 }
675 
676 struct cached_thread_entry {
677  volatile rb_thread_t **th_area;
679  struct cached_thread_entry *next;
680 };
681 
682 
683 #if USE_THREAD_CACHE
684 static pthread_mutex_t thread_cache_lock = PTHREAD_MUTEX_INITIALIZER;
685 struct cached_thread_entry *cached_thread_root;
686 
687 static rb_thread_t *
688 register_cached_thread_and_wait(void)
689 {
690  rb_thread_cond_t cond = { PTHREAD_COND_INITIALIZER, };
691  volatile rb_thread_t *th_area = 0;
692  struct cached_thread_entry *entry =
693  (struct cached_thread_entry *)malloc(sizeof(struct cached_thread_entry));
694 
695  struct timeval tv;
696  struct timespec ts;
697  gettimeofday(&tv, 0);
698  ts.tv_sec = tv.tv_sec + 60;
699  ts.tv_nsec = tv.tv_usec * 1000;
700 
701  pthread_mutex_lock(&thread_cache_lock);
702  {
703  entry->th_area = &th_area;
704  entry->cond = &cond;
705  entry->next = cached_thread_root;
706  cached_thread_root = entry;
707 
708  native_cond_timedwait(&cond, &thread_cache_lock, &ts);
709 
710  {
711  struct cached_thread_entry *e = cached_thread_root;
712  struct cached_thread_entry *prev = cached_thread_root;
713 
714  while (e) {
715  if (e == entry) {
716  if (prev == cached_thread_root) {
717  cached_thread_root = e->next;
718  }
719  else {
720  prev->next = e->next;
721  }
722  break;
723  }
724  prev = e;
725  e = e->next;
726  }
727  }
728 
729  free(entry); /* ok */
730  native_cond_destroy(&cond);
731  }
732  pthread_mutex_unlock(&thread_cache_lock);
733 
734  return (rb_thread_t *)th_area;
735 }
736 #endif
737 
738 static int
739 use_cached_thread(rb_thread_t *th)
740 {
741  int result = 0;
742 #if USE_THREAD_CACHE
743  struct cached_thread_entry *entry;
744 
745  if (cached_thread_root) {
746  pthread_mutex_lock(&thread_cache_lock);
747  entry = cached_thread_root;
748  {
749  if (cached_thread_root) {
750  cached_thread_root = entry->next;
751  *entry->th_area = th;
752  result = 1;
753  }
754  }
755  if (result) {
756  native_cond_signal(entry->cond);
757  }
758  pthread_mutex_unlock(&thread_cache_lock);
759  }
760 #endif
761  return result;
762 }
763 
764 enum {
765 #ifdef __SYMBIAN32__
766  RUBY_STACK_MIN_LIMIT = 64 * 1024, /* 64KB: Let's be slightly more frugal on mobile platform */
767 #else
768  RUBY_STACK_MIN_LIMIT = 512 * 1024, /* 512KB */
769 #endif
770  RUBY_STACK_SPACE_LIMIT = 1024 * 1024
771 };
772 
773 #ifdef PTHREAD_STACK_MIN
774 #define RUBY_STACK_MIN ((RUBY_STACK_MIN_LIMIT < PTHREAD_STACK_MIN) ? \
775  PTHREAD_STACK_MIN * 2 : RUBY_STACK_MIN_LIMIT)
776 #else
777 #define RUBY_STACK_MIN (RUBY_STACK_MIN_LIMIT)
778 #endif
779 #define RUBY_STACK_SPACE (RUBY_STACK_MIN/5 > RUBY_STACK_SPACE_LIMIT ? \
780  RUBY_STACK_SPACE_LIMIT : RUBY_STACK_MIN/5)
781 
782 static int
783 native_thread_create(rb_thread_t *th)
784 {
785  int err = 0;
786 
787  if (use_cached_thread(th)) {
788  thread_debug("create (use cached thread): %p\n", (void *)th);
789  }
790  else {
791  pthread_attr_t attr;
792  const size_t stack_size = RUBY_STACK_MIN;
793  const size_t space = RUBY_STACK_SPACE;
794 
795  th->machine_stack_maxsize = stack_size - space;
796 #ifdef __ia64
797  th->machine_stack_maxsize /= 2;
798  th->machine_register_stack_maxsize = th->machine_stack_maxsize;
799 #endif
800 
801  CHECK_ERR(pthread_attr_init(&attr));
802 
803 #ifdef PTHREAD_STACK_MIN
804  thread_debug("create - stack size: %lu\n", (unsigned long)stack_size);
805  CHECK_ERR(pthread_attr_setstacksize(&attr, stack_size));
806 #endif
807 
808 #ifdef HAVE_PTHREAD_ATTR_SETINHERITSCHED
809  CHECK_ERR(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
810 #endif
811  CHECK_ERR(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
812 
813  err = pthread_create(&th->thread_id, &attr, thread_start_func_1, th);
814  thread_debug("create: %p (%d)\n", (void *)th, err);
815  CHECK_ERR(pthread_attr_destroy(&attr));
816  }
817  return err;
818 }
819 
820 static void
821 native_thread_join(pthread_t th)
822 {
823  int err = pthread_join(th, 0);
824  if (err) {
825  rb_raise(rb_eThreadError, "native_thread_join() failed (%d)", err);
826  }
827 }
828 
829 
830 #if USE_NATIVE_THREAD_PRIORITY
831 
832 static void
833 native_thread_apply_priority(rb_thread_t *th)
834 {
835 #if defined(_POSIX_PRIORITY_SCHEDULING) && (_POSIX_PRIORITY_SCHEDULING > 0)
836  struct sched_param sp;
837  int policy;
838  int priority = 0 - th->priority;
839  int max, min;
840  pthread_getschedparam(th->thread_id, &policy, &sp);
841  max = sched_get_priority_max(policy);
842  min = sched_get_priority_min(policy);
843 
844  if (min > priority) {
845  priority = min;
846  }
847  else if (max < priority) {
848  priority = max;
849  }
850 
851  sp.sched_priority = priority;
852  pthread_setschedparam(th->thread_id, policy, &sp);
853 #else
854  /* not touched */
855 #endif
856 }
857 
858 #endif /* USE_NATIVE_THREAD_PRIORITY */
859 
860 static void
861 ubf_pthread_cond_signal(void *ptr)
862 {
863  rb_thread_t *th = (rb_thread_t *)ptr;
864  thread_debug("ubf_pthread_cond_signal (%p)\n", (void *)th);
865  native_cond_signal(&th->native_thread_data.sleep_cond);
866 }
867 
868 static void
869 native_sleep(rb_thread_t *th, struct timeval *timeout_tv)
870 {
871  struct timespec timeout;
872  pthread_mutex_t *lock = &th->interrupt_lock;
874 
875  if (timeout_tv) {
876  struct timespec timeout_rel;
877 
878  timeout_rel.tv_sec = timeout_tv->tv_sec;
879  timeout_rel.tv_nsec = timeout_tv->tv_usec * 1000;
880 
881  /* Solaris cond_timedwait() return EINVAL if an argument is greater than
882  * current_time + 100,000,000. So cut up to 100,000,000. This is
883  * considered as a kind of spurious wakeup. The caller to native_sleep
884  * should care about spurious wakeup.
885  *
886  * See also [Bug #1341] [ruby-core:29702]
887  * http://download.oracle.com/docs/cd/E19683-01/816-0216/6m6ngupgv/index.html
888  */
889  if (timeout_rel.tv_sec > 100000000) {
890  timeout_rel.tv_sec = 100000000;
891  timeout_rel.tv_nsec = 0;
892  }
893 
894  timeout = native_cond_timeout(cond, timeout_rel);
895  }
896 
898  {
899  pthread_mutex_lock(lock);
900  th->unblock.func = ubf_pthread_cond_signal;
901  th->unblock.arg = th;
902 
903  if (RUBY_VM_INTERRUPTED(th)) {
904  /* interrupted. return immediate */
905  thread_debug("native_sleep: interrupted before sleep\n");
906  }
907  else {
908  if (!timeout_tv)
909  native_cond_wait(cond, lock);
910  else
911  native_cond_timedwait(cond, lock, &timeout);
912  }
913  th->unblock.func = 0;
914  th->unblock.arg = 0;
915 
916  pthread_mutex_unlock(lock);
917  }
918  GVL_UNLOCK_END();
919 
920  thread_debug("native_sleep done\n");
921 }
922 
923 #ifdef USE_SIGNAL_THREAD_LIST
924 struct signal_thread_list {
925  rb_thread_t *th;
926  struct signal_thread_list *prev;
927  struct signal_thread_list *next;
928 };
929 
930 static struct signal_thread_list signal_thread_list_anchor = {
931  0, 0, 0,
932 };
933 
934 #define FGLOCK(lock, body) do { \
935  native_mutex_lock(lock); \
936  { \
937  body; \
938  } \
939  native_mutex_unlock(lock); \
940 } while (0)
941 
942 #if 0 /* for debug */
943 static void
944 print_signal_list(char *str)
945 {
946  struct signal_thread_list *list =
947  signal_thread_list_anchor.next;
948  thread_debug("list (%s)> ", str);
949  while(list){
950  thread_debug("%p (%p), ", list->th, list->th->thread_id);
951  list = list->next;
952  }
953  thread_debug("\n");
954 }
955 #endif
956 
957 static void
958 add_signal_thread_list(rb_thread_t *th)
959 {
961  FGLOCK(&signal_thread_list_lock, {
962  struct signal_thread_list *list =
963  malloc(sizeof(struct signal_thread_list));
964 
965  if (list == 0) {
966  fprintf(stderr, "[FATAL] failed to allocate memory\n");
967  exit(EXIT_FAILURE);
968  }
969 
970  list->th = th;
971 
972  list->prev = &signal_thread_list_anchor;
973  list->next = signal_thread_list_anchor.next;
974  if (list->next) {
975  list->next->prev = list;
976  }
977  signal_thread_list_anchor.next = list;
979  });
980  }
981 }
982 
983 static void
984 remove_signal_thread_list(rb_thread_t *th)
985 {
987  FGLOCK(&signal_thread_list_lock, {
988  struct signal_thread_list *list =
989  (struct signal_thread_list *)
991 
992  list->prev->next = list->next;
993  if (list->next) {
994  list->next->prev = list->prev;
995  }
997  list->th = 0;
998  free(list); /* ok */
999  });
1000  }
1001 }
1002 
1003 static void
1004 ubf_select_each(rb_thread_t *th)
1005 {
1006  thread_debug("ubf_select_each (%p)\n", (void *)th->thread_id);
1007  if (th) {
1008  pthread_kill(th->thread_id, SIGVTALRM);
1009  }
1010 }
1011 
1012 static void
1013 ubf_select(void *ptr)
1014 {
1015  rb_thread_t *th = (rb_thread_t *)ptr;
1016  add_signal_thread_list(th);
1017  if (pthread_self() != timer_thread_id)
1018  rb_thread_wakeup_timer_thread(); /* activate timer thread */
1019  ubf_select_each(th);
1020 }
1021 
1022 static void
1023 ping_signal_thread_list(void) {
1024  if (signal_thread_list_anchor.next) {
1025  FGLOCK(&signal_thread_list_lock, {
1026  struct signal_thread_list *list;
1027 
1028  list = signal_thread_list_anchor.next;
1029  while (list) {
1030  ubf_select_each(list->th);
1031  list = list->next;
1032  }
1033  });
1034  }
1035 }
1036 
1037 static int
1038 check_signal_thread_list(void)
1039 {
1040  if (signal_thread_list_anchor.next)
1041  return 1;
1042  else
1043  return 0;
1044 }
1045 #else /* USE_SIGNAL_THREAD_LIST */
1046 static void add_signal_thread_list(rb_thread_t *th) { }
1047 static void remove_signal_thread_list(rb_thread_t *th) { }
1048 #define ubf_select 0
1049 static void ping_signal_thread_list(void) { return; }
1050 static int check_signal_thread_list(void) { return 0; }
1051 #endif /* USE_SIGNAL_THREAD_LIST */
1052 
1053 static int timer_thread_pipe[2] = {-1, -1};
1054 static int timer_thread_pipe_owner_process;
1055 
1056 #define TT_DEBUG 0
1057 
1058 #define WRITE_CONST(fd, str) (void)(write((fd),(str),sizeof(str)-1)<0)
1059 
1060 /* only use signal-safe system calls here */
1061 void
1063 {
1064  ssize_t result;
1065 
1066  /* already opened */
1067  if (timer_thread_pipe_owner_process == getpid()) {
1068  const char *buff = "!";
1069  retry:
1070  if ((result = write(timer_thread_pipe[1], buff, 1)) <= 0) {
1071  switch (errno) {
1072  case EINTR: goto retry;
1073  case EAGAIN:
1074 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
1075  case EWOULDBLOCK:
1076 #endif
1077  break;
1078  default:
1079  rb_async_bug_errno("rb_thread_wakeup_timer_thread - write", errno);
1080  }
1081  }
1082  if (TT_DEBUG) WRITE_CONST(2, "rb_thread_wakeup_timer_thread: write\n");
1083  }
1084  else {
1085  /* ignore wakeup */
1086  }
1087 }
1088 
1089 /* VM-dependent API is not available for this function */
1090 static void
1091 consume_communication_pipe(void)
1092 {
1093 #define CCP_READ_BUFF_SIZE 1024
1094  /* buffer can be shared because no one refers to them. */
1095  static char buff[CCP_READ_BUFF_SIZE];
1096  ssize_t result;
1097 
1098  retry:
1099  result = read(timer_thread_pipe[0], buff, CCP_READ_BUFF_SIZE);
1100  if (result < 0) {
1101  switch (errno) {
1102  case EINTR: goto retry;
1103  default:
1104  rb_async_bug_errno("consume_communication_pipe: read\n", errno);
1105  }
1106  }
1107 }
1108 
1109 static void
1110 close_communication_pipe(void)
1111 {
1112  if (close(timer_thread_pipe[0]) < 0) {
1113  rb_bug_errno("native_stop_timer_thread - close(ttp[0])", errno);
1114  }
1115  if (close(timer_thread_pipe[1]) < 0) {
1116  rb_bug_errno("native_stop_timer_thread - close(ttp[1])", errno);
1117  }
1118  timer_thread_pipe[0] = timer_thread_pipe[1] = -1;
1119 }
1120 
1121 /* 100ms. 10ms is too small for user level thread scheduling
1122  * on recent Linux (tested on 2.6.35)
1123  */
1124 #define TIME_QUANTUM_USEC (100 * 1000)
1125 
1126 static void *
1127 thread_timer(void *p)
1128 {
1130  int result;
1131  struct timeval timeout;
1132 
1133  if (TT_DEBUG) WRITE_CONST(2, "start timer thread\n");
1134 
1135  while (system_working > 0) {
1136  fd_set rfds;
1137  int need_polling;
1138 
1139  /* timer function */
1140  ping_signal_thread_list();
1142  need_polling = check_signal_thread_list();
1143 
1144  if (TT_DEBUG) WRITE_CONST(2, "tick\n");
1145 
1146  /* wait */
1147  FD_ZERO(&rfds);
1148  FD_SET(timer_thread_pipe[0], &rfds);
1149 
1150  if (gvl->waiting > 0 || need_polling) {
1151  timeout.tv_sec = 0;
1152  timeout.tv_usec = TIME_QUANTUM_USEC;
1153 
1154  /* polling (TIME_QUANTUM_USEC usec) */
1155  result = select(timer_thread_pipe[0] + 1, &rfds, 0, 0, &timeout);
1156  }
1157  else {
1158  /* wait (infinite) */
1159  result = select(timer_thread_pipe[0] + 1, &rfds, 0, 0, 0);
1160  }
1161 
1162  if (result == 0) {
1163  /* maybe timeout */
1164  }
1165  else if (result > 0) {
1166  consume_communication_pipe();
1167  }
1168  else { /* result < 0 */
1169  switch (errno) {
1170  case EBADF:
1171  case EINVAL:
1172  case ENOMEM: /* from Linux man */
1173  case EFAULT: /* from FreeBSD man */
1174  rb_async_bug_errno("thread_timer: select", errno);
1175  default:
1176  /* ignore */;
1177  }
1178  }
1179  }
1180 
1181  if (TT_DEBUG) WRITE_CONST(2, "finish timer thread\n");
1182  return NULL;
1183 }
1184 
1185 static void
1186 rb_thread_create_timer_thread(void)
1187 {
1189 
1190  if (!timer_thread_id) {
1191  pthread_attr_t attr;
1192  int err;
1193 
1194  pthread_attr_init(&attr);
1195 #ifdef PTHREAD_STACK_MIN
1196  {
1197  const size_t min_size = (4096 * 4);
1198  /* Allocate the machine stack for the timer thread
1199  * at least 16KB (4 pages). FreeBSD 8.2 AMD64 causes
1200  * machine stack overflow only with PTHREAD_STACK_MIN.
1201  */
1202  size_t stack_size = PTHREAD_STACK_MIN; /* may be dynamic, get only once */
1203  if (stack_size < min_size) stack_size = min_size;
1204  if (THREAD_DEBUG) stack_size += BUFSIZ;
1205  pthread_attr_setstacksize(&attr, stack_size);
1206  }
1207 #endif
1208 
1209  /* communication pipe with timer thread and signal handler */
1210  if (timer_thread_pipe_owner_process != getpid()) {
1211  if (timer_thread_pipe[0] != -1) {
1212  /* close pipe of parent process */
1213  close_communication_pipe();
1214  }
1215 
1216  err = pipe(timer_thread_pipe);
1217  if (err != 0) {
1218  rb_bug_errno("thread_timer: Failed to create communication pipe for timer thread", errno);
1219  }
1220  rb_update_max_fd(timer_thread_pipe[0]);
1221  rb_update_max_fd(timer_thread_pipe[1]);
1222 #if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(F_SETFL)
1223  {
1224  int oflags;
1225 #if defined(O_NONBLOCK)
1226  oflags = fcntl(timer_thread_pipe[1], F_GETFL);
1227  oflags |= O_NONBLOCK;
1228  fcntl(timer_thread_pipe[1], F_SETFL, oflags);
1229 #endif /* defined(O_NONBLOCK) */
1230 #if defined(FD_CLOEXEC)
1231  oflags = fcntl(timer_thread_pipe[0], F_GETFD);
1232  fcntl(timer_thread_pipe[0], F_SETFD, oflags | FD_CLOEXEC);
1233  oflags = fcntl(timer_thread_pipe[1], F_GETFD);
1234  fcntl(timer_thread_pipe[1], F_SETFD, oflags | FD_CLOEXEC);
1235 #endif /* defined(FD_CLOEXEC) */
1236  }
1237 #endif /* defined(HAVE_FCNTL) && defined(F_GETFL) && defined(F_SETFL) */
1238 
1239  /* validate pipe on this process */
1240  timer_thread_pipe_owner_process = getpid();
1241  }
1242 
1243  /* create timer thread */
1244  if (timer_thread_id) {
1245  rb_bug("rb_thread_create_timer_thread: Timer thread was already created\n");
1246  }
1247  err = pthread_create(&timer_thread_id, &attr, thread_timer, &GET_VM()->gvl);
1248  if (err != 0) {
1249  fprintf(stderr, "[FATAL] Failed to create timer thread (errno: %d)\n", err);
1250  exit(EXIT_FAILURE);
1251  }
1252  pthread_attr_destroy(&attr);
1253  }
1254 
1255  rb_disable_interrupt(); /* only timer thread recieve signal */
1256 }
1257 
1258 static int
1259 native_stop_timer_thread(int close_anyway)
1260 {
1261  int stopped;
1262  stopped = --system_working <= 0;
1263 
1264  if (TT_DEBUG) fprintf(stderr, "stop timer thread\n");
1265  if (stopped) {
1266  /* join */
1268  native_thread_join(timer_thread_id);
1269  if (TT_DEBUG) fprintf(stderr, "joined timer thread\n");
1270  timer_thread_id = 0;
1271 
1272  /* close communication pipe */
1273  if (close_anyway) {
1274  /* TODO: Uninstall all signal handlers or mask all signals.
1275  * This pass is cleaning phase (terminate ruby process).
1276  * To avoid such race, we skip to close communication
1277  * pipe. OS will close it at process termination.
1278  * It may not good practice, but pragmatic.
1279  * We remain it is TODO.
1280  */
1281  /* close_communication_pipe(); */
1282  }
1283  }
1284  return stopped;
1285 }
1286 
1287 static void
1288 native_reset_timer_thread(void)
1289 {
1290  if (TT_DEBUG) fprintf(stderr, "reset timer thread\n");
1291 }
1292 
1293 #ifdef HAVE_SIGALTSTACK
1294 int
1295 ruby_stack_overflowed_p(const rb_thread_t *th, const void *addr)
1296 {
1297  void *base;
1298  size_t size;
1299  const size_t water_mark = 1024 * 1024;
1301 
1302  if (th) {
1303  size = th->machine_stack_maxsize;
1304  base = (char *)th->machine_stack_start - STACK_DIR_UPPER(0, size);
1305  }
1306 #ifdef STACKADDR_AVAILABLE
1307  else if (get_stack(&base, &size) == 0) {
1308  STACK_DIR_UPPER((void)(base = (char *)base + size), (void)0);
1309  }
1310 #endif
1311  else {
1312  return 0;
1313  }
1314  size /= 5;
1315  if (size > water_mark) size = water_mark;
1316  if (IS_STACK_DIR_UPPER()) {
1317  if (size > ~(size_t)base+1) size = ~(size_t)base+1;
1318  if (addr > base && addr <= (void *)((char *)base + size)) return 1;
1319  }
1320  else {
1321  if (size > (size_t)base) size = (size_t)base;
1322  if (addr > (void *)((char *)base - size) && addr <= base) return 1;
1323  }
1324  return 0;
1325 }
1326 #endif
1327 
1328 int
1329 rb_reserved_fd_p(int fd)
1330 {
1331  if (fd == timer_thread_pipe[0] ||
1332  fd == timer_thread_pipe[1]) {
1333  return 1;
1334  }
1335  else {
1336  return 0;
1337  }
1338 }
1339 
1340 #endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
#define cond(node)
Definition: ripper.c:392
rb_thread_t * th
Definition: thread.c:4099
void rb_bug(const char *fmt,...)
Definition: error.c:265
int gettimeofday(struct timeval *, struct timezone *)
Definition: win32.c:3746
void rb_update_max_fd(int fd)
Definition: io.c:156
int i
Definition: win32ole.c:776
#define TIMET_MAX
Definition: time.c:741
volatile unsigned long waiting
static int max(int a, int b)
rb_thread_lock_t interrupt_lock
Definition: vm_core.h:441
pthread_mutex_t rb_thread_lock_t
if(len<=MAX_WORD_LENGTH &&len >=MIN_WORD_LENGTH)
Definition: name2ctype.h:23841
rb_unblock_function_t * func
Definition: vm_core.h:381
#define THREAD_DEBUG
Definition: thread.c:72
const int id
Definition: nkf.c:209
long tv_sec
Definition: ossl_asn1.c:17
rb_thread_cond_t switch_cond
int fcntl(int, int,...)
Definition: win32.c:3579
void rb_async_bug_errno(const char *mesg, int errno_arg)
Definition: error.c:309
#define STACK_UPPER(x, a, b)
Definition: gc.h:74
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1574
static volatile int system_working
Definition: thread.c:88
unsigned long unsigned_time_t
Definition: time.c:732
static int min(int a, int b)
#define GET_THREAD()
Definition: vm_core.h:690
time_t tv_sec
Definition: missing.h:45
Win32OLEIDispatch * p
Definition: win32ole.c:778
pthread_t rb_thread_id_t
sighandler_t posix_signal(int signum, sighandler_t handler)
Definition: missing-pips.c:43
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:588
long tv_usec
Definition: ossl_asn1.c:18
long tv_nsec
Definition: missing.h:46
#define UNLIKELY(x)
Definition: vm_core.h:111
static char msg[50]
Definition: strerror.c:8
#define thread_debug
Definition: thread.c:197
int err
Definition: win32.c:78
#define EXIT_FAILURE
Definition: eval_intern.h:24
VALUE * machine_stack_start
Definition: vm_core.h:463
#define GVL_UNLOCK_BEGIN()
Definition: thread.c:119
#define FD_SET(fd, set)
Definition: win32.h:592
void rb_disable_interrupt(void)
Definition: signal.c:538
rb_thread_cond_t cond
int errno
#define STACK_DIR_UPPER(a, b)
Definition: gc.h:82
rb_thread_cond_t switch_wait_cond
int pthread_kill(pthread_t thread, int sig)
Definition: missing-pips.c:37
#define GVL_UNLOCK_END()
Definition: thread.c:124
unsigned long VALUE
Definition: ruby.h:88
#define STACK_GROW_DIR_DETECTION
Definition: gc.h:81
static VALUE result
Definition: nkf.c:40
void * malloc()
void Init_native_thread(void)
void rb_bug_errno(const char *mesg, int errno_arg)
Definition: error.c:288
static void timer_thread_function(void *)
Definition: thread.c:3024
void rb_sys_fail(const char *mesg)
Definition: error.c:1671
int rb_reserved_fd_p(int fd)
void rb_enable_interrupt(void)
Definition: signal.c:550
#define WRITE_CONST(fd, str)
Definition: error.c:306
#define thread_start_func_2(th, st, rst)
Definition: thread.c:201
int size
Definition: encoding.c:51
struct rb_unblock_callback unblock
Definition: vm_core.h:442
rb_thread_cond_t sleep_cond
#define O_NONBLOCK
Definition: win32.h:589
struct rb_encoding_entry * list
Definition: encoding.c:49
#define ETIMEDOUT
Definition: win32.h:556
native_thread_data_t native_thread_data
Definition: vm_core.h:431
#define EWOULDBLOCK
Definition: rubysocket.h:89
static VALUE thread_start(VALUE klass, VALUE args)
Definition: thread.c:626
pthread_cond_t cond
VALUE rb_eNotImpError
Definition: error.c:477
rb_global_vm_lock_t gvl
Definition: vm_core.h:280
#define RUBY_VM_INTERRUPTED(th)
Definition: vm_core.h:707
size_t machine_stack_maxsize
Definition: vm_core.h:465
#define NULL
Definition: _sdbm.c:107
void ruby_init_stack(volatile VALUE *)
free(psz)
VALUE rb_eThreadError
Definition: eval.c:623
rb_thread_id_t thread_id
Definition: vm_core.h:427
#define IS_STACK_DIR_UPPER()
Definition: gc.h:84
#define GET_VM()
Definition: vm_core.h:689