Why can't we use get_stack()

e$B>.:j$G$9e(B

p26549e$B$G!"e(BFreeBSDe$B$G$Oe(Bgetrlimit()e$B$G$O%a%$%s%9%l%C%I$N%9%?%C%/%5%$%:$,<h$l$J$$LdBj$KBP$9$kBP=h$,F~$j$^$7$?$,!"e(B
thread_pthread.c
e$B$K$O!“CN$j$&$k8B$jA4e(BOSe$B$rLVMe$7$F$$$k%9%?%C%/>pJs<hF@4X?te(Bget_stack()e$B$,$”$k$N$G!“e(B
e$B$3$l$r;H$&$Y$-$8$c$J$$$+$H$$$&5?Ld$,$”$j$^$9!#e(B

e$B$J$K$+ITET9g$"$j$^$9$G$7$g$&$+!)e(B

ps
IRCe$B$GJ9$+$l$^$7$?$,e(BLinuxe$B$Ne(Bglibce$B$Oe(Bpthread_getattr_np()e$B$G%a%$%s%9%l%C%I$N%9%?%C%/$b@5$7$/$H$l$k$N$G!"e(B
get_stack()e$B$O0U?^DL$jF0$-$^$9!#e(BLinuxThrade$B$^$GAL$k$H<+?.$J$$$1$Ie(B


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

2010e$BG/e(B2e$B7ne(B3e$BF|e(B21:19 KOSAKI Motohiro
[email protected]:

e$B>.:j$G$9e(B

p26549e$B$G!"e(BFreeBSDe$B$G$Oe(Bgetrlimit()e$B$G$O%a%$%s%9%l%C%I$N%9%?%C%/%5%$%:$,<h$l$J$$LdBj$KBP$9$kBP=h$,F~$j$^$7$?$,!"e(B
thread_pthread.c e$B$K$O!“CN$j$&$k8B$jA4e(BOSe$B$rLVMe$7$F$$$k%9%?%C%/>pJs<hF@4X?te(Bget_stack()e$B$,$”$k$N$G!“e(B
e$B$3$l$r;H$&$Y$-$8$c$J$$$+$H$$$&5?Ld$,$”$j$^$9!#e(B

e$B$J$K$+ITET9g$"$j$^$9$G$7$g$&$+!)e(B

ps IRCe$B$GJ9$+$l$^$7$?$,e(BLinuxe$B$Ne(Bglibce$B$Oe(Bpthread_getattr_np()e$B$G%a%$%s%9%l%C%I$N%9%?%C%/$b@5$7$/$H$l$k$N$G!"e(B
get_stack()e$B$O0U?^DL$jF0$-$^$9!#e(BLinuxThrade$B$^$GAL$k$H<+?.$J$$$1$Ie(B

e$B8=>u!"e(Bconfiguree$B$,e(BHAVE_PTHREAD_GETATTR_NP
e$B$r@dBPe(BONe$B$K$7$J$$$N$G!"e(Bget_stack()e$B$Oe(B
e$BDj5A$5$l$J$$$H$$$&;XE&$r$$$?$@$-$^$7$?e(B(*)
e$B=$@5$7$?%Q%C%A$rAw$j$^$9!#e(B

(*) e$B$9$G$K;H$o$l$F$$$k2U=j$,$“$k$N$K!”$J$s$G$=$&$J$k$Ne(B(T_T)


configure.in | 5 +++±
thread_pthread.c | 32 ++++++++++++±------------------
2 files changed, 17 insertions(+), 20 deletions(-)

diff --git a/configure.in b/configure.in
index da63857…9b51b80 100644
— a/configure.in
+++ b/configure.in
@@ -1507,7 +1507,10 @@ if test x"$enable_pthread" = xyes; then
else
AC_MSG_WARN(“Don’t know how to find pthread library on your system
– thread support disabled”)
fi

  • AC_CHECK_FUNCS(nanosleep sched_yield pthread_attr_setinheritsched)
  • AC_CHECK_FUNCS(nanosleep sched_yield pthread_attr_setinheritsched \
  • pthread_getattr_np pthread_attr_get_np \
  • pthread_get_stackaddr_np pthread_get_stacksize_np \
  • thr_stksegment pthread_stackseg_np)
    if test x"$ac_cv_func_nanosleep" = xno; then
    AC_CHECK_LIB(rt, nanosleep)
    if test x"$ac_cv_lib_rt_nanosleep" = xyes; then
    diff --git a/thread_pthread.c b/thread_pthread.c
    index d2d8b04…da97371 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,14 @@ 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;
    +#ifdef STACKADDR_AVAILABLE
    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;
  • size_t size;
  • 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