Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(335)

Unified Diff: base/threading/platform_thread_posix.cc

Issue 1150563002: base/threading: Lazy thread startup for pthreads (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase, and do not use private handle_ Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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,
- &params);
- 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;
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698