1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
6 
7 #include "base/platform_thread.h"
8 
9 #include "base/logging.h"
10 #include "base/win_util.h"
11 
12 namespace {
13 
14 // The information on how to set the thread name comes from
15 // a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx
16 const DWORD kVCThreadNameException = 0x406D1388;
17 
18 typedef struct tagTHREADNAME_INFO {
19   DWORD dwType;      // Must be 0x1000.
20   LPCSTR szName;     // Pointer to name (in user addr space).
21   DWORD dwThreadID;  // Thread ID (-1=caller thread).
22   DWORD dwFlags;     // Reserved for future use, must be zero.
23 } THREADNAME_INFO;
24 
ThreadFunc(void * closure)25 DWORD __stdcall ThreadFunc(void* closure) {
26   PlatformThread::Delegate* delegate =
27       static_cast<PlatformThread::Delegate*>(closure);
28   delegate->ThreadMain();
29   return 0;
30 }
31 
32 }  // namespace
33 
34 // static
CurrentId()35 PlatformThreadId PlatformThread::CurrentId() { return GetCurrentThreadId(); }
36 
37 // static
YieldCurrentThread()38 void PlatformThread::YieldCurrentThread() { ::Sleep(0); }
39 
40 // static
Sleep(int duration_ms)41 void PlatformThread::Sleep(int duration_ms) { ::Sleep(duration_ms); }
42 
43 // static
SetName(const char * name)44 void PlatformThread::SetName(const char* name) {
45 #ifdef HAVE_SEH_EXCEPTIONS
46   // The debugger needs to be around to catch the name in the exception.  If
47   // there isn't a debugger, we are just needlessly throwing an exception.
48   if (!::IsDebuggerPresent()) return;
49 
50   THREADNAME_INFO info;
51   info.dwType = 0x1000;
52   info.szName = name;
53   info.dwThreadID = CurrentId();
54   info.dwFlags = 0;
55 
56   MOZ_SEH_TRY {
57     RaiseException(kVCThreadNameException, 0, sizeof(info) / sizeof(DWORD),
58                    reinterpret_cast<DWORD_PTR*>(&info));
59   }
60   MOZ_SEH_EXCEPT(EXCEPTION_CONTINUE_EXECUTION) {}
61 #endif
62 }
63 
64 // static
Create(size_t stack_size,Delegate * delegate,PlatformThreadHandle * thread_handle)65 bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
66                             PlatformThreadHandle* thread_handle) {
67   unsigned int flags = 0;
68   if (stack_size > 0) {
69     flags = STACK_SIZE_PARAM_IS_A_RESERVATION;
70   } else {
71     stack_size = 0;
72   }
73 
74   // Using CreateThread here vs _beginthreadex makes thread creation a bit
75   // faster and doesn't require the loader lock to be available.  Our code will
76   // have to work running on CreateThread() threads anyway, since we run code
77   // on the Windows thread pool, etc.  For some background on the difference:
78   //   http://www.microsoft.com/msj/1099/win32/win321099.aspx
79   *thread_handle =
80       CreateThread(NULL, stack_size, ThreadFunc, delegate, flags, NULL);
81   return *thread_handle != NULL;
82 }
83 
84 // static
CreateNonJoinable(size_t stack_size,Delegate * delegate)85 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) {
86   PlatformThreadHandle thread_handle;
87   bool result = Create(stack_size, delegate, &thread_handle);
88   CloseHandle(thread_handle);
89   return result;
90 }
91 
92 // static
Join(PlatformThreadHandle thread_handle)93 void PlatformThread::Join(PlatformThreadHandle thread_handle) {
94   DCHECK(thread_handle);
95 
96   // Wait for the thread to exit.  It should already have terminated but make
97   // sure this assumption is valid.
98   DWORD result = WaitForSingleObject(thread_handle, INFINITE);
99   DCHECK_EQ(WAIT_OBJECT_0, result);
100 
101   CloseHandle(thread_handle);
102 }
103