1//===- Windows/Threading.inc - Win32 Threading Implementation - -*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file provides the Win32 specific implementation of Threading functions. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/ADT/SmallString.h" 15#include "llvm/ADT/Twine.h" 16 17#include "Windows/WindowsSupport.h" 18#include <process.h> 19 20// Windows will at times define MemoryFence. 21#ifdef MemoryFence 22#undef MemoryFence 23#endif 24 25namespace { 26 struct ThreadInfo { 27 void(*func)(void*); 28 void *param; 29 }; 30} 31 32static unsigned __stdcall ThreadCallback(void *param) { 33 struct ThreadInfo *info = reinterpret_cast<struct ThreadInfo *>(param); 34 info->func(info->param); 35 36 return 0; 37} 38 39void llvm::llvm_execute_on_thread(void(*Fn)(void*), void *UserData, 40 unsigned RequestedStackSize) { 41 struct ThreadInfo param = { Fn, UserData }; 42 43 HANDLE hThread = (HANDLE)::_beginthreadex(NULL, 44 RequestedStackSize, ThreadCallback, 45 ¶m, 0, NULL); 46 47 if (hThread) { 48 // We actually don't care whether the wait succeeds or fails, in 49 // the same way we don't care whether the pthread_join call succeeds 50 // or fails. There's not much we could do if this were to fail. But 51 // on success, this call will wait until the thread finishes executing 52 // before returning. 53 (void)::WaitForSingleObject(hThread, INFINITE); 54 ::CloseHandle(hThread); 55 } 56} 57 58uint64_t llvm::get_threadid() { 59 return uint64_t(::GetCurrentThreadId()); 60} 61 62void llvm::set_thread_name(const Twine &Name) { 63#if defined(_MSC_VER) 64 // Make sure the input is null terminated. 65 SmallString<64> Storage; 66 StringRef NameStr = Name.toNullTerminatedStringRef(Storage); 67 constexpr DWORD MS_VC_EXCEPTION = 0x406D1388; 68 69#pragma pack(push, 8) 70 struct THREADNAME_INFO { 71 DWORD dwType; // Must be 0x1000. 72 LPCSTR szName; // Pointer to thread name 73 DWORD dwThreadId; // Thread ID (-1 == current thread) 74 DWORD dwFlags; // Reserved. Do not use. 75 }; 76#pragma pack(pop) 77 78 THREADNAME_INFO info; 79 info.dwType = 0x1000; 80 info.szName = NameStr.data(); 81 info.dwThreadId = ::GetCurrentThreadId(); 82 info.dwFlags = 0; 83 84 __try { 85 ::RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), 86 (ULONG_PTR *)&info); 87 } 88 __except (EXCEPTION_EXECUTE_HANDLER) { 89 } 90#endif 91} 92 93void llvm::get_thread_name(SmallVectorImpl<char> &Name) { 94 // "Name" is not an inherent property of a thread on Windows. In fact, when 95 // you "set" the name, you are only firing a one-time message to a debugger 96 // which it interprets as a program setting its threads' name. We may be 97 // able to get fancy by creating a TLS entry when someone calls 98 // set_thread_name so that subsequent calls to get_thread_name return this 99 // value. 100 Name.clear(); 101} 102