2020-10-22 11:53:08 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#include <boost/nowide/convert.hpp>
|
|
|
|
#else
|
|
|
|
// any posix system
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
2020-12-13 18:41:47 +00:00
|
|
|
#include <atomic>
|
2020-10-22 11:53:08 +00:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
|
|
|
#include <tbb/parallel_for.h>
|
|
|
|
#include <tbb/tbb_thread.h>
|
2021-01-04 08:03:49 +00:00
|
|
|
#include <tbb/task_arena.h>
|
2020-10-23 05:51:58 +00:00
|
|
|
#include <tbb/task_scheduler_init.h>
|
2020-10-22 11:53:08 +00:00
|
|
|
|
|
|
|
#include "Thread.hpp"
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2020-10-22 12:29:40 +00:00
|
|
|
#ifdef _WIN32
|
2020-10-26 07:09:03 +00:00
|
|
|
// The new API is better than the old SEH style thread naming since the names also show up in crash dumpsand ETW traces.
|
|
|
|
// Because the new API is only available on newer Windows 10, look it up dynamically.
|
2020-10-22 11:53:08 +00:00
|
|
|
|
2020-10-26 07:09:03 +00:00
|
|
|
typedef HRESULT(__stdcall* SetThreadDescriptionType)(HANDLE, PCWSTR);
|
|
|
|
typedef HRESULT(__stdcall* GetThreadDescriptionType)(HANDLE, PWSTR*);
|
|
|
|
|
|
|
|
static bool s_SetGetThreadDescriptionInitialized = false;
|
|
|
|
static HMODULE s_hKernel32 = nullptr;
|
|
|
|
static SetThreadDescriptionType s_fnSetThreadDescription = nullptr;
|
|
|
|
static GetThreadDescriptionType s_fnGetThreadDescription = nullptr;
|
|
|
|
|
|
|
|
static bool WindowsGetSetThreadNameAPIInitialize()
|
|
|
|
{
|
|
|
|
if (! s_SetGetThreadDescriptionInitialized) {
|
|
|
|
// Not thread safe! It is therefore a good idea to name the main thread before spawning worker threads
|
|
|
|
// to initialize
|
|
|
|
s_hKernel32 = LoadLibraryW(L"Kernel32.dll");
|
|
|
|
if (s_hKernel32) {
|
|
|
|
s_fnSetThreadDescription = (SetThreadDescriptionType)::GetProcAddress(s_hKernel32, "SetThreadDescription");
|
|
|
|
s_fnGetThreadDescription = (GetThreadDescriptionType)::GetProcAddress(s_hKernel32, "GetThreadDescription");
|
2020-10-22 11:53:08 +00:00
|
|
|
}
|
2020-10-26 07:09:03 +00:00
|
|
|
s_SetGetThreadDescriptionInitialized = true;
|
2020-10-22 11:53:08 +00:00
|
|
|
}
|
2020-10-26 07:09:03 +00:00
|
|
|
return s_fnSetThreadDescription && s_fnGetThreadDescription;
|
|
|
|
}
|
2020-10-22 11:53:08 +00:00
|
|
|
|
2020-10-26 07:09:03 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// Use the old way by throwing an exception, so at least in Debug mode the thread names are shown by the debugger.
|
|
|
|
static constexpr DWORD MSVC_SEH_EXCEPTION_NAME_THREAD = 0x406D1388;
|
2020-10-22 11:53:08 +00:00
|
|
|
|
2020-10-26 07:09:03 +00:00
|
|
|
#pragma pack(push,8)
|
2020-10-22 11:53:08 +00:00
|
|
|
typedef struct tagTHREADNAME_INFO
|
|
|
|
{
|
2020-10-26 07:09:03 +00:00
|
|
|
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.
|
2020-10-22 11:53:08 +00:00
|
|
|
} THREADNAME_INFO;
|
2020-10-26 07:09:03 +00:00
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
static void WindowsSetThreadNameSEH(HANDLE hThread, const char* thread_name)
|
2020-10-22 11:53:08 +00:00
|
|
|
{
|
2020-10-26 07:09:03 +00:00
|
|
|
THREADNAME_INFO info;
|
|
|
|
info.dwType = 0x1000;
|
|
|
|
info.szName = thread_name;
|
|
|
|
info.dwThreadID = ::GetThreadId(hThread);
|
|
|
|
info.dwFlags = 0;
|
|
|
|
__try {
|
|
|
|
RaiseException(MSVC_SEH_EXCEPTION_NAME_THREAD, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
|
|
|
|
} __except (EXCEPTION_EXECUTE_HANDLER) {
|
|
|
|
}
|
2020-10-22 11:53:08 +00:00
|
|
|
}
|
2020-10-26 07:09:03 +00:00
|
|
|
#endif // NDEBUG
|
2020-10-22 11:53:08 +00:00
|
|
|
|
2020-10-26 07:09:03 +00:00
|
|
|
static bool WindowsSetThreadName(HANDLE hThread, const char *thread_name)
|
|
|
|
{
|
|
|
|
if (! WindowsGetSetThreadNameAPIInitialize()) {
|
|
|
|
#ifdef NDEBUG
|
|
|
|
return false;
|
|
|
|
#else // NDEBUG
|
|
|
|
// Running on Windows 7 or old Windows 7 in debug mode,
|
|
|
|
// inform the debugger about the thread name by throwing an SEH.
|
|
|
|
WindowsSetThreadNameSEH(hThread, thread_name);
|
|
|
|
return true;
|
|
|
|
#endif // NDEBUG
|
|
|
|
}
|
2020-10-22 11:53:08 +00:00
|
|
|
|
2020-10-26 07:09:03 +00:00
|
|
|
size_t len = strlen(thread_name);
|
|
|
|
if (len < 1024) {
|
|
|
|
// Allocate the temp string on stack.
|
|
|
|
wchar_t buf[1024];
|
|
|
|
s_fnSetThreadDescription(hThread, boost::nowide::widen(buf, 1024, thread_name));
|
|
|
|
} else {
|
|
|
|
// Allocate dynamically.
|
|
|
|
s_fnSetThreadDescription(hThread, boost::nowide::widen(thread_name).c_str());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool set_thread_name(std::thread &thread, const char *thread_name)
|
2020-10-22 11:53:08 +00:00
|
|
|
{
|
2020-10-26 07:09:03 +00:00
|
|
|
return WindowsSetThreadName(static_cast<HANDLE>(thread.native_handle()), thread_name);
|
2020-10-22 11:53:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 07:09:03 +00:00
|
|
|
bool set_thread_name(boost::thread &thread, const char *thread_name)
|
2020-10-22 11:53:08 +00:00
|
|
|
{
|
2020-10-26 07:09:03 +00:00
|
|
|
return WindowsSetThreadName(static_cast<HANDLE>(thread.native_handle()), thread_name);
|
2020-10-22 11:53:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 07:09:03 +00:00
|
|
|
bool set_current_thread_name(const char *thread_name)
|
2020-10-22 11:53:08 +00:00
|
|
|
{
|
2020-10-26 07:09:03 +00:00
|
|
|
return WindowsSetThreadName(::GetCurrentThread(), thread_name);
|
2020-10-22 11:53:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 07:09:03 +00:00
|
|
|
std::optional<std::string> get_current_thread_name()
|
2020-10-22 12:11:00 +00:00
|
|
|
{
|
2020-10-26 07:09:03 +00:00
|
|
|
if (! WindowsGetSetThreadNameAPIInitialize())
|
|
|
|
return std::nullopt;
|
|
|
|
|
2020-10-22 12:11:00 +00:00
|
|
|
wchar_t *ptr = nullptr;
|
2020-10-26 07:09:03 +00:00
|
|
|
s_fnGetThreadDescription(::GetCurrentThread(), &ptr);
|
2020-10-22 12:35:20 +00:00
|
|
|
return (ptr == nullptr) ? std::string() : boost::nowide::narrow(ptr);
|
2020-10-22 12:11:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 11:53:08 +00:00
|
|
|
#else // _WIN32
|
|
|
|
|
2020-10-22 12:45:15 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
|
|
|
// Appe screwed the Posix norm.
|
2020-10-26 07:09:03 +00:00
|
|
|
bool set_thread_name(std::thread &thread, const char *thread_name)
|
2020-10-22 12:45:15 +00:00
|
|
|
{
|
|
|
|
// not supported
|
|
|
|
// pthread_setname_np(thread.native_handle(), thread_name);
|
2020-10-26 07:09:03 +00:00
|
|
|
return false;
|
2020-10-22 12:45:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 07:09:03 +00:00
|
|
|
bool set_thread_name(boost::thread &thread, const char *thread_name)
|
2020-10-22 12:45:15 +00:00
|
|
|
{
|
|
|
|
// not supported
|
|
|
|
// pthread_setname_np(thread.native_handle(), thread_name);
|
2020-10-26 07:09:03 +00:00
|
|
|
return false;
|
2020-10-22 12:45:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 07:09:03 +00:00
|
|
|
bool set_current_thread_name(const char *thread_name)
|
2020-10-22 12:45:15 +00:00
|
|
|
{
|
|
|
|
pthread_setname_np(thread_name);
|
2020-10-26 07:09:03 +00:00
|
|
|
return true;
|
2020-10-22 12:45:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 07:09:03 +00:00
|
|
|
std::optional<std::string> get_current_thread_name()
|
2020-10-22 12:45:15 +00:00
|
|
|
{
|
2020-10-22 12:57:50 +00:00
|
|
|
// not supported
|
|
|
|
// char buf[16];
|
|
|
|
// return std::string(thread_getname_np(buf, 16) == 0 ? buf : "");
|
2020-10-26 07:09:03 +00:00
|
|
|
return std::nullopt;
|
2020-10-22 12:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2020-10-22 11:53:08 +00:00
|
|
|
// posix
|
2020-10-26 07:09:03 +00:00
|
|
|
bool set_thread_name(std::thread &thread, const char *thread_name)
|
2020-10-22 11:53:08 +00:00
|
|
|
{
|
2020-10-22 12:29:40 +00:00
|
|
|
pthread_setname_np(thread.native_handle(), thread_name);
|
2020-10-26 07:09:03 +00:00
|
|
|
return true;
|
2020-10-22 11:53:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 07:09:03 +00:00
|
|
|
bool set_thread_name(boost::thread &thread, const char *thread_name)
|
2020-10-22 11:53:08 +00:00
|
|
|
{
|
2020-10-22 12:29:40 +00:00
|
|
|
pthread_setname_np(thread.native_handle(), thread_name);
|
2020-10-26 07:09:03 +00:00
|
|
|
return true;
|
2020-10-22 11:53:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 07:09:03 +00:00
|
|
|
bool set_current_thread_name(const char *thread_name)
|
2020-10-22 11:53:08 +00:00
|
|
|
{
|
2020-10-22 12:29:40 +00:00
|
|
|
pthread_setname_np(pthread_self(), thread_name);
|
2020-10-26 07:09:03 +00:00
|
|
|
return true;
|
2020-10-22 11:53:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 07:09:03 +00:00
|
|
|
std::optional<std::string> get_current_thread_name()
|
2020-10-22 12:11:00 +00:00
|
|
|
{
|
|
|
|
char buf[16];
|
|
|
|
return std::string(pthread_getname_np(pthread_self(), buf, 16) == 0 ? buf : "");
|
|
|
|
}
|
|
|
|
|
2020-10-22 12:45:15 +00:00
|
|
|
#endif
|
|
|
|
|
2020-10-22 11:53:08 +00:00
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
// Spawn (n - 1) worker threads on Intel TBB thread pool and name them by an index and a system thread ID.
|
|
|
|
void name_tbb_thread_pool_threads()
|
|
|
|
{
|
|
|
|
static bool initialized = false;
|
|
|
|
if (initialized)
|
|
|
|
return;
|
|
|
|
initialized = true;
|
|
|
|
|
2021-01-04 08:03:49 +00:00
|
|
|
// see GH issue #5661 PrusaSlicer hangs on Linux when run with non standard task affinity
|
|
|
|
// TBB will respect the task affinity mask on Linux and spawn less threads than std::thread::hardware_concurrency().
|
|
|
|
// const size_t nthreads_hw = std::thread::hardware_concurrency();
|
|
|
|
const size_t nthreads_hw = tbb::this_task_arena::max_concurrency();
|
2020-10-22 11:53:08 +00:00
|
|
|
size_t nthreads = nthreads_hw;
|
|
|
|
|
|
|
|
#ifdef SLIC3R_PROFILE
|
|
|
|
// Shiny profiler is not thread safe, thus disable parallelization.
|
|
|
|
nthreads = 1;
|
|
|
|
#endif
|
|
|
|
|
2020-10-22 12:29:40 +00:00
|
|
|
if (nthreads != nthreads_hw)
|
2020-10-26 07:09:03 +00:00
|
|
|
new tbb::task_scheduler_init(int(nthreads));
|
2020-10-22 11:53:08 +00:00
|
|
|
|
|
|
|
std::atomic<size_t> nthreads_running(0);
|
|
|
|
std::condition_variable cv;
|
|
|
|
std::mutex cv_m;
|
|
|
|
auto master_thread_id = tbb::this_tbb_thread::get_id();
|
|
|
|
tbb::parallel_for(
|
|
|
|
tbb::blocked_range<size_t>(0, nthreads, 1),
|
|
|
|
[&nthreads_running, nthreads, &master_thread_id, &cv, &cv_m](const tbb::blocked_range<size_t> &range) {
|
|
|
|
assert(range.begin() + 1 == range.end());
|
|
|
|
if (nthreads_running.fetch_add(1) + 1 == nthreads) {
|
|
|
|
// All threads are spinning.
|
|
|
|
// Wake them up.
|
|
|
|
cv.notify_all();
|
|
|
|
} else {
|
|
|
|
// Wait for the last thread to wake the others.
|
|
|
|
std::unique_lock<std::mutex> lk(cv_m);
|
|
|
|
cv.wait(lk, [&nthreads_running, nthreads]{return nthreads_running == nthreads;});
|
|
|
|
}
|
|
|
|
auto thread_id = tbb::this_tbb_thread::get_id();
|
|
|
|
if (thread_id == master_thread_id) {
|
|
|
|
// The calling thread runs the 0'th task.
|
|
|
|
assert(range.begin() == 0);
|
|
|
|
} else {
|
|
|
|
assert(range.begin() > 0);
|
|
|
|
std::ostringstream name;
|
2020-10-22 12:11:00 +00:00
|
|
|
name << "slic3r_tbb_" << range.begin();
|
2020-10-22 12:29:40 +00:00
|
|
|
set_current_thread_name(name.str().c_str());
|
2020-10-22 11:53:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|