summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Carlstrom <bdc@google.com>2013-09-13 16:34:43 -0700
committerBrian Carlstrom <bdc@google.com>2013-09-13 16:44:47 -0700
commit50af69e8f326b2762a44d5fea2b118e7616e5d20 (patch)
tree46a173af8b370ce18d029c05c636bdc6fcc557ed
parent322e7bce235ec6e3f82f65669423a1d9e997b51e (diff)
downloadbionic-50af69e8f326b2762a44d5fea2b118e7616e5d20.zip
bionic-50af69e8f326b2762a44d5fea2b118e7616e5d20.tar.gz
bionic-50af69e8f326b2762a44d5fea2b118e7616e5d20.tar.bz2
Simplify main thread stack size initialization
Change-Id: Iec09433d9de501031cce09dc75848a5e8f3d96bf
-rw-r--r--libc/bionic/libc_init_common.cpp20
-rw-r--r--libc/bionic/pthread_attr.cpp8
-rw-r--r--libc/bionic/pthread_internal.h9
3 files changed, 18 insertions, 19 deletions
diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp
index c10abad..f5cb1e5 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 d7c6c13..2763b0c 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 31b8ca7..6fe2a98 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;