1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/threading/platform_thread.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/safe_strerror_posix.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread_id_name_manager.h"
16 #include "base/threading/thread_restrictions.h"
17 #include "base/tracked_objects.h"
19 #if defined(OS_MACOSX)
20 #include <sys/resource.h>
25 #include <sys/prctl.h>
26 #include <sys/resource.h>
27 #include <sys/syscall.h>
36 void TerminateOnThread();
37 size_t GetDefaultThreadStackSize(const pthread_attr_t
& attributes
);
45 priority(kThreadPriority_Normal
),
47 handle_set(false, false) {
50 PlatformThread::Delegate
* delegate
;
52 ThreadPriority priority
;
53 PlatformThreadHandle
* handle
;
54 WaitableEvent handle_set
;
57 void* ThreadFunc(void* params
) {
59 ThreadParams
* thread_params
= static_cast<ThreadParams
*>(params
);
61 PlatformThread::Delegate
* delegate
= thread_params
->delegate
;
62 if (!thread_params
->joinable
)
63 base::ThreadRestrictions::SetSingletonAllowed(false);
65 if (thread_params
->priority
!= kThreadPriority_Normal
) {
66 PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(),
67 thread_params
->priority
);
70 // Stash the id in the handle so the calling thread has a complete
71 // handle, and unblock the parent thread.
72 *(thread_params
->handle
) = PlatformThreadHandle(pthread_self(),
73 PlatformThread::CurrentId());
74 thread_params
->handle_set
.Signal();
76 ThreadIdNameManager::GetInstance()->RegisterThread(
77 PlatformThread::CurrentHandle().platform_handle(),
78 PlatformThread::CurrentId());
80 delegate
->ThreadMain();
82 ThreadIdNameManager::GetInstance()->RemoveName(
83 PlatformThread::CurrentHandle().platform_handle(),
84 PlatformThread::CurrentId());
86 base::TerminateOnThread();
90 bool CreateThread(size_t stack_size
, bool joinable
,
91 PlatformThread::Delegate
* delegate
,
92 PlatformThreadHandle
* thread_handle
,
93 ThreadPriority priority
) {
94 base::InitThreading();
97 pthread_attr_t attributes
;
98 pthread_attr_init(&attributes
);
100 // Pthreads are joinable by default, so only specify the detached
101 // attribute if the thread should be non-joinable.
103 pthread_attr_setdetachstate(&attributes
, PTHREAD_CREATE_DETACHED
);
106 // Get a better default if available.
108 stack_size
= base::GetDefaultThreadStackSize(attributes
);
111 pthread_attr_setstacksize(&attributes
, stack_size
);
114 params
.delegate
= delegate
;
115 params
.joinable
= joinable
;
116 params
.priority
= priority
;
117 params
.handle
= thread_handle
;
120 int err
= pthread_create(&handle
,
126 // Value of |handle| is undefined if pthread_create fails.
129 PLOG(ERROR
) << "pthread_create";
132 pthread_attr_destroy(&attributes
);
134 // Don't let this call complete until the thread id
135 // is set in the handle.
137 params
.handle_set
.Wait();
138 CHECK_EQ(handle
, thread_handle
->platform_handle());
146 PlatformThreadId
PlatformThread::CurrentId() {
147 // Pthreads doesn't have the concept of a thread ID, so we have to reach down
149 #if defined(OS_MACOSX)
150 return pthread_mach_thread_np(pthread_self());
151 #elif defined(OS_LINUX)
152 return syscall(__NR_gettid
);
153 #elif defined(OS_ANDROID)
155 #elif defined(OS_SOLARIS) || defined(OS_QNX)
156 return pthread_self();
157 #elif defined(OS_NACL) && defined(__GLIBC__)
158 return pthread_self();
159 #elif defined(OS_NACL) && !defined(__GLIBC__)
160 // Pointers are 32-bits in NaCl.
161 return reinterpret_cast<int32
>(pthread_self());
162 #elif defined(OS_POSIX)
163 return reinterpret_cast<int64
>(pthread_self());
168 PlatformThreadRef
PlatformThread::CurrentRef() {
169 return PlatformThreadRef(pthread_self());
173 PlatformThreadHandle
PlatformThread::CurrentHandle() {
174 return PlatformThreadHandle(pthread_self(), CurrentId());
178 void PlatformThread::YieldCurrentThread() {
183 void PlatformThread::Sleep(TimeDelta duration
) {
184 struct timespec sleep_time
, remaining
;
186 // Break the duration into seconds and nanoseconds.
187 // NOTE: TimeDelta's microseconds are int64s while timespec's
188 // nanoseconds are longs, so this unpacking must prevent overflow.
189 sleep_time
.tv_sec
= duration
.InSeconds();
190 duration
-= TimeDelta::FromSeconds(sleep_time
.tv_sec
);
191 sleep_time
.tv_nsec
= duration
.InMicroseconds() * 1000; // nanoseconds
193 while (nanosleep(&sleep_time
, &remaining
) == -1 && errno
== EINTR
)
194 sleep_time
= remaining
;
198 const char* PlatformThread::GetName() {
199 return ThreadIdNameManager::GetInstance()->GetName(CurrentId());
203 bool PlatformThread::Create(size_t stack_size
, Delegate
* delegate
,
204 PlatformThreadHandle
* thread_handle
) {
205 base::ThreadRestrictions::ScopedAllowWait allow_wait
;
206 return CreateThread(stack_size
, true /* joinable thread */,
207 delegate
, thread_handle
, kThreadPriority_Normal
);
211 bool PlatformThread::CreateWithPriority(size_t stack_size
, Delegate
* delegate
,
212 PlatformThreadHandle
* thread_handle
,
213 ThreadPriority priority
) {
214 base::ThreadRestrictions::ScopedAllowWait allow_wait
;
215 return CreateThread(stack_size
, true, // joinable thread
216 delegate
, thread_handle
, priority
);
220 bool PlatformThread::CreateNonJoinable(size_t stack_size
, Delegate
* delegate
) {
221 PlatformThreadHandle unused
;
223 base::ThreadRestrictions::ScopedAllowWait allow_wait
;
224 bool result
= CreateThread(stack_size
, false /* non-joinable thread */,
225 delegate
, &unused
, kThreadPriority_Normal
);
230 void PlatformThread::Join(PlatformThreadHandle thread_handle
) {
231 // Joining another thread may block the current thread for a long time, since
232 // the thread referred to by |thread_handle| may still be running long-lived /
234 base::ThreadRestrictions::AssertIOAllowed();
235 CHECK_EQ(0, pthread_join(thread_handle
.handle_
, NULL
));