diff --git a/stlab/concurrency/config.hpp b/stlab/concurrency/config.hpp index f4897e6ff..7c3b5ae97 100644 --- a/stlab/concurrency/config.hpp +++ b/stlab/concurrency/config.hpp @@ -16,9 +16,6 @@ #define STLAB_FEATURE_PRIVATE_OBJC_ARC() 0 #define STLAB_FEATURE_PRIVATE_COROUTINES() 0 -#define STLAB_FEATURE_PRIVATE_THREAD_NAME_APPLE() 0 -#define STLAB_FEATURE_PRIVATE_THREAD_NAME_POSIX() 0 - #define STLAB_FEATURE(X) (STLAB_FEATURE_PRIVATE_##X()) /**************************************************************************************************/ @@ -52,18 +49,6 @@ #endif -#if __APPLE__ - -#undef STLAB_FEATURE_PRIVATE_THREAD_NAME_APPLE -#define STLAB_FEATURE_PRIVATE_THREAD_NAME_APPLE() 1 - -#elif defined(__has_include) && __has_include() - -#undef STLAB_FEATURE_PRIVATE_THREAD_NAME_POSIX -#define STLAB_FEATURE_PRIVATE_THREAD_NAME_POSIX() 1 - -#endif - #if !defined(STLAB_CPP_VERSION_PRIVATE) #if __cplusplus == 201103L #define STLAB_CPP_VERSION_PRIVATE() 11 diff --git a/stlab/concurrency/default_executor.hpp b/stlab/concurrency/default_executor.hpp index 3de60f322..3afc2ada5 100644 --- a/stlab/concurrency/default_executor.hpp +++ b/stlab/concurrency/default_executor.hpp @@ -10,6 +10,7 @@ #define STLAB_CONCURRENCY_DEFAULT_EXECUTOR_HPP #include +#include #include #include @@ -40,12 +41,6 @@ /**************************************************************************************************/ -#if STLAB_FEATURE(THREAD_NAME_POSIX) || STLAB_FEATURE(THREAD_NAME_APPLE) -#include -#endif - -/**************************************************************************************************/ - namespace stlab { /**************************************************************************************************/ @@ -340,11 +335,7 @@ class priority_task_system { std::atomic_bool _done{false}; void run(unsigned i) { - #if STLAB_FEATURE(THREAD_NAME_POSIX) - pthread_setname_np(pthread_self(), "cc.stlab.default_executor"); - #elif STLAB_FEATURE(THREAD_NAME_APPLE) - pthread_setname_np("cc.stlab.default_executor"); - #endif + stlab::set_current_thread_name("cc.stlab.default_executor"); while (true) { task f; diff --git a/stlab/concurrency/set_current_thread_name.hpp b/stlab/concurrency/set_current_thread_name.hpp new file mode 100644 index 000000000..844c16209 --- /dev/null +++ b/stlab/concurrency/set_current_thread_name.hpp @@ -0,0 +1,81 @@ +#ifndef STLAB_SET_CURRENT_THREAD_NAME_HPP +#define STLAB_SET_CURRENT_THREAD_NAME_HPP + +/**************************************************************************************************/ +#if defined(__APPLE__) + +#include + +/**************************************************************************************************/ +#elif defined(_MSC_VER) + +#include +#include + +#include + +#include +#include + +/**************************************************************************************************/ +#elif defined(__EMSCRIPTEN__) && defined(__EMSCRIPTEN_PTHREADS__) + +#include + +#include + +/**************************************************************************************************/ +#elif defined(__has_include) && __has_include() + +#include + +/**************************************************************************************************/ +#endif + +/**************************************************************************************************/ + +namespace stlab { + +/**************************************************************************************************/ +#if defined(__APPLE__) + +inline void set_current_thread_name(const char* name) { pthread_setname_np(name); } + +/**************************************************************************************************/ +#elif defined(_MSC_VER) + +inline void set_current_thread_name(const char* name) { + /* Should string->wstring be split out to a utility? */ + int count = MultiByteToWideChar(CP_UTF8, 0, name, static_cast(std::strlen(name)), NULL, 0); + if (count <= 0) return; + std::wstring str(count, wchar_t{}); + count = MultiByteToWideChar(CP_UTF8, 0, name, static_cast(std::strlen(name)), &str[0], + static_cast(str.size())); + if (count <= 0) return; + + (void)SetThreadDescription(GetCurrentThread(), str.c_str()); +} + +/**************************************************************************************************/ +#elif defined(__EMSCRIPTEN__) && defined(__EMSCRIPTEN_PTHREADS__) + +inline void set_current_thread_name(const char* name) { + emscripten_set_thread_name(pthread_self(), name); +} + +/**************************************************************************************************/ +#elif defined(__has_include) && __has_include() + +inline void set_current_thread_name(const char* name) { pthread_setname_np(pthread_self(), name); } + +/**************************************************************************************************/ +#else + +inline void set_current_thread_name(const char*) {} + +/**************************************************************************************************/ +#endif + +} // namespace stlab + +#endif