1 //===- Windows/Threading.inc - Win32 Threading Implementation - -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the Win32 specific implementation of Threading functions.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/Twine.h"
17 #include "Windows/WindowsSupport.h"
20 // Windows will at times define MemoryFence.
32 static unsigned __stdcall ThreadCallback(void *param) {
33 struct ThreadInfo *info = reinterpret_cast<struct ThreadInfo *>(param);
34 info->func(info->param);
39 void llvm::llvm_execute_on_thread(void(*Fn)(void*), void *UserData,
40 unsigned RequestedStackSize) {
41 struct ThreadInfo param = { Fn, UserData };
43 HANDLE hThread = (HANDLE)::_beginthreadex(NULL,
44 RequestedStackSize, ThreadCallback,
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
53 (void)::WaitForSingleObject(hThread, INFINITE);
54 ::CloseHandle(hThread);
58 uint64_t llvm::get_threadid() {
59 return uint64_t(::GetCurrentThreadId());
62 uint32_t llvm::get_max_thread_name_length() { return 0; }
65 static void SetThreadName(DWORD Id, LPCSTR Name) {
66 constexpr DWORD MS_VC_EXCEPTION = 0x406D1388;
69 struct THREADNAME_INFO {
70 DWORD dwType; // Must be 0x1000.
71 LPCSTR szName; // Pointer to thread name
72 DWORD dwThreadId; // Thread ID (-1 == current thread)
73 DWORD dwFlags; // Reserved. Do not use.
84 ::RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR),
87 __except (EXCEPTION_EXECUTE_HANDLER) {
92 void llvm::set_thread_name(const Twine &Name) {
94 // Make sure the input is null terminated.
95 SmallString<64> Storage;
96 StringRef NameStr = Name.toNullTerminatedStringRef(Storage);
97 SetThreadName(::GetCurrentThreadId(), NameStr.data());
101 void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
102 // "Name" is not an inherent property of a thread on Windows. In fact, when
103 // you "set" the name, you are only firing a one-time message to a debugger
104 // which it interprets as a program setting its threads' name. We may be
105 // able to get fancy by creating a TLS entry when someone calls
106 // set_thread_name so that subsequent calls to get_thread_name return this