michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/platform_thread.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #if defined(OS_MACOSX) michael@0: #include michael@0: #elif defined(OS_NETBSD) michael@0: #include michael@0: #elif defined(OS_LINUX) michael@0: #include michael@0: #include michael@0: #elif defined(OS_FREEBSD) && !defined(__GLIBC__) michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #if !defined(OS_MACOSX) michael@0: #include michael@0: #endif michael@0: michael@0: #if defined(OS_BSD) && !defined(OS_NETBSD) && !defined(__GLIBC__) michael@0: #include michael@0: #endif michael@0: michael@0: #if defined(OS_MACOSX) michael@0: namespace base { michael@0: void InitThreading(); michael@0: } // namespace michael@0: #endif michael@0: michael@0: static void* ThreadFunc(void* closure) { michael@0: PlatformThread::Delegate* delegate = michael@0: static_cast(closure); michael@0: delegate->ThreadMain(); michael@0: return NULL; michael@0: } michael@0: michael@0: // static michael@0: PlatformThreadId PlatformThread::CurrentId() { michael@0: // Pthreads doesn't have the concept of a thread ID, so we have to reach down michael@0: // into the kernel. michael@0: #if defined(OS_MACOSX) michael@0: mach_port_t port = mach_thread_self(); michael@0: mach_port_deallocate(mach_task_self(), port); michael@0: return port; michael@0: #elif defined(OS_LINUX) michael@0: #ifdef MOZ_WIDGET_GONK michael@0: return (intptr_t) (pthread_self()); michael@0: #else michael@0: return syscall(__NR_gettid); michael@0: #endif michael@0: #elif defined(OS_OPENBSD) || defined(__GLIBC__) michael@0: return (intptr_t) (pthread_self()); michael@0: #elif defined(OS_NETBSD) michael@0: return _lwp_self(); michael@0: #elif defined(OS_DRAGONFLY) michael@0: return lwp_gettid(); michael@0: #elif defined(OS_FREEBSD) michael@0: # if __FreeBSD_version > 900030 michael@0: return pthread_getthreadid_np(); michael@0: # else michael@0: long lwpid; michael@0: thr_self(&lwpid); michael@0: return lwpid; michael@0: # endif michael@0: #endif michael@0: } michael@0: michael@0: // static michael@0: void PlatformThread::YieldCurrentThread() { michael@0: sched_yield(); michael@0: } michael@0: michael@0: // static michael@0: void PlatformThread::Sleep(int duration_ms) { michael@0: struct timespec sleep_time, remaining; michael@0: michael@0: // Contains the portion of duration_ms >= 1 sec. michael@0: sleep_time.tv_sec = duration_ms / 1000; michael@0: duration_ms -= sleep_time.tv_sec * 1000; michael@0: michael@0: // Contains the portion of duration_ms < 1 sec. michael@0: sleep_time.tv_nsec = duration_ms * 1000 * 1000; // nanoseconds. michael@0: michael@0: while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) michael@0: sleep_time = remaining; michael@0: } michael@0: michael@0: #ifndef OS_MACOSX michael@0: // Mac is implemented in platform_thread_mac.mm. michael@0: michael@0: // static michael@0: void PlatformThread::SetName(const char* name) { michael@0: // On linux we can get the thread names to show up in the debugger by setting michael@0: // the process name for the LWP. We don't want to do this for the main michael@0: // thread because that would rename the process, causing tools like killall michael@0: // to stop working. michael@0: if (PlatformThread::CurrentId() == getpid()) michael@0: return; michael@0: michael@0: // http://0pointer.de/blog/projects/name-your-threads.html michael@0: // Set the name for the LWP (which gets truncated to 15 characters). michael@0: // Note that glibc also has a 'pthread_setname_np' api, but it may not be michael@0: // available everywhere and it's only benefit over using prctl directly is michael@0: // that it can set the name of threads other than the current thread. michael@0: #if defined(OS_LINUX) michael@0: prctl(PR_SET_NAME, reinterpret_cast(name), 0, 0, 0); michael@0: #elif defined(OS_NETBSD) michael@0: pthread_setname_np(pthread_self(), "%s", (void *)name); michael@0: #elif defined(OS_BSD) && !defined(__GLIBC__) michael@0: pthread_set_name_np(pthread_self(), name); michael@0: #else michael@0: #endif michael@0: } michael@0: #endif // !OS_MACOSX michael@0: michael@0: namespace { michael@0: michael@0: bool CreateThread(size_t stack_size, bool joinable, michael@0: PlatformThread::Delegate* delegate, michael@0: PlatformThreadHandle* thread_handle) { michael@0: #if defined(OS_MACOSX) michael@0: base::InitThreading(); michael@0: #endif // OS_MACOSX michael@0: michael@0: bool success = false; michael@0: pthread_attr_t attributes; michael@0: pthread_attr_init(&attributes); michael@0: michael@0: // Pthreads are joinable by default, so only specify the detached attribute if michael@0: // the thread should be non-joinable. michael@0: if (!joinable) { michael@0: pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); michael@0: } michael@0: michael@0: if (stack_size > 0) michael@0: pthread_attr_setstacksize(&attributes, stack_size); michael@0: michael@0: success = !pthread_create(thread_handle, &attributes, ThreadFunc, delegate); michael@0: michael@0: pthread_attr_destroy(&attributes); michael@0: return success; michael@0: } michael@0: michael@0: } // anonymous namespace michael@0: michael@0: // static michael@0: bool PlatformThread::Create(size_t stack_size, Delegate* delegate, michael@0: PlatformThreadHandle* thread_handle) { michael@0: return CreateThread(stack_size, true /* joinable thread */, michael@0: delegate, thread_handle); michael@0: } michael@0: michael@0: // static michael@0: bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { michael@0: PlatformThreadHandle unused; michael@0: michael@0: bool result = CreateThread(stack_size, false /* non-joinable thread */, michael@0: delegate, &unused); michael@0: return result; michael@0: } michael@0: michael@0: // static michael@0: void PlatformThread::Join(PlatformThreadHandle thread_handle) { michael@0: pthread_join(thread_handle, NULL); michael@0: }