blob: 93b2dfe09457dd4dd77f486037de069a278bc48a [file] [log] [blame]
Zachary Turner757dbc92017-03-03 17:15:17 +00001//===- 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 &param, 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
Zachary Turner1f004c42017-03-04 18:53:09 +000062uint32_t llvm::get_max_thread_name_length() { return 0; }
Zachary Turner777de772017-03-04 16:42:25 +000063
Zachary Turner757dbc92017-03-03 17:15:17 +000064void llvm::set_thread_name(const Twine &Name) {
65#if defined(_MSC_VER)
66 // Make sure the input is null terminated.
67 SmallString<64> Storage;
68 StringRef NameStr = Name.toNullTerminatedStringRef(Storage);
69 constexpr DWORD MS_VC_EXCEPTION = 0x406D1388;
70
71#pragma pack(push, 8)
72 struct THREADNAME_INFO {
73 DWORD dwType; // Must be 0x1000.
74 LPCSTR szName; // Pointer to thread name
75 DWORD dwThreadId; // Thread ID (-1 == current thread)
76 DWORD dwFlags; // Reserved. Do not use.
77 };
78#pragma pack(pop)
79
80 THREADNAME_INFO info;
81 info.dwType = 0x1000;
82 info.szName = NameStr.data();
83 info.dwThreadId = ::GetCurrentThreadId();
84 info.dwFlags = 0;
85
86 __try {
87 ::RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR),
88 (ULONG_PTR *)&info);
89 }
90 __except (EXCEPTION_EXECUTE_HANDLER) {
91 }
92#endif
93}
94
95void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
96 // "Name" is not an inherent property of a thread on Windows. In fact, when
97 // you "set" the name, you are only firing a one-time message to a debugger
98 // which it interprets as a program setting its threads' name. We may be
99 // able to get fancy by creating a TLS entry when someone calls
100 // set_thread_name so that subsequent calls to get_thread_name return this
101 // value.
102 Name.clear();
103}