# Why can't we use get\_stack()

**URL:** <https://www.ruby-forum.com/t/why-cant-we-use-get-stack/182830>\
**Category:** Development\
**Created:** [February 3, 2010, 1:19pm UTC](https://www.ruby-forum.com/t/why-cant-we-use-get-stack/182830 "2010-02-03T13:19:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![KOSAKI\_Motohiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e47c2d/32.png) [@KOSAKI\_Motohiro](https://www.ruby-forum.com/u/KOSAKI_Motohiro)\
**Post date:** [February 3, 2010, 1:19pm UTC](https://www.ruby-forum.com/t/why-cant-we-use-get-stack/182830/1 "2010-02-03T13:19:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KOSAKI\_Motohiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e47c2d/32.png) [@KOSAKI\_Motohiro](https://www.ruby-forum.com/u/KOSAKI_Motohiro)\
**Post date:** [February 3, 2010, 2:36pm UTC](https://www.ruby-forum.com/t/why-cant-we-use-get-stack/182830/2 "2010-02-03T14:36:36Z")

</div>

2010e$BG/e(B2e$B7ne(B3e$BF|e(B21:19 KOSAKI Motohiro  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
