michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/platform_thread.h" michael@0: michael@0: #include "base/logging.h" michael@0: #include "base/win_util.h" michael@0: michael@0: namespace { michael@0: michael@0: // The information on how to set the thread name comes from michael@0: // a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx michael@0: const DWORD kVCThreadNameException = 0x406D1388; michael@0: michael@0: typedef struct tagTHREADNAME_INFO { michael@0: DWORD dwType; // Must be 0x1000. michael@0: LPCSTR szName; // Pointer to name (in user addr space). michael@0: DWORD dwThreadID; // Thread ID (-1=caller thread). michael@0: DWORD dwFlags; // Reserved for future use, must be zero. michael@0: } THREADNAME_INFO; michael@0: michael@0: DWORD __stdcall ThreadFunc(void* closure) { michael@0: PlatformThread::Delegate* delegate = michael@0: static_cast(closure); michael@0: delegate->ThreadMain(); michael@0: return 0; michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: // static michael@0: PlatformThreadId PlatformThread::CurrentId() { michael@0: return GetCurrentThreadId(); michael@0: } michael@0: michael@0: // static michael@0: void PlatformThread::YieldCurrentThread() { michael@0: ::Sleep(0); michael@0: } michael@0: michael@0: // static michael@0: void PlatformThread::Sleep(int duration_ms) { michael@0: ::Sleep(duration_ms); michael@0: } michael@0: michael@0: // static michael@0: void PlatformThread::SetName(const char* name) { michael@0: // The debugger needs to be around to catch the name in the exception. If michael@0: // there isn't a debugger, we are just needlessly throwing an exception. michael@0: if (!::IsDebuggerPresent()) michael@0: return; michael@0: michael@0: THREADNAME_INFO info; michael@0: info.dwType = 0x1000; michael@0: info.szName = name; michael@0: info.dwThreadID = CurrentId(); michael@0: info.dwFlags = 0; michael@0: michael@0: MOZ_SEH_TRY { michael@0: RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD), michael@0: reinterpret_cast(&info)); michael@0: } MOZ_SEH_EXCEPT(EXCEPTION_CONTINUE_EXECUTION) { michael@0: } michael@0: } michael@0: michael@0: // static michael@0: bool PlatformThread::Create(size_t stack_size, Delegate* delegate, michael@0: PlatformThreadHandle* thread_handle) { michael@0: unsigned int flags = 0; michael@0: if (stack_size > 0 && win_util::GetWinVersion() >= win_util::WINVERSION_XP) { michael@0: flags = STACK_SIZE_PARAM_IS_A_RESERVATION; michael@0: } else { michael@0: stack_size = 0; michael@0: } michael@0: michael@0: // Using CreateThread here vs _beginthreadex makes thread creation a bit michael@0: // faster and doesn't require the loader lock to be available. Our code will michael@0: // have to work running on CreateThread() threads anyway, since we run code michael@0: // on the Windows thread pool, etc. For some background on the difference: michael@0: // http://www.microsoft.com/msj/1099/win32/win321099.aspx michael@0: *thread_handle = CreateThread( michael@0: NULL, stack_size, ThreadFunc, delegate, flags, NULL); michael@0: return *thread_handle != NULL; michael@0: } michael@0: michael@0: // static michael@0: bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { michael@0: PlatformThreadHandle thread_handle; michael@0: bool result = Create(stack_size, delegate, &thread_handle); michael@0: CloseHandle(thread_handle); michael@0: return result; michael@0: } michael@0: michael@0: // static michael@0: void PlatformThread::Join(PlatformThreadHandle thread_handle) { michael@0: DCHECK(thread_handle); michael@0: michael@0: // Wait for the thread to exit. It should already have terminated but make michael@0: // sure this assumption is valid. michael@0: DWORD result = WaitForSingleObject(thread_handle, INFINITE); michael@0: DCHECK_EQ(WAIT_OBJECT_0, result); michael@0: michael@0: CloseHandle(thread_handle); michael@0: }