| 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 return result; | 199 return result; |
| 199 } | 200 } |
| 200 | 201 |
| 201 // static | 202 // static |
| 202 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { | 203 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { |
| 203 return CreateThreadInternal(stack_size, delegate, NULL); | 204 return CreateThreadInternal(stack_size, delegate, NULL); |
| 204 } | 205 } |
| 205 | 206 |
| 206 // static | 207 // static |
| 207 void PlatformThread::Join(PlatformThreadHandle thread_handle) { | 208 void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
| 208 DCHECK(thread_handle.handle_); | 209 DCHECK(thread_handle.platform_handle()); |
| 209 // TODO(willchan): Enable this check once I can get it to work for Windows | 210 // TODO(willchan): Enable this check once I can get it to work for Windows |
| 210 // shutdown. | 211 // shutdown. |
| 211 // Joining another thread may block the current thread for a long time, since | 212 // Joining another thread may block the current thread for a long time, since |
| 212 // the thread referred to by |thread_handle| may still be running long-lived / | 213 // the thread referred to by |thread_handle| may still be running long-lived / |
| 213 // blocking tasks. | 214 // blocking tasks. |
| 214 #if 0 | 215 #if 0 |
| 215 base::ThreadRestrictions::AssertIOAllowed(); | 216 base::ThreadRestrictions::AssertIOAllowed(); |
| 216 #endif | 217 #endif |
| 217 | 218 |
| 218 // Wait for the thread to exit. It should already have terminated but make | 219 // Wait for the thread to exit. It should already have terminated but make |
| 219 // sure this assumption is valid. | 220 // sure this assumption is valid. |
| 220 DWORD result = WaitForSingleObject(thread_handle.handle_, INFINITE); | 221 DWORD result = WaitForSingleObject(thread_handle.platform_handle(), INFINITE); |
| 221 if (result != WAIT_OBJECT_0) { | 222 if (result != WAIT_OBJECT_0) { |
| 222 // Debug info for bug 127931. | 223 // Debug info for bug 127931. |
| 223 DWORD error = GetLastError(); | 224 DWORD error = GetLastError(); |
| 224 debug::Alias(&error); | 225 debug::Alias(&error); |
| 225 debug::Alias(&result); | 226 debug::Alias(&result); |
| 226 debug::Alias(&thread_handle.handle_); | |
| 227 CHECK(false); | 227 CHECK(false); |
| 228 } | 228 } |
| 229 | 229 |
| 230 CloseHandle(thread_handle.handle_); | 230 CloseHandle(thread_handle.platform_handle()); |
| 231 } | 231 } |
| 232 | 232 |
| 233 // static | 233 // static |
| 234 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, | 234 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, |
| 235 ThreadPriority priority) { | 235 ThreadPriority priority) { |
| 236 DCHECK(!handle.is_null()); | 236 DCHECK(!handle.is_null()); |
| 237 | 237 |
| 238 int desired_priority = THREAD_PRIORITY_ERROR_RETURN; | 238 int desired_priority = THREAD_PRIORITY_ERROR_RETURN; |
| 239 switch (priority) { | 239 switch (priority) { |
| 240 case ThreadPriority::BACKGROUND: | 240 case ThreadPriority::BACKGROUND: |
| (...skipping 10 matching lines...) Expand all Loading... |
| 251 break; | 251 break; |
| 252 default: | 252 default: |
| 253 NOTREACHED() << "Unknown priority."; | 253 NOTREACHED() << "Unknown priority."; |
| 254 break; | 254 break; |
| 255 } | 255 } |
| 256 DCHECK_NE(desired_priority, THREAD_PRIORITY_ERROR_RETURN); | 256 DCHECK_NE(desired_priority, THREAD_PRIORITY_ERROR_RETURN); |
| 257 | 257 |
| 258 #ifndef NDEBUG | 258 #ifndef NDEBUG |
| 259 const BOOL success = | 259 const BOOL success = |
| 260 #endif | 260 #endif |
| 261 ::SetThreadPriority(handle.handle_, desired_priority); | 261 ::SetThreadPriority(handle.platform_handle(), desired_priority); |
| 262 DPLOG_IF(ERROR, !success) << "Failed to set thread priority to " | 262 DPLOG_IF(ERROR, !success) << "Failed to set thread priority to " |
| 263 << desired_priority; | 263 << desired_priority; |
| 264 } | 264 } |
| 265 | 265 |
| 266 // static | 266 // static |
| 267 ThreadPriority PlatformThread::GetThreadPriority(PlatformThreadHandle handle) { | 267 ThreadPriority PlatformThread::GetThreadPriority(PlatformThreadHandle handle) { |
| 268 DCHECK(!handle.is_null()); | 268 DCHECK(!handle.is_null()); |
| 269 | 269 |
| 270 int priority = ::GetThreadPriority(handle.handle_); | 270 int priority = ::GetThreadPriority(handle.platform_handle()); |
| 271 switch (priority) { | 271 switch (priority) { |
| 272 case THREAD_PRIORITY_LOWEST: | 272 case THREAD_PRIORITY_LOWEST: |
| 273 return ThreadPriority::BACKGROUND; | 273 return ThreadPriority::BACKGROUND; |
| 274 case THREAD_PRIORITY_NORMAL: | 274 case THREAD_PRIORITY_NORMAL: |
| 275 return ThreadPriority::NORMAL; | 275 return ThreadPriority::NORMAL; |
| 276 case THREAD_PRIORITY_ABOVE_NORMAL: | 276 case THREAD_PRIORITY_ABOVE_NORMAL: |
| 277 return ThreadPriority::DISPLAY; | 277 return ThreadPriority::DISPLAY; |
| 278 case THREAD_PRIORITY_TIME_CRITICAL: | 278 case THREAD_PRIORITY_TIME_CRITICAL: |
| 279 return ThreadPriority::REALTIME_AUDIO; | 279 return ThreadPriority::REALTIME_AUDIO; |
| 280 case THREAD_PRIORITY_ERROR_RETURN: | 280 case THREAD_PRIORITY_ERROR_RETURN: |
| 281 DPCHECK(false) << "GetThreadPriority error"; // Falls through. | 281 DPCHECK(false) << "GetThreadPriority error"; // Falls through. |
| 282 default: | 282 default: |
| 283 NOTREACHED() << "Unexpected priority: " << priority; | 283 NOTREACHED() << "Unexpected priority: " << priority; |
| 284 return ThreadPriority::NORMAL; | 284 return ThreadPriority::NORMAL; |
| 285 } | 285 } |
| 286 } | 286 } |
| 287 | 287 |
| 288 } // namespace base | 288 } // namespace base |
| OLD | NEW |