Vince's CSV Parser
Loading...
Searching...
No Matches
scheduler.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <exception>
8#include <functional>
9#include <utility>
10
11#include "../common.hpp"
12
13#ifdef CSV_HAS_CXX20
14#include <concepts>
15#endif
16
17#if CSV_ENABLE_THREADS
18#include <mutex>
19#include <thread>
20#endif
21
22namespace csv {
23 namespace internals {
31 public:
32 SynchronousCSVReadScheduler() noexcept = default;
33
34 explicit SynchronousCSVReadScheduler(bool enabled) noexcept {
36 }
37
38 void set_threading_enabled(bool enabled) noexcept {
40 }
41
42 void run(std::function<void()> task) {
43 task();
44 }
45
46 void run(
47 std::function<void()> task,
48 const std::function<void()>& before_async_run
49 ) {
51 this->run(std::move(task));
52 }
53
54 void join() noexcept {}
55
56 bool wait_if_active(
57 const std::function<bool()>& is_waitable,
58 const std::function<void()>& wait
59 ) {
60 (void)is_waitable;
61 (void)wait;
62 return false;
63 }
64
65 void clear_exception() noexcept {}
66
67 std::exception_ptr take_exception() noexcept {
68 return nullptr;
69 }
70
71 void adopt_exception(std::exception_ptr eptr) noexcept {
72 (void)eptr;
73 }
74
75 void rethrow_exception_if_any() {}
76 };
77
78#if CSV_ENABLE_THREADS
87 public:
89
92
94 this->join();
95 }
96
97 void run(
98 std::function<void()> task,
99 const std::function<void()>& before_async_run = std::function<void()>()
100 ) {
101 this->join();
102 this->clear_exception();
103
104 if (before_async_run) {
105 before_async_run();
106 }
107
108 this->worker_ = std::thread([this, task]() mutable {
109 this->run_now(std::move(task));
110 });
111 }
112
113 void join() noexcept {
114 if (this->worker_.joinable()) {
115 this->worker_.join();
116 }
117 }
118
119 bool wait_if_active(
120 const std::function<bool()>& is_waitable,
121 const std::function<void()>& wait
122 ) {
123 if (is_waitable()) {
124 wait();
125 return true;
126 }
127
128 return false;
129 }
130
131 void clear_exception() noexcept {
132 std::lock_guard<std::mutex> lock(this->exception_lock_);
133 this->exception_ = nullptr;
134 }
135
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;
140 return eptr;
141 }
142
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);
146 }
147
148 void rethrow_exception_if_any() {
149#if CSV_EXCEPTIONS_ENABLED
150 if (auto eptr = this->take_exception()) {
151 std::rethrow_exception(eptr);
152 }
153#endif
154 }
155
156 private:
157 std::thread worker_;
158 std::exception_ptr exception_ = nullptr;
159 std::mutex exception_lock_;
160
161 void run_now(std::function<void()> task) noexcept {
162 CSV_TRY {
163 task();
164 }
165 CSV_CATCH((...), {
166 this->adopt_exception(std::current_exception());
167 })
168 }
169 };
170
177 class CSVReadScheduler {
178 public:
179 explicit CSVReadScheduler(bool threading_enabled = true) noexcept {
180 this->select(threading_enabled);
181 }
182
183 CSVReadScheduler(const CSVReadScheduler&) = delete;
184 CSVReadScheduler& operator=(const CSVReadScheduler&) = delete;
185
186 ~CSVReadScheduler() {
187 this->join();
188 }
189
190 void set_threading_enabled(bool enabled) noexcept {
191 this->join();
192 auto eptr = this->take_exception();
193 this->select(enabled);
194 this->adopt_exception(std::move(eptr));
195 }
196
197 void run(
198 std::function<void()> task,
199 const std::function<void()>& before_async_run = std::function<void()>()
200 ) {
201 if (this->is_threaded()) {
202 this->threaded_.run(std::move(task), before_async_run);
203 }
204 else {
205 this->sync_.run(std::move(task));
206 }
207 }
208
209 bool wait_if_active(
210 const std::function<bool()>& is_waitable,
211 const std::function<void()>& wait
212 ) {
213 return this->is_threaded()
214 && this->threaded_.wait_if_active(is_waitable, wait);
215 }
216
217 void join() noexcept {
218 if (this->is_threaded()) {
219 this->threaded_.join();
220 }
221 }
222
223 void clear_exception() noexcept {
224 if (this->is_threaded()) {
225 this->threaded_.clear_exception();
226 }
227 else {
228 this->sync_.clear_exception();
229 }
230 }
231
232 std::exception_ptr take_exception() noexcept {
233 return this->is_threaded()
234 ? this->threaded_.take_exception()
235 : this->sync_.take_exception();
236 }
237
238 void adopt_exception(std::exception_ptr eptr) noexcept {
239 if (this->is_threaded()) {
240 this->threaded_.adopt_exception(std::move(eptr));
241 }
242 else {
243 this->sync_.adopt_exception(std::move(eptr));
244 }
245 }
246
247 void rethrow_exception_if_any() {
248#if CSV_EXCEPTIONS_ENABLED
249 if (auto eptr = this->take_exception()) {
250 std::rethrow_exception(eptr);
251 }
252#endif
253 }
254
255 private:
256 SynchronousCSVReadScheduler sync_;
257 ThreadedCSVReadScheduler threaded_;
258 void* scheduler_ = &sync_;
259
260 void select(bool threading_enabled) noexcept {
261 if (threading_enabled) {
262 this->scheduler_ = &this->threaded_;
263 }
264 else {
265 this->scheduler_ = &this->sync_;
266 }
267 }
268
269 bool is_threaded() const noexcept {
270 return this->scheduler_ == &this->threaded_;
271 }
272 };
273#else
274 using CSVReadScheduler = SynchronousCSVReadScheduler;
275#endif
276
277#ifdef CSV_HAS_CXX20
278 template<typename Scheduler>
280 Scheduler scheduler,
281 std::function<void()> task,
282 std::function<void()> before_async_run,
283 std::function<bool()> is_waitable,
284 std::exception_ptr eptr
285 ) {
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>;
294 };
295
296 template<typename Scheduler>
298 && requires(Scheduler scheduler, bool enabled) {
299 { scheduler.set_threading_enabled(enabled) } -> std::same_as<void>;
300 };
301
302 static_assert(
304 "SynchronousCSVReadScheduler must satisfy the concrete read scheduler contract."
305 );
306#if CSV_ENABLE_THREADS
307 static_assert(
309 "ThreadedCSVReadScheduler must satisfy the concrete read scheduler contract."
310 );
311#endif
312 static_assert(
314 "CSVReadScheduler must satisfy the reader-facing scheduler contract."
315 );
316#endif
317 }
318}
Borrow a row and error state for synchronous, single-pass field access.
Synchronous read scheduler used by no-thread builds and runtime opt-out.
Definition scheduler.hpp:30
The all encompassing namespace.