Skip to content

Commit

Permalink
Sean parent/set thread name (stlab#376)
Browse files Browse the repository at this point in the history
* Splitting out set_current_thread_name() into a  separate file

The previous implementation did not work on wasm, I also added Windows support. This should make it simple to provide platform-specific implementations.
  • Loading branch information
sean-parent authored Apr 9, 2021
1 parent b23087a commit c86c645
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 26 deletions.
15 changes: 0 additions & 15 deletions stlab/concurrency/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())

/**************************************************************************************************/
Expand Down Expand Up @@ -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(<pthread.h>)

#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
Expand Down
13 changes: 2 additions & 11 deletions stlab/concurrency/default_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define STLAB_CONCURRENCY_DEFAULT_EXECUTOR_HPP

#include <stlab/concurrency/config.hpp>
#include <stlab/concurrency/set_current_thread_name.hpp>
#include <stlab/concurrency/task.hpp>

#include <cassert>
Expand Down Expand Up @@ -40,12 +41,6 @@

/**************************************************************************************************/

#if STLAB_FEATURE(THREAD_NAME_POSIX) || STLAB_FEATURE(THREAD_NAME_APPLE)
#include <pthread.h>
#endif

/**************************************************************************************************/

namespace stlab {

/**************************************************************************************************/
Expand Down Expand Up @@ -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<void()> f;

Expand Down
81 changes: 81 additions & 0 deletions stlab/concurrency/set_current_thread_name.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#ifndef STLAB_SET_CURRENT_THREAD_NAME_HPP
#define STLAB_SET_CURRENT_THREAD_NAME_HPP

/**************************************************************************************************/
#if defined(__APPLE__)

#include <pthread.h>

/**************************************************************************************************/
#elif defined(_MSC_VER)

#include <cstring>
#include <string>

#include <windows.h>

#include <processthreadsapi.h>
#include <stringapiset.h>

/**************************************************************************************************/
#elif defined(__EMSCRIPTEN__) && defined(__EMSCRIPTEN_PTHREADS__)

#include <pthread.h>

#include <emscripten/threading.h>

/**************************************************************************************************/
#elif defined(__has_include) && __has_include(<pthread.h>)

#include <pthread.h>

/**************************************************************************************************/
#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<int>(std::strlen(name)), NULL, 0);
if (count <= 0) return;
std::wstring str(count, wchar_t{});
count = MultiByteToWideChar(CP_UTF8, 0, name, static_cast<int>(std::strlen(name)), &str[0],
static_cast<int>(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(<pthread.h>)

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

0 comments on commit c86c645

Please sign in to comment.