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
Esse commit está contido em:
willchan@chromium.org
2009-03-05 23:23:59 +00:00
commit 6b53d9adb9
3 arquivos alterados com 41 adições e 6 exclusões
+5 -1
Ver Arquivo
@@ -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_
+28 -5
Ver Arquivo
@@ -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);
+8
Ver Arquivo
@@ -84,6 +84,14 @@ bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
return *thread_handle != NULL;
}
// 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);