OSDN Git Service

Simplify main thread stack size initialization
authorBrian Carlstrom <bdc@google.com>
Fri, 13 Sep 2013 23:34:43 +0000 (16:34 -0700)
committerBrian Carlstrom <bdc@google.com>
Fri, 13 Sep 2013 23:44:47 +0000 (16:44 -0700)
Change-Id: Iec09433d9de501031cce09dc75848a5e8f3d96bf

libc/bionic/libc_init_common.cpp
libc/bionic/pthread_attr.cpp
libc/bionic/pthread_internal.h

index c10abad..f5cb1e5 100644 (file)
@@ -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];
index d7c6c13..2763b0c 100644 (file)
 
 #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;
index 31b8ca7..6fe2a98 100644 (file)
@@ -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;