-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_pool.cppm
More file actions
300 lines (239 loc) · 8.68 KB
/
Copy paththread_pool.cppm
File metadata and controls
300 lines (239 loc) · 8.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
export module mcpplibs.cmp:thread_pool;
import std;
import :cancellation;
namespace mcpplibs::cmp::detail {
enum class ThreadPoolOutcome {
pending,
completed,
cancelled
};
struct ThreadPoolCancellation final {
std::atomic<ThreadPoolOutcome> outcome_ { ThreadPoolOutcome::pending };
};
struct ThreadPoolCancelCallback final {
ThreadPoolCancellation* cancellation_ {};
void operator()() const noexcept {
auto expected = ThreadPoolOutcome::pending;
cancellation_->outcome_.compare_exchange_strong(
expected,
ThreadPoolOutcome::cancelled,
std::memory_order_acq_rel,
std::memory_order_acquire);
}
};
struct ThreadPoolEntry final {
std::coroutine_handle<> continuation_ {};
ThreadPoolCancellation* cancellation_ {};
};
class ThreadPoolState final {
private:
std::mutex mutex_ {};
std::condition_variable condition_ {};
std::deque<ThreadPoolEntry> ready_ {};
bool accepting_ { true };
public:
void enqueue(
std::coroutine_handle<> continuation,
ThreadPoolCancellation* cancellation = nullptr) {
if (!continuation) {
throw std::invalid_argument { "cannot schedule an empty coroutine" };
}
{
const std::lock_guard lock { mutex_ };
if (!accepting_) {
throw std::logic_error { "thread pool is stopping" };
}
ready_.push_back(ThreadPoolEntry {
continuation,
cancellation
});
}
condition_.notify_one();
}
void run_worker() noexcept {
while (true) {
ThreadPoolEntry entry {};
{
std::unique_lock lock { mutex_ };
condition_.wait(lock, [&] {
return !ready_.empty() || !accepting_;
});
if (ready_.empty()) {
return;
}
entry = ready_.front();
ready_.pop_front();
if (entry.cancellation_) {
auto expected = ThreadPoolOutcome::pending;
entry.cancellation_->outcome_.compare_exchange_strong(
expected,
ThreadPoolOutcome::completed,
std::memory_order_acq_rel,
std::memory_order_acquire);
}
}
if (!entry.continuation_ || entry.continuation_.done()) {
std::terminate();
}
// 锁外恢复,协程再次调度到当前池时不会自锁
entry.continuation_.resume();
}
}
void close() noexcept {
{
const std::lock_guard lock { mutex_ };
accepting_ = false;
}
condition_.notify_all();
}
};
} // namespace mcpplibs::cmp::detail
export namespace mcpplibs::cmp {
class ThreadPool final {
public:
class Scheduler final {
private:
[[nodiscard]] static std::shared_ptr<detail::ThreadPoolState> lock_state_(
const std::weak_ptr<detail::ThreadPoolState>& state) {
const auto lockedState = state.lock();
if (!lockedState) {
throw std::logic_error {
"scheduler's thread pool no longer exists"
};
}
return lockedState;
}
class ScheduleAwaiter final {
private:
std::weak_ptr<detail::ThreadPoolState> state_ {};
public:
explicit ScheduleAwaiter(
std::weak_ptr<detail::ThreadPoolState> state) noexcept
: state_ { std::move(state) } {}
[[nodiscard]] constexpr bool await_ready() const noexcept {
return false;
}
void await_suspend(std::coroutine_handle<> continuation) {
const auto state = Scheduler::lock_state_(state_);
state->enqueue(continuation);
}
constexpr void await_resume() const noexcept {}
};
class CancellableScheduleAwaiter final {
private:
using StopCallback =
std::stop_callback<detail::ThreadPoolCancelCallback>;
std::weak_ptr<detail::ThreadPoolState> state_ {};
std::stop_token stopToken_ {};
detail::ThreadPoolCancellation cancellation_ {};
// 最先析构,阻止回调继续访问 awaiter 内状态
std::optional<StopCallback> stopCallback_ {};
public:
CancellableScheduleAwaiter(
std::weak_ptr<detail::ThreadPoolState> state,
std::stop_token stopToken) noexcept
: state_ { std::move(state) },
stopToken_ { std::move(stopToken) } {}
[[nodiscard]] constexpr bool await_ready() const noexcept {
return false;
}
void await_suspend(std::coroutine_handle<> continuation) {
const auto state = Scheduler::lock_state_(state_);
stopCallback_.emplace(
stopToken_,
detail::ThreadPoolCancelCallback { &cancellation_ });
// 发布后 awaiter 可能立即销毁,此后不得再读取成员
state->enqueue(continuation, &cancellation_);
}
void await_resume() {
stopCallback_.reset();
if (cancellation_.outcome_.load(
std::memory_order_acquire) ==
detail::ThreadPoolOutcome::cancelled) {
throw OperationCancelled {};
}
}
};
std::weak_ptr<detail::ThreadPoolState> state_ {};
explicit Scheduler(
const std::shared_ptr<detail::ThreadPoolState>& state) noexcept
: state_ { state } {}
friend class ThreadPool;
public:
Scheduler() = delete;
Scheduler(const Scheduler&) = default;
Scheduler& operator=(const Scheduler&) = default;
Scheduler(Scheduler&&) noexcept = default;
Scheduler& operator=(Scheduler&&) noexcept = default;
~Scheduler() = default;
[[nodiscard]] auto schedule() const noexcept {
return ScheduleAwaiter { state_ };
}
[[nodiscard]] auto schedule(
std::stop_token stopToken) const noexcept {
return CancellableScheduleAwaiter {
state_,
std::move(stopToken)
};
}
friend bool operator==(
const Scheduler& left,
const Scheduler& right) noexcept {
return !left.state_.owner_before(right.state_) &&
!right.state_.owner_before(left.state_);
}
};
private:
std::shared_ptr<detail::ThreadPoolState> state_ {
std::make_shared<detail::ThreadPoolState>()
};
// state_ 必须晚于 worker 析构,确保线程退出前共享状态仍然存在
std::vector<std::jthread> workers_ {};
[[nodiscard]] static std::size_t default_worker_count_() noexcept {
const auto count = std::thread::hardware_concurrency();
return count == 0 ? 1 : static_cast<std::size_t>(count);
}
public:
ThreadPool()
: ThreadPool { default_worker_count_() } {}
explicit ThreadPool(std::size_t workerCount) {
if (workerCount == 0) {
throw std::invalid_argument {
"thread pool requires at least one worker"
};
}
workers_.reserve(workerCount);
try {
for (std::size_t index { 0 }; index < workerCount; ++index) {
workers_.emplace_back([state = state_] {
state->run_worker();
});
}
} catch (...) {
// 先关闭并唤醒,随后 jthread 的成员析构会安全 join
state_->close();
throw;
}
}
ThreadPool(const ThreadPool&) = delete;
ThreadPool& operator=(const ThreadPool&) = delete;
ThreadPool(ThreadPool&&) = delete;
ThreadPool& operator=(ThreadPool&&) = delete;
~ThreadPool() {
const auto currentThread = std::this_thread::get_id();
if (std::ranges::any_of(workers_, [&](const std::jthread& worker) {
return worker.get_id() == currentThread;
})) {
std::terminate();
}
// jthread 的 stop token 不参与关闭;worker 会先排空已接纳任务
state_->close();
}
[[nodiscard]] std::size_t thread_count() const noexcept {
return workers_.size();
}
[[nodiscard]] Scheduler get_scheduler() const noexcept {
return Scheduler { state_ };
}
};
} // namespace mcpplibs::cmp