summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwillchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-05 23:23:59 +0000
committerwillchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-05 23:23:59 +0000
commit7bb813b123af8c00374b97c738712cd3629644ec (patch)
treec21de2bd74dbbfe50c11fa19d4d9f9f82c1297d1
parentc915560f44ab7a7a9dce6fef15cb16eea736cffa (diff)
downloadchromium_src-7bb813b123af8c00374b97c738712cd3629644ec.zip
chromium_src-7bb813b123af8c00374b97c738712cd3629644ec.tar.gz
chromium_src-7bb813b123af8c00374b97c738712cd3629644ec.tar.bz2
Added a new method CreateNonJoinable() to PlatformThread. Added the windows and posix implementations.
Review URL: http://codereview.chromium.org/40133 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@11053 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/platform_thread.h6
-rw-r--r--base/platform_thread_posix.cc33
-rw-r--r--base/platform_thread_win.cc8
3 files changed, 41 insertions, 6 deletions
diff --git a/base/platform_thread.h b/base/platform_thread.h
index 1ae9c0f..4a2d507 100644
--- a/base/platform_thread.h
+++ b/base/platform_thread.h
@@ -65,6 +65,11 @@ class PlatformThread {
static bool Create(size_t stack_size, Delegate* delegate,
PlatformThreadHandle* thread_handle);
+ // CreateNonJoinable() does the same thing as Create() except the thread
+ // cannot be Join()'d. Therefore, it also does not output a
+ // PlatformThreadHandle.
+ static bool CreateNonJoinable(size_t stack_size, Delegate* delegate);
+
// Joins with a thread created via the Create function. This function blocks
// the caller until the designated thread exits. This will invalidate
// |thread_handle|.
@@ -75,4 +80,3 @@ class PlatformThread {
};
#endif // BASE_PLATFORM_THREAD_H_
-
diff --git a/base/platform_thread_posix.cc b/base/platform_thread_posix.cc
index 4cd56d9..3789715 100644
--- a/base/platform_thread_posix.cc
+++ b/base/platform_thread_posix.cc
@@ -68,9 +68,11 @@ void PlatformThread::SetName(const char* name) {
// structure would be useful for debugging or not.
}
-// static
-bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
- PlatformThreadHandle* thread_handle) {
+namespace {
+
+bool CreateThread(size_t stack_size, bool joinable,
+ PlatformThread::Delegate* delegate,
+ PlatformThreadHandle* thread_handle) {
#if defined(OS_MACOSX)
base::InitThreading();
#endif // OS_MACOSX
@@ -79,8 +81,11 @@ bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
pthread_attr_t attributes;
pthread_attr_init(&attributes);
- // Pthreads are joinable by default, so we don't need to specify any special
- // attributes to be able to call pthread_join later.
+ // Pthreads are joinable by default, so only specify the detached attribute if
+ // the thread should be non-joinable.
+ if (!joinable) {
+ pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED);
+ }
if (stack_size > 0)
pthread_attr_setstacksize(&attributes, stack_size);
@@ -91,6 +96,24 @@ bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
return success;
}
+} // anonymous namespace
+
+// static
+bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
+ PlatformThreadHandle* thread_handle) {
+ return CreateThread(stack_size, true /* joinable thread */,
+ delegate, thread_handle);
+}
+
+// static
+bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) {
+ PlatformThreadHandle unused;
+
+ bool result = CreateThread(stack_size, false /* non-joinable thread */,
+ delegate, &unused);
+ return result;
+}
+
// static
void PlatformThread::Join(PlatformThreadHandle thread_handle) {
pthread_join(thread_handle, NULL);
diff --git a/base/platform_thread_win.cc b/base/platform_thread_win.cc
index 236c00b..2dd325e 100644
--- a/base/platform_thread_win.cc
+++ b/base/platform_thread_win.cc
@@ -85,6 +85,14 @@ bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
}
// static
+bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) {
+ PlatformThreadHandle thread_handle;
+ bool result = Create(stack_size, delegate, &thread_handle);
+ CloseHandle(thread_handle);
+ return result;
+}
+
+// static
void PlatformThread::Join(PlatformThreadHandle thread_handle) {
DCHECK(thread_handle);