| 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/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/threading/thread_local.h" |
| 8 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
| 10 |
| 9 #include "base/win/windows_version.h" | 11 #include "base/win/windows_version.h" |
| 10 | 12 |
| 11 namespace base { | 13 namespace base { |
| 12 | 14 |
| 13 namespace { | 15 namespace { |
| 14 | 16 |
| 17 static ThreadLocalPointer<char> current_thread_name; |
| 18 |
| 15 // The information on how to set the thread name comes from | 19 // The information on how to set the thread name comes from |
| 16 // a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx | 20 // a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx |
| 17 const DWORD kVCThreadNameException = 0x406D1388; | 21 const DWORD kVCThreadNameException = 0x406D1388; |
| 18 | 22 |
| 19 typedef struct tagTHREADNAME_INFO { | 23 typedef struct tagTHREADNAME_INFO { |
| 20 DWORD dwType; // Must be 0x1000. | 24 DWORD dwType; // Must be 0x1000. |
| 21 LPCSTR szName; // Pointer to name (in user addr space). | 25 LPCSTR szName; // Pointer to name (in user addr space). |
| 22 DWORD dwThreadID; // Thread ID (-1=caller thread). | 26 DWORD dwThreadID; // Thread ID (-1=caller thread). |
| 23 DWORD dwFlags; // Reserved for future use, must be zero. | 27 DWORD dwFlags; // Reserved for future use, must be zero. |
| 24 } THREADNAME_INFO; | 28 } THREADNAME_INFO; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 ::Sleep(0); | 91 ::Sleep(0); |
| 88 } | 92 } |
| 89 | 93 |
| 90 // static | 94 // static |
| 91 void PlatformThread::Sleep(int duration_ms) { | 95 void PlatformThread::Sleep(int duration_ms) { |
| 92 ::Sleep(duration_ms); | 96 ::Sleep(duration_ms); |
| 93 } | 97 } |
| 94 | 98 |
| 95 // static | 99 // static |
| 96 void PlatformThread::SetName(const char* name) { | 100 void PlatformThread::SetName(const char* name) { |
| 101 current_thread_name.Set(const_cast<char*>(name)); |
| 102 |
| 97 // The debugger needs to be around to catch the name in the exception. If | 103 // The debugger needs to be around to catch the name in the exception. If |
| 98 // there isn't a debugger, we are just needlessly throwing an exception. | 104 // there isn't a debugger, we are just needlessly throwing an exception. |
| 99 if (!::IsDebuggerPresent()) | 105 if (!::IsDebuggerPresent()) |
| 100 return; | 106 return; |
| 101 | 107 |
| 102 THREADNAME_INFO info; | 108 THREADNAME_INFO info; |
| 103 info.dwType = 0x1000; | 109 info.dwType = 0x1000; |
| 104 info.szName = name; | 110 info.szName = name; |
| 105 info.dwThreadID = CurrentId(); | 111 info.dwThreadID = CurrentId(); |
| 106 info.dwFlags = 0; | 112 info.dwFlags = 0; |
| 107 | 113 |
| 108 __try { | 114 __try { |
| 109 RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD), | 115 RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD), |
| 110 reinterpret_cast<DWORD_PTR*>(&info)); | 116 reinterpret_cast<DWORD_PTR*>(&info)); |
| 111 } __except(EXCEPTION_CONTINUE_EXECUTION) { | 117 } __except(EXCEPTION_CONTINUE_EXECUTION) { |
| 112 } | 118 } |
| 113 } | 119 } |
| 114 | 120 |
| 115 // static | 121 // static |
| 122 const char* PlatformThread::GetName() { |
| 123 return current_thread_name.Get(); |
| 124 } |
| 125 |
| 126 // static |
| 116 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, | 127 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, |
| 117 PlatformThreadHandle* thread_handle) { | 128 PlatformThreadHandle* thread_handle) { |
| 118 DCHECK(thread_handle); | 129 DCHECK(thread_handle); |
| 119 return CreateThreadInternal(stack_size, delegate, thread_handle); | 130 return CreateThreadInternal(stack_size, delegate, thread_handle); |
| 120 } | 131 } |
| 121 | 132 |
| 122 // static | 133 // static |
| 123 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { | 134 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { |
| 124 return CreateThreadInternal(stack_size, delegate, NULL); | 135 return CreateThreadInternal(stack_size, delegate, NULL); |
| 125 } | 136 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 144 CloseHandle(thread_handle); | 155 CloseHandle(thread_handle); |
| 145 } | 156 } |
| 146 | 157 |
| 147 // static | 158 // static |
| 148 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { | 159 void PlatformThread::SetThreadPriority(PlatformThreadHandle, ThreadPriority) { |
| 149 // TODO(crogers): implement | 160 // TODO(crogers): implement |
| 150 NOTIMPLEMENTED(); | 161 NOTIMPLEMENTED(); |
| 151 } | 162 } |
| 152 | 163 |
| 153 } // namespace base | 164 } // namespace base |
| OLD | NEW |