ThreadSchedule 1.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
threadschedule::ThreadRegistry Class Reference

Central registry of threads indexed by OS-level thread ID (Tid). More...

#include <thread_registry.hpp>

Classes

class  QueryView
 Lazy, functional-style query/filter view over a snapshot of registered threads. More...

Public Member Functions

 ThreadRegistry (ThreadRegistry const &)=delete
auto operator= (ThreadRegistry const &) -> ThreadRegistry &=delete
void register_current_thread (std::string name=std::string(), std::string componentTag=std::string())
void register_current_thread (std::shared_ptr< ThreadControlBlock > const &controlBlock, std::string name=std::string(), std::string componentTag=std::string())
void unregister_current_thread ()
auto get (Tid tid) const -> std::optional< RegisteredThreadInfo >
auto query () const -> QueryView
template<typename Predicate>
auto filter (Predicate &&pred) const -> QueryView
auto count () const -> size_t
auto empty () const -> bool
template<typename Fn>
void for_each (Fn &&fn) const
template<typename Predicate, typename Fn>
void apply (Predicate &&pred, Fn &&fn) const
template<typename Fn>
auto map (Fn &&fn) const -> std::vector< std::invoke_result_t< Fn, RegisteredThreadInfo const & > >
template<typename Predicate>
auto find_if (Predicate &&pred) const -> std::optional< RegisteredThreadInfo >
template<typename Predicate>
auto any (Predicate &&pred) const -> bool
template<typename Predicate>
auto all (Predicate &&pred) const -> bool
template<typename Predicate>
auto none (Predicate &&pred) const -> bool
auto take (size_t n) const -> QueryView
auto skip (size_t n) const -> QueryView
auto set_affinity (Tid tid, ThreadAffinity const &affinity) const -> expected< void, std::error_code >
auto set_priority (Tid tid, ThreadPriority priority) const -> expected< void, std::error_code >
auto set_scheduling_policy (Tid tid, SchedulingPolicy policy, ThreadPriority priority) const -> expected< void, std::error_code >
auto set_name (Tid tid, std::string const &name) const -> expected< void, std::error_code >
void set_on_register (std::function< void(RegisteredThreadInfo const &)> cb)
void set_on_unregister (std::function< void(RegisteredThreadInfo const &)> cb)

Detailed Description

Central registry of threads indexed by OS-level thread ID (Tid).

ThreadRegistry maintains a map of currently registered threads together with their metadata and optional ThreadControlBlock handles. It provides a functional-style query API (via QueryView) and convenience methods that delegate scheduling operations to each thread's control block.

Thread safety
All public methods are thread-safe. Internal state is protected by a std::shared_mutex: mutating operations (register, unregister, set callbacks) acquire a unique lock, while read-only operations (get, query, set_affinity, etc.) acquire a shared lock.
Copyability / movability
  • Not copyable (copy constructor and assignment are deleted).
  • Not movable (implicitly deleted because copy operations are deleted and the class holds a std::shared_mutex).
Registration semantics
  • register_current_thread() must be called from the thread being registered. Duplicate registration of the same TID is silently ignored (the first registration wins).
  • unregister_current_thread() removes the calling thread's entry and marks its alive flag as false in the snapshot passed to the callback.
Callbacks
The optional onRegister / onUnregister callbacks are invoked with the lock released to avoid deadlock if the callback itself interacts with the registry. The callback receives a copy of the RegisteredThreadInfo.
Querying
query() returns a QueryView holding a snapshot of the registry at the moment of the call. Subsequent changes to the registry (new registrations, unregistrations) are not reflected in an existing QueryView.
Scheduling helpers
set_affinity(), set_priority(), set_scheduling_policy(), and set_name() look up the ThreadControlBlock for the given TID under a shared lock and delegate to the control block. Returns std::errc::no_such_process if the TID is not registered or has no control block.

Definition at line 336 of file thread_registry.hpp.

Member Function Documentation

◆ all()

template<typename Predicate>
auto threadschedule::ThreadRegistry::all ( Predicate && pred) const -> bool
inlinenodiscard

Definition at line 650 of file thread_registry.hpp.

◆ any()

template<typename Predicate>
auto threadschedule::ThreadRegistry::any ( Predicate && pred) const -> bool
inlinenodiscard

Definition at line 644 of file thread_registry.hpp.

◆ apply()

template<typename Predicate, typename Fn>
void threadschedule::ThreadRegistry::apply ( Predicate && pred,
Fn && fn ) const
inline

Definition at line 626 of file thread_registry.hpp.

◆ count()

auto threadschedule::ThreadRegistry::count ( ) const -> size_t
inlinenodiscard

Definition at line 609 of file thread_registry.hpp.

◆ empty()

auto threadschedule::ThreadRegistry::empty ( ) const -> bool
inlinenodiscard

Definition at line 614 of file thread_registry.hpp.

◆ filter()

template<typename Predicate>
auto threadschedule::ThreadRegistry::filter ( Predicate && pred) const -> QueryView
inlinenodiscard

Definition at line 604 of file thread_registry.hpp.

◆ find_if()

template<typename Predicate>
auto threadschedule::ThreadRegistry::find_if ( Predicate && pred) const -> std::optional<RegisteredThreadInfo>
inlinenodiscard

Definition at line 638 of file thread_registry.hpp.

◆ for_each()

template<typename Fn>
void threadschedule::ThreadRegistry::for_each ( Fn && fn) const
inline

Definition at line 620 of file thread_registry.hpp.

◆ get()

auto threadschedule::ThreadRegistry::get ( Tid tid) const -> std::optional<RegisteredThreadInfo>
inlinenodiscard

Definition at line 426 of file thread_registry.hpp.

◆ map()

template<typename Fn>
auto threadschedule::ThreadRegistry::map ( Fn && fn) const -> std::vector<std::invoke_result_t<Fn, RegisteredThreadInfo const&>>
inlinenodiscard

Definition at line 632 of file thread_registry.hpp.

◆ none()

template<typename Predicate>
auto threadschedule::ThreadRegistry::none ( Predicate && pred) const -> bool
inlinenodiscard

Definition at line 656 of file thread_registry.hpp.

◆ query()

auto threadschedule::ThreadRegistry::query ( ) const -> QueryView
inlinenodiscard

Definition at line 591 of file thread_registry.hpp.

◆ register_current_thread() [1/2]

void threadschedule::ThreadRegistry::register_current_thread ( std::shared_ptr< ThreadControlBlock > const & controlBlock,
std::string name = std::string(),
std::string componentTag = std::string() )
inline

Definition at line 375 of file thread_registry.hpp.

◆ register_current_thread() [2/2]

void threadschedule::ThreadRegistry::register_current_thread ( std::string name = std::string(),
std::string componentTag = std::string() )
inline

Definition at line 344 of file thread_registry.hpp.

◆ set_affinity()

auto threadschedule::ThreadRegistry::set_affinity ( Tid tid,
ThreadAffinity const & affinity ) const -> expected<void, std::error_code>
inlinenodiscard

Definition at line 671 of file thread_registry.hpp.

◆ set_name()

auto threadschedule::ThreadRegistry::set_name ( Tid tid,
std::string const & name ) const -> expected<void, std::error_code>
inlinenodiscard

Definition at line 696 of file thread_registry.hpp.

◆ set_on_register()

void threadschedule::ThreadRegistry::set_on_register ( std::function< void(RegisteredThreadInfo const &)> cb)
inline

Definition at line 705 of file thread_registry.hpp.

◆ set_on_unregister()

void threadschedule::ThreadRegistry::set_on_unregister ( std::function< void(RegisteredThreadInfo const &)> cb)
inline

Definition at line 711 of file thread_registry.hpp.

◆ set_priority()

auto threadschedule::ThreadRegistry::set_priority ( Tid tid,
ThreadPriority priority ) const -> expected<void, std::error_code>
inlinenodiscard

Definition at line 679 of file thread_registry.hpp.

◆ set_scheduling_policy()

auto threadschedule::ThreadRegistry::set_scheduling_policy ( Tid tid,
SchedulingPolicy policy,
ThreadPriority priority ) const -> expected<void, std::error_code>
inlinenodiscard

Definition at line 687 of file thread_registry.hpp.

◆ skip()

auto threadschedule::ThreadRegistry::skip ( size_t n) const -> QueryView
inlinenodiscard

Definition at line 666 of file thread_registry.hpp.

◆ take()

auto threadschedule::ThreadRegistry::take ( size_t n) const -> QueryView
inlinenodiscard

Definition at line 661 of file thread_registry.hpp.

◆ unregister_current_thread()

void threadschedule::ThreadRegistry::unregister_current_thread ( )
inline

Definition at line 406 of file thread_registry.hpp.


The documentation for this class was generated from the following file: