Index: base/threading/platform_thread_posix.cc |
=================================================================== |
--- base/threading/platform_thread_posix.cc (revision 70321) |
+++ base/threading/platform_thread_posix.cc (working copy) |
@@ -1,8 +1,8 @@ |
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
+// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "base/platform_thread.h" |
+#include "base/threading/platform_thread.h" |
#include <errno.h> |
#include <sched.h> |
@@ -29,10 +29,10 @@ |
#include <sys/nacl_syscalls.h> |
#endif |
-#if defined(OS_MACOSX) |
namespace base { |
+ |
+#if defined(OS_MACOSX) |
void InitThreading(); |
-} // namespace base |
#endif |
namespace { |
@@ -42,9 +42,7 @@ |
bool joinable; |
}; |
-} // namespace |
- |
-static void* ThreadFunc(void* params) { |
+void* ThreadFunc(void* params) { |
ThreadParams* thread_params = static_cast<ThreadParams*>(params); |
PlatformThread::Delegate* delegate = thread_params->delegate; |
if (!thread_params->joinable) |
@@ -54,6 +52,70 @@ |
return NULL; |
} |
+bool CreateThread(size_t stack_size, bool joinable, |
+ PlatformThread::Delegate* delegate, |
+ PlatformThreadHandle* thread_handle) { |
+#if defined(OS_MACOSX) |
+ base::InitThreading(); |
+#endif // OS_MACOSX |
+ |
+ bool success = false; |
+ pthread_attr_t attributes; |
+ pthread_attr_init(&attributes); |
+ |
+ // Pthreads are joinable by default, so only specify the detached attribute if |
+ // the thread should be non-joinable. |
+ if (!joinable) { |
+ 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), |
+ // stack_size is left 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); |
+ |
+ ThreadParams* params = new ThreadParams; |
+ params->delegate = delegate; |
+ params->joinable = joinable; |
+ success = !pthread_create(thread_handle, &attributes, ThreadFunc, params); |
+ |
+ pthread_attr_destroy(&attributes); |
+ if (!success) |
+ delete params; |
+ return success; |
+} |
+ |
+} // namespace |
+ |
// static |
PlatformThreadId PlatformThread::CurrentId() { |
// Pthreads doesn't have the concept of a thread ID, so we have to reach down |
@@ -135,72 +197,6 @@ |
} |
#endif // defined(OS_LINUX) |
-namespace { |
- |
-bool CreateThread(size_t stack_size, bool joinable, |
- PlatformThread::Delegate* delegate, |
- PlatformThreadHandle* thread_handle) { |
-#if defined(OS_MACOSX) |
- base::InitThreading(); |
-#endif // OS_MACOSX |
- |
- bool success = false; |
- pthread_attr_t attributes; |
- pthread_attr_init(&attributes); |
- |
- // Pthreads are joinable by default, so only specify the detached attribute if |
- // the thread should be non-joinable. |
- if (!joinable) { |
- 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), |
- // stack_size is left 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); |
- |
- ThreadParams* params = new ThreadParams; |
- params->delegate = delegate; |
- params->joinable = joinable; |
- success = !pthread_create(thread_handle, &attributes, ThreadFunc, params); |
- |
- pthread_attr_destroy(&attributes); |
- if (!success) |
- delete params; |
- return success; |
-} |
- |
-} // anonymous namespace |
- |
// static |
bool PlatformThread::Create(size_t stack_size, Delegate* delegate, |
PlatformThreadHandle* thread_handle) { |
@@ -225,3 +221,5 @@ |
base::ThreadRestrictions::AssertIOAllowed(); |
pthread_join(thread_handle, NULL); |
} |
+ |
+} // namespace base |