| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/threading/platform_thread.h" | 5 #include "base/threading/platform_thread.h" |
| 6 | 6 |
| 7 #include "base/debug/alias.h" | 7 #include "base/debug/alias.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/threading/thread_local.h" | 9 #include "base/threading/thread_local.h" |
| 10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
| 11 #include "base/tracked_objects.h" |
| 11 | 12 |
| 12 #include "base/win/windows_version.h" | 13 #include "base/win/windows_version.h" |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 static ThreadLocalPointer<char> current_thread_name; | 19 static ThreadLocalPointer<char> current_thread_name; |
| 19 | 20 |
| 20 // The information on how to set the thread name comes from | 21 // The information on how to set the thread name comes from |
| 21 // a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx | 22 // a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx |
| 22 const DWORD kVCThreadNameException = 0x406D1388; | 23 const DWORD kVCThreadNameException = 0x406D1388; |
| 23 | 24 |
| 24 typedef struct tagTHREADNAME_INFO { | 25 typedef struct tagTHREADNAME_INFO { |
| 25 DWORD dwType; // Must be 0x1000. | 26 DWORD dwType; // Must be 0x1000. |
| 26 LPCSTR szName; // Pointer to name (in user addr space). | 27 LPCSTR szName; // Pointer to name (in user addr space). |
| 27 DWORD dwThreadID; // Thread ID (-1=caller thread). | 28 DWORD dwThreadID; // Thread ID (-1=caller thread). |
| 28 DWORD dwFlags; // Reserved for future use, must be zero. | 29 DWORD dwFlags; // Reserved for future use, must be zero. |
| 29 } THREADNAME_INFO; | 30 } THREADNAME_INFO; |
| 30 | 31 |
| 32 // This function has try handling, so it is separated out of its caller. |
| 33 void SetNameInternal(PlatformThreadId thread_id, const char* name) { |
| 34 THREADNAME_INFO info; |
| 35 info.dwType = 0x1000; |
| 36 info.szName = name; |
| 37 info.dwThreadID = thread_id; |
| 38 info.dwFlags = 0; |
| 39 |
| 40 __try { |
| 41 RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD), |
| 42 reinterpret_cast<DWORD_PTR*>(&info)); |
| 43 } __except(EXCEPTION_CONTINUE_EXECUTION) { |
| 44 } |
| 45 } |
| 46 |
| 31 struct ThreadParams { | 47 struct ThreadParams { |
| 32 PlatformThread::Delegate* delegate; | 48 PlatformThread::Delegate* delegate; |
| 33 bool joinable; | 49 bool joinable; |
| 34 }; | 50 }; |
| 35 | 51 |
| 36 DWORD __stdcall ThreadFunc(void* params) { | 52 DWORD __stdcall ThreadFunc(void* params) { |
| 37 ThreadParams* thread_params = static_cast<ThreadParams*>(params); | 53 ThreadParams* thread_params = static_cast<ThreadParams*>(params); |
| 38 PlatformThread::Delegate* delegate = thread_params->delegate; | 54 PlatformThread::Delegate* delegate = thread_params->delegate; |
| 39 if (!thread_params->joinable) | 55 if (!thread_params->joinable) |
| 40 base::ThreadRestrictions::SetSingletonAllowed(false); | 56 base::ThreadRestrictions::SetSingletonAllowed(false); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 } | 109 } |
| 94 | 110 |
| 95 // static | 111 // static |
| 96 void PlatformThread::Sleep(int duration_ms) { | 112 void PlatformThread::Sleep(int duration_ms) { |
| 97 ::Sleep(duration_ms); | 113 ::Sleep(duration_ms); |
| 98 } | 114 } |
| 99 | 115 |
| 100 // static | 116 // static |
| 101 void PlatformThread::SetName(const char* name) { | 117 void PlatformThread::SetName(const char* name) { |
| 102 current_thread_name.Set(const_cast<char*>(name)); | 118 current_thread_name.Set(const_cast<char*>(name)); |
| 119 tracked_objects::ThreadData::InitializeThreadContext(name); |
| 103 | 120 |
| 104 // The debugger needs to be around to catch the name in the exception. If | 121 // The debugger needs to be around to catch the name in the exception. If |
| 105 // there isn't a debugger, we are just needlessly throwing an exception. | 122 // there isn't a debugger, we are just needlessly throwing an exception. |
| 106 if (!::IsDebuggerPresent()) | 123 if (!::IsDebuggerPresent()) |
| 107 return; | 124 return; |
| 108 | 125 |
| 109 THREADNAME_INFO info; | 126 SetNameInternal(CurrentId(), name); |
| 110 info.dwType = 0x1000; | |
| 111 info.szName = name; | |
| 112 info.dwThreadID = CurrentId(); | |
| 113 info.dwFlags = 0; | |
| 114 | |
| 115 __try { | |
| 116 RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD), | |
| 117 reinterpret_cast<DWORD_PTR*>(&info)); | |
| 118 } __except(EXCEPTION_CONTINUE_EXECUTION) { | |
| 119 } | |
| 120 } | 127 } |
| 121 | 128 |
| 122 // static | 129 // static |
| 123 const char* PlatformThread::GetName() { | 130 const char* PlatformThread::GetName() { |
| 124 return current_thread_name.Get(); | 131 return current_thread_name.Get(); |
| 125 } | 132 } |
| 126 | 133 |
| 127 // static | 134 // static |
| 128 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, | 135 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, |
| 129 PlatformThreadHandle* thread_handle) { | 136 PlatformThreadHandle* thread_handle) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 case kThreadPriority_RealtimeAudio: | 173 case kThreadPriority_RealtimeAudio: |
| 167 ::SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL); | 174 ::SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL); |
| 168 break; | 175 break; |
| 169 default: | 176 default: |
| 170 NOTIMPLEMENTED(); | 177 NOTIMPLEMENTED(); |
| 171 break; | 178 break; |
| 172 } | 179 } |
| 173 } | 180 } |
| 174 | 181 |
| 175 } // namespace base | 182 } // namespace base |
| OLD | NEW |