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