17 #if defined(__APPLE__) 18 #include <mach/mach_init.h> 19 #include <mach/mach_port.h> 24 #if defined(__FreeBSD__) || defined(__OpenBSD__) 25 #include <pthread_np.h> 28 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 30 #include <sys/sysctl.h> 35 #if defined(__NetBSD__) 39 #if defined(__linux__) 40 #include <sys/syscall.h> 46 void(*UserFn)(
void *);
51 static void *ExecuteOnThread_Dispatch(
void *
Arg) {
52 ThreadInfo *TI =
reinterpret_cast<ThreadInfo*
>(
Arg);
53 TI->UserFn(TI->UserData);
58 unsigned RequestedStackSize) {
59 ThreadInfo
Info = { Fn, UserData };
64 if (::pthread_attr_init(&Attr) != 0)
68 if (RequestedStackSize != 0) {
69 if (::pthread_attr_setstacksize(&Attr, RequestedStackSize) != 0)
74 if (::pthread_create(&Thread, &Attr, ExecuteOnThread_Dispatch, &Info) != 0)
78 ::pthread_join(Thread,
nullptr);
81 ::pthread_attr_destroy(&Attr);
86 #if defined(__APPLE__) 90 thread_port_t Self = mach_thread_self();
91 mach_port_deallocate(mach_task_self(), Self);
93 #elif defined(__FreeBSD__) 94 return uint64_t(pthread_getthreadid_np());
95 #elif defined(__NetBSD__) 96 return uint64_t(_lwp_self());
97 #elif defined(__ANDROID__) 98 return uint64_t(gettid());
99 #elif defined(__linux__) 100 return uint64_t(syscall(SYS_gettid));
102 return uint64_t(pthread_self());
107 static constexpr
uint32_t get_max_thread_name_length_impl() {
108 #if defined(__NetBSD__) 109 return PTHREAD_MAX_NAMELEN_NP;
110 #elif defined(__APPLE__) 112 #elif defined(__linux__) 113 #if HAVE_PTHREAD_SETNAME_NP 118 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 120 #elif defined(__OpenBSD__) 128 return get_max_thread_name_length_impl();
133 SmallString<64> Storage;
134 StringRef NameStr = Name.toNullTerminatedStringRef(Storage);
145 #if defined(__linux__) 146 #if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__) 147 #if HAVE_PTHREAD_SETNAME_NP 148 ::pthread_setname_np(::pthread_self(), NameStr.data());
151 #elif defined(__FreeBSD__) || defined(__OpenBSD__) 152 ::pthread_set_name_np(::pthread_self(), NameStr.data());
153 #elif defined(__NetBSD__) 154 ::pthread_setname_np(::pthread_self(),
"%s",
155 const_cast<char *>(NameStr.data()));
156 #elif defined(__APPLE__) 157 ::pthread_setname_np(NameStr.data());
164 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 165 int pid = ::getpid();
168 struct kinfo_proc *kp =
nullptr, *nkp;
171 int ctl[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
175 error = sysctl(ctl, 4, kp, &len,
nullptr, 0);
176 if (kp ==
nullptr || (error != 0 && errno == ENOMEM)) {
178 len +=
sizeof(*kp) + len / 10;
179 nkp = (
struct kinfo_proc *)::realloc(kp, len);
180 if (nkp ==
nullptr) {
192 for (
size_t i = 0; i < len /
sizeof(*kp); i++) {
193 if (kp[i].ki_tid == (lwpid_t)tid) {
194 Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname));
200 #elif defined(__NetBSD__) 201 constexpr
uint32_t len = get_max_thread_name_length_impl();
203 ::pthread_getname_np(::pthread_self(), buf, len);
205 Name.append(buf, buf + strlen(buf));
206 #elif defined(__OpenBSD__) 207 constexpr
uint32_t len = get_max_thread_name_length_impl();
209 ::pthread_get_name_np(::pthread_self(), buf, len);
211 Name.append(buf, buf + strlen(buf));
212 #elif defined(__linux__) 213 #if HAVE_PTHREAD_GETNAME_NP 214 constexpr
uint32_t len = get_max_thread_name_length_impl();
215 char Buffer[len] = {
'\0'};
216 if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len))
217 Name.append(Buffer, Buffer + strlen(Buffer));
void set_thread_name(const Twine &Name)
Set the name of the current thread.
amdgpu Simplify well known AMD library false Value Value const Twine & Name
uint32_t get_max_thread_name_length()
Get the maximum length of a thread name on this platform.
void get_thread_name(SmallVectorImpl< char > &Name)
Get the name of the current thread.
Analysis containing CSE Info
uint64_t get_threadid()
Return the current thread id, as used in various OS system calls.
void llvm_execute_on_thread(void(*UserFn)(void *), void *UserData, unsigned RequestedStackSize=0)
llvm_execute_on_thread - Execute the given UserFn on a separate thread, passing it the provided UserD...
amdgpu Simplify well known AMD library false Value Value * Arg