summaryrefslogtreecommitdiffstats
path: root/base/platform_thread_win.cc
diff options
context:
space:
mode:
authorwillchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-19 01:53:38 +0000
committerwillchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-19 01:53:38 +0000
commite87b8bbba3be2889489f5b4a94b708e67d0b8c67 (patch)
tree9c2b2210ab14fbc784b04cd0467d8f8b5bf42c68 /base/platform_thread_win.cc
parent244c1077eef720364e988b013c1b9b7bd5776a92 (diff)
downloadchromium_src-e87b8bbba3be2889489f5b4a94b708e67d0b8c67.zip
chromium_src-e87b8bbba3be2889489f5b4a94b708e67d0b8c67.tar.gz
chromium_src-e87b8bbba3be2889489f5b4a94b708e67d0b8c67.tar.bz2
Reland r65996. Disallows Singletons on non-joinable thread.
Test breakages caused by this change have been fixed here or in other changelists. BUG=61753 TEST=none Review URL: http://codereview.chromium.org/5024003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@66719 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/platform_thread_win.cc')
-rw-r--r--base/platform_thread_win.cc74
1 files changed, 52 insertions, 22 deletions
diff --git a/base/platform_thread_win.cc b/base/platform_thread_win.cc
index a4a1b52..e5afc52 100644
--- a/base/platform_thread_win.cc
+++ b/base/platform_thread_win.cc
@@ -5,6 +5,7 @@
#include "base/platform_thread.h"
#include "base/logging.h"
+#include "base/thread_restrictions.h"
#include "base/win/windows_version.h"
namespace {
@@ -20,13 +21,58 @@ typedef struct tagTHREADNAME_INFO {
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
-DWORD __stdcall ThreadFunc(void* closure) {
- PlatformThread::Delegate* delegate =
- static_cast<PlatformThread::Delegate*>(closure);
+struct ThreadParams {
+ PlatformThread::Delegate* delegate;
+ bool joinable;
+};
+
+DWORD __stdcall ThreadFunc(void* params) {
+ ThreadParams* thread_params = static_cast<ThreadParams*>(params);
+ PlatformThread::Delegate* delegate = thread_params->delegate;
+ if (!thread_params->joinable)
+ base::ThreadRestrictions::SetSingletonAllowed(false);
+ delete thread_params;
delegate->ThreadMain();
return NULL;
}
+// CreateThreadInternal() matches PlatformThread::Create(), except that
+// |out_thread_handle| may be NULL, in which case a non-joinable thread is
+// created.
+bool CreateThreadInternal(size_t stack_size,
+ PlatformThread::Delegate* delegate,
+ PlatformThreadHandle* out_thread_handle) {
+ PlatformThreadHandle thread_handle;
+ unsigned int flags = 0;
+ if (stack_size > 0 && base::win::GetVersion() >= base::win::VERSION_XP) {
+ flags = STACK_SIZE_PARAM_IS_A_RESERVATION;
+ } else {
+ stack_size = 0;
+ }
+
+ ThreadParams* params = new ThreadParams;
+ params->delegate = delegate;
+ params->joinable = out_thread_handle != NULL;
+
+ // Using CreateThread here vs _beginthreadex makes thread creation a bit
+ // faster and doesn't require the loader lock to be available. Our code will
+ // have to work running on CreateThread() threads anyway, since we run code
+ // on the Windows thread pool, etc. For some background on the difference:
+ // http://www.microsoft.com/msj/1099/win32/win321099.aspx
+ thread_handle = CreateThread(
+ NULL, stack_size, ThreadFunc, params, flags, NULL);
+ if (!thread_handle) {
+ delete params;
+ return false;
+ }
+
+ if (out_thread_handle)
+ *out_thread_handle = thread_handle;
+ else
+ CloseHandle(thread_handle);
+ return true;
+}
+
} // namespace
// static
@@ -67,29 +113,13 @@ void PlatformThread::SetName(const char* name) {
// static
bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
PlatformThreadHandle* thread_handle) {
- unsigned int flags = 0;
- if (stack_size > 0 && base::win::GetVersion() >= base::win::VERSION_XP) {
- flags = STACK_SIZE_PARAM_IS_A_RESERVATION;
- } else {
- stack_size = 0;
- }
-
- // Using CreateThread here vs _beginthreadex makes thread creation a bit
- // faster and doesn't require the loader lock to be available. Our code will
- // have to work running on CreateThread() threads anyway, since we run code
- // on the Windows thread pool, etc. For some background on the difference:
- // http://www.microsoft.com/msj/1099/win32/win321099.aspx
- *thread_handle = CreateThread(
- NULL, stack_size, ThreadFunc, delegate, flags, NULL);
- return *thread_handle != NULL;
+ DCHECK(thread_handle);
+ return CreateThreadInternal(stack_size, delegate, thread_handle);
}
// 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;
+ return CreateThreadInternal(stack_size, delegate, NULL);
}
// static