Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't set thread name on main thread #1329

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 32 additions & 26 deletions libs/pika/runtime/src/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#include <mutex>
#include <sstream>
#include <string>
#include <string_view>
#include <thread>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -1385,42 +1386,47 @@ namespace pika::detail {
PIKA_ASSERT(context != nullptr);
PIKA_ASSERT(context[0]);

std::ostringstream fullname;
std::ostringstream shortname;
fullname << "pika/" << context;
shortname << "pika/" << context[0];
if (pool_name && *pool_name)
// Only set thread name for threads that we create
if (std::string_view(context) != std::string_view("main-thread"))
{
fullname << "/pool:" << pool_name;
shortname << '/' << pool_name[0];
}
if (postfix && *postfix) { fullname << '/' << postfix; }
if (global_thread_num != std::size_t(-1))
{
fullname << "/global:" + std::to_string(global_thread_num);
}
if (local_thread_num != std::size_t(-1))
{
fullname << "/local:" + std::to_string(local_thread_num);
shortname << '/' << std::to_string(local_thread_num);
}
std::ostringstream fullname;
std::ostringstream shortname;
fullname << "pika/" << context;
shortname << "pika/" << context[0];
if (pool_name && *pool_name)
{
fullname << "/pool:" << pool_name;
shortname << '/' << pool_name[0];
}
if (postfix && *postfix) { fullname << '/' << postfix; }
if (global_thread_num != std::size_t(-1))
{
fullname << "/global:" + std::to_string(global_thread_num);
}
if (local_thread_num != std::size_t(-1))
{
fullname << "/local:" + std::to_string(local_thread_num);
shortname << '/' << std::to_string(local_thread_num);
}

PIKA_ASSERT(detail::get_thread_name_internal().empty());
detail::set_thread_name(fullname.str(), shortname.str());
PIKA_ASSERT(!detail::get_thread_name_internal().empty());
PIKA_ASSERT(detail::get_thread_name_internal().empty());
detail::set_thread_name(fullname.str(), shortname.str());
PIKA_ASSERT(!detail::get_thread_name_internal().empty());
#if defined(PIKA_HAVE_APEX) || defined(PIKA_HAVE_TRACY)
// Use internal name to ensure C-strings are backed by strings that will not go out of
// scope.
char const* name = detail::get_thread_name_internal().c_str();
// Use internal name to ensure C-strings are backed by strings that
// will not go out of scope.
char const* name = detail::get_thread_name_internal().c_str();

# if defined(PIKA_HAVE_APEX)
if (std::strstr(name, "worker") != nullptr) detail::external_timer::register_thread(name);
if (std::strstr(name, "worker") != nullptr)
detail::external_timer::register_thread(name);
# endif

# ifdef PIKA_HAVE_TRACY
tracy::SetThreadName(name);
tracy::SetThreadName(name);
# endif
#endif
}

// call thread-specific user-supplied on_start handler
if (on_start_func_)
Expand Down
Loading