ipc/chromium/src/base/platform_thread_win.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial