Index: base/threading/platform_thread_posix.cc |
diff --git a/base/threading/platform_thread_posix.cc b/base/threading/platform_thread_posix.cc |
index 95cc5d55cda76c2a0bd82e28700a5fc6de89dc78..da685d6aa51e92cc51958b31445cb23afa4361c7 100644 |
--- a/base/threading/platform_thread_posix.cc |
+++ b/base/threading/platform_thread_posix.cc |
@@ -36,23 +36,17 @@ namespace { |
struct ThreadParams { |
ThreadParams() |
- : delegate(NULL), |
- joinable(false), |
- priority(ThreadPriority::NORMAL), |
- handle(NULL), |
- handle_set(false, false) { |
- } |
+ : delegate(NULL), joinable(false), priority(ThreadPriority::NORMAL) {} |
PlatformThread::Delegate* delegate; |
bool joinable; |
ThreadPriority priority; |
- PlatformThreadHandle* handle; |
- WaitableEvent handle_set; |
}; |
void* ThreadFunc(void* params) { |
+ CHECK(params); |
base::InitOnThread(); |
- ThreadParams* thread_params = static_cast<ThreadParams*>(params); |
+ scoped_ptr<ThreadParams> thread_params(static_cast<ThreadParams*>(params)); |
PlatformThread::Delegate* delegate = thread_params->delegate; |
if (!thread_params->joinable) |
@@ -61,11 +55,6 @@ void* ThreadFunc(void* params) { |
if (thread_params->priority != ThreadPriority::NORMAL) |
PlatformThread::SetCurrentThreadPriority(thread_params->priority); |
- // Stash the id in the handle so the calling thread has a complete |
- // handle, and unblock the parent thread. |
- *(thread_params->handle) = PlatformThreadHandle(pthread_self()); |
- thread_params->handle_set.Signal(); |
- |
ThreadIdNameManager::GetInstance()->RegisterThread( |
PlatformThread::CurrentHandle().platform_handle(), |
PlatformThread::CurrentId()); |
@@ -80,21 +69,21 @@ void* ThreadFunc(void* params) { |
return NULL; |
} |
-bool CreateThread(size_t stack_size, bool joinable, |
+bool CreateThread(size_t stack_size, |
+ bool joinable, |
PlatformThread::Delegate* delegate, |
PlatformThreadHandle* thread_handle, |
ThreadPriority priority) { |
+ CHECK(thread_handle); |
base::InitThreading(); |
- bool success = false; |
pthread_attr_t attributes; |
pthread_attr_init(&attributes); |
// Pthreads are joinable by default, so only specify the detached |
// attribute if the thread should be non-joinable. |
- if (!joinable) { |
+ if (!joinable) |
pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); |
- } |
// Get a better default if available. |
if (stack_size == 0) |
@@ -103,33 +92,29 @@ bool CreateThread(size_t stack_size, bool joinable, |
if (stack_size > 0) |
pthread_attr_setstacksize(&attributes, stack_size); |
- ThreadParams params; |
- params.delegate = delegate; |
- params.joinable = joinable; |
- params.priority = priority; |
- params.handle = thread_handle; |
+ scoped_ptr<ThreadParams> params(new ThreadParams); |
+ params->delegate = delegate; |
+ params->joinable = joinable; |
+ params->priority = priority; |
pthread_t handle; |
- int err = pthread_create(&handle, |
- &attributes, |
- ThreadFunc, |
- ¶ms); |
- success = !err; |
- if (!success) { |
- // Value of |handle| is undefined if pthread_create fails. |
+ int err = |
+ pthread_create(&handle, &attributes, ThreadFunc, params.get()); |
+ bool success = !err; |
+ if (success) { |
+ // ThreadParams should be deleted on the created thread after used. |
+ ThreadParams* unused_params = params.release(); |
+ ALLOW_UNUSED_LOCAL(unused_params); |
kinuko
2015/07/02 14:23:35
nit: ignore_result(param.release()) would work
Takashi Toyoshima
2015/07/03 04:31:35
Done.
|
+ } else { |
+ // Value of |thread_handle| is undefined if pthread_create fails. |
Takashi Toyoshima
2015/07/02 13:57:50
Oops, self review note: s/thread_handle/handle/.
Takashi Toyoshima
2015/07/03 04:31:35
Done.
|
handle = 0; |
errno = err; |
PLOG(ERROR) << "pthread_create"; |
} |
+ *thread_handle = PlatformThreadHandle(handle); |
pthread_attr_destroy(&attributes); |
- // Don't let this call complete until the thread id |
- // is set in the handle. |
- if (success) |
- params.handle_set.Wait(); |
- CHECK_EQ(handle, thread_handle->platform_handle()); |
- |
return success; |
} |