forked from stlab/libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sean parent/set thread name (stlab#376)
* 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
1 parent
b23087a
commit c86c645
Showing
3 changed files
with
83 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |