Skip to content

Commit c8d7f90

Browse files
committed
Refactor ThreadName() to improve its portability
* Test for the presence of pthread_getname_np() during cmake time and only use it if it is available. At least FreeBSD does not have it. When not available, fall back to (the first 15 chars of) the executable name, as if pthread_setname_np() has never been called. * Use std::this_thread::get_id() to retrieve the thread id in a portable way instead of pthread_threadid_np() or syscall() (none of which is available on FreeBSD but it has its own pthread_getthreadid_np()). C++11 is here to help. * According to pthread_getname_np(3) the thread name length is restricted to 16 characters, including the terminating null byte. So reduce our thread_name[] from 17 to 16 chars.
1 parent fe76b28 commit c8d7f90

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@
55
cmake_minimum_required(VERSION 3.0)
66
project("Libmultiprocess" CXX)
77
include(CTest)
8+
include(CheckCXXSourceCompiles)
89
find_package(Boost)
910
find_package(CapnProto)
1011

12+
check_cxx_source_compiles("
13+
#include <pthread.h>
14+
int main(int argc, char** argv)
15+
{
16+
char thread_name[16];
17+
return pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name));
18+
}"
19+
HAVE_PTHREAD_GETNAME_NP)
20+
1121
capnp_generate_cpp(MP_PROXY_SRCS MP_PROXY_HDRS include/mp/proxy.capnp)
1222

1323
set(MP_PUBLIC_HEADERS

include/mp/config.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@
88
#cmakedefine CMAKE_INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@"
99
#cmakedefine capnp_PREFIX "@capnp_PREFIX@"
1010

11+
#cmakedefine HAVE_PTHREAD_GETNAME_NP @HAVE_PTHREAD_GETNAME_NP@
12+
1113
#endif // MP_CONFIG_H

src/mp/util.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <mp/config.h>
56
#include <mp/util.h>
67

78
#include <kj/array.h>
@@ -13,6 +14,7 @@
1314
#include <sys/types.h>
1415
#include <sys/un.h>
1516
#include <sys/wait.h>
17+
#include <thread>
1618
#include <unistd.h>
1719

1820
#if __linux__
@@ -37,14 +39,17 @@ size_t MaxFd()
3739

3840
std::string ThreadName(const char* exe_name)
3941
{
40-
char thread_name[17] = {0};
42+
char thread_name[16] = {0};
43+
#ifdef HAVE_PTHREAD_GETNAME_NP
4144
pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name));
42-
uint64_t tid = 0;
43-
#if __linux__
44-
tid = syscall(SYS_gettid);
4545
#else
46-
pthread_threadid_np(NULL, &tid);
47-
#endif
46+
// By default, all the threads inherit the program name. So use that one
47+
// if we don't have pthread_getname_np().
48+
snprintf(thread_name, sizeof(thread_name), "%s", exe_name);
49+
#endif // HAVE_PTHREAD_GETNAME_NP
50+
51+
const auto tid = std::this_thread::get_id();
52+
4853
std::ostringstream buffer;
4954
buffer << (exe_name ? exe_name : "") << "-" << getpid() << "/" << thread_name << "-" << tid;
5055
return std::move(buffer.str());

0 commit comments

Comments
 (0)