threadman 0.1.0
Header-only C++23 managed threads, dynamic pools, futures, and executors
Loading...
Searching...
No Matches
parcel.hpp
Go to the documentation of this file.
1#pragma once
2
14
15#include <threadman/config.hpp>
16
17#if !THREADMAN_WITH_NLOHMANN_JSON
18#error "threadman/parcel.hpp requires THREADMAN_WITH_NLOHMANN_JSON to be enabled \
19(parcel pulls in nlohmann/json — the threadman cell adapters rely on the JSON-gated \
20to_json/from_json overloads of the snapshot types)."
21#endif
22
23#include <threadman/json.hpp>
24#include <threadman/stats.hpp>
25
26#include <parcel/parcel.h>
27
28#include <memory>
29#include <sstream>
30#include <string>
31#include <string_view>
32
33namespace threadman {
34
35class ThreadSnapshotCell : public parcel::BaseCell<ThreadSnapshotCell, ThreadSnapshot> {
36 using base_t = parcel::BaseCell<ThreadSnapshotCell, ThreadSnapshot>;
37
38public:
39 using base_t::base_t;
40 using base_t::operator=;
41 static constexpr std::string_view kind_id = "tm:thread_snapshot";
42
43 [[nodiscard]] std::string to_string() const override {
44 std::ostringstream os;
45 os << this->value.name << "#" << this->value.id;
46 return os.str();
47 }
48
49 [[maybe_unused]] static parcel::cell_t from_json(parcel::json_t const& j,
50 parcel::ParcelRegistry const&) {
51 return base_t::from_json_strict(j);
52 }
53
54 static parcel::cell_type_descriptor_t descriptor() {
55 static const auto d =
56 std::make_shared<parcel::SimpleCellTypeDescriptor<ThreadSnapshotCell>>(
57 parcel::DisplayInfo{
58 .name = "ThreadSnapshot",
59 .description = "Snapshot of a ManagedThread.",
60 });
61 return d;
62 }
63};
64
65class ThreadPoolStatsCell : public parcel::BaseCell<ThreadPoolStatsCell, ThreadPoolStats> {
66 using base_t = parcel::BaseCell<ThreadPoolStatsCell, ThreadPoolStats>;
67
68public:
69 using base_t::base_t;
70 using base_t::operator=;
71 static constexpr std::string_view kind_id = "tm:thread_pool_stats";
72
73 [[nodiscard]] std::string to_string() const override {
74 std::ostringstream os;
75 os << this->value.name << "#" << this->value.pool_id;
76 return os.str();
77 }
78
79 [[maybe_unused]] static parcel::cell_t from_json(parcel::json_t const& j,
80 parcel::ParcelRegistry const&) {
81 return base_t::from_json_strict(j);
82 }
83
84 static parcel::cell_type_descriptor_t descriptor() {
85 static const auto d =
86 std::make_shared<parcel::SimpleCellTypeDescriptor<ThreadPoolStatsCell>>(
87 parcel::DisplayInfo{
88 .name = "ThreadPoolStats",
89 .description = "Headline counters and state of a ThreadPool.",
90 });
91 return d;
92 }
93};
94
95class TaskSnapshotCell : public parcel::BaseCell<TaskSnapshotCell, TaskSnapshot> {
96 using base_t = parcel::BaseCell<TaskSnapshotCell, TaskSnapshot>;
97
98public:
99 using base_t::base_t;
100 using base_t::operator=;
101 static constexpr std::string_view kind_id = "tm:task_snapshot";
102
103 [[nodiscard]] std::string to_string() const override {
104 std::ostringstream os;
105 os << "task#" << this->value.id;
106 return os.str();
107 }
108
109 [[maybe_unused]] static parcel::cell_t from_json(parcel::json_t const& j,
110 parcel::ParcelRegistry const&) {
111 return base_t::from_json_strict(j);
112 }
113
114 static parcel::cell_type_descriptor_t descriptor() {
115 static const auto d = std::make_shared<parcel::SimpleCellTypeDescriptor<TaskSnapshotCell>>(
116 parcel::DisplayInfo{
117 .name = "TaskSnapshot",
118 .description = "Snapshot of a single submitted task.",
119 });
120 return d;
121 }
122};
123
124class FutureSnapshotCell : public parcel::BaseCell<FutureSnapshotCell, FutureSnapshot> {
125 using base_t = parcel::BaseCell<FutureSnapshotCell, FutureSnapshot>;
126
127public:
128 using base_t::base_t;
129 using base_t::operator=;
130 static constexpr std::string_view kind_id = "tm:future_snapshot";
131
132 [[nodiscard]] std::string to_string() const override {
133 return this->value.ready ? "future:ready" : "future:pending";
134 }
135
136 [[maybe_unused]] static parcel::cell_t from_json(parcel::json_t const& j,
137 parcel::ParcelRegistry const&) {
138 return base_t::from_json_strict(j);
139 }
140
141 static parcel::cell_type_descriptor_t descriptor() {
142 static const auto d =
143 std::make_shared<parcel::SimpleCellTypeDescriptor<FutureSnapshotCell>>(
144 parcel::DisplayInfo{
145 .name = "FutureSnapshot",
146 .description = "Snapshot of a Future/SharedFuture state.",
147 });
148 return d;
149 }
150};
151
152class ManagerSummaryCell : public parcel::BaseCell<ManagerSummaryCell, ManagerSummary> {
153 using base_t = parcel::BaseCell<ManagerSummaryCell, ManagerSummary>;
154
155public:
156 using base_t::base_t;
157 using base_t::operator=;
158 static constexpr std::string_view kind_id = "tm:manager_summary";
159
160 [[nodiscard]] std::string to_string() const override {
161 std::ostringstream os;
162 os << "summary{pools=" << this->value.total_pools
163 << ", workers=" << this->value.total_live_workers
164 << ", queued=" << this->value.total_queued << "}";
165 return os.str();
166 }
167
168 [[maybe_unused]] static parcel::cell_t from_json(parcel::json_t const& j,
169 parcel::ParcelRegistry const&) {
170 return base_t::from_json_strict(j);
171 }
172
173 static parcel::cell_type_descriptor_t descriptor() {
174 static const auto d =
175 std::make_shared<parcel::SimpleCellTypeDescriptor<ManagerSummaryCell>>(
176 parcel::DisplayInfo{
177 .name = "ManagerSummary",
178 .description = "Aggregate periodic snapshot of the ThreadManager world.",
179 });
180 return d;
181 }
182};
183
184inline void register_cells(parcel::ParcelRegistry& registry) {
185 registry.register_kind(ThreadSnapshotCell::descriptor());
186 registry.register_kind(ThreadPoolStatsCell::descriptor());
187 registry.register_kind(TaskSnapshotCell::descriptor());
188 registry.register_kind(FutureSnapshotCell::descriptor());
189 registry.register_kind(ManagerSummaryCell::descriptor());
190}
191
192} // namespace threadman
193
199
200#define THREADMAN_HAS_PARCEL 1
Definition parcel.hpp:124
static parcel::cell_type_descriptor_t descriptor()
Definition parcel.hpp:141
static parcel::cell_t from_json(parcel::json_t const &j, parcel::ParcelRegistry const &)
Definition parcel.hpp:136
static constexpr std::string_view kind_id
Definition parcel.hpp:130
std::string to_string() const override
Definition parcel.hpp:132
Definition parcel.hpp:152
static constexpr std::string_view kind_id
Definition parcel.hpp:158
std::string to_string() const override
Definition parcel.hpp:160
static parcel::cell_type_descriptor_t descriptor()
Definition parcel.hpp:173
static parcel::cell_t from_json(parcel::json_t const &j, parcel::ParcelRegistry const &)
Definition parcel.hpp:168
Definition parcel.hpp:95
std::string to_string() const override
Definition parcel.hpp:103
static parcel::cell_type_descriptor_t descriptor()
Definition parcel.hpp:114
static constexpr std::string_view kind_id
Definition parcel.hpp:101
static parcel::cell_t from_json(parcel::json_t const &j, parcel::ParcelRegistry const &)
Definition parcel.hpp:109
Definition parcel.hpp:65
static parcel::cell_type_descriptor_t descriptor()
Definition parcel.hpp:84
static constexpr std::string_view kind_id
Definition parcel.hpp:71
static parcel::cell_t from_json(parcel::json_t const &j, parcel::ParcelRegistry const &)
Definition parcel.hpp:79
std::string to_string() const override
Definition parcel.hpp:73
Definition parcel.hpp:35
static constexpr std::string_view kind_id
Definition parcel.hpp:41
std::string to_string() const override
Definition parcel.hpp:43
static parcel::cell_type_descriptor_t descriptor()
Definition parcel.hpp:54
static parcel::cell_t from_json(parcel::json_t const &j, parcel::ParcelRegistry const &)
Definition parcel.hpp:49
Central feature-gate header for ThreadMan's optional integrations and tunable defaults.
nlohmann/json serialization hooks for the snapshot value-types and their enums.
Definition exceptions.hpp:22
void register_cells(parcel::ParcelRegistry &registry)
Definition parcel.hpp:184
PARCEL_DEFAULT_CELL(threadman::ThreadSnapshotCell)
Plain value snapshots of the live state of the ThreadMan world — threads, pools, tasks,...