Skip to content

Commit a06cbed

Browse files
authored
Add support for Windows thread pool and GCD queue (#339)
1 parent 2eeaf28 commit a06cbed

6 files changed

Lines changed: 201 additions & 54 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ option(
100100
ON
101101
)
102102

103+
option(
104+
BEMAN_EXECUTION_WITH_GCD_SUPPORT
105+
"Enable support for libdispatch (Grand Central Dispatch). Default: OFF. Values: { ON, OFF }."
106+
OFF
107+
)
108+
103109
add_subdirectory(src/beman/execution)
104110

105111
if(NOT BEMAN_USE_MODULES)

‎examples/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ foreach(EXAMPLE ${EXAMPLES})
5050
set(EXAMPLE_TARGET ${PROJECT_NAME}.${EXAMPLE})
5151
add_executable(${EXAMPLE_TARGET})
5252
target_sources(${EXAMPLE_TARGET} PRIVATE ${EXAMPLE}.cpp)
53+
target_compile_definitions(${EXAMPLE_TARGET} PRIVATE NOMINMAX)
5354
if(BEMAN_USE_MODULES)
5455
target_compile_definitions(${EXAMPLE_TARGET} PUBLIC BEMAN_HAS_MODULES)
5556
target_link_libraries(${EXAMPLE_TARGET} PRIVATE beman::execution)

‎include/beman/execution/detail/thread_pool_backend.hpp‎

Lines changed: 144 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
#include <cassert>
88
#include <beman/execution/detail/common.hpp>
9+
#if BEMAN_EXECUTION_WITH_GCD_SUPPORT && __has_include(<dispatch/dispatch.h>)
10+
#include <dispatch/dispatch.h>
11+
#endif
12+
#if defined(_WIN32) && __has_include(<windows.h>)
13+
#include <windows.h>
14+
#include <threadpoolapiset.h>
15+
#endif
916
#ifdef BEMAN_HAS_IMPORT_STD
1017
import std;
1118
#else
@@ -18,6 +25,7 @@ import std;
1825
#include <mutex>
1926
#include <new>
2027
#include <span>
28+
#include <system_error>
2129
#include <thread>
2230
#include <utility>
2331
#endif
@@ -48,7 +56,7 @@ class thread_pool_backend_base
4856

4957
auto operator=(task_base&&) -> task_base& = delete;
5058

51-
virtual auto exec() noexcept -> void = 0;
59+
virtual auto exec(int ec = 0) noexcept -> void = 0;
5260

5361
task_base* next;
5462
};
@@ -57,10 +65,14 @@ class thread_pool_backend_base
5765
explicit schedule_task(::beman::execution::parallel_scheduler_replacement::receiver_proxy& p) noexcept
5866
: proxy(p) {}
5967

60-
auto exec() noexcept -> void override {
68+
auto exec(int ec) noexcept -> void override {
6169
auto& proxy_ref = proxy;
6270
::std::destroy_at(this);
63-
proxy_ref.set_value();
71+
if (ec) [[unlikely]] {
72+
proxy_ref.set_error(::std::make_exception_ptr(::std::system_error(ec, ::std::system_category())));
73+
} else {
74+
proxy_ref.set_value();
75+
}
6476
}
6577

6678
::beman::execution::parallel_scheduler_replacement::receiver_proxy& proxy;
@@ -71,18 +83,32 @@ class thread_pool_backend_base
7183

7284
struct single_bulk_task : task_base {
7385
single_bulk_task(::beman::execution::parallel_scheduler_replacement::bulk_item_receiver_proxy& p,
74-
::std::size_t shape) noexcept
75-
: proxy(p), shape(shape) {}
76-
77-
auto exec() noexcept -> void override {
78-
proxy.execute(0uz, shape);
86+
::std::size_t shape,
87+
bool one_by_one) noexcept
88+
: proxy(p), shape(shape), one_by_one(one_by_one) {}
89+
90+
auto exec(int ec) noexcept -> void override {
91+
if (ec == 0) [[likely]] {
92+
if (one_by_one) {
93+
for (::std::size_t i = 0; i < shape; ++i) {
94+
proxy.execute(i, i + 1uz);
95+
}
96+
} else {
97+
proxy.execute(0uz, shape);
98+
}
99+
}
79100
auto& proxy_ref = proxy;
80101
::std::destroy_at(this);
81-
proxy_ref.set_value();
102+
if (ec) [[unlikely]] {
103+
proxy_ref.set_error(::std::make_exception_ptr(::std::system_error(ec, ::std::system_category())));
104+
} else {
105+
proxy_ref.set_value();
106+
}
82107
}
83108

84109
::beman::execution::parallel_scheduler_replacement::bulk_item_receiver_proxy& proxy;
85110
::std::size_t shape;
111+
bool one_by_one;
86112
};
87113

88114
// `single_bulk_task` is small enough to fit directly into the pre-allocated storage provided by
@@ -97,6 +123,7 @@ class thread_pool_backend_base
97123
batched_bulk_task* head;
98124
::std::size_t chunk_count;
99125
bool one_by_one;
126+
int ec = 0;
100127
::std::size_t ref_count;
101128
};
102129

@@ -106,25 +133,36 @@ class thread_pool_backend_base
106133
::std::size_t j) noexcept
107134
: cookie(cookie), proxy(proxy), i(i), j(j) {}
108135

109-
auto exec() noexcept -> void override {
110-
if (cookie->one_by_one) {
111-
for (::std::size_t k = i; k < j; ++k) {
112-
proxy.execute(k, k + 1uz);
136+
auto exec(int ec) noexcept -> void override {
137+
if (ec) [[unlikely]] {
138+
if (next == nullptr) {
139+
cookie->ec = ec;
113140
}
114141
} else {
115-
proxy.execute(i, j);
142+
if (cookie->one_by_one) {
143+
for (::std::size_t k = i; k < j; ++k) {
144+
proxy.execute(k, k + 1uz);
145+
}
146+
} else {
147+
proxy.execute(i, j);
148+
}
116149
}
117150

118151
::std::atomic_ref<::std::size_t> ref_count{cookie->ref_count};
119152
if (ref_count.fetch_sub(1uz, ::std::memory_order_acq_rel) == 1uz) {
120153
auto head = cookie->head;
121154
const auto chunk_count = cookie->chunk_count;
155+
auto error = ::std::error_code{cookie->ec, ::std::system_category()};
122156
auto& proxy_ref = proxy;
123157
::std::destroy_at(cookie);
124158
::std::destroy_n(head, chunk_count);
125159
::operator delete(
126160
head, chunk_count * sizeof(batched_bulk_task), ::std::align_val_t{alignof(batched_bulk_task)});
127-
proxy_ref.set_value();
161+
if (error) [[unlikely]] {
162+
proxy_ref.set_error(::std::make_exception_ptr(::std::system_error{error}));
163+
} else {
164+
proxy_ref.set_value();
165+
}
128166
}
129167
}
130168

@@ -152,16 +190,9 @@ class thread_pool_backend_base
152190

153191
auto operator=(thread_pool_backend_base&&) -> thread_pool_backend_base& = delete;
154192

155-
auto shutdown() noexcept -> void {
156-
::std::unique_lock guard{mtx};
157-
shutdown_requested = true;
158-
guard.unlock();
159-
cv.notify_all();
160-
}
161-
162193
auto schedule(::beman::execution::parallel_scheduler_replacement::receiver_proxy& proxy,
163194
::std::span<::std::byte> storage) noexcept -> void override {
164-
push_back(::std::construct_at(reinterpret_cast<schedule_task*>(storage.data()), proxy)); // nothrow!
195+
submit(::std::construct_at(reinterpret_cast<schedule_task*>(storage.data()), proxy)); // nothrow!
165196
}
166197

167198
auto schedule_bulk_chunked(::std::size_t shape,
@@ -178,10 +209,11 @@ class thread_pool_backend_base
178209

179210
protected:
180211
[[nodiscard]] static auto num_threads() noexcept -> ::std::size_t {
181-
static const ::std::size_t count = ::std::max(1u, ::std::thread::hardware_concurrency());
212+
static const ::std::size_t count = (::std::max)(1u, ::std::thread::hardware_concurrency());
182213
return count;
183214
}
184215

216+
private:
185217
auto schedule_bulk(::std::size_t shape,
186218
bool one_by_one,
187219
::beman::execution::parallel_scheduler_replacement::bulk_item_receiver_proxy& proxy,
@@ -191,11 +223,13 @@ class thread_pool_backend_base
191223
return;
192224
}
193225

194-
const ::std::size_t chunk_length = (shape + num_threads() - 1uz) / num_threads();
195-
const ::std::size_t chunk_count = (shape + chunk_length - 1uz) / chunk_length;
226+
const ::std::size_t threads = num_threads();
227+
const ::std::size_t chunk_length = shape / threads + (shape % threads != 0uz);
228+
const ::std::size_t chunk_count = shape / chunk_length + (shape % chunk_length != 0uz);
196229
try {
197230
if (chunk_count == 1uz) {
198-
push_back(::std::construct_at(reinterpret_cast<single_bulk_task*>(storage.data()), proxy, shape));
231+
submit(::std::construct_at(
232+
reinterpret_cast<single_bulk_task*>(storage.data()), proxy, shape, one_by_one));
199233
} else {
200234
auto head = static_cast<batched_bulk_task*>(::operator new(
201235
chunk_count * sizeof(batched_bulk_task), ::std::align_val_t{alignof(batched_bulk_task)}));
@@ -206,22 +240,51 @@ class thread_pool_backend_base
206240
batched_bulk_task* prev = nullptr;
207241
for (::std::size_t i = 0; i < chunk_count; ++i) {
208242
const ::std::size_t begin = i * chunk_length;
209-
const ::std::size_t end = ::std::min(begin + chunk_length, shape);
243+
const ::std::size_t end = begin + (::std::min)(chunk_length, shape - begin);
210244
auto task = ::std::construct_at(head + i, cookie, proxy, begin, end);
211245
if (prev) {
212246
prev->next = task;
213247
}
214248
prev = task;
215249
}
216250
// NOLINTEND(*-reinterpret-cast, *-pointer-arithmetic-on-polymorphic-object, *-ctr56-cpp)
217-
push_back(head, chunk_count);
251+
submit(head, chunk_count);
218252
}
219253
} catch (...) {
220254
proxy.set_error(::std::current_exception());
221255
}
222256
}
223257

224-
auto push_back(task_base* t, ::std::size_t n = 1uz) noexcept -> void {
258+
virtual auto submit(task_base* t, ::std::size_t n = 1uz) noexcept -> void = 0;
259+
};
260+
261+
struct simple_thread_pool_backend : ::beman::execution::detail::thread_pool_backend_base {
262+
explicit simple_thread_pool_backend(::std::in_place_t) : workers(::new ::std::thread[num_threads()]) {}
263+
264+
simple_thread_pool_backend() : simple_thread_pool_backend(::std::in_place) {
265+
for (auto& worker : ::std::span(workers.get(), num_threads())) {
266+
worker = ::std::thread(&simple_thread_pool_backend::run, this);
267+
}
268+
}
269+
270+
~simple_thread_pool_backend() override {
271+
shutdown();
272+
for (auto& worker : ::std::span(workers.get(), num_threads())) {
273+
if (worker.joinable()) {
274+
worker.join();
275+
}
276+
}
277+
}
278+
279+
private:
280+
auto shutdown() noexcept -> void {
281+
::std::unique_lock guard{mtx};
282+
shutdown_requested = true;
283+
guard.unlock();
284+
cv.notify_all();
285+
}
286+
287+
auto submit(task_base* t, ::std::size_t n) noexcept -> void override {
225288
::std::unique_lock guard{mtx};
226289
for (::std::size_t i = 0; i < n; ++i) {
227290
if (auto prev_back = ::std::exchange(back, t)) {
@@ -249,41 +312,68 @@ class thread_pool_backend_base
249312
return front ? ::std::exchange(front, front->next) : nullptr;
250313
}
251314

252-
protected:
253-
bool shutdown_requested = false;
254-
::std::mutex mtx;
255-
::std::condition_variable cv;
256-
task_base* front = nullptr;
257-
task_base* back = nullptr;
258-
};
259-
260-
struct thread_pool_backend : ::beman::execution::detail::thread_pool_backend_base {
261-
explicit thread_pool_backend(::std::in_place_t) : workers(::new ::std::thread[num_threads()]) {}
262-
263-
thread_pool_backend() : thread_pool_backend(::std::in_place) {
264-
for (auto& worker : ::std::span(workers.get(), num_threads())) {
265-
worker = ::std::thread(&thread_pool_backend::run, this);
315+
auto run() noexcept -> void {
316+
while (auto task = pop_front()) {
317+
task->exec();
266318
}
267319
}
268320

269-
~thread_pool_backend() override {
270-
shutdown();
271-
for (auto& worker : ::std::span(workers.get(), num_threads())) {
272-
if (worker.joinable()) {
273-
worker.join();
274-
}
321+
bool shutdown_requested = false;
322+
::std::mutex mtx;
323+
::std::condition_variable cv;
324+
task_base* front = nullptr;
325+
task_base* back = nullptr;
326+
::std::unique_ptr<::std::thread[]> workers;
327+
};
328+
329+
#if BEMAN_EXECUTION_WITH_GCD_SUPPORT && __has_include(<dispatch/dispatch.h>)
330+
struct gcd_thread_pool_backend : ::beman::execution::detail::thread_pool_backend_base {
331+
private:
332+
auto submit(task_base* t, ::std::size_t n) noexcept -> void override {
333+
auto queue = ::dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
334+
for (::std::size_t i = 0; i < n; ++i) {
335+
auto* next = t->next;
336+
::dispatch_async_f(queue, t, &run);
337+
t = next;
275338
}
339+
assert(t == nullptr);
276340
}
277341

342+
static auto run(void* ctx) noexcept -> void { static_cast<task_base*>(ctx)->exec(); }
343+
};
344+
#endif
345+
346+
#if defined(_WIN32) && __has_include(<windows.h>)
347+
struct win_thread_pool_backend : ::beman::execution::detail::thread_pool_backend_base {
278348
private:
279-
auto run() noexcept -> void {
280-
while (auto task = pop_front()) {
281-
task->exec();
349+
auto submit(task_base* t, ::std::size_t n) noexcept -> void override {
350+
int ec = 0;
351+
for (::std::size_t i = 0; i < n; ++i) {
352+
auto* next = t->next;
353+
if (ec) [[unlikely]] {
354+
t->exec(ec);
355+
} else if (!::TrySubmitThreadpoolCallback(&run, t, nullptr)) {
356+
ec = static_cast<int>(::GetLastError());
357+
t->exec(ec);
358+
}
359+
t = next;
282360
}
361+
assert(t == nullptr);
283362
}
284363

285-
::std::unique_ptr<::std::thread[]> workers;
364+
static auto CALLBACK run(::PTP_CALLBACK_INSTANCE, void* ctx) noexcept -> void {
365+
static_cast<task_base*>(ctx)->exec();
366+
}
286367
};
368+
#endif
369+
370+
#if BEMAN_EXECUTION_WITH_GCD_SUPPORT && __has_include(<dispatch/dispatch.h>)
371+
using thread_pool_backend = gcd_thread_pool_backend;
372+
#elif defined(_WIN32) && __has_include(<windows.h>)
373+
using thread_pool_backend = win_thread_pool_backend;
374+
#else
375+
using thread_pool_backend = simple_thread_pool_backend;
376+
#endif
287377

288378
} // namespace beman::execution::detail
289379

‎src/beman/execution/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ target_compile_definitions(
251251
${BEMAN_EXECUTION_TARGET_NAME}
252252
INTERFACE
253253
BEMAN_EXECUTION_WITH_DEFAULT_PARALLEL_SCHEDULER_BACKEND=$<BOOL:${BEMAN_EXECUTION_WITH_DEFAULT_PARALLEL_SCHEDULER_BACKEND}>
254+
BEMAN_EXECUTION_WITH_GCD_SUPPORT=$<BOOL:${BEMAN_EXECUTION_WITH_GCD_SUPPORT}>
254255
)
255256

256257
if(BEMAN_USE_MODULES)

‎tests/beman/execution/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ foreach(test ${execution_tests})
142142
else()
143143
target_link_libraries(${TEST_EXE} PRIVATE beman::execution_headers)
144144
endif()
145+
target_compile_definitions(${TEST_EXE} PRIVATE NOMINMAX)
145146
add_test(NAME ${TEST_EXE} COMMAND $<TARGET_FILE:${TEST_EXE}>)
146147
endforeach()
147148

0 commit comments

Comments
 (0)