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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 bool joinable; | 48 bool joinable; |
49 ThreadPriority priority; | 49 ThreadPriority priority; |
50 }; | 50 }; |
51 | 51 |
52 DWORD __stdcall ThreadFunc(void* params) { | 52 DWORD __stdcall ThreadFunc(void* params) { |
53 ThreadParams* thread_params = static_cast<ThreadParams*>(params); | 53 ThreadParams* thread_params = static_cast<ThreadParams*>(params); |
54 PlatformThread::Delegate* delegate = thread_params->delegate; | 54 PlatformThread::Delegate* delegate = thread_params->delegate; |
55 if (!thread_params->joinable) | 55 if (!thread_params->joinable) |
56 base::ThreadRestrictions::SetSingletonAllowed(false); | 56 base::ThreadRestrictions::SetSingletonAllowed(false); |
57 | 57 |
58 if (thread_params->priority != ThreadPriority::NORMAL) { | 58 if (thread_params->priority != ThreadPriority::NORMAL) |
59 PlatformThread::SetThreadPriority( | 59 PlatformThread::SetCurrentThreadPriority(thread_params->priority); |
60 PlatformThread::CurrentHandle(), thread_params->priority); | |
61 } | |
62 | 60 |
63 // Retrieve a copy of the thread handle to use as the key in the | 61 // Retrieve a copy of the thread handle to use as the key in the |
64 // thread name mapping. | 62 // thread name mapping. |
65 PlatformThreadHandle::Handle platform_handle; | 63 PlatformThreadHandle::Handle platform_handle; |
66 BOOL did_dup = DuplicateHandle(GetCurrentProcess(), | 64 BOOL did_dup = DuplicateHandle(GetCurrentProcess(), |
67 GetCurrentThread(), | 65 GetCurrentThread(), |
68 GetCurrentProcess(), | 66 GetCurrentProcess(), |
69 &platform_handle, | 67 &platform_handle, |
70 0, | 68 0, |
71 FALSE, | 69 FALSE, |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 DWORD error = GetLastError(); | 229 DWORD error = GetLastError(); |
232 debug::Alias(&error); | 230 debug::Alias(&error); |
233 debug::Alias(&result); | 231 debug::Alias(&result); |
234 CHECK(false); | 232 CHECK(false); |
235 } | 233 } |
236 | 234 |
237 CloseHandle(thread_handle.platform_handle()); | 235 CloseHandle(thread_handle.platform_handle()); |
238 } | 236 } |
239 | 237 |
240 // static | 238 // static |
241 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, | 239 void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) { |
242 ThreadPriority priority) { | |
243 DCHECK(!handle.is_null()); | |
244 | |
245 int desired_priority = THREAD_PRIORITY_ERROR_RETURN; | 240 int desired_priority = THREAD_PRIORITY_ERROR_RETURN; |
246 switch (priority) { | 241 switch (priority) { |
247 case ThreadPriority::BACKGROUND: | 242 case ThreadPriority::BACKGROUND: |
248 desired_priority = THREAD_PRIORITY_LOWEST; | 243 desired_priority = THREAD_PRIORITY_LOWEST; |
249 break; | 244 break; |
250 case ThreadPriority::NORMAL: | 245 case ThreadPriority::NORMAL: |
251 desired_priority = THREAD_PRIORITY_NORMAL; | 246 desired_priority = THREAD_PRIORITY_NORMAL; |
252 break; | 247 break; |
253 case ThreadPriority::DISPLAY: | 248 case ThreadPriority::DISPLAY: |
254 desired_priority = THREAD_PRIORITY_ABOVE_NORMAL; | 249 desired_priority = THREAD_PRIORITY_ABOVE_NORMAL; |
255 break; | 250 break; |
256 case ThreadPriority::REALTIME_AUDIO: | 251 case ThreadPriority::REALTIME_AUDIO: |
257 desired_priority = THREAD_PRIORITY_TIME_CRITICAL; | 252 desired_priority = THREAD_PRIORITY_TIME_CRITICAL; |
258 break; | 253 break; |
259 default: | 254 default: |
260 NOTREACHED() << "Unknown priority."; | 255 NOTREACHED() << "Unknown priority."; |
261 break; | 256 break; |
262 } | 257 } |
263 DCHECK_NE(desired_priority, THREAD_PRIORITY_ERROR_RETURN); | 258 DCHECK_NE(desired_priority, THREAD_PRIORITY_ERROR_RETURN); |
264 | 259 |
265 #ifndef NDEBUG | 260 #ifndef NDEBUG |
266 const BOOL success = | 261 const BOOL success = |
267 #endif | 262 #endif |
268 ::SetThreadPriority(handle.platform_handle(), desired_priority); | 263 ::SetThreadPriority(PlatformThread::CurrentHandle().platform_handle(), |
| 264 desired_priority); |
269 DPLOG_IF(ERROR, !success) << "Failed to set thread priority to " | 265 DPLOG_IF(ERROR, !success) << "Failed to set thread priority to " |
270 << desired_priority; | 266 << desired_priority; |
271 } | 267 } |
272 | 268 |
273 // static | 269 // static |
274 ThreadPriority PlatformThread::GetThreadPriority(PlatformThreadHandle handle) { | 270 ThreadPriority PlatformThread::GetCurrentThreadPriority() { |
275 DCHECK(!handle.is_null()); | 271 int priority = |
276 | 272 ::GetThreadPriority(PlatformThread::CurrentHandle().platform_handle()); |
277 int priority = ::GetThreadPriority(handle.platform_handle()); | |
278 switch (priority) { | 273 switch (priority) { |
279 case THREAD_PRIORITY_LOWEST: | 274 case THREAD_PRIORITY_LOWEST: |
280 return ThreadPriority::BACKGROUND; | 275 return ThreadPriority::BACKGROUND; |
281 case THREAD_PRIORITY_NORMAL: | 276 case THREAD_PRIORITY_NORMAL: |
282 return ThreadPriority::NORMAL; | 277 return ThreadPriority::NORMAL; |
283 case THREAD_PRIORITY_ABOVE_NORMAL: | 278 case THREAD_PRIORITY_ABOVE_NORMAL: |
284 return ThreadPriority::DISPLAY; | 279 return ThreadPriority::DISPLAY; |
285 case THREAD_PRIORITY_TIME_CRITICAL: | 280 case THREAD_PRIORITY_TIME_CRITICAL: |
286 return ThreadPriority::REALTIME_AUDIO; | 281 return ThreadPriority::REALTIME_AUDIO; |
287 case THREAD_PRIORITY_ERROR_RETURN: | 282 case THREAD_PRIORITY_ERROR_RETURN: |
288 DPCHECK(false) << "GetThreadPriority error"; // Falls through. | 283 DPCHECK(false) << "GetThreadPriority error"; // Falls through. |
289 default: | 284 default: |
290 NOTREACHED() << "Unexpected priority: " << priority; | 285 NOTREACHED() << "Unexpected priority: " << priority; |
291 return ThreadPriority::NORMAL; | 286 return ThreadPriority::NORMAL; |
292 } | 287 } |
293 } | 288 } |
294 | 289 |
295 } // namespace base | 290 } // namespace base |
OLD | NEW |