From 50af69e8f326b2762a44d5fea2b118e7616e5d20 Mon Sep 17 00:00:00 2001 From: Brian Carlstrom Date: Fri, 13 Sep 2013 16:34:43 -0700 Subject: [PATCH] Simplify main thread stack size initialization Change-Id: Iec09433d9de501031cce09dc75848a5e8f3d96bf --- libc/bionic/libc_init_common.cpp | 20 ++++++++------------ libc/bionic/pthread_attr.cpp | 8 +------- libc/bionic/pthread_internal.h | 9 +++++++++ 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp index c10abadf7..f5cb1e53b 100644 --- a/libc/bionic/libc_init_common.cpp +++ b/libc/bionic/libc_init_common.cpp @@ -64,19 +64,15 @@ uintptr_t __stack_chk_guard = 0; unsigned int __page_size = PAGE_SIZE; unsigned int __page_shift = PAGE_SHIFT; -static size_t get_stack_size() { - const size_t minimal_stack_size = 128 * 1024; - size_t stack_size = minimal_stack_size; - struct rlimit stack_limit; +static size_t get_main_thread_stack_size() { + rlimit stack_limit; int rlimit_result = getrlimit(RLIMIT_STACK, &stack_limit); - if ((rlimit_result == 0) && (stack_limit.rlim_cur != RLIM_INFINITY)) { - stack_size = stack_limit.rlim_cur; - stack_size = (stack_size & ~(PAGE_SIZE - 1)); - if (stack_size < minimal_stack_size) { - stack_size = minimal_stack_size; - } + if ((rlimit_result == 0) && + (stack_limit.rlim_cur != RLIM_INFINITY) && + (stack_limit.rlim_cur > PTHREAD_STACK_MIN)) { + return (stack_limit.rlim_cur & ~(PAGE_SIZE - 1)); } - return stack_size; + return PTHREAD_STACK_SIZE_DEFAULT; } /* Init TLS for the initial thread. Called by the linker _before_ libc is mapped @@ -94,7 +90,7 @@ void __libc_init_tls(KernelArgumentBlock& args) { __libc_auxv = args.auxv; uintptr_t stack_top = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE; - size_t stack_size = get_stack_size(); + size_t stack_size = get_main_thread_stack_size(); uintptr_t stack_bottom = stack_top - stack_size; static void* tls[BIONIC_TLS_SLOTS]; diff --git a/libc/bionic/pthread_attr.cpp b/libc/bionic/pthread_attr.cpp index d7c6c1396..2763b0c23 100644 --- a/libc/bionic/pthread_attr.cpp +++ b/libc/bionic/pthread_attr.cpp @@ -30,16 +30,10 @@ #include "pthread_internal.h" -// Traditionally we give threads a 1MiB stack. When we started allocating per-thread -// alternate signal stacks to ease debugging of stack overflows, we subtracted the -// same amount we were using there from the default thread stack size. This should -// keep memory usage roughly constant. -#define DEFAULT_THREAD_STACK_SIZE ((1 * 1024 * 1024) - SIGSTKSZ) - int pthread_attr_init(pthread_attr_t* attr) { attr->flags = 0; attr->stack_base = NULL; - attr->stack_size = DEFAULT_THREAD_STACK_SIZE; + attr->stack_size = PTHREAD_STACK_SIZE_DEFAULT; attr->guard_size = PAGE_SIZE; attr->sched_policy = SCHED_NORMAL; attr->sched_priority = 0; diff --git a/libc/bionic/pthread_internal.h b/libc/bionic/pthread_internal.h index 31b8ca7f1..6fe2a9839 100644 --- a/libc/bionic/pthread_internal.h +++ b/libc/bionic/pthread_internal.h @@ -77,6 +77,15 @@ __LIBC_HIDDEN__ void _pthread_internal_remove_locked(pthread_internal_t* thread) /* Has the thread already exited but not been joined? */ #define PTHREAD_ATTR_FLAG_ZOMBIE 0x00000008 +/* + * Traditionally we give threads a 1MiB stack. When we started + * allocating per-thread alternate signal stacks to ease debugging of + * stack overflows, we subtracted the same amount we were using there + * from the default thread stack size. This should keep memory usage + * roughly constant. + */ +#define PTHREAD_STACK_SIZE_DEFAULT ((1 * 1024 * 1024) - SIGSTKSZ) + __LIBC_HIDDEN__ extern pthread_internal_t* gThreadList; __LIBC_HIDDEN__ extern pthread_mutex_t gThreadListLock; -- 2.11.0