ThreadSchedule 1.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
registered_threads.hpp
1#pragma once
2
3#include "pthread_wrapper.hpp"
4#include "thread_registry.hpp"
5#include "thread_wrapper.hpp"
6#include <functional>
7#include <string>
8#include <type_traits>
9#include <utility>
10
11namespace threadschedule
12{
13
23class ThreadWrapperReg : public ThreadWrapper
24{
25 public:
26 ThreadWrapperReg() = default;
27
28 ThreadWrapperReg(ThreadWrapperReg&&) noexcept = default;
29 auto operator=(ThreadWrapperReg&&) noexcept -> ThreadWrapperReg& = default;
30
31 ThreadWrapperReg(ThreadWrapperReg const&) = delete;
32 auto operator=(ThreadWrapperReg const&) -> ThreadWrapperReg& = delete;
33
34 template <typename F, typename... Args>
35 explicit ThreadWrapperReg(std::string name, std::string componentTag, F&& f, Args&&... args)
36 : ThreadWrapper(
37 [n = std::move(name), c = std::move(componentTag), func = std::forward<F>(f)](auto&&... inner) {
38 AutoRegisterCurrentThread guard(n, c);
39 std::invoke(func, std::forward<decltype(inner)>(inner)...);
40 },
41 std::forward<Args>(args)...)
42 {
43 }
44};
45
46#if __cplusplus >= 202002L || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
56class JThreadWrapperReg : public JThreadWrapper
57{
58 public:
59 JThreadWrapperReg() = default;
60
61 JThreadWrapperReg(JThreadWrapperReg&&) noexcept = default;
62 auto operator=(JThreadWrapperReg&&) noexcept -> JThreadWrapperReg& = default;
63
64 JThreadWrapperReg(JThreadWrapperReg const&) = delete;
65 auto operator=(JThreadWrapperReg const&) -> JThreadWrapperReg& = delete;
66
67 template <typename F, typename... Args>
68 explicit JThreadWrapperReg(std::string name, std::string componentTag, F&& f, Args&&... args)
69 : JThreadWrapper(
70 [n = std::move(name), c = std::move(componentTag),
71 func = std::forward<F>(f)](std::stop_token st, auto&&... inner) {
72 AutoRegisterCurrentThread guard(n, c);
73 if constexpr (std::is_invocable_v<decltype(func), std::stop_token,
74 decltype(inner)...>)
75 std::invoke(func, std::move(st), std::forward<decltype(inner)>(inner)...);
76 else if constexpr (std::is_invocable_v<decltype(func), decltype(inner)...,
77 std::stop_token>)
78 std::invoke(func, std::forward<decltype(inner)>(inner)..., std::move(st));
79 else
80 std::invoke(func, std::forward<decltype(inner)>(inner)...);
81 },
82 std::forward<Args>(args)...)
83 {
84 }
85};
86#endif
87
88#ifndef _WIN32
96class PThreadWrapperReg : public PThreadWrapper
97{
98 public:
99 PThreadWrapperReg() = default;
100
101 PThreadWrapperReg(PThreadWrapperReg&&) noexcept = default;
102 auto operator=(PThreadWrapperReg&&) noexcept -> PThreadWrapperReg& = default;
103
104 PThreadWrapperReg(PThreadWrapperReg const&) = delete;
105 auto operator=(PThreadWrapperReg const&) -> PThreadWrapperReg& = delete;
106
107 template <typename F, typename... Args>
108 explicit PThreadWrapperReg(std::string name, std::string componentTag, F&& f, Args&&... args)
109 : PThreadWrapper(
110 [n = std::move(name), c = std::move(componentTag), func = std::forward<F>(f)](auto&&... inner) {
111 AutoRegisterCurrentThread guard(n, c);
112 std::invoke(func, std::forward<decltype(inner)>(inner)...);
113 },
114 std::forward<Args>(args)...)
115 {
116 }
117};
118#endif
119
120} // namespace threadschedule
RAII guard that registers the current thread on construction and unregisters it on destruction.