| 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 &platform_handle, | 67 &platform_handle, |
| 68 0, | 68 0, |
| 69 FALSE, | 69 FALSE, |
| 70 DUPLICATE_SAME_ACCESS); | 70 DUPLICATE_SAME_ACCESS); |
| 71 | 71 |
| 72 win::ScopedHandle scoped_platform_handle; | 72 win::ScopedHandle scoped_platform_handle; |
| 73 | 73 |
| 74 if (did_dup) { | 74 if (did_dup) { |
| 75 scoped_platform_handle.Set(platform_handle); | 75 scoped_platform_handle.Set(platform_handle); |
| 76 ThreadIdNameManager::GetInstance()->RegisterThread( | 76 ThreadIdNameManager::GetInstance()->RegisterThread( |
| 77 scoped_platform_handle.Get(), | 77 PlatformThread::CurrentHandle(), PlatformThread::CurrentId()); |
| 78 PlatformThread::CurrentId()); | |
| 79 } | 78 } |
| 80 | 79 |
| 81 delete thread_params; | 80 delete thread_params; |
| 82 delegate->ThreadMain(); | 81 delegate->ThreadMain(); |
| 83 | 82 |
| 84 if (did_dup) { | 83 if (did_dup) { |
| 85 ThreadIdNameManager::GetInstance()->RemoveName( | 84 ThreadIdNameManager::GetInstance()->RemoveName( |
| 86 scoped_platform_handle.Get(), | 85 PlatformThread::CurrentHandle(), PlatformThread::CurrentId()); |
| 87 PlatformThread::CurrentId()); | |
| 88 } | 86 } |
| 89 | 87 |
| 90 return NULL; | 88 return NULL; |
| 91 } | 89 } |
| 92 | 90 |
| 93 // CreateThreadInternal() matches PlatformThread::CreateWithPriority(), except | 91 // CreateThreadInternal() matches PlatformThread::CreateWithPriority(), except |
| 94 // that |out_thread_handle| may be NULL, in which case a non-joinable thread is | 92 // that |out_thread_handle| may be NULL, in which case a non-joinable thread is |
| 95 // created. | 93 // created. |
| 96 bool CreateThreadInternal(size_t stack_size, | 94 bool CreateThreadInternal(size_t stack_size, |
| 97 PlatformThread::Delegate* delegate, | 95 PlatformThread::Delegate* delegate, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 116 // http://www.microsoft.com/msj/1099/win32/win321099.aspx | 114 // http://www.microsoft.com/msj/1099/win32/win321099.aspx |
| 117 PlatformThreadId thread_id; | 115 PlatformThreadId thread_id; |
| 118 void* thread_handle = CreateThread( | 116 void* thread_handle = CreateThread( |
| 119 NULL, stack_size, ThreadFunc, params, flags, &thread_id); | 117 NULL, stack_size, ThreadFunc, params, flags, &thread_id); |
| 120 if (!thread_handle) { | 118 if (!thread_handle) { |
| 121 delete params; | 119 delete params; |
| 122 return false; | 120 return false; |
| 123 } | 121 } |
| 124 | 122 |
| 125 if (out_thread_handle) | 123 if (out_thread_handle) |
| 126 *out_thread_handle = PlatformThreadHandle(thread_handle, thread_id); | 124 *out_thread_handle = PlatformThreadHandle(thread_handle); |
| 127 else | 125 else |
| 128 CloseHandle(thread_handle); | 126 CloseHandle(thread_handle); |
| 129 return true; | 127 return true; |
| 130 } | 128 } |
| 131 | 129 |
| 132 } // namespace | 130 } // namespace |
| 133 | 131 |
| 134 // static | 132 // static |
| 135 PlatformThreadId PlatformThread::CurrentId() { | 133 PlatformThreadId PlatformThread::CurrentId() { |
| 136 return ::GetCurrentThreadId(); | 134 return ::GetCurrentThreadId(); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 } | 202 } |
| 205 | 203 |
| 206 // static | 204 // static |
| 207 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { | 205 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { |
| 208 return CreateThreadInternal( | 206 return CreateThreadInternal( |
| 209 stack_size, delegate, NULL, ThreadPriority::NORMAL); | 207 stack_size, delegate, NULL, ThreadPriority::NORMAL); |
| 210 } | 208 } |
| 211 | 209 |
| 212 // static | 210 // static |
| 213 void PlatformThread::Join(PlatformThreadHandle thread_handle) { | 211 void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
| 214 DCHECK(thread_handle.platform_handle()); | 212 DCHECK(thread_handle.handle_); |
| 215 // TODO(willchan): Enable this check once I can get it to work for Windows | 213 // TODO(willchan): Enable this check once I can get it to work for Windows |
| 216 // shutdown. | 214 // shutdown. |
| 217 // Joining another thread may block the current thread for a long time, since | 215 // Joining another thread may block the current thread for a long time, since |
| 218 // the thread referred to by |thread_handle| may still be running long-lived / | 216 // the thread referred to by |thread_handle| may still be running long-lived / |
| 219 // blocking tasks. | 217 // blocking tasks. |
| 220 #if 0 | 218 #if 0 |
| 221 base::ThreadRestrictions::AssertIOAllowed(); | 219 base::ThreadRestrictions::AssertIOAllowed(); |
| 222 #endif | 220 #endif |
| 223 | 221 |
| 224 // Wait for the thread to exit. It should already have terminated but make | 222 // Wait for the thread to exit. It should already have terminated but make |
| 225 // sure this assumption is valid. | 223 // sure this assumption is valid. |
| 226 DWORD result = WaitForSingleObject(thread_handle.platform_handle(), INFINITE); | 224 DWORD result = WaitForSingleObject(thread_handle.handle_, INFINITE); |
| 227 if (result != WAIT_OBJECT_0) { | 225 if (result != WAIT_OBJECT_0) { |
| 228 // Debug info for bug 127931. | 226 // Debug info for bug 127931. |
| 229 DWORD error = GetLastError(); | 227 DWORD error = GetLastError(); |
| 230 debug::Alias(&error); | 228 debug::Alias(&error); |
| 231 debug::Alias(&result); | 229 debug::Alias(&result); |
| 232 CHECK(false); | 230 CHECK(false); |
| 233 } | 231 } |
| 234 | 232 |
| 235 CloseHandle(thread_handle.platform_handle()); | 233 CloseHandle(thread_handle.handle_); |
| 236 } | 234 } |
| 237 | 235 |
| 238 // static | 236 // static |
| 239 void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) { | 237 void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) { |
| 240 int desired_priority = THREAD_PRIORITY_ERROR_RETURN; | 238 int desired_priority = THREAD_PRIORITY_ERROR_RETURN; |
| 241 switch (priority) { | 239 switch (priority) { |
| 242 case ThreadPriority::BACKGROUND: | 240 case ThreadPriority::BACKGROUND: |
| 243 desired_priority = THREAD_PRIORITY_LOWEST; | 241 desired_priority = THREAD_PRIORITY_LOWEST; |
| 244 break; | 242 break; |
| 245 case ThreadPriority::NORMAL: | 243 case ThreadPriority::NORMAL: |
| 246 desired_priority = THREAD_PRIORITY_NORMAL; | 244 desired_priority = THREAD_PRIORITY_NORMAL; |
| 247 break; | 245 break; |
| 248 case ThreadPriority::DISPLAY: | 246 case ThreadPriority::DISPLAY: |
| 249 desired_priority = THREAD_PRIORITY_ABOVE_NORMAL; | 247 desired_priority = THREAD_PRIORITY_ABOVE_NORMAL; |
| 250 break; | 248 break; |
| 251 case ThreadPriority::REALTIME_AUDIO: | 249 case ThreadPriority::REALTIME_AUDIO: |
| 252 desired_priority = THREAD_PRIORITY_TIME_CRITICAL; | 250 desired_priority = THREAD_PRIORITY_TIME_CRITICAL; |
| 253 break; | 251 break; |
| 254 default: | 252 default: |
| 255 NOTREACHED() << "Unknown priority."; | 253 NOTREACHED() << "Unknown priority."; |
| 256 break; | 254 break; |
| 257 } | 255 } |
| 258 DCHECK_NE(desired_priority, THREAD_PRIORITY_ERROR_RETURN); | 256 DCHECK_NE(desired_priority, THREAD_PRIORITY_ERROR_RETURN); |
| 259 | 257 |
| 260 #ifndef NDEBUG | 258 #ifndef NDEBUG |
| 261 const BOOL success = | 259 const BOOL success = |
| 262 #endif | 260 #endif |
| 263 ::SetThreadPriority(PlatformThread::CurrentHandle().platform_handle(), | 261 ::SetThreadPriority(PlatformThread::CurrentHandle().handle_, |
| 264 desired_priority); | 262 desired_priority); |
| 265 DPLOG_IF(ERROR, !success) << "Failed to set thread priority to " | 263 DPLOG_IF(ERROR, !success) << "Failed to set thread priority to " |
| 266 << desired_priority; | 264 << desired_priority; |
| 267 } | 265 } |
| 268 | 266 |
| 269 // static | 267 // static |
| 270 ThreadPriority PlatformThread::GetCurrentThreadPriority() { | 268 ThreadPriority PlatformThread::GetCurrentThreadPriority() { |
| 271 int priority = | 269 int priority = ::GetThreadPriority(PlatformThread::CurrentHandle().handle_); |
| 272 ::GetThreadPriority(PlatformThread::CurrentHandle().platform_handle()); | |
| 273 switch (priority) { | 270 switch (priority) { |
| 274 case THREAD_PRIORITY_LOWEST: | 271 case THREAD_PRIORITY_LOWEST: |
| 275 return ThreadPriority::BACKGROUND; | 272 return ThreadPriority::BACKGROUND; |
| 276 case THREAD_PRIORITY_NORMAL: | 273 case THREAD_PRIORITY_NORMAL: |
| 277 return ThreadPriority::NORMAL; | 274 return ThreadPriority::NORMAL; |
| 278 case THREAD_PRIORITY_ABOVE_NORMAL: | 275 case THREAD_PRIORITY_ABOVE_NORMAL: |
| 279 return ThreadPriority::DISPLAY; | 276 return ThreadPriority::DISPLAY; |
| 280 case THREAD_PRIORITY_TIME_CRITICAL: | 277 case THREAD_PRIORITY_TIME_CRITICAL: |
| 281 return ThreadPriority::REALTIME_AUDIO; | 278 return ThreadPriority::REALTIME_AUDIO; |
| 282 case THREAD_PRIORITY_ERROR_RETURN: | 279 case THREAD_PRIORITY_ERROR_RETURN: |
| 283 DPCHECK(false) << "GetThreadPriority error"; // Falls through. | 280 DPCHECK(false) << "GetThreadPriority error"; // Falls through. |
| 284 default: | 281 default: |
| 285 NOTREACHED() << "Unexpected priority: " << priority; | 282 NOTREACHED() << "Unexpected priority: " << priority; |
| 286 return ThreadPriority::NORMAL; | 283 return ThreadPriority::NORMAL; |
| 287 } | 284 } |
| 288 } | 285 } |
| 289 | 286 |
| 290 } // namespace base | 287 } // namespace base |
| OLD | NEW |