| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/debug/profiler.h" | 8 #include "base/debug/profiler.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/threading/thread_id_name_manager.h" | 10 #include "base/threading/thread_id_name_manager.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 | 101 |
| 102 ThreadParams* params = new ThreadParams; | 102 ThreadParams* params = new ThreadParams; |
| 103 params->delegate = delegate; | 103 params->delegate = delegate; |
| 104 params->joinable = out_thread_handle != NULL; | 104 params->joinable = out_thread_handle != NULL; |
| 105 | 105 |
| 106 // Using CreateThread here vs _beginthreadex makes thread creation a bit | 106 // Using CreateThread here vs _beginthreadex makes thread creation a bit |
| 107 // faster and doesn't require the loader lock to be available. Our code will | 107 // faster and doesn't require the loader lock to be available. Our code will |
| 108 // have to work running on CreateThread() threads anyway, since we run code | 108 // have to work running on CreateThread() threads anyway, since we run code |
| 109 // on the Windows thread pool, etc. For some background on the difference: | 109 // on the Windows thread pool, etc. For some background on the difference: |
| 110 // http://www.microsoft.com/msj/1099/win32/win321099.aspx | 110 // http://www.microsoft.com/msj/1099/win32/win321099.aspx |
| 111 PlatformThreadId thread_id; |
| 111 void* thread_handle = CreateThread( | 112 void* thread_handle = CreateThread( |
| 112 NULL, stack_size, ThreadFunc, params, flags, NULL); | 113 NULL, stack_size, ThreadFunc, params, flags, &thread_id); |
| 113 if (!thread_handle) { | 114 if (!thread_handle) { |
| 114 delete params; | 115 delete params; |
| 115 return false; | 116 return false; |
| 116 } | 117 } |
| 117 | 118 |
| 118 if (out_thread_handle) | 119 if (out_thread_handle) |
| 119 *out_thread_handle = PlatformThreadHandle(thread_handle); | 120 *out_thread_handle = PlatformThreadHandle(thread_handle, thread_id); |
| 120 else | 121 else |
| 121 CloseHandle(thread_handle); | 122 CloseHandle(thread_handle); |
| 122 return true; | 123 return true; |
| 123 } | 124 } |
| 124 | 125 |
| 125 } // namespace | 126 } // namespace |
| 126 | 127 |
| 127 // static | 128 // static |
| 128 PlatformThreadId PlatformThread::CurrentId() { | 129 PlatformThreadId PlatformThread::CurrentId() { |
| 129 return ::GetCurrentThreadId(); | 130 return ::GetCurrentThreadId(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 147 // static | 148 // static |
| 148 void PlatformThread::Sleep(TimeDelta duration) { | 149 void PlatformThread::Sleep(TimeDelta duration) { |
| 149 // When measured with a high resolution clock, Sleep() sometimes returns much | 150 // When measured with a high resolution clock, Sleep() sometimes returns much |
| 150 // too early. We may need to call it repeatedly to get the desired duration. | 151 // too early. We may need to call it repeatedly to get the desired duration. |
| 151 TimeTicks end = TimeTicks::Now() + duration; | 152 TimeTicks end = TimeTicks::Now() + duration; |
| 152 for (TimeTicks now = TimeTicks::Now(); now < end; now = TimeTicks::Now()) | 153 for (TimeTicks now = TimeTicks::Now(); now < end; now = TimeTicks::Now()) |
| 153 ::Sleep(static_cast<DWORD>((end - now).InMillisecondsRoundedUp())); | 154 ::Sleep(static_cast<DWORD>((end - now).InMillisecondsRoundedUp())); |
| 154 } | 155 } |
| 155 | 156 |
| 156 // static | 157 // static |
| 157 void PlatformThread::SetName(const char* name) { | 158 void PlatformThread::SetName(const std::string& name) { |
| 158 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); | 159 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); |
| 159 | 160 |
| 160 // On Windows only, we don't need to tell the profiler about the "BrokerEvent" | 161 // On Windows only, we don't need to tell the profiler about the "BrokerEvent" |
| 161 // thread, as it exists only in the chrome.exe image, and never spawns or runs | 162 // thread, as it exists only in the chrome.exe image, and never spawns or runs |
| 162 // tasks (items which could be profiled). This test avoids the notification, | 163 // tasks (items which could be profiled). This test avoids the notification, |
| 163 // which would also (as a side effect) initialize the profiler in this unused | 164 // which would also (as a side effect) initialize the profiler in this unused |
| 164 // context, including setting up thread local storage, etc. The performance | 165 // context, including setting up thread local storage, etc. The performance |
| 165 // impact is not terrible, but there is no reason to do initialize it. | 166 // impact is not terrible, but there is no reason to do initialize it. |
| 166 if (0 != strcmp(name, "BrokerEvent")) | 167 if (name != "BrokerEvent") |
| 167 tracked_objects::ThreadData::InitializeThreadContext(name); | 168 tracked_objects::ThreadData::InitializeThreadContext(name); |
| 168 | 169 |
| 169 // The debugger needs to be around to catch the name in the exception. If | 170 // The debugger needs to be around to catch the name in the exception. If |
| 170 // there isn't a debugger, we are just needlessly throwing an exception. | 171 // there isn't a debugger, we are just needlessly throwing an exception. |
| 171 // If this image file is instrumented, we raise the exception anyway | 172 // If this image file is instrumented, we raise the exception anyway |
| 172 // to provide the profiler with human-readable thread names. | 173 // to provide the profiler with human-readable thread names. |
| 173 if (!::IsDebuggerPresent() && !base::debug::IsBinaryInstrumented()) | 174 if (!::IsDebuggerPresent() && !base::debug::IsBinaryInstrumented()) |
| 174 return; | 175 return; |
| 175 | 176 |
| 176 SetNameInternal(CurrentId(), name); | 177 SetNameInternal(CurrentId(), name.c_str()); |
| 177 } | 178 } |
| 178 | 179 |
| 179 // static | 180 // static |
| 180 const char* PlatformThread::GetName() { | 181 const char* PlatformThread::GetName() { |
| 181 return ThreadIdNameManager::GetInstance()->GetName(CurrentId()); | 182 return ThreadIdNameManager::GetInstance()->GetName(CurrentId()); |
| 182 } | 183 } |
| 183 | 184 |
| 184 // static | 185 // static |
| 185 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, | 186 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, |
| 186 PlatformThreadHandle* thread_handle) { | 187 PlatformThreadHandle* thread_handle) { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 return ThreadPriority::REALTIME_AUDIO; | 280 return ThreadPriority::REALTIME_AUDIO; |
| 280 case THREAD_PRIORITY_ERROR_RETURN: | 281 case THREAD_PRIORITY_ERROR_RETURN: |
| 281 DPCHECK(false) << "GetThreadPriority error"; // Falls through. | 282 DPCHECK(false) << "GetThreadPriority error"; // Falls through. |
| 282 default: | 283 default: |
| 283 NOTREACHED() << "Unexpected priority: " << priority; | 284 NOTREACHED() << "Unexpected priority: " << priority; |
| 284 return ThreadPriority::NORMAL; | 285 return ThreadPriority::NORMAL; |
| 285 } | 286 } |
| 286 } | 287 } |
| 287 | 288 |
| 288 } // namespace base | 289 } // namespace base |
| OLD | NEW |