13#include <sys/resource.h>
14#include <sys/syscall.h>
21static constexpr const std::size_t THREAD_NAME_MAX_SIZE = 16;
28#if !defined(UNIT_TEST)
30 return ::syscall(SYS_gettid);
34void SetThreadName(std::string_view thread_name, std::error_code& ec)
noexcept {
35 if (thread_name.size() >= THREAD_NAME_MAX_SIZE) {
36 ec = std::make_error_code(std::errc::result_out_of_range);
40 std::array<char, THREAD_NAME_MAX_SIZE> c_str;
41 std::strncpy(c_str.data(), thread_name.data(), thread_name.size());
42 c_str[thread_name.size()] =
'\0';
44 if (prctl(PR_SET_NAME, c_str.data()) == -1) {
45 ec = std::make_error_code(
static_cast<std::errc
>(errno));
55 throw std::system_error(ec,
"Failed to set thread name for current thread");
60 std::string thread_name;
61 std::array<char, THREAD_NAME_MAX_SIZE> c_str;
62 if (prctl(PR_GET_NAME, c_str.data()) == -1) {
63 ec = std::make_error_code(
static_cast<std::errc
>(errno));
68 thread_name.assign(c_str.data());
72 ec = std::make_error_code(std::errc::not_enough_memory);
81 throw std::system_error(ec,
"failed to get thread name");
89ThreadInitializer::ThreadInitializer(std::string_view name, NumaPolicies
const& policies)
90 : m_name(name), m_policies(policies), m_result(), m_cond(), m_mtx(), m_lck(m_mtx) {
93auto ThreadInitializer::Wait() -> std::system_error
const& {
94 m_cond.wait(m_lck, [&] {
return m_result.has_value(); });
98auto ThreadInitializer::Initialize() -> std::error_code {
104 auto lck = std::unique_lock(m_mtx);
110 std::system_error(ec,
"MakeThread: numapp::thisThread::SetThreadName() failed");
118 m_result = std::system_error(ec,
"MakeThread: numapp::thisThread::Apply() failed");
125 if (m_policies.GetMemPolicy()) {
127 MemPolicyFlag::Move | MemPolicyFlag::Strict);
129 m_result = std::system_error(ec,
"MakeThread: numapp::ApplyStack() failed");
136 m_result = std::system_error(std::error_code());
std::error_code Apply(CpuAffinity const &affinity) noexcept
Apply policy to calling thread.
std::error_code ApplyStack(MemPolicy const &policy, MemPolicyFlag flags=MemPolicyFlag::Move|MemPolicyFlag::Strict) noexcept
Convenience function that applies a memory policy to current thread stack memory.
pid_t GetThreadId() noexcept
Query the thread id "TID" of the current thread.
void SetThreadName(std::string_view thread_name, std::error_code &ec) noexcept
Set thread name for current thread.
std::string GetThreadName()
Get name of current thread (throwing version).
Contains declarations for numapp thread utilities.