OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/platform_thread.h" | 5 #include "base/platform_thread.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sched.h> | 8 #include <sched.h> |
9 | 9 |
10 #if defined(OS_MACOSX) | 10 #if defined(OS_MACOSX) |
11 #include <mach/mach.h> | 11 #include <mach/mach.h> |
| 12 #include <sys/resource.h> |
| 13 #include <algorithm> |
12 #else | 14 #else |
13 #include <sys/syscall.h> | 15 #include <sys/syscall.h> |
14 #include <unistd.h> | 16 #include <unistd.h> |
15 #endif | 17 #endif |
16 | 18 |
17 #if defined(OS_MACOSX) | 19 #if defined(OS_MACOSX) |
18 namespace base { | 20 namespace base { |
19 void InitThreading(); | 21 void InitThreading(); |
20 } // namespace | 22 } // namespace |
21 #endif | 23 #endif |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 bool success = false; | 85 bool success = false; |
84 pthread_attr_t attributes; | 86 pthread_attr_t attributes; |
85 pthread_attr_init(&attributes); | 87 pthread_attr_init(&attributes); |
86 | 88 |
87 // Pthreads are joinable by default, so only specify the detached attribute if | 89 // Pthreads are joinable by default, so only specify the detached attribute if |
88 // the thread should be non-joinable. | 90 // the thread should be non-joinable. |
89 if (!joinable) { | 91 if (!joinable) { |
90 pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); | 92 pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); |
91 } | 93 } |
92 | 94 |
| 95 #if defined(OS_MACOSX) |
| 96 // The Mac OS X default for a pthread stack size is 512kB. |
| 97 // Libc-594.1.4/pthreads/pthread.c's pthread_attr_init uses |
| 98 // DEFAULT_STACK_SIZE for this purpose. |
| 99 // |
| 100 // 512kB isn't quite generous enough for some deeply recursive threads that |
| 101 // otherwise request the default stack size by specifying 0. Here, adopt |
| 102 // glibc's behavior as on Linux, which is to use the current stack size |
| 103 // limit (ulimit -s) as the default stack size. See |
| 104 // glibc-2.11.1/nptl/nptl-init.c's __pthread_initialize_minimal_internal. To |
| 105 // avoid setting the limit below the Mac OS X default or the minimum usable |
| 106 // stack size, these values are also considered. If any of these values |
| 107 // can't be determined, or if stack size is unlimited (ulimit -s unlimited), |
| 108 // just leave stack_size at 0 to get the system default. |
| 109 // |
| 110 // Mac OS X normally only applies ulimit -s to the main thread stack. On |
| 111 // contemporary OS X and Linux systems alike, this value is generally 8MB |
| 112 // or in that neighborhood. |
| 113 if (stack_size == 0) { |
| 114 size_t default_stack_size; |
| 115 struct rlimit stack_rlimit; |
| 116 if (pthread_attr_getstacksize(&attributes, &default_stack_size) == 0 && |
| 117 getrlimit(RLIMIT_STACK, &stack_rlimit) == 0 && |
| 118 stack_rlimit.rlim_cur != RLIM_INFINITY) { |
| 119 stack_size = std::max(std::max(default_stack_size, |
| 120 static_cast<size_t>(PTHREAD_STACK_MIN)), |
| 121 static_cast<size_t>(stack_rlimit.rlim_cur)); |
| 122 } |
| 123 } |
| 124 #endif // OS_MACOSX |
| 125 |
93 if (stack_size > 0) | 126 if (stack_size > 0) |
94 pthread_attr_setstacksize(&attributes, stack_size); | 127 pthread_attr_setstacksize(&attributes, stack_size); |
95 | 128 |
96 success = !pthread_create(thread_handle, &attributes, ThreadFunc, delegate); | 129 success = !pthread_create(thread_handle, &attributes, ThreadFunc, delegate); |
97 | 130 |
98 pthread_attr_destroy(&attributes); | 131 pthread_attr_destroy(&attributes); |
99 return success; | 132 return success; |
100 } | 133 } |
101 | 134 |
102 } // anonymous namespace | 135 } // anonymous namespace |
(...skipping 11 matching lines...) Expand all Loading... |
114 | 147 |
115 bool result = CreateThread(stack_size, false /* non-joinable thread */, | 148 bool result = CreateThread(stack_size, false /* non-joinable thread */, |
116 delegate, &unused); | 149 delegate, &unused); |
117 return result; | 150 return result; |
118 } | 151 } |
119 | 152 |
120 // static | 153 // static |
121 void PlatformThread::Join(PlatformThreadHandle thread_handle) { | 154 void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
122 pthread_join(thread_handle, NULL); | 155 pthread_join(thread_handle, NULL); |
123 } | 156 } |
OLD | NEW |