From: KOSAKI Motohiro Date: 2010-02-03T21:19:16+09:00 Subject: [ruby-dev:40309] Why can't we use get_stack() 小崎です p26549で、FreeBSDではgetrlimit()ではメインスレッドのスタックサイズが取れない問題に対する対処が入りましたが、 thread_pthread.c には、知りうる限り全OSを網羅しているスタック情報取得関数get_stack()があるので、 これを使うべきじゃないかという疑問があります。 なにか不都合ありますでしょうか? ps IRCで聞かれましたがLinuxのglibcはpthread_getattr_np()でメインスレッドのスタックも正しくとれるので、 get_stack()は意図通り動きます。LinuxThradまで遡ると自信ないけど --- thread_pthread.c | 31 +++++++++++-------------------- 1 files changed, 11 insertions(+), 20 deletions(-) diff --git a/thread_pthread.c b/thread_pthread.c index d2d8b04..ca3ccff 100644 --- a/thread_pthread.c +++ b/thread_pthread.c @@ -234,10 +234,10 @@ get_stack(void **addr, size_t *size) CHECK_ERR(pthread_attr_getstacksize(&attr, size)); # endif CHECK_ERR(pthread_attr_getguardsize(&attr, &guard)); + *size -= guard; # ifndef HAVE_PTHREAD_GETATTR_NP pthread_attr_destroy(&attr); # endif - size -= guard; #elif defined HAVE_PTHREAD_GET_STACKADDR_NP && defined HAVE_PTHREAD_GET_STACKSIZE_NP pthread_t th = pthread_self(); *addr = pthread_get_stackaddr_np(th); @@ -296,14 +296,11 @@ ruby_init_stack(volatile VALUE *addr } #endif { - size_t size = 0, space = 0; -#if defined(__FreeBSD__) || defined(__DragonFly) - pthread_attr_t attr; - if (pthread_attr_init(&attr) == 0) { - if (pthread_attr_get_np(native_main_thread.id, &attr) == 0) - pthread_attr_getstacksize(&attr, &size); - pthread_attr_destroy(&attr); - } + size_t size = 0; + size_t space = 0; +#ifdef STACKADDR_AVAILABLE + void* addr; + get_stack(&addr, &size); #elif defined(HAVE_GETRLIMIT) struct rlimit rlim; if (getrlimit(RLIMIT_STACK, &rlim) == 0) { @@ -328,17 +325,11 @@ native_thread_init_stack(rb_thread_t *th) th->machine_stack_maxsize = native_main_thread.stack_maxsize; } else { -#ifdef HAVE_PTHREAD_GETATTR_NP - pthread_attr_t attr; - void *start; - CHECK_ERR(pthread_getattr_np(curr, &attr)); -# if defined HAVE_PTHREAD_ATTR_GETSTACK - CHECK_ERR(pthread_attr_getstack(&attr, &start, &th->machine_stack_maxsize)); -# elif defined HAVE_PTHREAD_ATTR_GETSTACKSIZE && defined HAVE_PTHREAD_ATTR_GETSTACKADDR - CHECK_ERR(pthread_attr_getstackaddr(&attr, &start)); - CHECK_ERR(pthread_attr_getstacksize(&attr, &th->machine_stack_maxsize)); -# endif - th->machine_stack_start = start; +#ifdef STACKADDR_AVAILABLE + if (get_stack(&start, &size) == 0) { + th->machine_stack_start = start; + th->machine_stack_maxsize = size; + } #else rb_raise(rb_eNotImpError, "ruby engine can initialize only in the main thread"); #endif -- 1.6.6