| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/threading/platform_thread.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <sched.h> | 8 #include <sched.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/safe_strerror_posix.h" | 11 #include "base/safe_strerror_posix.h" |
| 12 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
| 13 #include "base/thread_restrictions.h" | 13 #include "base/thread_restrictions.h" |
| 14 | 14 |
| 15 #if defined(OS_MACOSX) | 15 #if defined(OS_MACOSX) |
| 16 #include <mach/mach.h> | 16 #include <mach/mach.h> |
| 17 #include <sys/resource.h> | 17 #include <sys/resource.h> |
| 18 #include <algorithm> | 18 #include <algorithm> |
| 19 #endif | 19 #endif |
| 20 | 20 |
| 21 #if defined(OS_LINUX) | 21 #if defined(OS_LINUX) |
| 22 #include <dlfcn.h> | 22 #include <dlfcn.h> |
| 23 #include <sys/prctl.h> | 23 #include <sys/prctl.h> |
| 24 #include <sys/syscall.h> | 24 #include <sys/syscall.h> |
| 25 #include <unistd.h> | 25 #include <unistd.h> |
| 26 #endif | 26 #endif |
| 27 | 27 |
| 28 #if defined(OS_NACL) | 28 #if defined(OS_NACL) |
| 29 #include <sys/nacl_syscalls.h> | 29 #include <sys/nacl_syscalls.h> |
| 30 #endif | 30 #endif |
| 31 | 31 |
| 32 namespace base { |
| 33 |
| 32 #if defined(OS_MACOSX) | 34 #if defined(OS_MACOSX) |
| 33 namespace base { | |
| 34 void InitThreading(); | 35 void InitThreading(); |
| 35 } // namespace base | |
| 36 #endif | 36 #endif |
| 37 | 37 |
| 38 namespace { | 38 namespace { |
| 39 | 39 |
| 40 struct ThreadParams { | 40 struct ThreadParams { |
| 41 PlatformThread::Delegate* delegate; | 41 PlatformThread::Delegate* delegate; |
| 42 bool joinable; | 42 bool joinable; |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 } // namespace | 45 void* ThreadFunc(void* params) { |
| 46 | |
| 47 static void* ThreadFunc(void* params) { | |
| 48 ThreadParams* thread_params = static_cast<ThreadParams*>(params); | 46 ThreadParams* thread_params = static_cast<ThreadParams*>(params); |
| 49 PlatformThread::Delegate* delegate = thread_params->delegate; | 47 PlatformThread::Delegate* delegate = thread_params->delegate; |
| 50 if (!thread_params->joinable) | 48 if (!thread_params->joinable) |
| 51 base::ThreadRestrictions::SetSingletonAllowed(false); | 49 base::ThreadRestrictions::SetSingletonAllowed(false); |
| 52 delete thread_params; | 50 delete thread_params; |
| 53 delegate->ThreadMain(); | 51 delegate->ThreadMain(); |
| 54 return NULL; | 52 return NULL; |
| 55 } | 53 } |
| 56 | 54 |
| 55 bool CreateThread(size_t stack_size, bool joinable, |
| 56 PlatformThread::Delegate* delegate, |
| 57 PlatformThreadHandle* thread_handle) { |
| 58 #if defined(OS_MACOSX) |
| 59 base::InitThreading(); |
| 60 #endif // OS_MACOSX |
| 61 |
| 62 bool success = false; |
| 63 pthread_attr_t attributes; |
| 64 pthread_attr_init(&attributes); |
| 65 |
| 66 // Pthreads are joinable by default, so only specify the detached attribute if |
| 67 // the thread should be non-joinable. |
| 68 if (!joinable) { |
| 69 pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); |
| 70 } |
| 71 |
| 72 #if defined(OS_MACOSX) |
| 73 // The Mac OS X default for a pthread stack size is 512kB. |
| 74 // Libc-594.1.4/pthreads/pthread.c's pthread_attr_init uses |
| 75 // DEFAULT_STACK_SIZE for this purpose. |
| 76 // |
| 77 // 512kB isn't quite generous enough for some deeply recursive threads that |
| 78 // otherwise request the default stack size by specifying 0. Here, adopt |
| 79 // glibc's behavior as on Linux, which is to use the current stack size |
| 80 // limit (ulimit -s) as the default stack size. See |
| 81 // glibc-2.11.1/nptl/nptl-init.c's __pthread_initialize_minimal_internal. To |
| 82 // avoid setting the limit below the Mac OS X default or the minimum usable |
| 83 // stack size, these values are also considered. If any of these values |
| 84 // can't be determined, or if stack size is unlimited (ulimit -s unlimited), |
| 85 // stack_size is left at 0 to get the system default. |
| 86 // |
| 87 // Mac OS X normally only applies ulimit -s to the main thread stack. On |
| 88 // contemporary OS X and Linux systems alike, this value is generally 8MB |
| 89 // or in that neighborhood. |
| 90 if (stack_size == 0) { |
| 91 size_t default_stack_size; |
| 92 struct rlimit stack_rlimit; |
| 93 if (pthread_attr_getstacksize(&attributes, &default_stack_size) == 0 && |
| 94 getrlimit(RLIMIT_STACK, &stack_rlimit) == 0 && |
| 95 stack_rlimit.rlim_cur != RLIM_INFINITY) { |
| 96 stack_size = std::max(std::max(default_stack_size, |
| 97 static_cast<size_t>(PTHREAD_STACK_MIN)), |
| 98 static_cast<size_t>(stack_rlimit.rlim_cur)); |
| 99 } |
| 100 } |
| 101 #endif // OS_MACOSX |
| 102 |
| 103 if (stack_size > 0) |
| 104 pthread_attr_setstacksize(&attributes, stack_size); |
| 105 |
| 106 ThreadParams* params = new ThreadParams; |
| 107 params->delegate = delegate; |
| 108 params->joinable = joinable; |
| 109 success = !pthread_create(thread_handle, &attributes, ThreadFunc, params); |
| 110 |
| 111 pthread_attr_destroy(&attributes); |
| 112 if (!success) |
| 113 delete params; |
| 114 return success; |
| 115 } |
| 116 |
| 117 } // namespace |
| 118 |
| 57 // static | 119 // static |
| 58 PlatformThreadId PlatformThread::CurrentId() { | 120 PlatformThreadId PlatformThread::CurrentId() { |
| 59 // Pthreads doesn't have the concept of a thread ID, so we have to reach down | 121 // Pthreads doesn't have the concept of a thread ID, so we have to reach down |
| 60 // into the kernel. | 122 // into the kernel. |
| 61 #if defined(OS_MACOSX) | 123 #if defined(OS_MACOSX) |
| 62 return mach_thread_self(); | 124 return mach_thread_self(); |
| 63 #elif defined(OS_LINUX) | 125 #elif defined(OS_LINUX) |
| 64 return syscall(__NR_gettid); | 126 return syscall(__NR_gettid); |
| 65 #elif defined(OS_FREEBSD) | 127 #elif defined(OS_FREEBSD) |
| 66 // TODO(BSD): find a better thread ID | 128 // TODO(BSD): find a better thread ID |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 #else | 190 #else |
| 129 // static | 191 // static |
| 130 void PlatformThread::SetName(const char* name) { | 192 void PlatformThread::SetName(const char* name) { |
| 131 // Leave it unimplemented. | 193 // Leave it unimplemented. |
| 132 | 194 |
| 133 // (This should be relatively simple to implement for the BSDs; I | 195 // (This should be relatively simple to implement for the BSDs; I |
| 134 // just don't have one handy to test the code on.) | 196 // just don't have one handy to test the code on.) |
| 135 } | 197 } |
| 136 #endif // defined(OS_LINUX) | 198 #endif // defined(OS_LINUX) |
| 137 | 199 |
| 138 namespace { | |
| 139 | |
| 140 bool CreateThread(size_t stack_size, bool joinable, | |
| 141 PlatformThread::Delegate* delegate, | |
| 142 PlatformThreadHandle* thread_handle) { | |
| 143 #if defined(OS_MACOSX) | |
| 144 base::InitThreading(); | |
| 145 #endif // OS_MACOSX | |
| 146 | |
| 147 bool success = false; | |
| 148 pthread_attr_t attributes; | |
| 149 pthread_attr_init(&attributes); | |
| 150 | |
| 151 // Pthreads are joinable by default, so only specify the detached attribute if | |
| 152 // the thread should be non-joinable. | |
| 153 if (!joinable) { | |
| 154 pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); | |
| 155 } | |
| 156 | |
| 157 #if defined(OS_MACOSX) | |
| 158 // The Mac OS X default for a pthread stack size is 512kB. | |
| 159 // Libc-594.1.4/pthreads/pthread.c's pthread_attr_init uses | |
| 160 // DEFAULT_STACK_SIZE for this purpose. | |
| 161 // | |
| 162 // 512kB isn't quite generous enough for some deeply recursive threads that | |
| 163 // otherwise request the default stack size by specifying 0. Here, adopt | |
| 164 // glibc's behavior as on Linux, which is to use the current stack size | |
| 165 // limit (ulimit -s) as the default stack size. See | |
| 166 // glibc-2.11.1/nptl/nptl-init.c's __pthread_initialize_minimal_internal. To | |
| 167 // avoid setting the limit below the Mac OS X default or the minimum usable | |
| 168 // stack size, these values are also considered. If any of these values | |
| 169 // can't be determined, or if stack size is unlimited (ulimit -s unlimited), | |
| 170 // stack_size is left at 0 to get the system default. | |
| 171 // | |
| 172 // Mac OS X normally only applies ulimit -s to the main thread stack. On | |
| 173 // contemporary OS X and Linux systems alike, this value is generally 8MB | |
| 174 // or in that neighborhood. | |
| 175 if (stack_size == 0) { | |
| 176 size_t default_stack_size; | |
| 177 struct rlimit stack_rlimit; | |
| 178 if (pthread_attr_getstacksize(&attributes, &default_stack_size) == 0 && | |
| 179 getrlimit(RLIMIT_STACK, &stack_rlimit) == 0 && | |
| 180 stack_rlimit.rlim_cur != RLIM_INFINITY) { | |
| 181 stack_size = std::max(std::max(default_stack_size, | |
| 182 static_cast<size_t>(PTHREAD_STACK_MIN)), | |
| 183 static_cast<size_t>(stack_rlimit.rlim_cur)); | |
| 184 } | |
| 185 } | |
| 186 #endif // OS_MACOSX | |
| 187 | |
| 188 if (stack_size > 0) | |
| 189 pthread_attr_setstacksize(&attributes, stack_size); | |
| 190 | |
| 191 ThreadParams* params = new ThreadParams; | |
| 192 params->delegate = delegate; | |
| 193 params->joinable = joinable; | |
| 194 success = !pthread_create(thread_handle, &attributes, ThreadFunc, params); | |
| 195 | |
| 196 pthread_attr_destroy(&attributes); | |
| 197 if (!success) | |
| 198 delete params; | |
| 199 return success; | |
| 200 } | |
| 201 | |
| 202 } // anonymous namespace | |
| 203 | |
| 204 // static | 200 // static |
| 205 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, | 201 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, |
| 206 PlatformThreadHandle* thread_handle) { | 202 PlatformThreadHandle* thread_handle) { |
| 207 return CreateThread(stack_size, true /* joinable thread */, | 203 return CreateThread(stack_size, true /* joinable thread */, |
| 208 delegate, thread_handle); | 204 delegate, thread_handle); |
| 209 } | 205 } |
| 210 | 206 |
| 211 // static | 207 // static |
| 212 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { | 208 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { |
| 213 PlatformThreadHandle unused; | 209 PlatformThreadHandle unused; |
| 214 | 210 |
| 215 bool result = CreateThread(stack_size, false /* non-joinable thread */, | 211 bool result = CreateThread(stack_size, false /* non-joinable thread */, |
| 216 delegate, &unused); | 212 delegate, &unused); |
| 217 return result; | 213 return result; |
| 218 } | 214 } |
| 219 | 215 |
| 220 // static | 216 // static |
| 221 void PlatformThread::Join(PlatformThreadHandle thread_handle) { | 217 void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
| 222 // Joining another thread may block the current thread for a long time, since | 218 // Joining another thread may block the current thread for a long time, since |
| 223 // the thread referred to by |thread_handle| may still be running long-lived / | 219 // the thread referred to by |thread_handle| may still be running long-lived / |
| 224 // blocking tasks. | 220 // blocking tasks. |
| 225 base::ThreadRestrictions::AssertIOAllowed(); | 221 base::ThreadRestrictions::AssertIOAllowed(); |
| 226 pthread_join(thread_handle, NULL); | 222 pthread_join(thread_handle, NULL); |
| 227 } | 223 } |
| 224 |
| 225 } // namespace base |
| OLD | NEW |