diff options
Diffstat (limited to 'src/common/compat.cc')
-rw-r--r-- | src/common/compat.cc | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/common/compat.cc b/src/common/compat.cc index 82b57ad94b5..84a395c5a19 100644 --- a/src/common/compat.cc +++ b/src/common/compat.cc @@ -565,3 +565,66 @@ ssize_t get_self_exe_path(char* path, int buff_length) { } #endif /* _WIN32 */ + + +static thread_local char cached_thread_name[256]{}; + +int ceph_pthread_setname(char const* name) +{ + strncpy(cached_thread_name, name, sizeof cached_thread_name - 1); +#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. + return 0; +#elif defined(HAVE_PTHREAD_SETNAME_NP) + #if defined(__APPLE__) + return pthread_setname_np(name); + #else + return pthread_setname_np(pthread_self(), name); + #endif +#elif defined(HAVE_PTHREAD_SET_NAME_NP) + pthread_set_name_np(pthread_self(), name); \ + return 0; +#else + return 0; +#endif +} + +int ceph_pthread_getname(char* name, size_t len) +{ + if (cached_thread_name[0]) { + if (len > 0) { + strncpy(name, cached_thread_name, len); + name[len-1] = 0; + } + return 0; + } else { +#if defined(_WIN32) && defined(__clang__) && \ + !defined(_LIBCPP_HAS_THREAD_API_PTHREAD) + if (len > 0) { + strcpy(name, ""); + } + return 0; +#elif defined(HAVE_PTHREAD_GETNAME_NP) || defined(HAVE_PTHREAD_GET_NAME_NP) +# if defined(HAVE_PTHREAD_GETNAME_NP) + int rc = pthread_getname_np(pthread_self(), cached_thread_name, sizeof cached_thread_name); +# else + int rc = pthread_get_name_np(pthread_self(), cached_thread_name, sizeof cached_thread_name); +# endif + if (rc == 0) { + strncpy(name, cached_thread_name, len); + name[len-1] = 0; + return 0; + } else { + return rc; + } +#else + if (len > 0) { + strcpy(name, ""); + } + return 0; +#endif + } +} |