Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/threading/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/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/safe_strerror_posix.h" | 12 #include "base/safe_strerror_posix.h" |
| 13 #include "base/debug/trace_event.h" | |
| 13 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
| 14 | 15 |
| 15 #if defined(OS_MACOSX) | 16 #if defined(OS_MACOSX) |
| 16 #include <mach/mach.h> | 17 #include <mach/mach.h> |
| 17 #include <sys/resource.h> | 18 #include <sys/resource.h> |
| 18 #include <algorithm> | 19 #include <algorithm> |
| 19 #endif | 20 #endif |
| 20 | 21 |
| 21 #if defined(OS_LINUX) | 22 #if defined(OS_LINUX) |
| 22 #include <dlfcn.h> | 23 #include <dlfcn.h> |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 sleep_time.tv_sec = duration_ms / 1000; | 146 sleep_time.tv_sec = duration_ms / 1000; |
| 146 duration_ms -= sleep_time.tv_sec * 1000; | 147 duration_ms -= sleep_time.tv_sec * 1000; |
| 147 | 148 |
| 148 // Contains the portion of duration_ms < 1 sec. | 149 // Contains the portion of duration_ms < 1 sec. |
| 149 sleep_time.tv_nsec = duration_ms * 1000 * 1000; // nanoseconds. | 150 sleep_time.tv_nsec = duration_ms * 1000 * 1000; // nanoseconds. |
| 150 | 151 |
| 151 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) | 152 while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) |
| 152 sleep_time = remaining; | 153 sleep_time = remaining; |
| 153 } | 154 } |
| 154 | 155 |
| 155 // Linux SetName is currently disabled, as we need to distinguish between | 156 // Linux SetName is currently disabled, as we need to distinguish between |
|
joth
2011/07/26 09:53:17
FWIW we should be able to distinguish the main thr
| |
| 156 // helper threads (where it's ok to make this call) and the main thread | 157 // helper threads (where it's ok to make this call) and the main thread |
| 157 // (where making this call renames our process, causing tools like killall | 158 // (where making this call renames our process, causing tools like killall |
| 158 // to stop working). | 159 // to stop working). |
|
joth
2011/07/26 09:53:17
we should be able to distinguish the main thread f
| |
| 159 #if 0 && defined(OS_LINUX) | 160 #if 0 && defined(OS_LINUX) |
| 160 // static | 161 // static |
| 161 void PlatformThread::SetName(const char* name) { | 162 void PlatformThread::SetName(const char* name) { |
| 163 base::debug::TraceLog::GetInstance()->SetCurrentThreadName(name); | |
| 164 | |
| 162 // http://0pointer.de/blog/projects/name-your-threads.html | 165 // http://0pointer.de/blog/projects/name-your-threads.html |
| 163 | 166 |
| 164 // glibc recently added support for pthread_setname_np, but it's not | 167 // glibc recently added support for pthread_setname_np, but it's not |
| 165 // commonly available yet. So test for it at runtime. | 168 // commonly available yet. So test for it at runtime. |
| 166 int (*dynamic_pthread_setname_np)(pthread_t, const char*); | 169 int (*dynamic_pthread_setname_np)(pthread_t, const char*); |
| 167 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = | 170 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) = |
| 168 dlsym(RTLD_DEFAULT, "pthread_setname_np"); | 171 dlsym(RTLD_DEFAULT, "pthread_setname_np"); |
| 169 | 172 |
| 170 if (dynamic_pthread_setname_np) { | 173 if (dynamic_pthread_setname_np) { |
| 171 // This limit comes from glibc, which gets it from the kernel | 174 // This limit comes from glibc, which gets it from the kernel |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 182 // truncated by the callee (see TASK_COMM_LEN above).) | 185 // truncated by the callee (see TASK_COMM_LEN above).) |
| 183 int err = prctl(PR_SET_NAME, name); | 186 int err = prctl(PR_SET_NAME, name); |
| 184 if (err < 0) | 187 if (err < 0) |
| 185 PLOG(ERROR) << "prctl(PR_SET_NAME)"; | 188 PLOG(ERROR) << "prctl(PR_SET_NAME)"; |
| 186 } | 189 } |
| 187 } | 190 } |
| 188 #elif defined(OS_MACOSX) | 191 #elif defined(OS_MACOSX) |
| 189 // Mac is implemented in platform_thread_mac.mm. | 192 // Mac is implemented in platform_thread_mac.mm. |
| 190 #else | 193 #else |
| 191 // static | 194 // static |
| 192 void PlatformThread::SetName(const char* /*name*/) { | 195 void PlatformThread::SetName(const char* name) { |
| 193 // Leave it unimplemented. | 196 base::debug::TraceLog::GetInstance()->SetCurrentThreadName(name); |
| 194 | 197 |
| 195 // (This should be relatively simple to implement for the BSDs; I | 198 // (This should be relatively simple to implement for the BSDs; I |
| 196 // just don't have one handy to test the code on.) | 199 // just don't have one handy to test the code on.) |
| 197 } | 200 } |
| 198 #endif // defined(OS_LINUX) | 201 #endif // defined(OS_LINUX) |
| 199 | 202 |
| 200 // static | 203 // static |
| 201 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, | 204 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, |
| 202 PlatformThreadHandle* thread_handle) { | 205 PlatformThreadHandle* thread_handle) { |
| 203 return CreateThread(stack_size, true /* joinable thread */, | 206 return CreateThread(stack_size, true /* joinable thread */, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 226 // Mac OS X uses lower-level mach APIs | 229 // Mac OS X uses lower-level mach APIs |
| 227 | 230 |
| 228 // static | 231 // static |
| 229 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { | 232 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { |
| 230 // TODO(crogers): implement | 233 // TODO(crogers): implement |
| 231 NOTIMPLEMENTED(); | 234 NOTIMPLEMENTED(); |
| 232 } | 235 } |
| 233 #endif | 236 #endif |
| 234 | 237 |
| 235 } // namespace base | 238 } // namespace base |
| OLD | NEW |