blob: 2dd325e9f4851e8ab14cc42ea83fb2f56bff2413 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
[email protected]e9ba26d2008-08-21 09:46:324
5#include "base/platform_thread.h"
6
[email protected]e9ba26d2008-08-21 09:46:327#include "base/logging.h"
8#include "base/win_util.h"
9
10namespace {
11
12// The information on how to set the thread name comes from
13// a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx
14const DWORD kVCThreadNameException = 0x406D1388;
15
16typedef struct tagTHREADNAME_INFO {
17 DWORD dwType; // Must be 0x1000.
18 LPCSTR szName; // Pointer to name (in user addr space).
19 DWORD dwThreadID; // Thread ID (-1=caller thread).
20 DWORD dwFlags; // Reserved for future use, must be zero.
21} THREADNAME_INFO;
22
[email protected]4bc9e802008-11-04 07:59:3723DWORD __stdcall ThreadFunc(void* closure) {
[email protected]e9ba26d2008-08-21 09:46:3224 PlatformThread::Delegate* delegate =
25 static_cast<PlatformThread::Delegate*>(closure);
[email protected]52a261f2009-03-03 15:01:1226 delegate->ThreadMain();
[email protected]e9ba26d2008-08-21 09:46:3227 return NULL;
28}
29
30} // namespace
31
32// static
[email protected]00493982009-01-23 00:25:2933PlatformThreadId PlatformThread::CurrentId() {
[email protected]e9ba26d2008-08-21 09:46:3234 return GetCurrentThreadId();
35}
36
37// static
38void PlatformThread::YieldCurrentThread() {
39 ::Sleep(0);
40}
41
42// static
43void PlatformThread::Sleep(int duration_ms) {
44 ::Sleep(duration_ms);
45}
46
47// static
[email protected]7d0f94452008-08-25 13:54:1848void PlatformThread::SetName(const char* name) {
[email protected]5f945e0f2008-10-02 15:10:4649 // The debugger needs to be around to catch the name in the exception. If
50 // there isn't a debugger, we are just needlessly throwing an exception.
51 if (!::IsDebuggerPresent())
52 return;
53
[email protected]e9ba26d2008-08-21 09:46:3254 THREADNAME_INFO info;
55 info.dwType = 0x1000;
56 info.szName = name;
[email protected]7d0f94452008-08-25 13:54:1857 info.dwThreadID = CurrentId();
[email protected]e9ba26d2008-08-21 09:46:3258 info.dwFlags = 0;
59
60 __try {
61 RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD),
62 reinterpret_cast<DWORD_PTR*>(&info));
63 } __except(EXCEPTION_CONTINUE_EXECUTION) {
64 }
65}
66
67// static
68bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
69 PlatformThreadHandle* thread_handle) {
70 unsigned int flags = 0;
71 if (stack_size > 0 && win_util::GetWinVersion() >= win_util::WINVERSION_XP) {
72 flags = STACK_SIZE_PARAM_IS_A_RESERVATION;
73 } else {
74 stack_size = 0;
75 }
76
[email protected]4bc9e802008-11-04 07:59:3777 // Using CreateThread here vs _beginthreadex makes thread creation a bit
78 // faster and doesn't require the loader lock to be available. Our code will
79 // have to work running on CreateThread() threads anyway, since we run code
80 // on the Windows thread pool, etc. For some background on the difference:
81 // http://www.microsoft.com/msj/1099/win32/win321099.aspx
82 *thread_handle = CreateThread(
83 NULL, stack_size, ThreadFunc, delegate, flags, NULL);
[email protected]e9ba26d2008-08-21 09:46:3284 return *thread_handle != NULL;
85}
86
87// static
[email protected]7bb813b2009-03-05 23:23:5988bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) {
89 PlatformThreadHandle thread_handle;
90 bool result = Create(stack_size, delegate, &thread_handle);
91 CloseHandle(thread_handle);
92 return result;
93}
94
95// static
[email protected]e9ba26d2008-08-21 09:46:3296void PlatformThread::Join(PlatformThreadHandle thread_handle) {
97 DCHECK(thread_handle);
98
99 // Wait for the thread to exit. It should already have terminated but make
100 // sure this assumption is valid.
101 DWORD result = WaitForSingleObject(thread_handle, INFINITE);
102 DCHECK_EQ(WAIT_OBJECT_0, result);
103
104 CloseHandle(thread_handle);
105}