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 28 matching lines...) Expand all Loading... |
39 __try { | 39 __try { |
40 RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD), | 40 RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD), |
41 reinterpret_cast<DWORD_PTR*>(&info)); | 41 reinterpret_cast<DWORD_PTR*>(&info)); |
42 } __except(EXCEPTION_CONTINUE_EXECUTION) { | 42 } __except(EXCEPTION_CONTINUE_EXECUTION) { |
43 } | 43 } |
44 } | 44 } |
45 | 45 |
46 struct ThreadParams { | 46 struct ThreadParams { |
47 PlatformThread::Delegate* delegate; | 47 PlatformThread::Delegate* delegate; |
48 bool joinable; | 48 bool joinable; |
| 49 ThreadPriority priority; |
49 }; | 50 }; |
50 | 51 |
51 DWORD __stdcall ThreadFunc(void* params) { | 52 DWORD __stdcall ThreadFunc(void* params) { |
52 ThreadParams* thread_params = static_cast<ThreadParams*>(params); | 53 ThreadParams* thread_params = static_cast<ThreadParams*>(params); |
53 PlatformThread::Delegate* delegate = thread_params->delegate; | 54 PlatformThread::Delegate* delegate = thread_params->delegate; |
54 if (!thread_params->joinable) | 55 if (!thread_params->joinable) |
55 base::ThreadRestrictions::SetSingletonAllowed(false); | 56 base::ThreadRestrictions::SetSingletonAllowed(false); |
56 | 57 |
| 58 if (thread_params->priority != ThreadPriority::NORMAL) { |
| 59 PlatformThread::SetThreadPriority( |
| 60 PlatformThread::CurrentHandle(), thread_params->priority); |
| 61 } |
| 62 |
57 // Retrieve a copy of the thread handle to use as the key in the | 63 // Retrieve a copy of the thread handle to use as the key in the |
58 // thread name mapping. | 64 // thread name mapping. |
59 PlatformThreadHandle::Handle platform_handle; | 65 PlatformThreadHandle::Handle platform_handle; |
60 BOOL did_dup = DuplicateHandle(GetCurrentProcess(), | 66 BOOL did_dup = DuplicateHandle(GetCurrentProcess(), |
61 GetCurrentThread(), | 67 GetCurrentThread(), |
62 GetCurrentProcess(), | 68 GetCurrentProcess(), |
63 &platform_handle, | 69 &platform_handle, |
64 0, | 70 0, |
65 FALSE, | 71 FALSE, |
66 DUPLICATE_SAME_ACCESS); | 72 DUPLICATE_SAME_ACCESS); |
(...skipping 12 matching lines...) Expand all Loading... |
79 | 85 |
80 if (did_dup) { | 86 if (did_dup) { |
81 ThreadIdNameManager::GetInstance()->RemoveName( | 87 ThreadIdNameManager::GetInstance()->RemoveName( |
82 scoped_platform_handle.Get(), | 88 scoped_platform_handle.Get(), |
83 PlatformThread::CurrentId()); | 89 PlatformThread::CurrentId()); |
84 } | 90 } |
85 | 91 |
86 return NULL; | 92 return NULL; |
87 } | 93 } |
88 | 94 |
89 // CreateThreadInternal() matches PlatformThread::Create(), except that | 95 // CreateThreadInternal() matches PlatformThread::CreateWithPriority(), except |
90 // |out_thread_handle| may be NULL, in which case a non-joinable thread is | 96 // that |out_thread_handle| may be NULL, in which case a non-joinable thread is |
91 // created. | 97 // created. |
92 bool CreateThreadInternal(size_t stack_size, | 98 bool CreateThreadInternal(size_t stack_size, |
93 PlatformThread::Delegate* delegate, | 99 PlatformThread::Delegate* delegate, |
94 PlatformThreadHandle* out_thread_handle) { | 100 PlatformThreadHandle* out_thread_handle, |
| 101 ThreadPriority priority) { |
95 unsigned int flags = 0; | 102 unsigned int flags = 0; |
96 if (stack_size > 0 && base::win::GetVersion() >= base::win::VERSION_XP) { | 103 if (stack_size > 0 && base::win::GetVersion() >= base::win::VERSION_XP) { |
97 flags = STACK_SIZE_PARAM_IS_A_RESERVATION; | 104 flags = STACK_SIZE_PARAM_IS_A_RESERVATION; |
98 } else { | 105 } else { |
99 stack_size = 0; | 106 stack_size = 0; |
100 } | 107 } |
101 | 108 |
102 ThreadParams* params = new ThreadParams; | 109 ThreadParams* params = new ThreadParams; |
103 params->delegate = delegate; | 110 params->delegate = delegate; |
104 params->joinable = out_thread_handle != NULL; | 111 params->joinable = out_thread_handle != NULL; |
| 112 params->priority = priority; |
105 | 113 |
106 // Using CreateThread here vs _beginthreadex makes thread creation a bit | 114 // 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 | 115 // 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 | 116 // 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: | 117 // on the Windows thread pool, etc. For some background on the difference: |
110 // http://www.microsoft.com/msj/1099/win32/win321099.aspx | 118 // http://www.microsoft.com/msj/1099/win32/win321099.aspx |
111 PlatformThreadId thread_id; | 119 PlatformThreadId thread_id; |
112 void* thread_handle = CreateThread( | 120 void* thread_handle = CreateThread( |
113 NULL, stack_size, ThreadFunc, params, flags, &thread_id); | 121 NULL, stack_size, ThreadFunc, params, flags, &thread_id); |
114 if (!thread_handle) { | 122 if (!thread_handle) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 } | 186 } |
179 | 187 |
180 // static | 188 // static |
181 const char* PlatformThread::GetName() { | 189 const char* PlatformThread::GetName() { |
182 return ThreadIdNameManager::GetInstance()->GetName(CurrentId()); | 190 return ThreadIdNameManager::GetInstance()->GetName(CurrentId()); |
183 } | 191 } |
184 | 192 |
185 // static | 193 // static |
186 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, | 194 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, |
187 PlatformThreadHandle* thread_handle) { | 195 PlatformThreadHandle* thread_handle) { |
188 DCHECK(thread_handle); | 196 return CreateWithPriority( |
189 return CreateThreadInternal(stack_size, delegate, thread_handle); | 197 stack_size, delegate, thread_handle, ThreadPriority::NORMAL); |
190 } | 198 } |
191 | 199 |
192 // static | 200 // static |
193 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate, | 201 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate, |
194 PlatformThreadHandle* thread_handle, | 202 PlatformThreadHandle* thread_handle, |
195 ThreadPriority priority) { | 203 ThreadPriority priority) { |
196 bool result = Create(stack_size, delegate, thread_handle); | 204 DCHECK(thread_handle); |
197 if (result) | 205 return CreateThreadInternal(stack_size, delegate, thread_handle, priority); |
198 SetThreadPriority(*thread_handle, priority); | |
199 return result; | |
200 } | 206 } |
201 | 207 |
202 // static | 208 // static |
203 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { | 209 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { |
204 return CreateThreadInternal(stack_size, delegate, NULL); | 210 return CreateThreadInternal( |
| 211 stack_size, delegate, NULL, ThreadPriority::NORMAL); |
205 } | 212 } |
206 | 213 |
207 // static | 214 // static |
208 void PlatformThread::Join(PlatformThreadHandle thread_handle) { | 215 void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
209 DCHECK(thread_handle.platform_handle()); | 216 DCHECK(thread_handle.platform_handle()); |
210 // TODO(willchan): Enable this check once I can get it to work for Windows | 217 // TODO(willchan): Enable this check once I can get it to work for Windows |
211 // shutdown. | 218 // shutdown. |
212 // Joining another thread may block the current thread for a long time, since | 219 // Joining another thread may block the current thread for a long time, since |
213 // the thread referred to by |thread_handle| may still be running long-lived / | 220 // the thread referred to by |thread_handle| may still be running long-lived / |
214 // blocking tasks. | 221 // blocking tasks. |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 return ThreadPriority::REALTIME_AUDIO; | 286 return ThreadPriority::REALTIME_AUDIO; |
280 case THREAD_PRIORITY_ERROR_RETURN: | 287 case THREAD_PRIORITY_ERROR_RETURN: |
281 DPCHECK(false) << "GetThreadPriority error"; // Falls through. | 288 DPCHECK(false) << "GetThreadPriority error"; // Falls through. |
282 default: | 289 default: |
283 NOTREACHED() << "Unexpected priority: " << priority; | 290 NOTREACHED() << "Unexpected priority: " << priority; |
284 return ThreadPriority::NORMAL; | 291 return ThreadPriority::NORMAL; |
285 } | 292 } |
286 } | 293 } |
287 | 294 |
288 } // namespace base | 295 } // namespace base |
OLD | NEW |