* transcode.c (econv_init): accept Encoding object as source_encoding
[ruby-svn.git] / thread_pthread.c
blob0f8214416ab8cd26f0979e72a0f863e540906818
1 /* -*-c-*- */
2 /**********************************************************************
4 thread_pthread.c -
6 $Author$
8 Copyright (C) 2004-2007 Koichi Sasada
10 **********************************************************************/
12 #ifdef THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION
14 #include "gc.h"
16 #ifdef HAVE_SYS_RESOURCE_H
17 #include <sys/resource.h>
18 #endif
20 static void native_mutex_lock(pthread_mutex_t *lock);
21 static void native_mutex_unlock(pthread_mutex_t *lock);
22 static int native_mutex_trylock(pthread_mutex_t *lock);
23 static void native_mutex_initialize(pthread_mutex_t *lock);
24 static void native_mutex_destroy(pthread_mutex_t *lock);
26 static void native_cond_signal(pthread_cond_t *cond);
27 static void native_cond_broadcast(pthread_cond_t *cond);
28 static void native_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
29 static void native_cond_initialize(pthread_cond_t *cond);
30 static void native_cond_destroy(pthread_cond_t *cond);
32 static void
33 native_mutex_lock(pthread_mutex_t *lock)
35 int r;
36 if ((r = pthread_mutex_lock(lock)) != 0) {
37 rb_bug("pthread_mutex_lock: %d", r);
41 static void
42 native_mutex_unlock(pthread_mutex_t *lock)
44 int r;
45 if ((r = pthread_mutex_unlock(lock)) != 0) {
46 rb_bug("native_mutex_unlock return non-zero: %d", r);
50 static inline int
51 native_mutex_trylock(pthread_mutex_t *lock)
53 int r;
54 if ((r = pthread_mutex_trylock(lock)) != 0) {
55 if (r == EBUSY) {
56 return EBUSY;
58 else {
59 rb_bug("native_mutex_trylock return non-zero: %d", r);
62 return 0;
65 static void
66 native_mutex_initialize(pthread_mutex_t *lock)
68 int r = pthread_mutex_init(lock, 0);
69 if (r != 0) {
70 rb_bug("native_mutex_initialize return non-zero: %d", r);
74 static void
75 native_mutex_destroy(pthread_mutex_t *lock)
77 int r = pthread_mutex_destroy(lock);
78 if (r != 0) {
79 rb_bug("native_mutex_destroy return non-zero: %d", r);
83 static void
84 native_cond_initialize(pthread_cond_t *cond)
86 int r = pthread_cond_init(cond, 0);
87 if (r != 0) {
88 rb_bug("native_cond_initialize return non-zero: %d", r);
92 static void
93 native_cond_destroy(pthread_cond_t *cond)
95 int r = pthread_cond_destroy(cond);
96 if (r != 0) {
97 rb_bug("native_cond_destroy return non-zero: %d", r);
101 static void
102 native_cond_signal(pthread_cond_t *cond)
104 pthread_cond_signal(cond);
107 static void
108 native_cond_broadcast(pthread_cond_t *cond)
110 pthread_cond_broadcast(cond);
113 static void
114 native_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
116 pthread_cond_wait(cond, mutex);
120 #define native_cleanup_push pthread_cleanup_push
121 #define native_cleanup_pop pthread_cleanup_pop
122 #ifdef HAVE_SCHED_YIELD
123 #define native_thread_yield() (void)sched_yield()
124 #else
125 #define native_thread_yield() ((void)0)
126 #endif
128 #ifndef __CYGWIN__
129 static void add_signal_thread_list(rb_thread_t *th);
130 #endif
131 static void remove_signal_thread_list(rb_thread_t *th);
133 static rb_thread_lock_t signal_thread_list_lock;
135 static pthread_key_t ruby_native_thread_key;
137 static void
138 null_func(int i)
140 /* null */
143 static rb_thread_t *
144 ruby_thread_from_native(void)
146 return pthread_getspecific(ruby_native_thread_key);
149 static int
150 ruby_thread_set_native(rb_thread_t *th)
152 return pthread_setspecific(ruby_native_thread_key, th) == 0;
155 static void
156 Init_native_thread(void)
158 rb_thread_t *th = GET_THREAD();
160 pthread_key_create(&ruby_native_thread_key, NULL);
161 th->thread_id = pthread_self();
162 native_cond_initialize(&th->native_thread_data.sleep_cond);
163 ruby_thread_set_native(th);
164 native_mutex_initialize(&signal_thread_list_lock);
165 posix_signal(SIGVTALRM, null_func);
168 static void
169 native_thread_destroy(rb_thread_t *th)
171 pthread_mutex_destroy(&th->interrupt_lock);
172 pthread_cond_destroy(&th->native_thread_data.sleep_cond);
175 #define USE_THREAD_CACHE 0
177 static struct {
178 rb_thread_id_t id;
179 size_t stack_maxsize;
180 VALUE *stack_start;
181 #ifdef __ia64
182 VALUE *register_stack_start;
183 #endif
184 } native_main_thread;
186 #ifdef STACK_END_ADDRESS
187 extern void *STACK_END_ADDRESS;
188 #endif
190 #undef ruby_init_stack
191 void
192 ruby_init_stack(VALUE *addr
193 #ifdef __ia64
194 , void *bsp
195 #endif
198 native_main_thread.id = pthread_self();
199 #ifdef STACK_END_ADDRESS
200 native_main_thread.stack_start = STACK_END_ADDRESS;
201 #else
202 if (!native_main_thread.stack_start ||
203 STACK_UPPER(&addr,
204 native_main_thread.stack_start > addr,
205 native_main_thread.stack_start < addr)) {
206 native_main_thread.stack_start = addr;
208 #endif
209 #ifdef __ia64
210 if (!native_main_thread.register_stack_start ||
211 (VALUE*)bsp < native_main_thread.register_stack_start) {
212 native_main_thread.register_stack_start = (VALUE*)bsp;
214 #endif
215 #ifdef HAVE_GETRLIMIT
217 struct rlimit rlim;
219 if (getrlimit(RLIMIT_STACK, &rlim) == 0) {
220 unsigned int space = rlim.rlim_cur/5;
222 if (space > 1024*1024) space = 1024*1024;
223 native_main_thread.stack_maxsize = rlim.rlim_cur - space;
226 #endif
229 #define CHECK_ERR(expr) \
230 {int err = (expr); if (err) {rb_bug("err: %d - %s", err, #expr);}}
232 static int
233 native_thread_init_stack(rb_thread_t *th)
235 rb_thread_id_t curr = pthread_self();
237 if (pthread_equal(curr, native_main_thread.id)) {
238 th->machine_stack_start = native_main_thread.stack_start;
239 th->machine_stack_maxsize = native_main_thread.stack_maxsize;
241 else {
242 #ifdef HAVE_PTHREAD_GETATTR_NP
243 pthread_attr_t attr;
244 void *start;
245 CHECK_ERR(pthread_getattr_np(curr, &attr));
246 # if defined HAVE_PTHREAD_ATTR_GETSTACK
247 CHECK_ERR(pthread_attr_getstack(&attr, &start, &th->machine_stack_maxsize));
248 # elif defined HAVE_PTHREAD_ATTR_GETSTACKSIZE && defined HAVE_PTHREAD_ATTR_GETSTACKADDR
249 CHECK_ERR(pthread_attr_getstackaddr(&attr, &start));
250 CHECK_ERR(pthread_attr_getstacksize(&attr, &th->machine_stack_maxsize));
251 # endif
252 th->machine_stack_start = start;
253 #else
254 rb_raise(rb_eNotImpError, "ruby engine can initialize only in the main thread");
255 #endif
257 #ifdef __ia64
258 th->machine_register_stack_start = native_main_thread.register_stack_start;
259 th->machine_stack_maxsize /= 2;
260 th->machine_register_stack_maxsize = th->machine_stack_maxsize;
261 #endif
262 return 0;
265 static void *
266 thread_start_func_1(void *th_ptr)
268 #if USE_THREAD_CACHE
269 thread_start:
270 #endif
272 rb_thread_t *th = th_ptr;
273 VALUE stack_start;
275 /* run */
276 thread_start_func_2(th, &stack_start, rb_ia64_bsp());
278 #if USE_THREAD_CACHE
279 if (1) {
280 /* cache thread */
281 rb_thread_t *th;
282 static rb_thread_t *register_cached_thread_and_wait(void);
283 if ((th = register_cached_thread_and_wait()) != 0) {
284 th_ptr = (void *)th;
285 th->thread_id = pthread_self();
286 goto thread_start;
289 #endif
290 return 0;
293 void rb_thread_create_control_thread(void);
295 struct cached_thread_entry {
296 volatile rb_thread_t **th_area;
297 pthread_cond_t *cond;
298 struct cached_thread_entry *next;
302 #if USE_THREAD_CACHE
303 static pthread_mutex_t thread_cache_lock = PTHREAD_MUTEX_INITIALIZER;
304 struct cached_thread_entry *cached_thread_root;
306 static rb_thread_t *
307 register_cached_thread_and_wait(void)
309 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
310 volatile rb_thread_t *th_area = 0;
311 struct cached_thread_entry *entry =
312 (struct cached_thread_entry *)malloc(sizeof(struct cached_thread_entry));
314 struct timeval tv;
315 struct timespec ts;
316 gettimeofday(&tv, 0);
317 ts.tv_sec = tv.tv_sec + 60;
318 ts.tv_nsec = tv.tv_usec * 1000;
320 pthread_mutex_lock(&thread_cache_lock);
322 entry->th_area = &th_area;
323 entry->cond = &cond;
324 entry->next = cached_thread_root;
325 cached_thread_root = entry;
327 pthread_cond_timedwait(&cond, &thread_cache_lock, &ts);
330 struct cached_thread_entry *e = cached_thread_root;
331 struct cached_thread_entry *prev = cached_thread_root;
333 while (e) {
334 if (e == entry) {
335 if (prev == cached_thread_root) {
336 cached_thread_root = e->next;
338 else {
339 prev->next = e->next;
341 break;
343 prev = e;
344 e = e->next;
348 free(entry); /* ok */
349 pthread_cond_destroy(&cond);
351 pthread_mutex_unlock(&thread_cache_lock);
353 return (rb_thread_t *)th_area;
355 #endif
357 static int
358 use_cached_thread(rb_thread_t *th)
360 int result = 0;
361 #if USE_THREAD_CACHE
362 struct cached_thread_entry *entry;
364 if (cached_thread_root) {
365 pthread_mutex_lock(&thread_cache_lock);
366 entry = cached_thread_root;
368 if (cached_thread_root) {
369 cached_thread_root = entry->next;
370 *entry->th_area = th;
371 result = 1;
374 if (result) {
375 pthread_cond_signal(entry->cond);
377 pthread_mutex_unlock(&thread_cache_lock);
379 #endif
380 return result;
383 static int
384 native_thread_create(rb_thread_t *th)
386 int err = 0;
388 if (use_cached_thread(th)) {
389 thread_debug("create (use cached thread): %p\n", th);
391 else {
392 pthread_attr_t attr;
393 size_t stack_size = 512 * 1024; /* 512KB */
394 size_t space;
396 #ifdef PTHREAD_STACK_MIN
397 if (stack_size < PTHREAD_STACK_MIN) {
398 stack_size = PTHREAD_STACK_MIN * 2;
400 #endif
401 space = stack_size/5;
402 if (space > 1024*1024) space = 1024*1024;
403 th->machine_stack_maxsize = stack_size - space;
404 #ifdef __ia64
405 th->machine_stack_maxsize /= 2;
406 th->machine_register_stack_maxsize = th->machine_stack_maxsize;
407 #endif
409 CHECK_ERR(pthread_attr_init(&attr));
411 #ifdef PTHREAD_STACK_MIN
412 thread_debug("create - stack size: %lu\n", (unsigned long)stack_size);
413 CHECK_ERR(pthread_attr_setstacksize(&attr, stack_size));
414 #endif
416 #ifdef HAVE_PTHREAD_ATTR_SETINHERITSCHED
417 CHECK_ERR(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
418 #endif
419 CHECK_ERR(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
421 err = pthread_create(&th->thread_id, &attr, thread_start_func_1, th);
422 thread_debug("create: %p (%d)", th, err);
423 CHECK_ERR(pthread_attr_destroy(&attr));
425 if (!err) {
426 pthread_cond_init(&th->native_thread_data.sleep_cond, 0);
428 else {
429 st_delete_wrap(th->vm->living_threads, th->self);
430 th->status = THREAD_KILLED;
431 rb_raise(rb_eThreadError, "can't create Thread (%d)", err);
434 return err;
437 static void
438 native_thread_join(pthread_t th)
440 int err = pthread_join(th, 0);
441 if (err) {
442 rb_raise(rb_eThreadError, "native_thread_join() failed (%d)", err);
447 #if USE_NATIVE_THREAD_PRIORITY
449 static void
450 native_thread_apply_priority(rb_thread_t *th)
452 #if defined(_POSIX_PRIORITY_SCHEDULING) && (_POSIX_PRIORITY_SCHEDULING > 0)
453 struct sched_param sp;
454 int policy;
455 int priority = 0 - th->priority;
456 int max, min;
457 pthread_getschedparam(th->thread_id, &policy, &sp);
458 max = sched_get_priority_max(policy);
459 min = sched_get_priority_min(policy);
461 if (min > priority) {
462 priority = min;
464 else if (max < priority) {
465 priority = max;
468 sp.sched_priority = priority;
469 pthread_setschedparam(th->thread_id, policy, &sp);
470 #else
471 /* not touched */
472 #endif
475 #endif /* USE_NATIVE_THREAD_PRIORITY */
477 static void
478 ubf_pthread_cond_signal(void *ptr)
480 rb_thread_t *th = (rb_thread_t *)ptr;
481 thread_debug("ubf_pthread_cond_signal (%p)\n", th);
482 pthread_cond_signal(&th->native_thread_data.sleep_cond);
485 #ifndef __CYGWIN__
486 static void
487 ubf_select_each(rb_thread_t *th)
489 thread_debug("ubf_select_each (%p)\n", (void *)th->thread_id);
490 if (th) {
491 pthread_kill(th->thread_id, SIGVTALRM);
495 static void
496 ubf_select(void *ptr)
498 rb_thread_t *th = (rb_thread_t *)ptr;
499 add_signal_thread_list(th);
500 ubf_select_each(th);
502 #else
503 #define ubf_select 0
504 #endif
506 static void
507 native_sleep(rb_thread_t *th, struct timeval *tv)
509 struct timespec ts;
510 struct timeval tvn;
512 if (tv) {
513 gettimeofday(&tvn, NULL);
514 ts.tv_sec = tvn.tv_sec + tv->tv_sec;
515 ts.tv_nsec = (tvn.tv_usec + tv->tv_usec) * 1000;
516 if (ts.tv_nsec >= 1000000000){
517 ts.tv_sec += 1;
518 ts.tv_nsec -= 1000000000;
522 thread_debug("native_sleep %ld\n", tv ? tv->tv_sec : -1);
523 GVL_UNLOCK_BEGIN();
525 pthread_mutex_lock(&th->interrupt_lock);
526 th->unblock.func = ubf_pthread_cond_signal;
527 th->unblock.arg = th;
529 if (RUBY_VM_INTERRUPTED(th)) {
530 /* interrupted. return immediate */
531 thread_debug("native_sleep: interrupted before sleep\n");
533 else {
534 if (tv == 0 || ts.tv_sec < tvn.tv_sec /* overflow */ ) {
535 int r;
536 thread_debug("native_sleep: pthread_cond_wait start\n");
537 r = pthread_cond_wait(&th->native_thread_data.sleep_cond,
538 &th->interrupt_lock);
539 if (r) rb_bug("pthread_cond_wait: %d", r);
540 thread_debug("native_sleep: pthread_cond_wait end\n");
542 else {
543 int r;
544 thread_debug("native_sleep: pthread_cond_timedwait start (%ld, %ld)\n",
545 (unsigned long)ts.tv_sec, ts.tv_nsec);
546 r = pthread_cond_timedwait(&th->native_thread_data.sleep_cond,
547 &th->interrupt_lock, &ts);
548 if (r && r != ETIMEDOUT) rb_bug("pthread_cond_timedwait: %d", r);
550 thread_debug("native_sleep: pthread_cond_timedwait end (%d)\n", r);
553 th->unblock.func = 0;
554 th->unblock.arg = 0;
556 pthread_mutex_unlock(&th->interrupt_lock);
558 GVL_UNLOCK_END();
560 thread_debug("native_sleep done\n");
563 struct signal_thread_list {
564 rb_thread_t *th;
565 struct signal_thread_list *prev;
566 struct signal_thread_list *next;
569 #ifndef __CYGWIN__
570 static struct signal_thread_list signal_thread_list_anchor = {
571 0, 0, 0,
573 #endif
575 #define FGLOCK(lock, body) do { \
576 native_mutex_lock(lock); \
578 body; \
580 native_mutex_unlock(lock); \
581 } while (0)
583 #if 0 /* for debug */
584 static void
585 print_signal_list(char *str)
587 struct signal_thread_list *list =
588 signal_thread_list_anchor.next;
589 thread_debug("list (%s)> ", str);
590 while(list){
591 thread_debug("%p (%p), ", list->th, list->th->thread_id);
592 list = list->next;
594 thread_debug("\n");
596 #endif
598 #ifndef __CYGWIN__
599 static void
600 add_signal_thread_list(rb_thread_t *th)
602 if (!th->native_thread_data.signal_thread_list) {
603 FGLOCK(&signal_thread_list_lock, {
604 struct signal_thread_list *list =
605 malloc(sizeof(struct signal_thread_list));
607 if (list == 0) {
608 fprintf(stderr, "[FATAL] failed to allocate memory\n");
609 exit(1);
612 list->th = th;
614 list->prev = &signal_thread_list_anchor;
615 list->next = signal_thread_list_anchor.next;
616 if (list->next) {
617 list->next->prev = list;
619 signal_thread_list_anchor.next = list;
620 th->native_thread_data.signal_thread_list = list;
624 #endif
626 static void
627 remove_signal_thread_list(rb_thread_t *th)
629 if (th->native_thread_data.signal_thread_list) {
630 FGLOCK(&signal_thread_list_lock, {
631 struct signal_thread_list *list =
632 (struct signal_thread_list *)
633 th->native_thread_data.signal_thread_list;
635 list->prev->next = list->next;
636 if (list->next) {
637 list->next->prev = list->prev;
639 th->native_thread_data.signal_thread_list = 0;
640 list->th = 0;
641 free(list); /* ok */
644 else {
645 /* */
649 static pthread_t timer_thread_id;
651 static void *
652 thread_timer(void *dummy)
654 while (system_working) {
655 #ifdef HAVE_NANOSLEEP
656 struct timespec req, rem;
657 req.tv_sec = 0;
658 req.tv_nsec = 10 * 1000 * 1000; /* 10 ms */
659 nanosleep(&req, &rem);
660 #else
661 struct timeval tv;
662 tv.tv_sec = 0;
663 tv.tv_usec = 10000; /* 10 ms */
664 select(0, NULL, NULL, NULL, &tv);
665 #endif
666 #ifndef __CYGWIN__
667 if (signal_thread_list_anchor.next) {
668 FGLOCK(&signal_thread_list_lock, {
669 struct signal_thread_list *list;
670 list = signal_thread_list_anchor.next;
671 while (list) {
672 ubf_select_each(list->th);
673 list = list->next;
677 #endif
678 timer_thread_function(dummy);
680 return NULL;
683 static void
684 rb_thread_create_timer_thread(void)
686 rb_enable_interrupt();
688 if (!timer_thread_id) {
689 pthread_attr_t attr;
690 int err;
692 pthread_attr_init(&attr);
693 #ifdef PTHREAD_STACK_MIN
694 pthread_attr_setstacksize(&attr,
695 PTHREAD_STACK_MIN + (THREAD_DEBUG ? BUFSIZ : 0));
696 #endif
697 err = pthread_create(&timer_thread_id, &attr, thread_timer, GET_VM());
698 if (err != 0) {
699 rb_bug("rb_thread_create_timer_thread: return non-zero (%d)", err);
702 rb_disable_interrupt(); /* only timer thread recieve signal */
705 #endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */