8#include "../detail/thread/control.hpp"
9#include "../detail/thread/identity.hpp"
10#include "../detail/try_result.hpp"
11#include "../result.hpp"
16#include <system_error>
50 [[nodiscard]]
static auto
56 auto identity = unique_identity(name);
67 [[nodiscard]]
static auto
71 [name]() ->
result<std::vector<thread_by_name_view>>
77 std::vector<thread_by_name_view> views;
78 views.reserve(identities->size());
79 for (
auto const& identity : identities.value())
107 auto checked = with_live_identity([](
auto ) ->
result<void> {
return {}; });
112 auto named =
set_name(*native.name);
116 if (native.scheduling)
118 auto scheduled = with_live_identity(
133 return with_live_identity(
145 return with_live_identity(
155 return detail::portable_thread_control::get_priority(std::move(value));
163 return detail::portable_thread_control::get_nice(std::move(value));
170 return with_live_identity([&](
auto id) {
return detail::apply_name(
id, name); });
177 return with_live_identity([](
auto id) {
return detail::read_name(
id); });
196 auto native = with_live_identity([](
auto id) {
return detail::read_affinity(
id); });
197 return detail::portable_thread_control::get_affinity(std::move(native));
209 [[nodiscard]]
static auto
210 unique_identity(std::string_view name) -> result<detail::native_thread_identity>
214 return unexpected(identities.error());
215 if (identities->empty())
216 return unexpected(std::make_error_code(std::errc::no_such_process));
217 if (identities->size() != 1)
218 return unexpected(std::make_error_code(std::errc::invalid_argument));
219 return identities->front();
222 [[nodiscard]]
static auto
223 unique_identity_or_throw(std::string_view name) -> detail::native_thread_identity
225 auto identity = unique_identity(name);
227 throw std::system_error(identity.error(),
"thread_by_name_view");
228 return identity.value();
231 template <
typename Function>
233 with_live_identity(Function&& function)
const
234 ->
decltype(std::declval<Function>()(std::declval<detail::native_thread_id>()))
236 using result_type =
decltype(std::declval<Function>()(std::declval<detail::native_thread_id>()));
237 auto const inactive = [] {
return result_type(unexpected(std::make_error_code(std::errc::no_such_process))); };
241 auto result_value = std::forward<Function>(function)(identity_.
id);
242 if (result_value && !
alive())
247 detail::native_thread_identity identity_;
Non-owning control view found by an exact process-thread name.
auto configure(thread_config const &config) -> result< void >
Apply a portable thread configuration to the target.
auto alive() const noexcept -> bool
Return whether the original native thread identity is still live.
auto set_nice(nice_value value) -> result< void >
Set an explicit nice value on the target.
static auto find_all(std::string_view name) -> result< std::vector< thread_by_name_view > >
Return every exact name match ordered by native thread ID.
static auto create(std::string_view name) -> result< thread_by_name_view >
Find exactly one named process thread without throwing.
thread_by_name_view(std::string_view name)
Find exactly one process thread with name.
auto set_priority(priority_level level) -> result< void >
Set a portable priority preset on the target.
auto set_affinity(thread_affinity const &affinity) -> result< void >
Set target CPU affinity with exact readback and rollback.
auto get_priority() const -> result< priority_level >
Query the effective portable priority preset.
auto get_affinity() const -> result< thread_affinity >
Query target CPU affinity.
auto get_name() const -> result< std::string >
Query the current OS-visible target name.
auto native_id() const noexcept -> native_thread_id
Return the platform-native process thread ID.
auto get_nice() const -> result< nice_value >
Query the effective nice value.
auto set_name(std::string const &name) -> result< void >
Set the OS-visible target name without changing view identity.
Strongly-typed nice value in the POSIX range $[-20, 19]$.
Portable thread configuration bundle.
constexpr auto posix_nice(int nice_value) noexcept -> native_scheduling_config
auto read_effective_nice(NativeHandle handle, native_thread_id tid) -> expected< int, std::error_code >
auto apply_affinity_checked(NativeHandle handle, native_thread_affinity const &affinity) -> expected< void, std::error_code >
constexpr auto to_native(shutdown_policy policy) noexcept -> shutdown_policy_backend
auto native_thread_is_alive(native_thread_identity const &identity) noexcept -> bool
auto apply_scheduling_config(NativeHandle handle, native_thread_id tid, native_scheduling_config const &config) -> expected< void, std::error_code >
auto try_result(Function &&function) -> decltype(std::forward< Function >(function)())
auto find_native_threads_by_name(std::string_view name) -> result< std::vector< native_thread_identity > >
priority_level
Portable non-realtime priority levels.
Native thread identity and handle escape hatches.