Index: base/platform_thread_posix.cc |
=================================================================== |
--- base/platform_thread_posix.cc (revision 46695) |
+++ base/platform_thread_posix.cc (working copy) |
@@ -9,6 +9,8 @@ |
#if defined(OS_MACOSX) |
#include <mach/mach.h> |
+#include <sys/resource.h> |
+#include <algorithm> |
#else |
#include <sys/syscall.h> |
#include <unistd.h> |
@@ -90,6 +92,37 @@ |
pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); |
} |
+#if defined(OS_MACOSX) |
+ // The Mac OS X default for a pthread stack size is 512kB. |
+ // Libc-594.1.4/pthreads/pthread.c's pthread_attr_init uses |
+ // DEFAULT_STACK_SIZE for this purpose. |
+ // |
+ // 512kB isn't quite generous enough for some deeply recursive threads that |
+ // otherwise request the default stack size by specifying 0. Here, adopt |
+ // glibc's behavior as on Linux, which is to use the current stack size |
+ // limit (ulimit -s) as the default stack size. See |
+ // glibc-2.11.1/nptl/nptl-init.c's __pthread_initialize_minimal_internal. To |
+ // avoid setting the limit below the Mac OS X default or the minimum usable |
+ // stack size, these values are also considered. If any of these values |
+ // can't be determined, or if stack size is unlimited (ulimit -s unlimited), |
+ // just leave stack_size at 0 to get the system default. |
+ // |
+ // Mac OS X normally only applies ulimit -s to the main thread stack. On |
+ // contemporary OS X and Linux systems alike, this value is generally 8MB |
+ // or in that neighborhood. |
+ if (stack_size == 0) { |
+ size_t default_stack_size; |
+ struct rlimit stack_rlimit; |
+ if (pthread_attr_getstacksize(&attributes, &default_stack_size) == 0 && |
+ getrlimit(RLIMIT_STACK, &stack_rlimit) == 0 && |
+ stack_rlimit.rlim_cur != RLIM_INFINITY) { |
+ stack_size = std::max(std::max(default_stack_size, |
+ static_cast<size_t>(PTHREAD_STACK_MIN)), |
+ static_cast<size_t>(stack_rlimit.rlim_cur)); |
+ } |
+ } |
+#endif // OS_MACOSX |
+ |
if (stack_size > 0) |
pthread_attr_setstacksize(&attributes, stack_size); |