diff --git a/CMakeLists.txt b/CMakeLists.txt index b71d7834..a8afc6d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,9 +4,43 @@ cmake_minimum_required(VERSION 3.0) project("Libmultiprocess" CXX) +include(CMakePushCheckState) include(CTest) +include(CheckCXXSourceCompiles) find_package(Boost) find_package(CapnProto) +find_package(Threads REQUIRED) + +cmake_push_check_state() +set(CMAKE_REQUIRED_LIBRARIES Threads::Threads) +check_cxx_source_compiles(" + #include + int main(int argc, char** argv) + { + char thread_name[16]; + return pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name)); + }" + HAVE_PTHREAD_GETNAME_NP) + +check_cxx_source_compiles(" + #include + int main(int argc, char** argv) + { + uint64_t tid; + pthread_threadid_np(NULL, &tid); + return 0; + }" + HAVE_PTHREAD_THREADID_NP) + +check_cxx_source_compiles(" + #include + #include + int main(int argc, char** argv) + { + return pthread_getthreadid_np(); + }" + HAVE_PTHREAD_GETTHREADID_NP) +cmake_pop_check_state() capnp_generate_cpp(MP_PROXY_SRCS MP_PROXY_HDRS include/mp/proxy.capnp) diff --git a/include/mp/config.h.in b/include/mp/config.h.in index c0ec4dd4..72f0d5e6 100644 --- a/include/mp/config.h.in +++ b/include/mp/config.h.in @@ -8,4 +8,8 @@ #cmakedefine CMAKE_INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@" #cmakedefine capnp_PREFIX "@capnp_PREFIX@" +#cmakedefine HAVE_PTHREAD_GETNAME_NP @HAVE_PTHREAD_GETNAME_NP@ +#cmakedefine HAVE_PTHREAD_THREADID_NP @HAVE_PTHREAD_THREADID_NP@ +#cmakedefine HAVE_PTHREAD_GETTHREADID_NP @HAVE_PTHREAD_GETTHREADID_NP@ + #endif // MP_CONFIG_H diff --git a/src/mp/util.cpp b/src/mp/util.cpp index 79b2287e..8be50d9b 100644 --- a/src/mp/util.cpp +++ b/src/mp/util.cpp @@ -2,6 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include @@ -13,12 +14,17 @@ #include #include #include +#include #include #if __linux__ #include #endif +#ifdef HAVE_PTHREAD_GETTHREADID_NP +#include +#endif // HAVE_PTHREAD_GETTHREADID_NP + namespace mp { namespace { @@ -37,16 +43,28 @@ size_t MaxFd() std::string ThreadName(const char* exe_name) { - char thread_name[17] = {0}; + char thread_name[16] = {0}; +#ifdef HAVE_PTHREAD_GETNAME_NP pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name)); - uint64_t tid = 0; +#endif // HAVE_PTHREAD_GETNAME_NP + + std::ostringstream buffer; + buffer << (exe_name ? exe_name : "") << "-" << getpid() << "/" << thread_name << "-"; + + // Prefer platform specific thread ids over the standard C++11 ones because + // the former are shorter and are the same as what gdb prints "LWP ...". #if __linux__ - tid = syscall(SYS_gettid); -#else + buffer << syscall(SYS_gettid); +#elif defined(HAVE_PTHREAD_THREADID_NP) + uint64_t tid = 0; pthread_threadid_np(NULL, &tid); + buffer << tid; +#elif defined(HAVE_PTHREAD_GETTHREADID_NP) + buffer << pthread_getthreadid_np(); +#else + buffer << std::this_thread::get_id(); #endif - std::ostringstream buffer; - buffer << (exe_name ? exe_name : "") << "-" << getpid() << "/" << thread_name << "-" << tid; + return std::move(buffer.str()); }