platform_thread_win.cc 21 KB

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