]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: avoid using pthread native handle if not available
authorLucian Petrut <lpetrut@cloudbasesolutions.com>
Tue, 18 Apr 2023 14:50:41 +0000 (14:50 +0000)
committerLucian Petrut <lpetrut@cloudbasesolutions.com>
Wed, 30 Aug 2023 12:59:00 +0000 (12:59 +0000)
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 <lpetrut@cloudbasesolutions.com>
src/common/Thread.cc
src/common/ceph_timer.h
src/include/compat.h

index aaee01aeff31b02300a2d5a419b159e4f36c82d5..9a7a31923c1b79e4c6fe25e73bda2661e8413776 100644 (file)
@@ -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());
   }
index 2be077834b17e45129b0eb6f544d03672fd7ae98..16625208024c8a140595411b4b86db9f05631557 100644 (file)
@@ -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
index 1100d69ebb0239628b3fb2422e0ab33728971937..53285243d91fc7263a8c54bad8986bf69bd5e432 100644 (file)
@@ -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);