On 09.08.2019 20:11, Martin Husemann wrote: > On Fri, Aug 09, 2019 at 07:09:01PM +0200, Kamil Rytarowski wrote: >> Delayed adoption of a standardized version of pthread_setname() >> pthread_getname() will result in no API & ABI breakage with any existing >> code as we will keep our own _np() invention, at least until we will >> bump major number for libpthread. and drop redundant _np() variation > > This is a good argument. And we may even opt to not drop the _np variant at > all. > > However, if we go for this - why change anything at all right now? > I see no reason to change anything now/soon, there is just the taste issue in '(void *)'. It probably wasn't needed in the past, but nowadays compilers complain about our API and expect this particular odd usage. Technically there is nothing broken and so nothing needs fixing. The problem is lack in a standard way so each mainstream OS is different and that part needs fixing. > Keeping everything unchanged, pushing for a posix version, and then adopt > that as soon as the signature is fixed sounds like a good plan to me. > > If we touch it, we should do it cleanly. > I will submit a proposal to POSIX to adapt the Linux variation + NetBSD specific PTHREAD_MAX_NAMELEN constant: - PTHREAD_MAX_NAMELEN - pthread_setname() - pthread_getname() Below is an elaborated version in LLVM [1] that is still incomplete as misses pthread for Windows, Tru64, .. Domain specific OSs like RTMS pick one of the existing APIs or keep inventing distinct ones. As we can see there is not even a consensus inside the Linux world. Adding more ifdefs for NetBSD version can cause need for more painkillers. static constexpr uint32_t get_max_thread_name_length_impl() { #if defined(__NetBSD__) return PTHREAD_MAX_NAMELEN_NP; #elif defined(__APPLE__) return 64; #elif defined(__linux__) #if HAVE_PTHREAD_SETNAME_NP return 16; #else return 0; #endif #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) return 16; #elif defined(__OpenBSD__) return 32; #else return 0; #endif } uint32_t llvm::get_max_thread_name_length() { return get_max_thread_name_length_impl(); } void llvm::set_thread_name(const Twine &Name) { // Make sure the input is null terminated. SmallString<64> Storage; StringRef NameStr = Name.toNullTerminatedStringRef(Storage); // Truncate from the beginning, not the end, if the specified name is too // long. For one, this ensures that the resulting string is still null // terminated, but additionally the end of a long thread name will usually // be more unique than the beginning, since a common pattern is for similar // threads to share a common prefix. // Note that the name length includes the null terminator. if (get_max_thread_name_length() > 0) NameStr = NameStr.take_back(get_max_thread_name_length() - 1); (void)NameStr; #if defined(__linux__) #if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__) #if HAVE_PTHREAD_SETNAME_NP ::pthread_setname_np(::pthread_self(), NameStr.data()); #endif #endif #elif defined(__FreeBSD__) || defined(__OpenBSD__) ::pthread_set_name_np(::pthread_self(), NameStr.data()); #elif defined(__NetBSD__) ::pthread_setname_np(::pthread_self(), "%s", const_cast<char *>(NameStr.data())); #elif defined(__APPLE__) ::pthread_setname_np(NameStr.data()); #endif } void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) int pid = ::getpid(); uint64_t tid = get_threadid(); struct kinfo_proc *kp = nullptr, *nkp; size_t len = 0; int error; int ctl[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD, (int)pid }; while (1) { error = sysctl(ctl, 4, kp, &len, nullptr, 0); if (kp == nullptr || (error != 0 && errno == ENOMEM)) { // Add extra space in case threads are added before next call. len += sizeof(*kp) + len / 10; nkp = (struct kinfo_proc *)::realloc(kp, len); if (nkp == nullptr) { free(kp); return; } kp = nkp; continue; } if (error != 0) len = 0; break; } for (size_t i = 0; i < len / sizeof(*kp); i++) { if (kp[i].ki_tid == (lwpid_t)tid) { Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname)); break; } } free(kp); return; #elif defined(__NetBSD__) constexpr uint32_t len = get_max_thread_name_length_impl(); char buf[len]; ::pthread_getname_np(::pthread_self(), buf, len); Name.append(buf, buf + strlen(buf)); #elif defined(__OpenBSD__) constexpr uint32_t len = get_max_thread_name_length_impl(); char buf[len]; ::pthread_get_name_np(::pthread_self(), buf, len); Name.append(buf, buf + strlen(buf)); #elif defined(__linux__) #if HAVE_PTHREAD_GETNAME_NP constexpr uint32_t len = get_max_thread_name_length_impl(); char Buffer[len] = {'\0'}; // FIXME: working around MSan false positive. if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len)) Name.append(Buffer, Buffer + strlen(Buffer)); #endif #endif } [1] https://github.com/llvm-mirror/llvm/blob/394ea6522c69c2668bf328fc923e1a11cd785265/lib/Support/Unix/Threading.inc > Martin >
Attachment:
signature.asc
Description: OpenPGP digital signature