00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
00013
00014 #include "gc.h"
00015
00016 #ifdef HAVE_SYS_RESOURCE_H
00017 #include <sys/resource.h>
00018 #endif
00019
00020 static void native_mutex_lock(pthread_mutex_t *lock);
00021 static void native_mutex_unlock(pthread_mutex_t *lock);
00022 static int native_mutex_trylock(pthread_mutex_t *lock);
00023 static void native_mutex_initialize(pthread_mutex_t *lock);
00024 static void native_mutex_destroy(pthread_mutex_t *lock);
00025
00026 static void native_cond_signal(pthread_cond_t *cond);
00027 static void native_cond_broadcast(pthread_cond_t *cond);
00028 static void native_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
00029 static void native_cond_initialize(pthread_cond_t *cond);
00030 static void native_cond_destroy(pthread_cond_t *cond);
00031
00032 static void
00033 native_mutex_lock(pthread_mutex_t *lock)
00034 {
00035 int r;
00036 if ((r = pthread_mutex_lock(lock)) != 0) {
00037 rb_bug_errno("pthread_mutex_lock", r);
00038 }
00039 }
00040
00041 static void
00042 native_mutex_unlock(pthread_mutex_t *lock)
00043 {
00044 int r;
00045 if ((r = pthread_mutex_unlock(lock)) != 0) {
00046 rb_bug_errno("pthread_mutex_unlock", r);
00047 }
00048 }
00049
00050 static inline int
00051 native_mutex_trylock(pthread_mutex_t *lock)
00052 {
00053 int r;
00054 if ((r = pthread_mutex_trylock(lock)) != 0) {
00055 if (r == EBUSY) {
00056 return EBUSY;
00057 }
00058 else {
00059 rb_bug_errno("pthread_mutex_trylock", r);
00060 }
00061 }
00062 return 0;
00063 }
00064
00065 static void
00066 native_mutex_initialize(pthread_mutex_t *lock)
00067 {
00068 int r = pthread_mutex_init(lock, 0);
00069 if (r != 0) {
00070 rb_bug_errno("pthread_mutex_init", r);
00071 }
00072 }
00073
00074 #define native_mutex_reinitialize_atfork(lock) (\
00075 native_mutex_unlock(lock), \
00076 native_mutex_initialize(lock), \
00077 native_mutex_lock(lock))
00078
00079 static void
00080 native_mutex_destroy(pthread_mutex_t *lock)
00081 {
00082 int r = pthread_mutex_destroy(lock);
00083 if (r != 0) {
00084 rb_bug_errno("pthread_mutex_destroy", r);
00085 }
00086 }
00087
00088 static void
00089 native_cond_initialize(pthread_cond_t *cond)
00090 {
00091 int r = pthread_cond_init(cond, 0);
00092 if (r != 0) {
00093 rb_bug_errno("pthread_cond_init", r);
00094 }
00095 }
00096
00097 static void
00098 native_cond_destroy(pthread_cond_t *cond)
00099 {
00100 int r = pthread_cond_destroy(cond);
00101 if (r != 0) {
00102 rb_bug_errno("pthread_cond_destroy", r);
00103 }
00104 }
00105
00106 static void
00107 native_cond_signal(pthread_cond_t *cond)
00108 {
00109 pthread_cond_signal(cond);
00110 }
00111
00112 static void
00113 native_cond_broadcast(pthread_cond_t *cond)
00114 {
00115 pthread_cond_broadcast(cond);
00116 }
00117
00118 static void
00119 native_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
00120 {
00121 pthread_cond_wait(cond, mutex);
00122 }
00123
00124 static int
00125 native_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, struct timespec *ts)
00126 {
00127 return pthread_cond_timedwait(cond, mutex, ts);
00128 }
00129
00130
00131 #define native_cleanup_push pthread_cleanup_push
00132 #define native_cleanup_pop pthread_cleanup_pop
00133 #ifdef HAVE_SCHED_YIELD
00134 #define native_thread_yield() (void)sched_yield()
00135 #else
00136 #define native_thread_yield() ((void)0)
00137 #endif
00138
00139 #ifndef __CYGWIN__
00140 static void add_signal_thread_list(rb_thread_t *th);
00141 #endif
00142 static void remove_signal_thread_list(rb_thread_t *th);
00143
00144 static rb_thread_lock_t signal_thread_list_lock;
00145
00146 static pthread_key_t ruby_native_thread_key;
00147
00148 static void
00149 null_func(int i)
00150 {
00151
00152 }
00153
00154 static rb_thread_t *
00155 ruby_thread_from_native(void)
00156 {
00157 return pthread_getspecific(ruby_native_thread_key);
00158 }
00159
00160 static int
00161 ruby_thread_set_native(rb_thread_t *th)
00162 {
00163 return pthread_setspecific(ruby_native_thread_key, th) == 0;
00164 }
00165
00166 void
00167 Init_native_thread(void)
00168 {
00169 rb_thread_t *th = GET_THREAD();
00170
00171 pthread_key_create(&ruby_native_thread_key, NULL);
00172 th->thread_id = pthread_self();
00173 native_cond_initialize(&th->native_thread_data.sleep_cond);
00174 ruby_thread_set_native(th);
00175 native_mutex_initialize(&signal_thread_list_lock);
00176 posix_signal(SIGVTALRM, null_func);
00177 }
00178
00179 static void
00180 native_thread_destroy(rb_thread_t *th)
00181 {
00182 pthread_mutex_destroy(&th->interrupt_lock);
00183 pthread_cond_destroy(&th->native_thread_data.sleep_cond);
00184 }
00185
00186 #define USE_THREAD_CACHE 0
00187
00188 #if STACK_GROW_DIRECTION
00189 #define STACK_GROW_DIR_DETECTION
00190 #define STACK_DIR_UPPER(a,b) STACK_UPPER(0, a, b)
00191 #else
00192 #define STACK_GROW_DIR_DETECTION VALUE stack_grow_dir_detection
00193 #define STACK_DIR_UPPER(a,b) STACK_UPPER(&stack_grow_dir_detection, a, b)
00194 #endif
00195
00196 #if defined HAVE_PTHREAD_GETATTR_NP || defined HAVE_PTHREAD_ATTR_GET_NP
00197 #define STACKADDR_AVAILABLE 1
00198 #elif defined HAVE_PTHREAD_GET_STACKADDR_NP && defined HAVE_PTHREAD_GET_STACKSIZE_NP
00199 #define STACKADDR_AVAILABLE 1
00200 #elif defined HAVE_THR_STKSEGMENT || defined HAVE_PTHREAD_STACKSEG_NP
00201 #define STACKADDR_AVAILABLE 1
00202 #elif defined HAVE_PTHREAD_GETTHRDS_NP
00203 #define STACKADDR_AVAILABLE 1
00204 #endif
00205
00206 #ifdef STACKADDR_AVAILABLE
00207 static int
00208 get_stack(void **addr, size_t *size)
00209 {
00210 #define CHECK_ERR(expr) \
00211 {int err = (expr); if (err) return err;}
00212 #if defined HAVE_PTHREAD_GETATTR_NP || defined HAVE_PTHREAD_ATTR_GET_NP
00213 pthread_attr_t attr;
00214 size_t guard = 0;
00215
00216 # ifdef HAVE_PTHREAD_GETATTR_NP
00217 CHECK_ERR(pthread_getattr_np(pthread_self(), &attr));
00218 # ifdef HAVE_PTHREAD_ATTR_GETSTACK
00219 CHECK_ERR(pthread_attr_getstack(&attr, addr, size));
00220 # else
00221 CHECK_ERR(pthread_attr_getstackaddr(&attr, addr));
00222 CHECK_ERR(pthread_attr_getstacksize(&attr, size));
00223 # endif
00224 if (pthread_attr_getguardsize(&attr, &guard) == 0) {
00225 STACK_GROW_DIR_DETECTION;
00226 STACK_DIR_UPPER((void)0, (void)(*addr = (char *)*addr + guard));
00227 *size -= guard;
00228 }
00229 # else
00230 CHECK_ERR(pthread_attr_init(&attr));
00231 CHECK_ERR(pthread_attr_get_np(pthread_self(), &attr));
00232 CHECK_ERR(pthread_attr_getstackaddr(&attr, addr));
00233 CHECK_ERR(pthread_attr_getstacksize(&attr, size));
00234 # endif
00235 CHECK_ERR(pthread_attr_getguardsize(&attr, &guard));
00236 *size -= guard;
00237 pthread_attr_destroy(&attr);
00238 #elif defined HAVE_PTHREAD_GET_STACKADDR_NP && defined HAVE_PTHREAD_GET_STACKSIZE_NP
00239 pthread_t th = pthread_self();
00240 *addr = pthread_get_stackaddr_np(th);
00241 *size = pthread_get_stacksize_np(th);
00242 #elif defined HAVE_THR_STKSEGMENT || defined HAVE_PTHREAD_STACKSEG_NP
00243 stack_t stk;
00244 # if defined HAVE_THR_STKSEGMENT
00245 CHECK_ERR(thr_stksegment(&stk));
00246 # else
00247 CHECK_ERR(pthread_stackseg_np(pthread_self(), &stk));
00248 # endif
00249 *addr = stk.ss_sp;
00250 *size = stk.ss_size;
00251 #elif defined HAVE_PTHREAD_GETTHRDS_NP
00252 pthread_t th = pthread_self();
00253 struct __pthrdsinfo thinfo;
00254 char reg[256];
00255 int regsiz=sizeof(reg);
00256 CHECK_ERR(pthread_getthrds_np(&th, PTHRDSINFO_QUERY_ALL,
00257 &thinfo, sizeof(thinfo),
00258 ®, ®siz));
00259 *addr = thinfo.__pi_stackaddr;
00260 *size = thinfo.__pi_stacksize;
00261 #endif
00262 return 0;
00263 #undef CHECK_ERR
00264 }
00265 #endif
00266
00267 static struct {
00268 rb_thread_id_t id;
00269 size_t stack_maxsize;
00270 VALUE *stack_start;
00271 #ifdef __ia64
00272 VALUE *register_stack_start;
00273 #endif
00274 } native_main_thread;
00275
00276 #ifdef STACK_END_ADDRESS
00277 extern void *STACK_END_ADDRESS;
00278 #endif
00279
00280 #undef ruby_init_stack
00281 void
00282 ruby_init_stack(volatile VALUE *addr
00283 #ifdef __ia64
00284 , void *bsp
00285 #endif
00286 )
00287 {
00288 native_main_thread.id = pthread_self();
00289 #ifdef STACK_END_ADDRESS
00290 native_main_thread.stack_start = STACK_END_ADDRESS;
00291 #else
00292 if (!native_main_thread.stack_start ||
00293 STACK_UPPER((VALUE *)(void *)&addr,
00294 native_main_thread.stack_start > addr,
00295 native_main_thread.stack_start < addr)) {
00296 native_main_thread.stack_start = (VALUE *)addr;
00297 }
00298 #endif
00299 #ifdef __ia64
00300 if (!native_main_thread.register_stack_start ||
00301 (VALUE*)bsp < native_main_thread.register_stack_start) {
00302 native_main_thread.register_stack_start = (VALUE*)bsp;
00303 }
00304 #endif
00305 {
00306 size_t size = 0;
00307 size_t space = 0;
00308 #if defined(HAVE_PTHREAD_ATTR_GET_NP)
00309 void* addr;
00310 get_stack(&addr, &size);
00311 #elif defined(HAVE_GETRLIMIT)
00312 struct rlimit rlim;
00313 if (getrlimit(RLIMIT_STACK, &rlim) == 0) {
00314 size = (size_t)rlim.rlim_cur;
00315 }
00316 #endif
00317 space = size > 5 * 1024 * 1024 ? 1024 * 1024 : size / 5;
00318 native_main_thread.stack_maxsize = size - space;
00319 }
00320 }
00321
00322 #define CHECK_ERR(expr) \
00323 {int err = (expr); if (err) {rb_bug_errno(#expr, err);}}
00324
00325 static int
00326 native_thread_init_stack(rb_thread_t *th)
00327 {
00328 rb_thread_id_t curr = pthread_self();
00329
00330 if (pthread_equal(curr, native_main_thread.id)) {
00331 th->machine_stack_start = native_main_thread.stack_start;
00332 th->machine_stack_maxsize = native_main_thread.stack_maxsize;
00333 }
00334 else {
00335 #ifdef STACKADDR_AVAILABLE
00336 void *start;
00337 size_t size;
00338
00339 if (get_stack(&start, &size) == 0) {
00340 th->machine_stack_start = start;
00341 th->machine_stack_maxsize = size;
00342 }
00343 #else
00344 rb_raise(rb_eNotImpError, "ruby engine can initialize only in the main thread");
00345 #endif
00346 }
00347 #ifdef __ia64
00348 th->machine_register_stack_start = native_main_thread.register_stack_start;
00349 th->machine_stack_maxsize /= 2;
00350 th->machine_register_stack_maxsize = th->machine_stack_maxsize;
00351 #endif
00352 return 0;
00353 }
00354
00355 static void *
00356 thread_start_func_1(void *th_ptr)
00357 {
00358 #if USE_THREAD_CACHE
00359 thread_start:
00360 #endif
00361 {
00362 rb_thread_t *th = th_ptr;
00363 VALUE stack_start;
00364
00365 #ifndef __CYGWIN__
00366 native_thread_init_stack(th);
00367 #endif
00368
00369 thread_start_func_2(th, &stack_start, rb_ia64_bsp());
00370 }
00371 #if USE_THREAD_CACHE
00372 if (1) {
00373
00374 rb_thread_t *th;
00375 static rb_thread_t *register_cached_thread_and_wait(void);
00376 if ((th = register_cached_thread_and_wait()) != 0) {
00377 th_ptr = (void *)th;
00378 th->thread_id = pthread_self();
00379 goto thread_start;
00380 }
00381 }
00382 #endif
00383 return 0;
00384 }
00385
00386 void rb_thread_create_control_thread(void);
00387
00388 struct cached_thread_entry {
00389 volatile rb_thread_t **th_area;
00390 pthread_cond_t *cond;
00391 struct cached_thread_entry *next;
00392 };
00393
00394
00395 #if USE_THREAD_CACHE
00396 static pthread_mutex_t thread_cache_lock = PTHREAD_MUTEX_INITIALIZER;
00397 struct cached_thread_entry *cached_thread_root;
00398
00399 static rb_thread_t *
00400 register_cached_thread_and_wait(void)
00401 {
00402 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
00403 volatile rb_thread_t *th_area = 0;
00404 struct cached_thread_entry *entry =
00405 (struct cached_thread_entry *)malloc(sizeof(struct cached_thread_entry));
00406
00407 struct timeval tv;
00408 struct timespec ts;
00409 gettimeofday(&tv, 0);
00410 ts.tv_sec = tv.tv_sec + 60;
00411 ts.tv_nsec = tv.tv_usec * 1000;
00412
00413 pthread_mutex_lock(&thread_cache_lock);
00414 {
00415 entry->th_area = &th_area;
00416 entry->cond = &cond;
00417 entry->next = cached_thread_root;
00418 cached_thread_root = entry;
00419
00420 pthread_cond_timedwait(&cond, &thread_cache_lock, &ts);
00421
00422 {
00423 struct cached_thread_entry *e = cached_thread_root;
00424 struct cached_thread_entry *prev = cached_thread_root;
00425
00426 while (e) {
00427 if (e == entry) {
00428 if (prev == cached_thread_root) {
00429 cached_thread_root = e->next;
00430 }
00431 else {
00432 prev->next = e->next;
00433 }
00434 break;
00435 }
00436 prev = e;
00437 e = e->next;
00438 }
00439 }
00440
00441 free(entry);
00442 pthread_cond_destroy(&cond);
00443 }
00444 pthread_mutex_unlock(&thread_cache_lock);
00445
00446 return (rb_thread_t *)th_area;
00447 }
00448 #endif
00449
00450 static int
00451 use_cached_thread(rb_thread_t *th)
00452 {
00453 int result = 0;
00454 #if USE_THREAD_CACHE
00455 struct cached_thread_entry *entry;
00456
00457 if (cached_thread_root) {
00458 pthread_mutex_lock(&thread_cache_lock);
00459 entry = cached_thread_root;
00460 {
00461 if (cached_thread_root) {
00462 cached_thread_root = entry->next;
00463 *entry->th_area = th;
00464 result = 1;
00465 }
00466 }
00467 if (result) {
00468 pthread_cond_signal(entry->cond);
00469 }
00470 pthread_mutex_unlock(&thread_cache_lock);
00471 }
00472 #endif
00473 return result;
00474 }
00475
00476 enum {
00477 #ifdef __SYMBIAN32__
00478 RUBY_STACK_MIN_LIMIT = 64 * 1024,
00479 #else
00480 RUBY_STACK_MIN_LIMIT = 512 * 1024,
00481 #endif
00482 RUBY_STACK_SPACE_LIMIT = 1024 * 1024
00483 };
00484
00485 #ifdef PTHREAD_STACK_MIN
00486 #define RUBY_STACK_MIN ((RUBY_STACK_MIN_LIMIT < PTHREAD_STACK_MIN) ? \
00487 PTHREAD_STACK_MIN * 2 : RUBY_STACK_MIN_LIMIT)
00488 #else
00489 #define RUBY_STACK_MIN (RUBY_STACK_MIN_LIMIT)
00490 #endif
00491 #define RUBY_STACK_SPACE (RUBY_STACK_MIN/5 > RUBY_STACK_SPACE_LIMIT ? \
00492 RUBY_STACK_SPACE_LIMIT : RUBY_STACK_MIN/5)
00493
00494 static int
00495 native_thread_create(rb_thread_t *th)
00496 {
00497 int err = 0;
00498
00499 if (use_cached_thread(th)) {
00500 thread_debug("create (use cached thread): %p\n", (void *)th);
00501 }
00502 else {
00503 pthread_attr_t attr;
00504 const size_t stack_size = RUBY_STACK_MIN;
00505 const size_t space = RUBY_STACK_SPACE;
00506
00507 th->machine_stack_maxsize = stack_size - space;
00508 #ifdef __ia64
00509 th->machine_stack_maxsize /= 2;
00510 th->machine_register_stack_maxsize = th->machine_stack_maxsize;
00511 #endif
00512
00513 CHECK_ERR(pthread_attr_init(&attr));
00514
00515 #ifdef PTHREAD_STACK_MIN
00516 thread_debug("create - stack size: %lu\n", (unsigned long)stack_size);
00517 CHECK_ERR(pthread_attr_setstacksize(&attr, stack_size));
00518 #endif
00519
00520 #ifdef HAVE_PTHREAD_ATTR_SETINHERITSCHED
00521 CHECK_ERR(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
00522 #endif
00523 CHECK_ERR(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
00524
00525 err = pthread_create(&th->thread_id, &attr, thread_start_func_1, th);
00526 thread_debug("create: %p (%d)", (void *)th, err);
00527 CHECK_ERR(pthread_attr_destroy(&attr));
00528
00529 if (!err) {
00530 pthread_cond_init(&th->native_thread_data.sleep_cond, 0);
00531 }
00532 }
00533 return err;
00534 }
00535
00536 static void
00537 native_thread_join(pthread_t th)
00538 {
00539 int err = pthread_join(th, 0);
00540 if (err) {
00541 rb_raise(rb_eThreadError, "native_thread_join() failed (%d)", err);
00542 }
00543 }
00544
00545
00546 #if USE_NATIVE_THREAD_PRIORITY
00547
00548 static void
00549 native_thread_apply_priority(rb_thread_t *th)
00550 {
00551 #if defined(_POSIX_PRIORITY_SCHEDULING) && (_POSIX_PRIORITY_SCHEDULING > 0)
00552 struct sched_param sp;
00553 int policy;
00554 int priority = 0 - th->priority;
00555 int max, min;
00556 pthread_getschedparam(th->thread_id, &policy, &sp);
00557 max = sched_get_priority_max(policy);
00558 min = sched_get_priority_min(policy);
00559
00560 if (min > priority) {
00561 priority = min;
00562 }
00563 else if (max < priority) {
00564 priority = max;
00565 }
00566
00567 sp.sched_priority = priority;
00568 pthread_setschedparam(th->thread_id, policy, &sp);
00569 #else
00570
00571 #endif
00572 }
00573
00574 #endif
00575
00576 static void
00577 ubf_pthread_cond_signal(void *ptr)
00578 {
00579 rb_thread_t *th = (rb_thread_t *)ptr;
00580 thread_debug("ubf_pthread_cond_signal (%p)\n", (void *)th);
00581 pthread_cond_signal(&th->native_thread_data.sleep_cond);
00582 }
00583
00584 #if !defined(__CYGWIN__) && !defined(__SYMBIAN32__)
00585 static void
00586 ubf_select_each(rb_thread_t *th)
00587 {
00588 thread_debug("ubf_select_each (%p)\n", (void *)th->thread_id);
00589 if (th) {
00590 pthread_kill(th->thread_id, SIGVTALRM);
00591 }
00592 }
00593
00594 static void
00595 ubf_select(void *ptr)
00596 {
00597 rb_thread_t *th = (rb_thread_t *)ptr;
00598 add_signal_thread_list(th);
00599 ubf_select_each(th);
00600 }
00601 #else
00602 #define ubf_select 0
00603 #endif
00604
00605 #define PER_NANO 1000000000
00606
00607 static void
00608 native_sleep(rb_thread_t *th, struct timeval *tv)
00609 {
00610 struct timespec ts;
00611 struct timeval tvn;
00612
00613 if (tv) {
00614 gettimeofday(&tvn, NULL);
00615 ts.tv_sec = tvn.tv_sec + tv->tv_sec;
00616 ts.tv_nsec = (tvn.tv_usec + tv->tv_usec) * 1000;
00617 if (ts.tv_nsec >= PER_NANO){
00618 ts.tv_sec += 1;
00619 ts.tv_nsec -= PER_NANO;
00620 }
00621 }
00622
00623 thread_debug("native_sleep %ld\n", tv ? tv->tv_sec : -1);
00624 GVL_UNLOCK_BEGIN();
00625 {
00626 pthread_mutex_lock(&th->interrupt_lock);
00627 th->unblock.func = ubf_pthread_cond_signal;
00628 th->unblock.arg = th;
00629
00630 if (RUBY_VM_INTERRUPTED(th)) {
00631
00632 thread_debug("native_sleep: interrupted before sleep\n");
00633 }
00634 else {
00635 if (tv == 0 || ts.tv_sec < tvn.tv_sec ) {
00636 int r;
00637 thread_debug("native_sleep: pthread_cond_wait start\n");
00638 r = pthread_cond_wait(&th->native_thread_data.sleep_cond,
00639 &th->interrupt_lock);
00640 if (r) rb_bug_errno("pthread_cond_wait", r);
00641 thread_debug("native_sleep: pthread_cond_wait end\n");
00642 }
00643 else {
00644 int r;
00645 thread_debug("native_sleep: pthread_cond_timedwait start (%ld, %ld)\n",
00646 (unsigned long)ts.tv_sec, ts.tv_nsec);
00647 r = pthread_cond_timedwait(&th->native_thread_data.sleep_cond,
00648 &th->interrupt_lock, &ts);
00649 if (r && r != ETIMEDOUT) rb_bug_errno("pthread_cond_timedwait", r);
00650
00651 thread_debug("native_sleep: pthread_cond_timedwait end (%d)\n", r);
00652 }
00653 }
00654 th->unblock.func = 0;
00655 th->unblock.arg = 0;
00656
00657 pthread_mutex_unlock(&th->interrupt_lock);
00658 }
00659 GVL_UNLOCK_END();
00660
00661 thread_debug("native_sleep done\n");
00662 }
00663
00664 struct signal_thread_list {
00665 rb_thread_t *th;
00666 struct signal_thread_list *prev;
00667 struct signal_thread_list *next;
00668 };
00669
00670 #ifndef __CYGWIN__
00671 static struct signal_thread_list signal_thread_list_anchor = {
00672 0, 0, 0,
00673 };
00674 #endif
00675
00676 #define FGLOCK(lock, body) do { \
00677 native_mutex_lock(lock); \
00678 { \
00679 body; \
00680 } \
00681 native_mutex_unlock(lock); \
00682 } while (0)
00683
00684 #if 0
00685 static void
00686 print_signal_list(char *str)
00687 {
00688 struct signal_thread_list *list =
00689 signal_thread_list_anchor.next;
00690 thread_debug("list (%s)> ", str);
00691 while(list){
00692 thread_debug("%p (%p), ", list->th, list->th->thread_id);
00693 list = list->next;
00694 }
00695 thread_debug("\n");
00696 }
00697 #endif
00698
00699 #ifndef __CYGWIN__
00700 static void
00701 add_signal_thread_list(rb_thread_t *th)
00702 {
00703 if (!th->native_thread_data.signal_thread_list) {
00704 FGLOCK(&signal_thread_list_lock, {
00705 struct signal_thread_list *list =
00706 malloc(sizeof(struct signal_thread_list));
00707
00708 if (list == 0) {
00709 fprintf(stderr, "[FATAL] failed to allocate memory\n");
00710 exit(1);
00711 }
00712
00713 list->th = th;
00714
00715 list->prev = &signal_thread_list_anchor;
00716 list->next = signal_thread_list_anchor.next;
00717 if (list->next) {
00718 list->next->prev = list;
00719 }
00720 signal_thread_list_anchor.next = list;
00721 th->native_thread_data.signal_thread_list = list;
00722 });
00723 }
00724 }
00725 #endif
00726
00727 static void
00728 remove_signal_thread_list(rb_thread_t *th)
00729 {
00730 if (th->native_thread_data.signal_thread_list) {
00731 FGLOCK(&signal_thread_list_lock, {
00732 struct signal_thread_list *list =
00733 (struct signal_thread_list *)
00734 th->native_thread_data.signal_thread_list;
00735
00736 list->prev->next = list->next;
00737 if (list->next) {
00738 list->next->prev = list->prev;
00739 }
00740 th->native_thread_data.signal_thread_list = 0;
00741 list->th = 0;
00742 free(list);
00743 });
00744 }
00745 else {
00746
00747 }
00748 }
00749
00750 static pthread_t timer_thread_id;
00751 static pthread_cond_t timer_thread_cond = PTHREAD_COND_INITIALIZER;
00752 static pthread_mutex_t timer_thread_lock = PTHREAD_MUTEX_INITIALIZER;
00753
00754 static struct timespec *
00755 get_ts(struct timespec *ts, unsigned long nsec)
00756 {
00757 struct timeval tv;
00758 gettimeofday(&tv, 0);
00759 ts->tv_sec = tv.tv_sec;
00760 ts->tv_nsec = tv.tv_usec * 1000 + nsec;
00761 if (ts->tv_nsec >= PER_NANO) {
00762 ts->tv_sec++;
00763 ts->tv_nsec -= PER_NANO;
00764 }
00765 return ts;
00766 }
00767
00768 static void *
00769 thread_timer(void *dummy)
00770 {
00771 struct timespec ts;
00772
00773 native_mutex_lock(&timer_thread_lock);
00774 native_cond_broadcast(&timer_thread_cond);
00775 #define WAIT_FOR_10MS() native_cond_timedwait(&timer_thread_cond, &timer_thread_lock, get_ts(&ts, PER_NANO/100))
00776 while (system_working > 0) {
00777 int err = WAIT_FOR_10MS();
00778 if (err == ETIMEDOUT);
00779 else if (err == 0 || err == EINTR) {
00780 if (rb_signal_buff_size() == 0) break;
00781 }
00782 else rb_bug_errno("thread_timer/timedwait", err);
00783
00784 #if !defined(__CYGWIN__) && !defined(__SYMBIAN32__)
00785 if (signal_thread_list_anchor.next) {
00786 FGLOCK(&signal_thread_list_lock, {
00787 struct signal_thread_list *list;
00788 list = signal_thread_list_anchor.next;
00789 while (list) {
00790 ubf_select_each(list->th);
00791 list = list->next;
00792 }
00793 });
00794 }
00795 #endif
00796 timer_thread_function(dummy);
00797 }
00798 native_mutex_unlock(&timer_thread_lock);
00799 return NULL;
00800 }
00801
00802 static void
00803 rb_thread_create_timer_thread(void)
00804 {
00805 rb_enable_interrupt();
00806
00807 if (!timer_thread_id) {
00808 pthread_attr_t attr;
00809 int err;
00810
00811 pthread_attr_init(&attr);
00812 #ifdef PTHREAD_STACK_MIN
00813 pthread_attr_setstacksize(&attr,
00814 PTHREAD_STACK_MIN + (THREAD_DEBUG ? BUFSIZ : 0));
00815 #endif
00816 native_mutex_lock(&timer_thread_lock);
00817 err = pthread_create(&timer_thread_id, &attr, thread_timer, 0);
00818 if (err != 0) {
00819 native_mutex_unlock(&timer_thread_lock);
00820 fprintf(stderr, "[FATAL] Failed to create timer thread (errno: %d)\n", err);
00821 exit(EXIT_FAILURE);
00822 }
00823 native_cond_wait(&timer_thread_cond, &timer_thread_lock);
00824 native_mutex_unlock(&timer_thread_lock);
00825 }
00826 rb_disable_interrupt();
00827 }
00828
00829 static int
00830 native_stop_timer_thread(void)
00831 {
00832 int stopped;
00833 native_mutex_lock(&timer_thread_lock);
00834 stopped = --system_working <= 0;
00835 if (stopped) {
00836 native_cond_signal(&timer_thread_cond);
00837 }
00838 native_mutex_unlock(&timer_thread_lock);
00839 if (stopped) {
00840 native_thread_join(timer_thread_id);
00841 }
00842 return stopped;
00843 }
00844
00845 static void
00846 native_reset_timer_thread(void)
00847 {
00848 timer_thread_id = 0;
00849 }
00850
00851 #ifdef HAVE_SIGALTSTACK
00852 int
00853 ruby_stack_overflowed_p(const rb_thread_t *th, const void *addr)
00854 {
00855 void *base;
00856 size_t size;
00857 const size_t water_mark = 1024 * 1024;
00858 STACK_GROW_DIR_DETECTION;
00859
00860 if (th) {
00861 size = th->machine_stack_maxsize;
00862 base = (char *)th->machine_stack_start - STACK_DIR_UPPER(0, size);
00863 }
00864 #ifdef STACKADDR_AVAILABLE
00865 else if (get_stack(&base, &size) == 0) {
00866 STACK_DIR_UPPER((void)(base = (char *)base + size), (void)0);
00867 }
00868 #endif
00869 else {
00870 return 0;
00871 }
00872 size /= 5;
00873 if (size > water_mark) size = water_mark;
00874 if (STACK_DIR_UPPER(1, 0)) {
00875 if (size > ~(size_t)base+1) size = ~(size_t)base+1;
00876 if (addr > base && addr <= (void *)((char *)base + size)) return 1;
00877 }
00878 else {
00879 if (size > (size_t)base) size = (size_t)base;
00880 if (addr > (void *)((char *)base - size) && addr <= base) return 1;
00881 }
00882 return 0;
00883 }
00884 #endif
00885
00886 #endif
00887