2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/threading.hpp>
|
2019-07-09 00:06:22 +00:00
|
|
|
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-09-12 18:19:25 +00:00
|
|
|
#include <cstring>
|
2019-07-09 00:06:22 +00:00
|
|
|
|
|
|
|
#ifdef POSIX
|
|
|
|
#include <pthread.h>
|
2019-07-31 21:28:00 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
2019-07-10 16:46:21 +00:00
|
|
|
#include <pthread_np.h>
|
|
|
|
#endif
|
2019-07-09 00:06:22 +00:00
|
|
|
#endif
|
|
|
|
|
2019-08-02 03:25:48 +00:00
|
|
|
#ifdef _MSC_VER
|
2019-07-31 20:33:32 +00:00
|
|
|
#include <windows.h>
|
2019-08-02 03:25:48 +00:00
|
|
|
extern "C" void
|
|
|
|
SetThreadName(DWORD dwThreadID, LPCSTR szThreadName);
|
2019-07-31 20:33:32 +00:00
|
|
|
#endif
|
|
|
|
|
2019-07-09 00:06:22 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace util
|
|
|
|
{
|
|
|
|
void
|
|
|
|
SetThreadName(const std::string& name)
|
|
|
|
{
|
|
|
|
#ifdef POSIX
|
2019-07-31 21:28:00 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
/* on bsd this function has void return type */
|
2019-07-12 12:31:37 +00:00
|
|
|
pthread_set_name_np(pthread_self(), name.c_str());
|
2019-07-09 00:06:22 +00:00
|
|
|
#else
|
2019-07-11 15:07:16 +00:00
|
|
|
#if defined(__MACH__)
|
|
|
|
const int rc = pthread_setname_np(name.c_str());
|
2019-07-31 20:22:23 +00:00
|
|
|
// API present upstream since v2.11.3 and imported downstream
|
2019-08-02 03:25:48 +00:00
|
|
|
// in CR 8158 <https://www.illumos.org/issues/8158>
|
2019-07-31 21:28:00 +00:00
|
|
|
// We only use the native function on Microsoft C++ builds
|
2019-08-02 04:42:32 +00:00
|
|
|
#elif defined(__linux__) || defined(__sun)
|
2019-07-09 00:06:22 +00:00
|
|
|
const int rc = pthread_setname_np(pthread_self(), name.c_str());
|
2019-07-11 15:07:16 +00:00
|
|
|
#else
|
|
|
|
#error "unsupported platform"
|
2019-07-10 22:03:52 +00:00
|
|
|
#endif
|
2020-04-07 18:38:56 +00:00
|
|
|
if (rc)
|
2019-07-09 00:06:22 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
LogError(
|
|
|
|
"Failed to set thread name to ", name, " errno = ", rc, " errstr = ", ::strerror(rc));
|
2019-07-09 00:06:22 +00:00
|
|
|
}
|
2019-07-11 15:07:16 +00:00
|
|
|
#endif
|
2019-07-31 20:33:32 +00:00
|
|
|
#elif _MSC_VER
|
|
|
|
::SetThreadName(::GetCurrentThreadId(), name.c_str());
|
2019-08-02 04:42:32 +00:00
|
|
|
#elif __MINGW32__
|
|
|
|
const int rc = pthread_setname_np(pthread_self(), name.c_str());
|
2019-07-09 00:06:22 +00:00
|
|
|
#else
|
|
|
|
LogInfo("Thread name setting not supported on this platform");
|
|
|
|
(void)name;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} // namespace util
|
|
|
|
} // namespace llarp
|