lokinet/llarp/util/thread/threading.cpp

58 lines
1.5 KiB
C++
Raw Normal View History

2019-09-01 13:26:16 +00:00
#include <util/thread/threading.hpp>
2019-09-01 12:10:49 +00:00
#include <util/logging/logger.hpp>
#include <cstring>
#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
#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
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());
#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)
const int rc = pthread_setname_np(pthread_self(), name.c_str());
2019-07-11 15:07:16 +00:00
#else
#error "unsupported platform"
#endif
if(rc)
{
LogError("Failed to set thread name to ", name, " errno = ", rc,
" errstr = ", ::strerror(rc));
}
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());
#else
LogInfo("Thread name setting not supported on this platform");
(void)name;
#endif
}
} // namespace util
} // namespace llarp