[FEAT] add api: set_thread_name, used in periodic_worker and thread_pool

pull/2417/head
izlyforever 3 years ago
parent d7690d8e7e
commit cf0c3aeb90

@ -60,6 +60,32 @@
#endif // unix
// set_thread_name
#if defined(_WIN32) && defined(_MSC_VER)
# include <windows.h> // RaiseException
#endif
#if defined(__linux__)
# include <sys/prctl.h> // prctl
#endif
#if defined(__APPLE__)
# include <pthread.h> // pthread_setname_np
#endif
#if defined(_AIX) || defined(__sun)
# include <pthread.h> // pthread_set_name_np, pthread_self
#endif
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
# include <pthread.h> // pthread_self
# include <pthread_np.h> // pthread_set_name_np
#endif
#if defined(__NetBSD__)
# include <pthread.h> // pthread_getname_np, pthread_self
#endif
#ifndef __has_feature // Clang - feature checking macros.
# define __has_feature(x) 0 // Compatibility with non-clang compilers.
#endif
@ -371,6 +397,58 @@ SPDLOG_INLINE size_t thread_id() SPDLOG_NOEXCEPT
#endif
}
// Set thread name for different platforms
SPDLOG_API void set_thread_name(const char *thread_name)
{
#if defined(_WIN32) && defined(_MSC_VER)
// http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.90).aspx
# pragma pack(push,8)
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
# pragma pack(pop)
const DWORD MS_VC_EXCEPTION = 0x406D1388;
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = thread_name;
info.dwThreadID = -1;
info.dwFlags = 0;
# pragma warning(push)
# pragma warning(disable: 6320 6322)
__try{
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
}
__except (EXCEPTION_EXECUTE_HANDLER){
}
# pragma warning(pop)
#elif defined(__linux__)
// https://man7.org/linux/man-pages/man2/prctl.2.html
::prctl(PR_SET_NAME, thread_name);
#elif defined(__APPLE__)
// https://www.manpagez.com/man/3/pthread_setname_np/
::pthread_setname_np(thread_name);
#elif defined(_AIX) || defined(__sun)
// https://www.ibm.com/docs/en/i/7.1?topic=ssw_ibm_i_71/apis/pthread_setname_np.htm
// https://docs.oracle.com/cd/E86824_01/html/E54766/pthread-setname-np-3c.html#scrolltoc
::pthread_setname_np(::pthread_self(), thread_name);
#elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
// https://www.dragonflybsd.org/cgi/web-man?command=pthread_set_name_np&section=3
// https://www.freebsd.org/cgi/man.cgi?query=pthread_set_name_np&sektion=3&manpath=FreeBSD+9.0-RELEASE+and+Ports
// https://man.openbsd.org/OpenBSD-2.7/pthread_set_name_np
::pthread_set_name_np(::pthread_self(), thread_name);
#elif defined(__NetBSD__)
// https://man.netbsd.org/NetBSD-6.0/pthread_getname_np.3
::pthread_setname_np(::pthread_self(), "%s", const_cast<char*>(thread_name));
#else
// do nothing
# warning "set_thread_name is not supported for this platform"
#endif
}
// This is avoid msvc issue in sleep_for that happens if the clock changes.
// See https://github.com/gabime/spdlog/issues/609
SPDLOG_INLINE void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT

@ -72,6 +72,9 @@ SPDLOG_API size_t _thread_id() SPDLOG_NOEXCEPT;
// Return current thread id as size_t (from thread local storage)
SPDLOG_API size_t thread_id() SPDLOG_NOEXCEPT;
// Set thread name for different platforms
SPDLOG_API void set_thread_name(const char *thread_name);
// This is avoid msvc issue in sleep_for that happens if the clock changes.
// See https://github.com/gabime/spdlog/issues/609
SPDLOG_API void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT;

@ -7,6 +7,8 @@
# include <spdlog/details/periodic_worker.h>
#endif
#include <spdlog/details/os.h>
namespace spdlog {
namespace details {
@ -19,6 +21,7 @@ SPDLOG_INLINE periodic_worker::periodic_worker(const std::function<void()> &call
}
worker_thread_ = std::thread([this, callback_fun, interval]() {
os::set_thread_name("spdlog_periodic");
for (;;)
{
std::unique_lock<std::mutex> lock(this->mutex_);

@ -8,6 +8,7 @@
#endif
#include <spdlog/common.h>
#include <spdlog/details/os.h>
#include <cassert>
namespace spdlog {
@ -24,8 +25,10 @@ SPDLOG_INLINE thread_pool::thread_pool(
}
for (size_t i = 0; i < threads_n; i++)
{
threads_.emplace_back([this, on_thread_start, on_thread_stop] {
threads_.emplace_back([this, on_thread_start, on_thread_stop, i] {
on_thread_start();
std::string thread_name = "spdlog_pool_" + std::to_string(i);
os::set_thread_name(thread_name.c_str());
this->thread_pool::worker_loop_();
on_thread_stop();
});

Loading…
Cancel
Save