18 #if defined(__APPLE__) 19 #include <mach/mach_init.h> 20 #include <mach/mach_port.h> 25 #if defined(__FreeBSD__) || defined(__OpenBSD__) 26 #include <pthread_np.h> 29 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 31 #include <sys/sysctl.h> 36 #if defined(__NetBSD__) 40 #if defined(__linux__) 41 #include <sys/syscall.h> 45 static void *threadFuncSync(
void *
Arg) {
46 SyncThreadInfo *TI =
static_cast<SyncThreadInfo *
>(
Arg);
47 TI->UserFn(TI->UserData);
51 static void *threadFuncAsync(
void *Arg) {
52 std::unique_ptr<AsyncThreadInfo>
Info(static_cast<AsyncThreadInfo *>(Arg));
58 llvm_execute_on_thread_impl(
void *(*ThreadFunc)(
void *),
void *Arg,
65 if ((errnum = ::pthread_attr_init(&Attr)) != 0) {
70 if ((errnum = ::pthread_attr_destroy(&Attr)) != 0) {
76 if (StackSizeInBytes) {
77 if ((errnum = ::pthread_attr_setstacksize(&Attr, *StackSizeInBytes)) != 0) {
84 if ((errnum = ::pthread_create(&Thread, &Attr, ThreadFunc, Arg)) != 0)
87 if (JP == JoiningPolicy::Join) {
89 if ((errnum = ::pthread_join(Thread,
nullptr)) != 0) {
96 #if defined(__APPLE__) 100 thread_port_t Self = mach_thread_self();
101 mach_port_deallocate(mach_task_self(), Self);
103 #elif defined(__FreeBSD__) 104 return uint64_t(pthread_getthreadid_np());
105 #elif defined(__NetBSD__) 106 return uint64_t(_lwp_self());
107 #elif defined(__ANDROID__) 108 return uint64_t(gettid());
109 #elif defined(__linux__) 110 return uint64_t(syscall(SYS_gettid));
112 return uint64_t(pthread_self());
117 static constexpr
uint32_t get_max_thread_name_length_impl() {
118 #if defined(__NetBSD__) 119 return PTHREAD_MAX_NAMELEN_NP;
120 #elif defined(__APPLE__) 122 #elif defined(__linux__) 123 #if HAVE_PTHREAD_SETNAME_NP 128 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 130 #elif defined(__OpenBSD__) 138 return get_max_thread_name_length_impl();
143 SmallString<64> Storage;
144 StringRef NameStr = Name.toNullTerminatedStringRef(Storage);
155 #if defined(__linux__) 156 #if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__) 157 #if HAVE_PTHREAD_SETNAME_NP 158 ::pthread_setname_np(::pthread_self(), NameStr.data());
161 #elif defined(__FreeBSD__) || defined(__OpenBSD__) 162 ::pthread_set_name_np(::pthread_self(), NameStr.data());
163 #elif defined(__NetBSD__) 164 ::pthread_setname_np(::pthread_self(),
"%s",
165 const_cast<char *>(NameStr.data()));
166 #elif defined(__APPLE__) 167 ::pthread_setname_np(NameStr.data());
174 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 175 int pid = ::getpid();
178 struct kinfo_proc *kp =
nullptr, *nkp;
181 int ctl[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
185 error = sysctl(ctl, 4, kp, &len,
nullptr, 0);
186 if (kp ==
nullptr || (error != 0 && errno == ENOMEM)) {
188 len +=
sizeof(*kp) + len / 10;
189 nkp = (
struct kinfo_proc *)::realloc(kp, len);
190 if (nkp ==
nullptr) {
202 for (
size_t i = 0; i < len /
sizeof(*kp); i++) {
203 if (kp[i].ki_tid == (lwpid_t)tid) {
204 Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname));
210 #elif defined(__NetBSD__) 211 constexpr
uint32_t len = get_max_thread_name_length_impl();
213 ::pthread_getname_np(::pthread_self(), buf, len);
215 Name.append(buf, buf + strlen(buf));
216 #elif defined(__OpenBSD__) 217 constexpr
uint32_t len = get_max_thread_name_length_impl();
219 ::pthread_get_name_np(::pthread_self(), buf, len);
221 Name.append(buf, buf + strlen(buf));
222 #elif defined(__linux__) 223 #if HAVE_PTHREAD_GETNAME_NP 224 constexpr
uint32_t len = get_max_thread_name_length_impl();
225 char Buffer[len] = {
'\0'};
226 if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len))
227 Name.append(Buffer, Buffer + strlen(Buffer));
233 #if defined(__linux__) && defined(SCHED_IDLE) 237 sched_param priority;
239 priority.sched_priority = 0;
242 return !pthread_setschedparam(
244 Priority == ThreadPriority::Background ? SCHED_IDLE : SCHED_OTHER,
246 ? SetThreadPriorityResult::SUCCESS
247 : SetThreadPriorityResult::FAILURE;
248 #elif defined(__APPLE__) 262 return !setpriority(PRIO_DARWIN_THREAD, 0,
263 Priority == ThreadPriority::Background ? PRIO_DARWIN_BG
265 ? SetThreadPriorityResult::SUCCESS
266 : SetThreadPriorityResult::FAILURE;
268 return SetThreadPriorityResult::FAILURE;
void set_thread_name(const Twine &Name)
Set the name of the current thread.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
LLVM_NODISCARD detail::scope_exit< typename std::decay< Callable >::type > make_scope_exit(Callable &&F)
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
SetThreadPriorityResult
If priority is Background tries to lower current threads priority such that it does not affect foregr...
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
uint64_t get_threadid()
Return the current thread id, as used in various OS system calls.
static LLVM_ATTRIBUTE_NORETURN void ReportErrnumFatal(const char *Msg, int errnum)
SetThreadPriorityResult set_thread_priority(ThreadPriority Priority)