Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/platform_thread.h" | 5 #include "base/platform_thread.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/thread_restrictions.h" | |
| 8 #include "base/win/windows_version.h" | 9 #include "base/win/windows_version.h" |
| 9 | 10 |
| 10 namespace { | 11 namespace { |
| 11 | 12 |
| 12 // The information on how to set the thread name comes from | 13 // The information on how to set the thread name comes from |
| 13 // a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx | 14 // a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx |
| 14 const DWORD kVCThreadNameException = 0x406D1388; | 15 const DWORD kVCThreadNameException = 0x406D1388; |
| 15 | 16 |
| 16 typedef struct tagTHREADNAME_INFO { | 17 typedef struct tagTHREADNAME_INFO { |
| 17 DWORD dwType; // Must be 0x1000. | 18 DWORD dwType; // Must be 0x1000. |
| 18 LPCSTR szName; // Pointer to name (in user addr space). | 19 LPCSTR szName; // Pointer to name (in user addr space). |
| 19 DWORD dwThreadID; // Thread ID (-1=caller thread). | 20 DWORD dwThreadID; // Thread ID (-1=caller thread). |
| 20 DWORD dwFlags; // Reserved for future use, must be zero. | 21 DWORD dwFlags; // Reserved for future use, must be zero. |
| 21 } THREADNAME_INFO; | 22 } THREADNAME_INFO; |
| 22 | 23 |
| 23 DWORD __stdcall ThreadFunc(void* closure) { | 24 struct ThreadParams { |
| 24 PlatformThread::Delegate* delegate = | 25 PlatformThread::Delegate* delegate; |
| 25 static_cast<PlatformThread::Delegate*>(closure); | 26 bool joinable; |
| 27 }; | |
| 28 | |
| 29 DWORD __stdcall ThreadFunc(void* params) { | |
| 30 ThreadParams* thread_params = static_cast<ThreadParams*>(params); | |
| 31 PlatformThread::Delegate* delegate = thread_params->delegate; | |
| 32 if (!thread_params->joinable) | |
| 33 base::ThreadRestrictions::SetSingletonAllowed(false); | |
| 34 delete thread_params; | |
| 26 delegate->ThreadMain(); | 35 delegate->ThreadMain(); |
| 27 return NULL; | 36 return NULL; |
| 28 } | 37 } |
| 29 | 38 |
| 30 } // namespace | 39 } // namespace |
| 31 | 40 |
| 32 // static | 41 // static |
| 33 PlatformThreadId PlatformThread::CurrentId() { | 42 PlatformThreadId PlatformThread::CurrentId() { |
| 34 return GetCurrentThreadId(); | 43 return GetCurrentThreadId(); |
| 35 } | 44 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 58 info.dwFlags = 0; | 67 info.dwFlags = 0; |
| 59 | 68 |
| 60 __try { | 69 __try { |
| 61 RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD), | 70 RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD), |
| 62 reinterpret_cast<DWORD_PTR*>(&info)); | 71 reinterpret_cast<DWORD_PTR*>(&info)); |
| 63 } __except(EXCEPTION_CONTINUE_EXECUTION) { | 72 } __except(EXCEPTION_CONTINUE_EXECUTION) { |
| 64 } | 73 } |
| 65 } | 74 } |
| 66 | 75 |
| 67 // static | 76 // static |
| 68 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, | 77 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, |
|
wtc
2010/11/17 01:56:18
You should document that passing out_thread_handle
willchan no longer on Chromium
2010/11/17 19:03:28
Thanks for pointing this out. I guess I missed th
| |
| 69 PlatformThreadHandle* thread_handle) { | 78 PlatformThreadHandle* out_thread_handle) { |
| 79 PlatformThreadHandle thread_handle; | |
| 70 unsigned int flags = 0; | 80 unsigned int flags = 0; |
| 71 if (stack_size > 0 && base::win::GetVersion() >= base::win::VERSION_XP) { | 81 if (stack_size > 0 && base::win::GetVersion() >= base::win::VERSION_XP) { |
| 72 flags = STACK_SIZE_PARAM_IS_A_RESERVATION; | 82 flags = STACK_SIZE_PARAM_IS_A_RESERVATION; |
| 73 } else { | 83 } else { |
| 74 stack_size = 0; | 84 stack_size = 0; |
| 75 } | 85 } |
| 76 | 86 |
| 87 ThreadParams* params = new ThreadParams; | |
| 88 params->delegate = delegate; | |
| 89 params->joinable = out_thread_handle != NULL; | |
| 90 | |
| 77 // Using CreateThread here vs _beginthreadex makes thread creation a bit | 91 // Using CreateThread here vs _beginthreadex makes thread creation a bit |
| 78 // faster and doesn't require the loader lock to be available. Our code will | 92 // faster and doesn't require the loader lock to be available. Our code will |
| 79 // have to work running on CreateThread() threads anyway, since we run code | 93 // have to work running on CreateThread() threads anyway, since we run code |
| 80 // on the Windows thread pool, etc. For some background on the difference: | 94 // on the Windows thread pool, etc. For some background on the difference: |
| 81 // http://www.microsoft.com/msj/1099/win32/win321099.aspx | 95 // http://www.microsoft.com/msj/1099/win32/win321099.aspx |
| 82 *thread_handle = CreateThread( | 96 thread_handle = CreateThread( |
| 83 NULL, stack_size, ThreadFunc, delegate, flags, NULL); | 97 NULL, stack_size, ThreadFunc, params, flags, NULL); |
| 84 return *thread_handle != NULL; | 98 if (!thread_handle) { |
| 99 delete params; | |
| 100 return false; | |
| 101 } | |
| 102 | |
| 103 if (out_thread_handle) | |
| 104 *out_thread_handle = thread_handle; | |
| 105 else | |
| 106 CloseHandle(thread_handle); | |
| 107 return true; | |
| 85 } | 108 } |
| 86 | 109 |
| 87 // static | 110 // static |
| 88 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { | 111 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { |
| 89 PlatformThreadHandle thread_handle; | 112 return Create(stack_size, delegate, NULL); |
| 90 bool result = Create(stack_size, delegate, &thread_handle); | |
| 91 CloseHandle(thread_handle); | |
| 92 return result; | |
| 93 } | 113 } |
| 94 | 114 |
| 95 // static | 115 // static |
| 96 void PlatformThread::Join(PlatformThreadHandle thread_handle) { | 116 void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
| 97 DCHECK(thread_handle); | 117 DCHECK(thread_handle); |
| 98 | 118 |
| 99 // Wait for the thread to exit. It should already have terminated but make | 119 // Wait for the thread to exit. It should already have terminated but make |
| 100 // sure this assumption is valid. | 120 // sure this assumption is valid. |
| 101 DWORD result = WaitForSingleObject(thread_handle, INFINITE); | 121 DWORD result = WaitForSingleObject(thread_handle, INFINITE); |
| 102 DCHECK_EQ(WAIT_OBJECT_0, result); | 122 DCHECK_EQ(WAIT_OBJECT_0, result); |
| 103 | 123 |
| 104 CloseHandle(thread_handle); | 124 CloseHandle(thread_handle); |
| 105 } | 125 } |
| OLD | NEW |