diff --git a/CMakeLists.txt b/CMakeLists.txt index a55070c7..b19b0ef9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,7 @@ set( IXWEBSOCKET_SOURCES ixwebsocket/IXSelectInterrupt.cpp ixwebsocket/IXSelectInterruptFactory.cpp ixwebsocket/IXSelectInterruptPipe.cpp + ixwebsocket/IXSetThreadName.cpp ixwebsocket/IXSocket.cpp ixwebsocket/IXSocketConnect.cpp ixwebsocket/IXSocketFactory.cpp @@ -101,17 +102,6 @@ set( IXWEBSOCKET_HEADERS ixwebsocket/IXWebSocketVersion.h ) -# Platform specific code -if (APPLE) - list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/apple/IXSetThreadName_apple.cpp) -elseif (WIN32) - list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/windows/IXSetThreadName_windows.cpp) -elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") - list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/freebsd/IXSetThreadName_freebsd.cpp) -else() - list( APPEND IXWEBSOCKET_SOURCES ixwebsocket/linux/IXSetThreadName_linux.cpp) -endif() - option(USE_TLS "Enable TLS support" FALSE) if (USE_TLS) diff --git a/ixwebsocket/IXSetThreadName.cpp b/ixwebsocket/IXSetThreadName.cpp new file mode 100644 index 00000000..9f656fc7 --- /dev/null +++ b/ixwebsocket/IXSetThreadName.cpp @@ -0,0 +1,81 @@ +/* + * IXSetThreadName.cpp + * Author: Benjamin Sergeant + * Copyright (c) 2018 2020 Machine Zone, Inc. All rights reserved. + */ +#include "IXSetThreadName.h" + +// unix systems +#if defined(__APPLE__) || defined(__linux__) || defined(BSD) +# include +#endif + +// freebsd needs this header as well +#if defined(BSD) +# include +#endif + +// Windows +#ifdef _WIN32 +# include +#endif + +namespace ix +{ +#ifdef _WIN32 + const DWORD MS_VC_EXCEPTION = 0x406D1388; + +#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) + + void SetThreadName(DWORD dwThreadID, const char* threadName) + { + THREADNAME_INFO info; + info.dwType = 0x1000; + info.szName = threadName; + info.dwThreadID = dwThreadID; + info.dwFlags = 0; + + __try + { + RaiseException( + MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*) &info); + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + } + } +#endif + + void setThreadName(const std::string& name) + { +#if defined(__APPLE__) + // + // Apple reserves 16 bytes for its thread names + // Notice that the Apple version of pthread_setname_np + // does not take a pthread_t argument + // + pthread_setname_np(name.substr(0, 63).c_str()); +#elif defined(__linux__) + // + // Linux only reserves 16 bytes for its thread names + // See prctl and PR_SET_NAME property in + // http://man7.org/linux/man-pages/man2/prctl.2.html + // + pthread_setname_np(pthread_self(), name.substr(0, 15).c_str()); +#elif defined(_WIN32) + SetThreadName(-1, name.c_str()); +#elif defined(BSD) + pthread_set_name_np(pthread_self(), name.substr(0, 15).c_str()); +#else + // ... assert here ? +#endif + } +} // namespace ix diff --git a/ixwebsocket/apple/IXSetThreadName_apple.cpp b/ixwebsocket/apple/IXSetThreadName_apple.cpp deleted file mode 100644 index c4291db2..00000000 --- a/ixwebsocket/apple/IXSetThreadName_apple.cpp +++ /dev/null @@ -1,20 +0,0 @@ -/* - * IXSetThreadName_apple.cpp - * Author: Benjamin Sergeant - * Copyright (c) 2018 Machine Zone, Inc. All rights reserved. - */ -#include "../IXSetThreadName.h" -#include - -namespace ix -{ - void setThreadName(const std::string& name) - { - // - // Apple reserves 16 bytes for its thread names - // Notice that the Apple version of pthread_setname_np - // does not take a pthread_t argument - // - pthread_setname_np(name.substr(0, 63).c_str()); - } -} // namespace ix diff --git a/ixwebsocket/freebsd/IXSetThreadName_freebsd.cpp b/ixwebsocket/freebsd/IXSetThreadName_freebsd.cpp deleted file mode 100644 index 9047e2c0..00000000 --- a/ixwebsocket/freebsd/IXSetThreadName_freebsd.cpp +++ /dev/null @@ -1,16 +0,0 @@ -/* - * IXSetThreadName_freebsd.cpp - * Author: Benjamin Sergeant - * Copyright (c) 2019 Machine Zone, Inc. All rights reserved. - */ -#include "../IXSetThreadName.h" -#include -#include - -namespace ix -{ - void setThreadName(const std::string& name) - { - pthread_set_name_np(pthread_self(), name.substr(0, 15).c_str()); - } -} // namespace ix diff --git a/ixwebsocket/linux/IXSetThreadName_linux.cpp b/ixwebsocket/linux/IXSetThreadName_linux.cpp deleted file mode 100644 index 1e548efa..00000000 --- a/ixwebsocket/linux/IXSetThreadName_linux.cpp +++ /dev/null @@ -1,20 +0,0 @@ -/* - * IXSetThreadName_linux.cpp - * Author: Benjamin Sergeant - * Copyright (c) 2018 Machine Zone, Inc. All rights reserved. - */ -#include "../IXSetThreadName.h" -#include - -namespace ix -{ - void setThreadName(const std::string& name) - { - // - // Linux only reserves 16 bytes for its thread names - // See prctl and PR_SET_NAME property in - // http://man7.org/linux/man-pages/man2/prctl.2.html - // - pthread_setname_np(pthread_self(), name.substr(0, 15).c_str()); - } -} // namespace ix diff --git a/ixwebsocket/windows/IXSetThreadName_windows.cpp b/ixwebsocket/windows/IXSetThreadName_windows.cpp deleted file mode 100644 index 7773e5f5..00000000 --- a/ixwebsocket/windows/IXSetThreadName_windows.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/* - * IXSetThreadName_windows.cpp - * Author: Benjamin Sergeant - * Copyright (c) 2019 Machine Zone, Inc. All rights reserved. - */ -#include "../IXSetThreadName.h" - -#include - -namespace ix -{ - const DWORD MS_VC_EXCEPTION = 0x406D1388; - -#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) - - void SetThreadName(DWORD dwThreadID, const char* threadName) - { - THREADNAME_INFO info; - info.dwType = 0x1000; - info.szName = threadName; - info.dwThreadID = dwThreadID; - info.dwFlags = 0; - - __try - { - RaiseException( - MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*) &info); - } - __except (EXCEPTION_EXECUTE_HANDLER) - { - } - } - - void setThreadName(const std::string& name) - { - SetThreadName(-1, name.c_str()); - } -} // namespace ix