1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/threading/platform_thread_win.h"
6
7 #include <windows.h>
8
9 #include <stddef.h>
10
11 #include <string>
12
13 #include "base/debug/alias.h"
14 #include "base/debug/crash_logging.h"
15 #include "base/debug/profiler.h"
16 #include "base/feature_list.h"
17 #include "base/logging.h"
18 #include "base/memory/raw_ptr.h"
19 #include "base/metrics/histogram_macros.h"
20 #include "base/process/memory.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "base/threading/scoped_blocking_call.h"
24 #include "base/threading/scoped_thread_priority.h"
25 #include "base/threading/thread_id_name_manager.h"
26 #include "base/threading/thread_restrictions.h"
27 #include "base/threading/threading_features.h"
28 #include "base/time/time_override.h"
29 #include "base/win/scoped_handle.h"
30 #include "base/win/windows_version.h"
31 #include "build/build_config.h"
32 #include "partition_alloc/partition_alloc_buildflags.h"
33
34 #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
35 #include "partition_alloc/stack/stack.h"
36 #endif
37
38 namespace base {
39
40 BASE_FEATURE(kUseThreadPriorityLowest,
41 "UseThreadPriorityLowest",
42 base::FEATURE_DISABLED_BY_DEFAULT);
43 BASE_FEATURE(kAboveNormalCompositingBrowserWin,
44 "AboveNormalCompositingBrowserWin",
45 base::FEATURE_ENABLED_BY_DEFAULT);
46
47 BASE_FEATURE(kBackgroundThreadNormalMemoryPriorityWin,
48 "BackgroundThreadNormalMemoryPriorityWin",
49 base::FEATURE_DISABLED_BY_DEFAULT);
50
51 namespace {
52
53 // Flag used to set thread priority to |THREAD_PRIORITY_LOWEST| for
54 // |kUseThreadPriorityLowest| Feature.
55 std::atomic<bool> g_use_thread_priority_lowest{false};
56 // Flag used to map Compositing ThreadType |THREAD_PRIORITY_ABOVE_NORMAL| on the
57 // UI thread for |kAboveNormalCompositingBrowserWin| Feature.
58 std::atomic<bool> g_above_normal_compositing_browser{true};
59 // Flag used to set thread memory priority to |MEMORY_PRIORITY_NORMAL| on
60 // background threads for |kThreadNormalMemoryPriorityWin| Feature.
61 std::atomic<bool> g_background_thread_normal_memory_priority_win{false};
62
63 // These values are sometimes returned by ::GetThreadPriority().
64 constexpr int kWinDisplayPriority1 = 5;
65 constexpr int kWinDisplayPriority2 = 6;
66
67 // The information on how to set the thread name comes from
68 // a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx
69 const DWORD kVCThreadNameException = 0x406D1388;
70
71 typedef struct tagTHREADNAME_INFO {
72 DWORD dwType; // Must be 0x1000.
73 LPCSTR szName; // Pointer to name (in user addr space).
74 DWORD dwThreadID; // Thread ID (-1=caller thread).
75 DWORD dwFlags; // Reserved for future use, must be zero.
76 } THREADNAME_INFO;
77
78 // The SetThreadDescription API was brought in version 1607 of Windows 10.
79 typedef HRESULT(WINAPI* SetThreadDescription)(HANDLE hThread,
80 PCWSTR lpThreadDescription);
81
82 // This function has try handling, so it is separated out of its caller.
SetNameInternal(PlatformThreadId thread_id,const char * name)83 void SetNameInternal(PlatformThreadId thread_id, const char* name) {
84 THREADNAME_INFO info;
85 info.dwType = 0x1000;
86 info.szName = name;
87 info.dwThreadID = thread_id;
88 info.dwFlags = 0;
89
90 __try {
91 RaiseException(kVCThreadNameException, 0, sizeof(info) / sizeof(ULONG_PTR),
92 reinterpret_cast<ULONG_PTR*>(&info));
93 } __except (EXCEPTION_EXECUTE_HANDLER) {
94 }
95 }
96
97 struct ThreadParams {
98 raw_ptr<PlatformThread::Delegate> delegate;
99 bool joinable;
100 ThreadType thread_type;
101 MessagePumpType message_pump_type;
102 };
103
ThreadFunc(void * params)104 DWORD __stdcall ThreadFunc(void* params) {
105 ThreadParams* thread_params = static_cast<ThreadParams*>(params);
106 PlatformThread::Delegate* delegate = thread_params->delegate;
107 if (!thread_params->joinable)
108 base::DisallowSingleton();
109
110 if (thread_params->thread_type != ThreadType::kDefault)
111 internal::SetCurrentThreadType(thread_params->thread_type,
112 thread_params->message_pump_type);
113
114 // Retrieve a copy of the thread handle to use as the key in the
115 // thread name mapping.
116 PlatformThreadHandle::Handle platform_handle;
117 BOOL did_dup = DuplicateHandle(GetCurrentProcess(),
118 GetCurrentThread(),
119 GetCurrentProcess(),
120 &platform_handle,
121 0,
122 FALSE,
123 DUPLICATE_SAME_ACCESS);
124
125 #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
126 partition_alloc::internal::StackTopRegistry::Get().NotifyThreadCreated();
127 #endif
128
129 win::ScopedHandle scoped_platform_handle;
130
131 if (did_dup) {
132 scoped_platform_handle.Set(platform_handle);
133 ThreadIdNameManager::GetInstance()->RegisterThread(
134 scoped_platform_handle.get(), PlatformThread::CurrentId());
135 }
136
137 delete thread_params;
138 delegate->ThreadMain();
139
140 if (did_dup) {
141 ThreadIdNameManager::GetInstance()->RemoveName(scoped_platform_handle.get(),
142 PlatformThread::CurrentId());
143 }
144
145 #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
146 partition_alloc::internal::StackTopRegistry::Get().NotifyThreadDestroyed();
147 #endif
148
149 // Ensure thread priority is at least NORMAL before initiating thread
150 // destruction. Thread destruction on Windows holds the LdrLock while
151 // performing TLS destruction which causes hangs if performed at background
152 // priority (priority inversion) (see: http://crbug.com/1096203).
153 if (::GetThreadPriority(::GetCurrentThread()) < THREAD_PRIORITY_NORMAL)
154 PlatformThread::SetCurrentThreadType(ThreadType::kDefault);
155
156 return 0;
157 }
158
159 // CreateThreadInternal() matches PlatformThread::CreateWithType(), except
160 // that |out_thread_handle| may be nullptr, in which case a non-joinable thread
161 // is created.
CreateThreadInternal(size_t stack_size,PlatformThread::Delegate * delegate,PlatformThreadHandle * out_thread_handle,ThreadType thread_type,MessagePumpType message_pump_type)162 bool CreateThreadInternal(size_t stack_size,
163 PlatformThread::Delegate* delegate,
164 PlatformThreadHandle* out_thread_handle,
165 ThreadType thread_type,
166 MessagePumpType message_pump_type) {
167 unsigned int flags = 0;
168 if (stack_size > 0) {
169 flags = STACK_SIZE_PARAM_IS_A_RESERVATION;
170 #if defined(ARCH_CPU_32_BITS)
171 } else {
172 // The process stack size is increased to give spaces to |RendererMain| in
173 // |chrome/BUILD.gn|, but keep the default stack size of other threads to
174 // 1MB for the address space pressure.
175 flags = STACK_SIZE_PARAM_IS_A_RESERVATION;
176 static BOOL is_wow64 = -1;
177 if (is_wow64 == -1 && !IsWow64Process(GetCurrentProcess(), &is_wow64))
178 is_wow64 = FALSE;
179 // When is_wow64 is set that means we are running on 64-bit Windows and we
180 // get 4 GiB of address space. In that situation we can afford to use 1 MiB
181 // of address space for stacks. When running on 32-bit Windows we only get
182 // 2 GiB of address space so we need to conserve. Typically stack usage on
183 // these threads is only about 100 KiB.
184 if (is_wow64)
185 stack_size = 1024 * 1024;
186 else
187 stack_size = 512 * 1024;
188 #endif
189 }
190
191 ThreadParams* params = new ThreadParams;
192 params->delegate = delegate;
193 params->joinable = out_thread_handle != nullptr;
194 params->thread_type = thread_type;
195 params->message_pump_type = message_pump_type;
196
197 // Using CreateThread here vs _beginthreadex makes thread creation a bit
198 // faster and doesn't require the loader lock to be available. Our code will
199 // have to work running on CreateThread() threads anyway, since we run code on
200 // the Windows thread pool, etc. For some background on the difference:
201 // http://www.microsoft.com/msj/1099/win32/win321099.aspx
202 void* thread_handle =
203 ::CreateThread(nullptr, stack_size, ThreadFunc, params, flags, nullptr);
204
205 if (!thread_handle) {
206 DWORD last_error = ::GetLastError();
207
208 switch (last_error) {
209 case ERROR_NOT_ENOUGH_MEMORY:
210 case ERROR_OUTOFMEMORY:
211 case ERROR_COMMITMENT_LIMIT:
212 case ERROR_COMMITMENT_MINIMUM:
213 TerminateBecauseOutOfMemory(stack_size);
214 break;
215
216 default:
217 static auto* last_error_crash_key = debug::AllocateCrashKeyString(
218 "create_thread_last_error", debug::CrashKeySize::Size32);
219 debug::SetCrashKeyString(last_error_crash_key,
220 base::NumberToString(last_error));
221 break;
222 }
223
224 delete params;
225 return false;
226 }
227
228 if (out_thread_handle)
229 *out_thread_handle = PlatformThreadHandle(thread_handle);
230 else
231 CloseHandle(thread_handle);
232 return true;
233 }
234
235 } // namespace
236
237 namespace internal {
238
AssertMemoryPriority(HANDLE thread,int memory_priority)239 void AssertMemoryPriority(HANDLE thread, int memory_priority) {
240 #if DCHECK_IS_ON()
241 static const auto get_thread_information_fn =
242 reinterpret_cast<decltype(&::GetThreadInformation)>(::GetProcAddress(
243 ::GetModuleHandle(L"Kernel32.dll"), "GetThreadInformation"));
244
245 DCHECK(get_thread_information_fn);
246
247 MEMORY_PRIORITY_INFORMATION memory_priority_information = {};
248 DCHECK(get_thread_information_fn(thread, ::ThreadMemoryPriority,
249 &memory_priority_information,
250 sizeof(memory_priority_information)));
251
252 DCHECK_EQ(memory_priority,
253 static_cast<int>(memory_priority_information.MemoryPriority));
254 #endif
255 }
256
257 } // namespace internal
258
259 // static
CurrentId()260 PlatformThreadId PlatformThread::CurrentId() {
261 return ::GetCurrentThreadId();
262 }
263
264 // static
CurrentRef()265 PlatformThreadRef PlatformThread::CurrentRef() {
266 return PlatformThreadRef(::GetCurrentThreadId());
267 }
268
269 // static
CurrentHandle()270 PlatformThreadHandle PlatformThread::CurrentHandle() {
271 return PlatformThreadHandle(::GetCurrentThread());
272 }
273
274 // static
YieldCurrentThread()275 void PlatformThread::YieldCurrentThread() {
276 ::Sleep(0);
277 }
278
279 // static
Sleep(TimeDelta duration)280 void PlatformThread::Sleep(TimeDelta duration) {
281 // When measured with a high resolution clock, Sleep() sometimes returns much
282 // too early. We may need to call it repeatedly to get the desired duration.
283 // PlatformThread::Sleep doesn't support mock-time, so this always uses
284 // real-time.
285 const TimeTicks end = subtle::TimeTicksNowIgnoringOverride() + duration;
286 for (TimeTicks now = subtle::TimeTicksNowIgnoringOverride(); now < end;
287 now = subtle::TimeTicksNowIgnoringOverride()) {
288 ::Sleep(static_cast<DWORD>((end - now).InMillisecondsRoundedUp()));
289 }
290 }
291
292 // static
SetName(const std::string & name)293 void PlatformThread::SetName(const std::string& name) {
294 SetNameCommon(name);
295
296 // The SetThreadDescription API works even if no debugger is attached.
297 static auto set_thread_description_func =
298 reinterpret_cast<SetThreadDescription>(::GetProcAddress(
299 ::GetModuleHandle(L"Kernel32.dll"), "SetThreadDescription"));
300 if (set_thread_description_func) {
301 set_thread_description_func(::GetCurrentThread(),
302 base::UTF8ToWide(name).c_str());
303 }
304
305 // The debugger needs to be around to catch the name in the exception. If
306 // there isn't a debugger, we are just needlessly throwing an exception.
307 if (!::IsDebuggerPresent())
308 return;
309
310 SetNameInternal(CurrentId(), name.c_str());
311 }
312
313 // static
GetName()314 const char* PlatformThread::GetName() {
315 return ThreadIdNameManager::GetInstance()->GetName(CurrentId());
316 }
317
318 // static
CreateWithType(size_t stack_size,Delegate * delegate,PlatformThreadHandle * thread_handle,ThreadType thread_type,MessagePumpType pump_type_hint)319 bool PlatformThread::CreateWithType(size_t stack_size,
320 Delegate* delegate,
321 PlatformThreadHandle* thread_handle,
322 ThreadType thread_type,
323 MessagePumpType pump_type_hint) {
324 DCHECK(thread_handle);
325 return CreateThreadInternal(stack_size, delegate, thread_handle, thread_type,
326 pump_type_hint);
327 }
328
329 // static
CreateNonJoinable(size_t stack_size,Delegate * delegate)330 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) {
331 return CreateNonJoinableWithType(stack_size, delegate, ThreadType::kDefault);
332 }
333
334 // static
CreateNonJoinableWithType(size_t stack_size,Delegate * delegate,ThreadType thread_type,MessagePumpType pump_type_hint)335 bool PlatformThread::CreateNonJoinableWithType(size_t stack_size,
336 Delegate* delegate,
337 ThreadType thread_type,
338 MessagePumpType pump_type_hint) {
339 return CreateThreadInternal(stack_size, delegate, nullptr /* non-joinable */,
340 thread_type, pump_type_hint);
341 }
342
343 // static
Join(PlatformThreadHandle thread_handle)344 void PlatformThread::Join(PlatformThreadHandle thread_handle) {
345 DCHECK(thread_handle.platform_handle());
346
347 DWORD thread_id = 0;
348 thread_id = ::GetThreadId(thread_handle.platform_handle());
349 DWORD last_error = 0;
350 if (!thread_id)
351 last_error = ::GetLastError();
352
353 // Record information about the exiting thread in case joining hangs.
354 base::debug::Alias(&thread_id);
355 base::debug::Alias(&last_error);
356
357 base::internal::ScopedBlockingCallWithBaseSyncPrimitives scoped_blocking_call(
358 FROM_HERE, base::BlockingType::MAY_BLOCK);
359
360 // Wait for the thread to exit. It should already have terminated but make
361 // sure this assumption is valid.
362 CHECK_EQ(WAIT_OBJECT_0,
363 WaitForSingleObject(thread_handle.platform_handle(), INFINITE));
364 CloseHandle(thread_handle.platform_handle());
365 }
366
367 // static
Detach(PlatformThreadHandle thread_handle)368 void PlatformThread::Detach(PlatformThreadHandle thread_handle) {
369 CloseHandle(thread_handle.platform_handle());
370 }
371
372 // static
CanChangeThreadType(ThreadType from,ThreadType to)373 bool PlatformThread::CanChangeThreadType(ThreadType from, ThreadType to) {
374 return true;
375 }
376
377 namespace {
378
SetCurrentThreadPriority(ThreadType thread_type,MessagePumpType pump_type_hint)379 void SetCurrentThreadPriority(ThreadType thread_type,
380 MessagePumpType pump_type_hint) {
381 if (thread_type == ThreadType::kCompositing &&
382 pump_type_hint == MessagePumpType::UI &&
383 !g_above_normal_compositing_browser) {
384 // Ignore kCompositing thread type for UI thread as Windows has a
385 // priority boost mechanism. See
386 // https://docs.microsoft.com/en-us/windows/win32/procthread/priority-boosts
387 return;
388 }
389
390 PlatformThreadHandle::Handle thread_handle =
391 PlatformThread::CurrentHandle().platform_handle();
392
393 if (!g_use_thread_priority_lowest && thread_type != ThreadType::kBackground) {
394 // Exit background mode if the new priority is not BACKGROUND. This is a
395 // no-op if not in background mode.
396 ::SetThreadPriority(thread_handle, THREAD_MODE_BACKGROUND_END);
397 // We used to DCHECK that memory priority is MEMORY_PRIORITY_NORMAL here,
398 // but found that it is not always the case (e.g. in the installer).
399 // crbug.com/1340578#c2
400 }
401
402 int desired_priority = THREAD_PRIORITY_ERROR_RETURN;
403 switch (thread_type) {
404 case ThreadType::kBackground:
405 // Using THREAD_MODE_BACKGROUND_BEGIN instead of THREAD_PRIORITY_LOWEST
406 // improves input latency and navigation time. See
407 // https://docs.google.com/document/d/16XrOwuwTwKWdgPbcKKajTmNqtB4Am8TgS9GjbzBYLc0
408 //
409 // MSDN recommends THREAD_MODE_BACKGROUND_BEGIN for threads that perform
410 // background work, as it reduces disk and memory priority in addition to
411 // CPU priority.
412 desired_priority =
413 g_use_thread_priority_lowest.load(std::memory_order_relaxed)
414 ? THREAD_PRIORITY_LOWEST
415 : THREAD_MODE_BACKGROUND_BEGIN;
416 break;
417 case ThreadType::kUtility:
418 desired_priority = THREAD_PRIORITY_BELOW_NORMAL;
419 break;
420 case ThreadType::kResourceEfficient:
421 case ThreadType::kDefault:
422 desired_priority = THREAD_PRIORITY_NORMAL;
423 break;
424 case ThreadType::kCompositing:
425 case ThreadType::kDisplayCritical:
426 desired_priority = THREAD_PRIORITY_ABOVE_NORMAL;
427 break;
428 case ThreadType::kRealtimeAudio:
429 desired_priority = THREAD_PRIORITY_TIME_CRITICAL;
430 break;
431 }
432 DCHECK_NE(desired_priority, THREAD_PRIORITY_ERROR_RETURN);
433
434 [[maybe_unused]] const BOOL cpu_priority_success =
435 ::SetThreadPriority(thread_handle, desired_priority);
436 DPLOG_IF(ERROR, !cpu_priority_success)
437 << "Failed to set thread priority to " << desired_priority;
438
439 if (g_background_thread_normal_memory_priority_win &&
440 desired_priority == THREAD_MODE_BACKGROUND_BEGIN) {
441 // Override the memory priority.
442 MEMORY_PRIORITY_INFORMATION memory_priority{.MemoryPriority =
443 MEMORY_PRIORITY_NORMAL};
444 [[maybe_unused]] const BOOL memory_priority_success =
445 SetThreadInformation(thread_handle, ::ThreadMemoryPriority,
446 &memory_priority, sizeof(memory_priority));
447 DPLOG_IF(ERROR, !memory_priority_success)
448 << "Set thread memory priority failed.";
449 }
450
451 if (!g_use_thread_priority_lowest && thread_type == ThreadType::kBackground) {
452 // In a background process, THREAD_MODE_BACKGROUND_BEGIN lowers the memory
453 // and I/O priorities but not the CPU priority (kernel bug?). Use
454 // THREAD_PRIORITY_LOWEST to also lower the CPU priority.
455 // https://crbug.com/901483
456 if (PlatformThread::GetCurrentThreadPriorityForTest() !=
457 ThreadPriorityForTest::kBackground) {
458 ::SetThreadPriority(thread_handle, THREAD_PRIORITY_LOWEST);
459 // We used to DCHECK that memory priority is MEMORY_PRIORITY_VERY_LOW
460 // here, but found that it is not always the case (e.g. in the installer).
461 // crbug.com/1340578#c2
462 }
463 }
464 }
465
SetCurrentThreadQualityOfService(ThreadType thread_type)466 void SetCurrentThreadQualityOfService(ThreadType thread_type) {
467 // QoS and power throttling were introduced in Win10 1709.
468 bool desire_ecoqos = false;
469 switch (thread_type) {
470 case ThreadType::kBackground:
471 case ThreadType::kUtility:
472 case ThreadType::kResourceEfficient:
473 desire_ecoqos = true;
474 break;
475 case ThreadType::kDefault:
476 case ThreadType::kCompositing:
477 case ThreadType::kDisplayCritical:
478 case ThreadType::kRealtimeAudio:
479 desire_ecoqos = false;
480 break;
481 }
482
483 THREAD_POWER_THROTTLING_STATE thread_power_throttling_state{
484 .Version = THREAD_POWER_THROTTLING_CURRENT_VERSION,
485 .ControlMask =
486 desire_ecoqos ? THREAD_POWER_THROTTLING_EXECUTION_SPEED : 0ul,
487 .StateMask =
488 desire_ecoqos ? THREAD_POWER_THROTTLING_EXECUTION_SPEED : 0ul,
489 };
490 [[maybe_unused]] const BOOL success = ::SetThreadInformation(
491 ::GetCurrentThread(), ::ThreadPowerThrottling,
492 &thread_power_throttling_state, sizeof(thread_power_throttling_state));
493 // Failure is expected on versions of Windows prior to RS3.
494 DPLOG_IF(ERROR, !success && win::GetVersion() >= win::Version::WIN10_RS3)
495 << "Failed to set EcoQoS to " << std::boolalpha << desire_ecoqos;
496 }
497
498 } // namespace
499
500 namespace internal {
501
SetCurrentThreadTypeImpl(ThreadType thread_type,MessagePumpType pump_type_hint)502 void SetCurrentThreadTypeImpl(ThreadType thread_type,
503 MessagePumpType pump_type_hint) {
504 SetCurrentThreadPriority(thread_type, pump_type_hint);
505 SetCurrentThreadQualityOfService(thread_type);
506 }
507
508 } // namespace internal
509
510 // static
GetCurrentThreadPriorityForTest()511 ThreadPriorityForTest PlatformThread::GetCurrentThreadPriorityForTest() {
512 static_assert(
513 THREAD_PRIORITY_IDLE < 0,
514 "THREAD_PRIORITY_IDLE is >= 0 and will incorrectly cause errors.");
515 static_assert(
516 THREAD_PRIORITY_LOWEST < 0,
517 "THREAD_PRIORITY_LOWEST is >= 0 and will incorrectly cause errors.");
518 static_assert(THREAD_PRIORITY_BELOW_NORMAL < 0,
519 "THREAD_PRIORITY_BELOW_NORMAL is >= 0 and will incorrectly "
520 "cause errors.");
521 static_assert(
522 THREAD_PRIORITY_NORMAL == 0,
523 "The logic below assumes that THREAD_PRIORITY_NORMAL is zero. If it is "
524 "not, ThreadPriorityForTest::kBackground may be incorrectly detected.");
525 static_assert(THREAD_PRIORITY_ABOVE_NORMAL >= 0,
526 "THREAD_PRIORITY_ABOVE_NORMAL is < 0 and will incorrectly be "
527 "translated to ThreadPriorityForTest::kBackground.");
528 static_assert(THREAD_PRIORITY_HIGHEST >= 0,
529 "THREAD_PRIORITY_HIGHEST is < 0 and will incorrectly be "
530 "translated to ThreadPriorityForTest::kBackground.");
531 static_assert(THREAD_PRIORITY_TIME_CRITICAL >= 0,
532 "THREAD_PRIORITY_TIME_CRITICAL is < 0 and will incorrectly be "
533 "translated to ThreadPriorityForTest::kBackground.");
534 static_assert(THREAD_PRIORITY_ERROR_RETURN >= 0,
535 "THREAD_PRIORITY_ERROR_RETURN is < 0 and will incorrectly be "
536 "translated to ThreadPriorityForTest::kBackground.");
537
538 const int priority =
539 ::GetThreadPriority(PlatformThread::CurrentHandle().platform_handle());
540
541 // Negative values represent a background priority. We have observed -3, -4,
542 // -6 when THREAD_MODE_BACKGROUND_* is used. THREAD_PRIORITY_IDLE,
543 // THREAD_PRIORITY_LOWEST and THREAD_PRIORITY_BELOW_NORMAL are other possible
544 // negative values.
545 if (priority < THREAD_PRIORITY_BELOW_NORMAL)
546 return ThreadPriorityForTest::kBackground;
547
548 switch (priority) {
549 case THREAD_PRIORITY_BELOW_NORMAL:
550 return ThreadPriorityForTest::kUtility;
551 case THREAD_PRIORITY_NORMAL:
552 return ThreadPriorityForTest::kNormal;
553 case kWinDisplayPriority1:
554 [[fallthrough]];
555 case kWinDisplayPriority2:
556 return ThreadPriorityForTest::kDisplay;
557 case THREAD_PRIORITY_ABOVE_NORMAL:
558 case THREAD_PRIORITY_HIGHEST:
559 return ThreadPriorityForTest::kDisplay;
560 case THREAD_PRIORITY_TIME_CRITICAL:
561 return ThreadPriorityForTest::kRealtimeAudio;
562 case THREAD_PRIORITY_ERROR_RETURN:
563 DPCHECK(false) << "::GetThreadPriority error";
564 }
565
566 NOTREACHED() << "::GetThreadPriority returned " << priority << ".";
567 return ThreadPriorityForTest::kNormal;
568 }
569
InitializePlatformThreadFeatures()570 void InitializePlatformThreadFeatures() {
571 g_use_thread_priority_lowest.store(
572 FeatureList::IsEnabled(kUseThreadPriorityLowest),
573 std::memory_order_relaxed);
574 g_above_normal_compositing_browser.store(
575 FeatureList::IsEnabled(kAboveNormalCompositingBrowserWin),
576 std::memory_order_relaxed);
577 g_background_thread_normal_memory_priority_win.store(
578 FeatureList::IsEnabled(kBackgroundThreadNormalMemoryPriorityWin),
579 std::memory_order_relaxed);
580 }
581
582 // static
GetDefaultThreadStackSize()583 size_t PlatformThread::GetDefaultThreadStackSize() {
584 return 0;
585 }
586
587 } // namespace base
588