diff options
-rw-r--r-- | base/platform_thread.h | 6 | ||||
-rw-r--r-- | base/platform_thread_posix.cc | 33 | ||||
-rw-r--r-- | base/platform_thread_win.cc | 8 |
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); |