11#include <sys/resource.h>
12#include <sys/syscall.h>
19static constexpr const std::size_t THREAD_NAME_MAX_SIZE = 16;
26#if !defined(UNIT_TEST)
28 return ::syscall(SYS_gettid);
32void SetThreadName(std::string_view thread_name, std::error_code& ec)
noexcept {
33 if (thread_name.size() >= THREAD_NAME_MAX_SIZE) {
34 ec = std::make_error_code(std::errc::result_out_of_range);
38 std::array<char, THREAD_NAME_MAX_SIZE> c_str;
39 std::strncpy(c_str.data(), thread_name.data(), thread_name.size());
40 c_str[thread_name.size()] =
'\0';
42 if (prctl(PR_SET_NAME, c_str.data()) == -1) {
43 ec = std::make_error_code(
static_cast<std::errc
>(errno));
53 throw std::system_error(ec,
"Failed to set thread name for current thread");
58 std::string thread_name;
59 std::array<char, THREAD_NAME_MAX_SIZE> c_str;
60 if (prctl(PR_GET_NAME, c_str.data()) == -1) {
61 ec = std::make_error_code(
static_cast<std::errc
>(errno));
66 thread_name.assign(c_str.data());
70 ec = std::make_error_code(std::errc::not_enough_memory);
79 throw std::system_error(ec,
"failed to get thread name");
87ThreadInitializer::ThreadInitializer(std::string_view name, NumaPolicies
const& policies)
88 : m_name(name), m_policies(policies), m_result(), m_cond(), m_mtx(), m_lck(m_mtx) {
91auto ThreadInitializer::Wait() -> std::system_error
const& {
92 m_cond.wait(m_lck, [&] {
return m_result.has_value(); });
96auto ThreadInitializer::Initialize() -> std::error_code {
102 auto lck = std::unique_lock(m_mtx);
108 std::system_error(ec,
"MakeThread: numapp::thisThread::SetThreadName() failed");
116 m_result = std::system_error(ec,
"MakeThread: numapp::thisThread::Apply() failed");
123 if (m_policies.GetMemPolicy()) {
125 MemPolicyFlag::Move | MemPolicyFlag::Strict);
127 m_result = std::system_error(ec,
"MakeThread: numapp::ApplyStack() failed");
134 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.