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 <lpetrut@cloudbasesolutions.com>
This commit is contained in:
Lucian Petrut 2023-04-18 14:50:41 +00:00
parent 4a0dceec99
commit 129fa17caa
3 changed files with 37 additions and 13 deletions

View 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());
}

View 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

View 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);