11#include "../common.hpp"
38 void set_threading_enabled(
bool enabled)
noexcept {
42 void run(std::function<
void()>
task) {
47 std::function<
void()>
task,
51 this->run(std::move(
task));
54 void join()
noexcept {}
57 const std::function<
bool()>& is_waitable,
58 const std::function<
void()>& wait
65 void clear_exception()
noexcept {}
67 std::exception_ptr take_exception()
noexcept {
71 void adopt_exception(std::exception_ptr
eptr)
noexcept {
75 void rethrow_exception_if_any() {}
98 std::function<
void()> task,
99 const std::function<
void()>& before_async_run = std::function<
void()>()
102 this->clear_exception();
104 if (before_async_run) {
108 this->worker_ = std::thread([
this, task]()
mutable {
109 this->run_now(std::move(task));
113 void join() noexcept {
114 if (this->worker_.joinable()) {
115 this->worker_.join();
120 const std::function<
bool()>& is_waitable,
121 const std::function<
void()>& wait
131 void clear_exception() noexcept {
132 std::lock_guard<std::mutex> lock(this->exception_lock_);
133 this->exception_ =
nullptr;
136 std::exception_ptr take_exception() noexcept {
137 std::lock_guard<std::mutex> lock(this->exception_lock_);
138 auto eptr = this->exception_;
139 this->exception_ =
nullptr;
143 void adopt_exception(std::exception_ptr eptr)
noexcept {
144 std::lock_guard<std::mutex> lock(this->exception_lock_);
145 this->exception_ = std::move(eptr);
148 void rethrow_exception_if_any() {
149#if CSV_EXCEPTIONS_ENABLED
150 if (
auto eptr = this->take_exception()) {
151 std::rethrow_exception(eptr);
158 std::exception_ptr exception_ =
nullptr;
159 std::mutex exception_lock_;
161 void run_now(std::function<
void()> task)
noexcept {
166 this->adopt_exception(std::current_exception());
177 class CSVReadScheduler {
179 explicit CSVReadScheduler(
bool threading_enabled =
true) noexcept {
180 this->select(threading_enabled);
183 CSVReadScheduler(
const CSVReadScheduler&) =
delete;
184 CSVReadScheduler& operator=(
const CSVReadScheduler&) =
delete;
186 ~CSVReadScheduler() {
190 void set_threading_enabled(
bool enabled)
noexcept {
192 auto eptr = this->take_exception();
193 this->select(enabled);
194 this->adopt_exception(std::move(eptr));
198 std::function<
void()> task,
199 const std::function<
void()>& before_async_run = std::function<
void()>()
201 if (this->is_threaded()) {
202 this->threaded_.run(std::move(task), before_async_run);
205 this->sync_.run(std::move(task));
210 const std::function<
bool()>& is_waitable,
211 const std::function<
void()>& wait
213 return this->is_threaded()
214 && this->threaded_.wait_if_active(is_waitable, wait);
217 void join() noexcept {
218 if (this->is_threaded()) {
219 this->threaded_.join();
223 void clear_exception() noexcept {
224 if (this->is_threaded()) {
225 this->threaded_.clear_exception();
228 this->sync_.clear_exception();
232 std::exception_ptr take_exception() noexcept {
233 return this->is_threaded()
234 ? this->threaded_.take_exception()
235 : this->sync_.take_exception();
238 void adopt_exception(std::exception_ptr eptr)
noexcept {
239 if (this->is_threaded()) {
240 this->threaded_.adopt_exception(std::move(eptr));
243 this->sync_.adopt_exception(std::move(eptr));
247 void rethrow_exception_if_any() {
248#if CSV_EXCEPTIONS_ENABLED
249 if (
auto eptr = this->take_exception()) {
250 std::rethrow_exception(eptr);
256 SynchronousCSVReadScheduler sync_;
257 ThreadedCSVReadScheduler threaded_;
258 void* scheduler_ = &sync_;
260 void select(
bool threading_enabled)
noexcept {
261 if (threading_enabled) {
262 this->scheduler_ = &this->threaded_;
265 this->scheduler_ = &this->sync_;
269 bool is_threaded() const noexcept {
270 return this->scheduler_ == &this->threaded_;
274 using CSVReadScheduler = SynchronousCSVReadScheduler;
278 template<
typename Scheduler>
281 std::function<void()> task,
282 std::function<void()> before_async_run,
283 std::function<bool()> is_waitable,
284 std::exception_ptr eptr
286 { scheduler.run(task) } -> std::same_as<void>;
287 { scheduler.run(task, before_async_run) } -> std::same_as<void>;
288 { scheduler.wait_if_active(is_waitable, before_async_run) } -> std::same_as<bool>;
289 { scheduler.join() } -> std::same_as<void>;
290 { scheduler.clear_exception() } -> std::same_as<void>;
291 { scheduler.take_exception() } -> std::same_as<std::exception_ptr>;
292 { scheduler.adopt_exception(eptr) } -> std::same_as<void>;
293 { scheduler.rethrow_exception_if_any() } -> std::same_as<void>;
296 template<
typename Scheduler>
298 &&
requires(Scheduler scheduler,
bool enabled) {
299 { scheduler.set_threading_enabled(enabled) } -> std::same_as<void>;
304 "SynchronousCSVReadScheduler must satisfy the concrete read scheduler contract."
306#if CSV_ENABLE_THREADS
309 "ThreadedCSVReadScheduler must satisfy the concrete read scheduler contract."
314 "CSVReadScheduler must satisfy the reader-facing scheduler contract."
Borrow a row and error state for synchronous, single-pass field access.
Synchronous read scheduler used by no-thread builds and runtime opt-out.
The all encompassing namespace.