From: Lucian Petrut Date: Tue, 18 Apr 2023 14:50:41 +0000 (+0000) Subject: common: avoid using pthread native handle if not available X-Git-Tag: v19.0.0~575^2~9 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=129fa17caa8bb022ca3c13a9aed83b22fe09e208;p=ceph.git common: avoid using pthread native handle if not available Especially when targeting Windows, llvm may not necessarily use pthreads for std::thread. In this case, we must not use the "native" thread handle with the pthreads API. We'll update the ceph_pthread_getname and ceph_pthread_setname wrappers, adding a new one: ceph_pthread_kill. Signed-off-by: Lucian Petrut --- diff --git a/src/common/Thread.cc b/src/common/Thread.cc index aaee01aeff31..9a7a31923c1b 100644 --- a/src/common/Thread.cc +++ b/src/common/Thread.cc @@ -223,7 +223,7 @@ std::string get_thread_name(const std::thread& t) { void kill(std::thread& t, int signal) { - auto r = pthread_kill(t.native_handle(), signal); + auto r = ceph_pthread_kill(t.native_handle(), signal); if (r != 0) { throw std::system_error(r, std::generic_category()); } diff --git a/src/common/ceph_timer.h b/src/common/ceph_timer.h index 2be077834b17..16625208024c 100644 --- a/src/common/ceph_timer.h +++ b/src/common/ceph_timer.h @@ -28,6 +28,7 @@ #include "include/compat.h" #include "common/detail/construct_suspended.h" +#include "common/Thread.h" namespace bi = boost::intrusive; namespace ceph { @@ -142,7 +143,7 @@ class timer { public: timer() : suspended(false) { thread = std::thread(&timer::timer_thread, this); - ceph_pthread_setname(thread.native_handle(), "ceph_timer"); + set_thread_name(thread, "ceph_timer"); } // Create a suspended timer, jobs will be executed in order when diff --git a/src/include/compat.h b/src/include/compat.h index 1100d69ebb02..53285243d91f 100644 --- a/src/include/compat.h +++ b/src/include/compat.h @@ -179,7 +179,27 @@ struct cpu_set_t; #define MSG_DONTWAIT MSG_NONBLOCK #endif -#if defined(HAVE_PTHREAD_SETNAME_NP) +/* compiler warning free success noop */ +#define pthread_setname_noop_helper(thread, name) ({ \ + int __i = 0; \ + __i; }) + +#define pthread_getname_noop_helper(thread, name, len) ({ \ + if (name != NULL) \ + *name = '\0'; \ + 0; }) + +#define pthread_kill_unsupported_helper(thread, signal) ({ \ + int __i = -ENOTSUP; \ + __i; }) + +#if defined(_WIN32) && defined(__clang__) && \ + !defined(_LIBCPP_HAS_THREAD_API_PTHREAD) + // In this case, llvm doesn't use the pthread api for std::thread. + // We cannot use native_handle() with the pthread api, nor can we pass + // it to Windows API functions. + #define ceph_pthread_setname pthread_setname_noop_helper +#elif defined(HAVE_PTHREAD_SETNAME_NP) #if defined(__APPLE__) #define ceph_pthread_setname(thread, name) ({ \ int __result = 0; \ @@ -195,24 +215,27 @@ struct cpu_set_t; pthread_set_name_np(thread, name); \ 0; }) #else - /* compiler warning free success noop */ - #define ceph_pthread_setname(thread, name) ({ \ - int __i = 0; \ - __i; }) + #define ceph_pthread_setname pthread_setname_noop_helper #endif -#if defined(HAVE_PTHREAD_GETNAME_NP) +#if defined(_WIN32) && defined(__clang__) && \ + !defined(_LIBCPP_HAS_THREAD_API_PTHREAD) + #define ceph_pthread_getname pthread_getname_noop_helper +#elif defined(HAVE_PTHREAD_GETNAME_NP) #define ceph_pthread_getname pthread_getname_np #elif defined(HAVE_PTHREAD_GET_NAME_NP) #define ceph_pthread_getname(thread, name, len) ({ \ pthread_get_name_np(thread, name, len); \ 0; }) #else - /* compiler warning free success noop */ - #define ceph_pthread_getname(thread, name, len) ({ \ - if (name != NULL) \ - *name = '\0'; \ - 0; }) + #define ceph_pthread_getname pthread_getname_noop_helper +#endif + +#if defined(_WIN32) && defined(__clang__) && \ + !defined(_LIBCPP_HAS_THREAD_API_PTHREAD) + #define ceph_pthread_kill pthread_kill_unsupported_helper +#else + #define ceph_pthread_kill pthread_kill #endif int ceph_posix_fallocate(int fd, off_t offset, off_t len);