Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: base/threading/platform_thread_win.cc

Issue 1641513004: Update //base to chromium 9659b08ea5a34f889dc4166217f438095ddc10d2 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/threading/platform_thread_unittest.cc ('k') | base/threading/post_task_and_reply_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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::SetCurrentThreadPriority(thread_params->priority);
60
57 // 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
58 // thread name mapping. 62 // thread name mapping.
59 PlatformThreadHandle::Handle platform_handle; 63 PlatformThreadHandle::Handle platform_handle;
60 BOOL did_dup = DuplicateHandle(GetCurrentProcess(), 64 BOOL did_dup = DuplicateHandle(GetCurrentProcess(),
61 GetCurrentThread(), 65 GetCurrentThread(),
62 GetCurrentProcess(), 66 GetCurrentProcess(),
63 &platform_handle, 67 &platform_handle,
64 0, 68 0,
65 FALSE, 69 FALSE,
66 DUPLICATE_SAME_ACCESS); 70 DUPLICATE_SAME_ACCESS);
(...skipping 12 matching lines...) Expand all
79 83
80 if (did_dup) { 84 if (did_dup) {
81 ThreadIdNameManager::GetInstance()->RemoveName( 85 ThreadIdNameManager::GetInstance()->RemoveName(
82 scoped_platform_handle.Get(), 86 scoped_platform_handle.Get(),
83 PlatformThread::CurrentId()); 87 PlatformThread::CurrentId());
84 } 88 }
85 89
86 return NULL; 90 return NULL;
87 } 91 }
88 92
89 // CreateThreadInternal() matches PlatformThread::Create(), except that 93 // CreateThreadInternal() matches PlatformThread::CreateWithPriority(), except
90 // |out_thread_handle| may be NULL, in which case a non-joinable thread is 94 // that |out_thread_handle| may be NULL, in which case a non-joinable thread is
91 // created. 95 // created.
92 bool CreateThreadInternal(size_t stack_size, 96 bool CreateThreadInternal(size_t stack_size,
93 PlatformThread::Delegate* delegate, 97 PlatformThread::Delegate* delegate,
94 PlatformThreadHandle* out_thread_handle) { 98 PlatformThreadHandle* out_thread_handle,
99 ThreadPriority priority) {
95 unsigned int flags = 0; 100 unsigned int flags = 0;
96 if (stack_size > 0 && base::win::GetVersion() >= base::win::VERSION_XP) { 101 if (stack_size > 0 && base::win::GetVersion() >= base::win::VERSION_XP) {
97 flags = STACK_SIZE_PARAM_IS_A_RESERVATION; 102 flags = STACK_SIZE_PARAM_IS_A_RESERVATION;
98 } else { 103 } else {
99 stack_size = 0; 104 stack_size = 0;
100 } 105 }
101 106
102 ThreadParams* params = new ThreadParams; 107 ThreadParams* params = new ThreadParams;
103 params->delegate = delegate; 108 params->delegate = delegate;
104 params->joinable = out_thread_handle != NULL; 109 params->joinable = out_thread_handle != NULL;
110 params->priority = priority;
105 111
106 // Using CreateThread here vs _beginthreadex makes thread creation a bit 112 // 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 113 // 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 114 // 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: 115 // on the Windows thread pool, etc. For some background on the difference:
110 // http://www.microsoft.com/msj/1099/win32/win321099.aspx 116 // http://www.microsoft.com/msj/1099/win32/win321099.aspx
111 PlatformThreadId thread_id; 117 PlatformThreadId thread_id;
112 void* thread_handle = CreateThread( 118 void* thread_handle = CreateThread(
113 NULL, stack_size, ThreadFunc, params, flags, &thread_id); 119 NULL, stack_size, ThreadFunc, params, flags, &thread_id);
114 if (!thread_handle) { 120 if (!thread_handle) {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 } 184 }
179 185
180 // static 186 // static
181 const char* PlatformThread::GetName() { 187 const char* PlatformThread::GetName() {
182 return ThreadIdNameManager::GetInstance()->GetName(CurrentId()); 188 return ThreadIdNameManager::GetInstance()->GetName(CurrentId());
183 } 189 }
184 190
185 // static 191 // static
186 bool PlatformThread::Create(size_t stack_size, Delegate* delegate, 192 bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
187 PlatformThreadHandle* thread_handle) { 193 PlatformThreadHandle* thread_handle) {
188 DCHECK(thread_handle); 194 return CreateWithPriority(stack_size, delegate, thread_handle,
189 return CreateThreadInternal(stack_size, delegate, thread_handle); 195 ThreadPriority::NORMAL);
190 } 196 }
191 197
192 // static 198 // static
193 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate, 199 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate,
194 PlatformThreadHandle* thread_handle, 200 PlatformThreadHandle* thread_handle,
195 ThreadPriority priority) { 201 ThreadPriority priority) {
196 bool result = Create(stack_size, delegate, thread_handle); 202 DCHECK(thread_handle);
197 if (result) 203 return CreateThreadInternal(stack_size, delegate, thread_handle, priority);
198 SetThreadPriority(*thread_handle, priority);
199 return result;
200 } 204 }
201 205
202 // static 206 // static
203 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { 207 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) {
204 return CreateThreadInternal(stack_size, delegate, NULL); 208 return CreateThreadInternal(stack_size, delegate, NULL,
209 ThreadPriority::NORMAL);
205 } 210 }
206 211
207 // static 212 // static
208 void PlatformThread::Join(PlatformThreadHandle thread_handle) { 213 void PlatformThread::Join(PlatformThreadHandle thread_handle) {
209 DCHECK(thread_handle.platform_handle()); 214 DCHECK(thread_handle.platform_handle());
210 // TODO(willchan): Enable this check once I can get it to work for Windows 215 // TODO(willchan): Enable this check once I can get it to work for Windows
211 // shutdown. 216 // shutdown.
212 // Joining another thread may block the current thread for a long time, since 217 // 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 / 218 // the thread referred to by |thread_handle| may still be running long-lived /
214 // blocking tasks. 219 // blocking tasks.
215 #if 0 220 #if 0
216 base::ThreadRestrictions::AssertIOAllowed(); 221 base::ThreadRestrictions::AssertIOAllowed();
217 #endif 222 #endif
218 223
219 // Wait for the thread to exit. It should already have terminated but make 224 // Wait for the thread to exit. It should already have terminated but make
220 // sure this assumption is valid. 225 // sure this assumption is valid.
221 DWORD result = WaitForSingleObject(thread_handle.platform_handle(), INFINITE); 226 DWORD result = WaitForSingleObject(thread_handle.platform_handle(), INFINITE);
222 if (result != WAIT_OBJECT_0) { 227 if (result != WAIT_OBJECT_0) {
223 // Debug info for bug 127931. 228 // Debug info for bug 127931.
224 DWORD error = GetLastError(); 229 DWORD error = GetLastError();
225 debug::Alias(&error); 230 debug::Alias(&error);
226 debug::Alias(&result); 231 debug::Alias(&result);
227 CHECK(false); 232 CHECK(false);
228 } 233 }
229 234
230 CloseHandle(thread_handle.platform_handle()); 235 CloseHandle(thread_handle.platform_handle());
231 } 236 }
232 237
233 // static 238 // static
234 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, 239 void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) {
235 ThreadPriority priority) {
236 DCHECK(!handle.is_null());
237
238 int desired_priority = THREAD_PRIORITY_ERROR_RETURN; 240 int desired_priority = THREAD_PRIORITY_ERROR_RETURN;
239 switch (priority) { 241 switch (priority) {
240 case ThreadPriority::BACKGROUND: 242 case ThreadPriority::BACKGROUND:
241 desired_priority = THREAD_PRIORITY_LOWEST; 243 desired_priority = THREAD_PRIORITY_LOWEST;
242 break; 244 break;
243 case ThreadPriority::NORMAL: 245 case ThreadPriority::NORMAL:
244 desired_priority = THREAD_PRIORITY_NORMAL; 246 desired_priority = THREAD_PRIORITY_NORMAL;
245 break; 247 break;
246 case ThreadPriority::DISPLAY: 248 case ThreadPriority::DISPLAY:
247 desired_priority = THREAD_PRIORITY_ABOVE_NORMAL; 249 desired_priority = THREAD_PRIORITY_ABOVE_NORMAL;
248 break; 250 break;
249 case ThreadPriority::REALTIME_AUDIO: 251 case ThreadPriority::REALTIME_AUDIO:
250 desired_priority = THREAD_PRIORITY_TIME_CRITICAL; 252 desired_priority = THREAD_PRIORITY_TIME_CRITICAL;
251 break; 253 break;
252 default: 254 default:
253 NOTREACHED() << "Unknown priority."; 255 NOTREACHED() << "Unknown priority.";
254 break; 256 break;
255 } 257 }
256 DCHECK_NE(desired_priority, THREAD_PRIORITY_ERROR_RETURN); 258 DCHECK_NE(desired_priority, THREAD_PRIORITY_ERROR_RETURN);
257 259
258 #ifndef NDEBUG 260 #ifndef NDEBUG
259 const BOOL success = 261 const BOOL success =
260 #endif 262 #endif
261 ::SetThreadPriority(handle.platform_handle(), desired_priority); 263 ::SetThreadPriority(PlatformThread::CurrentHandle().platform_handle(),
264 desired_priority);
262 DPLOG_IF(ERROR, !success) << "Failed to set thread priority to " 265 DPLOG_IF(ERROR, !success) << "Failed to set thread priority to "
263 << desired_priority; 266 << desired_priority;
264 } 267 }
265 268
266 // static 269 // static
267 ThreadPriority PlatformThread::GetThreadPriority(PlatformThreadHandle handle) { 270 ThreadPriority PlatformThread::GetCurrentThreadPriority() {
268 DCHECK(!handle.is_null()); 271 int priority =
269 272 ::GetThreadPriority(PlatformThread::CurrentHandle().platform_handle());
270 int priority = ::GetThreadPriority(handle.platform_handle());
271 switch (priority) { 273 switch (priority) {
272 case THREAD_PRIORITY_LOWEST: 274 case THREAD_PRIORITY_LOWEST:
273 return ThreadPriority::BACKGROUND; 275 return ThreadPriority::BACKGROUND;
274 case THREAD_PRIORITY_NORMAL: 276 case THREAD_PRIORITY_NORMAL:
275 return ThreadPriority::NORMAL; 277 return ThreadPriority::NORMAL;
276 case THREAD_PRIORITY_ABOVE_NORMAL: 278 case THREAD_PRIORITY_ABOVE_NORMAL:
277 return ThreadPriority::DISPLAY; 279 return ThreadPriority::DISPLAY;
278 case THREAD_PRIORITY_TIME_CRITICAL: 280 case THREAD_PRIORITY_TIME_CRITICAL:
279 return ThreadPriority::REALTIME_AUDIO; 281 return ThreadPriority::REALTIME_AUDIO;
280 case THREAD_PRIORITY_ERROR_RETURN: 282 case THREAD_PRIORITY_ERROR_RETURN:
281 DPCHECK(false) << "GetThreadPriority error"; // Falls through. 283 DPCHECK(false) << "GetThreadPriority error"; // Falls through.
282 default: 284 default:
283 NOTREACHED() << "Unexpected priority: " << priority; 285 NOTREACHED() << "Unexpected priority: " << priority;
284 return ThreadPriority::NORMAL; 286 return ThreadPriority::NORMAL;
285 } 287 }
286 } 288 }
287 289
288 } // namespace base 290 } // namespace base
OLDNEW
« no previous file with comments | « base/threading/platform_thread_unittest.cc ('k') | base/threading/post_task_and_reply_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698