Skip to content

Commit 1e65b76

Browse files
authored
[llvm][Support] Add support for thread naming under DragonFly BSD and Solaris/illumos (#106944)
1 parent c425124 commit 1e65b76

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

llvm/cmake/config-ix.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ if (NOT PURE_WINDOWS)
356356
endif()
357357
check_symbol_exists(pthread_getname_np pthread.h HAVE_PTHREAD_GETNAME_NP)
358358
check_symbol_exists(pthread_setname_np pthread.h HAVE_PTHREAD_SETNAME_NP)
359+
check_symbol_exists(pthread_get_name_np "pthread.h;pthread_np.h" HAVE_PTHREAD_GET_NAME_NP)
360+
check_symbol_exists(pthread_set_name_np "pthread.h;pthread_np.h" HAVE_PTHREAD_SET_NAME_NP)
359361
if (LLVM_PTHREAD_LIB)
360362
list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES ${LLVM_PTHREAD_LIB})
361363
endif()

llvm/include/llvm/Config/config.h.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@
125125
/* Define to 1 if you have the `pthread_setname_np' function. */
126126
#cmakedefine HAVE_PTHREAD_SETNAME_NP ${HAVE_PTHREAD_SETNAME_NP}
127127

128+
/* Define to 1 if you have the `pthread_get_name_np' function. */
129+
#cmakedefine HAVE_PTHREAD_GET_NAME_NP ${HAVE_PTHREAD_GET_NAME_NP}
130+
131+
/* Define to 1 if you have the `pthread_set_name_np' function. */
132+
#cmakedefine HAVE_PTHREAD_SET_NAME_NP ${HAVE_PTHREAD_SET_NAME_NP}
133+
128134
/* Define to 1 if you have the <mach/mach.h> header file. */
129135
#cmakedefine HAVE_MACH_MACH_H ${HAVE_MACH_MACH_H}
130136

llvm/lib/Support/Unix/Threading.inc

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,16 @@ uint64_t llvm::get_threadid() {
137137
}
138138

139139
static constexpr uint32_t get_max_thread_name_length_impl() {
140-
#if defined(__NetBSD__)
140+
#if defined(PTHREAD_MAX_NAMELEN_NP)
141141
return PTHREAD_MAX_NAMELEN_NP;
142142
#elif defined(__APPLE__)
143143
return 64;
144+
#elif defined(__sun__) && defined(__svr4__)
145+
return 31;
144146
#elif defined(__linux__) && HAVE_PTHREAD_SETNAME_NP
145147
return 16;
146-
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
148+
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
149+
defined(__DragonFly__)
147150
return 16;
148151
#elif defined(__OpenBSD__)
149152
return 24;
@@ -170,15 +173,17 @@ void llvm::set_thread_name(const Twine &Name) {
170173
if (get_max_thread_name_length() > 0)
171174
NameStr = NameStr.take_back(get_max_thread_name_length() - 1);
172175
(void)NameStr;
173-
#if defined(__linux__) && HAVE_PTHREAD_SETNAME_NP
174-
::pthread_setname_np(::pthread_self(), NameStr.data());
175-
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
176+
#if defined(HAVE_PTHREAD_SET_NAME_NP)
176177
::pthread_set_name_np(::pthread_self(), NameStr.data());
177-
#elif defined(__NetBSD__)
178+
#elif defined(HAVE_PTHREAD_SETNAME_NP)
179+
#if defined(__NetBSD__)
178180
::pthread_setname_np(::pthread_self(), "%s",
179181
const_cast<char *>(NameStr.data()));
180182
#elif defined(__APPLE__)
181183
::pthread_setname_np(NameStr.data());
184+
#else
185+
::pthread_setname_np(::pthread_self(), NameStr.data());
186+
#endif
182187
#endif
183188
}
184189

@@ -221,23 +226,24 @@ void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
221226
}
222227
free(kp);
223228
return;
224-
#elif defined(__NetBSD__)
229+
#elif defined(__linux__) && HAVE_PTHREAD_GETNAME_NP
230+
constexpr uint32_t len = get_max_thread_name_length_impl();
231+
char Buffer[len] = {'\0'}; // FIXME: working around MSan false positive.
232+
if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len))
233+
Name.append(Buffer, Buffer + strlen(Buffer));
234+
#elif defined(HAVE_PTHREAD_GET_NAME_NP)
225235
constexpr uint32_t len = get_max_thread_name_length_impl();
226236
char buf[len];
227-
::pthread_getname_np(::pthread_self(), buf, len);
237+
::pthread_get_name_np(::pthread_self(), buf, len);
228238

229239
Name.append(buf, buf + strlen(buf));
230-
#elif defined(__OpenBSD__)
240+
241+
#elif defined(HAVE_PTHREAD_GETNAME_NP)
231242
constexpr uint32_t len = get_max_thread_name_length_impl();
232243
char buf[len];
233-
::pthread_get_name_np(::pthread_self(), buf, len);
244+
::pthread_getname_np(::pthread_self(), buf, len);
234245

235246
Name.append(buf, buf + strlen(buf));
236-
#elif defined(__linux__) && HAVE_PTHREAD_GETNAME_NP
237-
constexpr uint32_t len = get_max_thread_name_length_impl();
238-
char Buffer[len] = {'\0'}; // FIXME: working around MSan false positive.
239-
if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len))
240-
Name.append(Buffer, Buffer + strlen(Buffer));
241247
#endif
242248
}
243249

0 commit comments

Comments
 (0)