diff --git a/lib/libthr/thread/thr_init.c b/lib/libthr/thread/thr_init.c index 937d83f1eb28..9bf0e2955158 100644 --- a/lib/libthr/thread/thr_init.c +++ b/lib/libthr/thread/thr_init.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -441,6 +442,7 @@ init_main_thread(struct pthread *thread) static void init_private(void) { + struct rlimit rlim; size_t len; int mib[2]; char *env; @@ -471,6 +473,12 @@ init_private(void) len = sizeof (_usrstack); if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1) PANIC("Cannot get kern.usrstack from sysctl"); + env = getenv("LIBPTHREAD_BIGSTACK_MAIN"); + if (env != NULL) { + if (getrlimit(RLIMIT_STACK, &rlim) == -1) + PANIC("Cannot get stack rlimit"); + _thr_stack_initial = rlim.rlim_cur; + } len = sizeof(_thr_is_smp); sysctlbyname("kern.smp.cpus", &_thr_is_smp, &len, NULL, 0); _thr_is_smp = (_thr_is_smp > 1); diff --git a/lib/libthr/thread/thr_stack.c b/lib/libthr/thread/thr_stack.c index 5f8cd4ed2e35..e5d149eedea8 100644 --- a/lib/libthr/thread/thr_stack.c +++ b/lib/libthr/thread/thr_stack.c @@ -246,7 +246,10 @@ _thr_stack_alloc(struct pthread_attr *attr) THREAD_LIST_UNLOCK(curthread); } else { - /* Allocate a stack from usrstack. */ + /* + * Allocate a stack from or below usrstack, depending + * on the LIBPTHREAD_BIGSTACK_MAIN env variable. + */ if (last_stack == NULL) last_stack = _usrstack - _thr_stack_initial - _thr_guard_default;