seams-core v2.14.3
libyodaLib, the C++ engine of d-SEAMS
Loading...
Searching...
No Matches
region_errors.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <exception>
5
6namespace seams {
7
8// An exception that leaves an OpenMP region calls std::terminate, even when
9// the region runs on one thread or its if clause is false. Run each
10// iteration's body through run(): the first exception is kept, iterations
11// that start after it return at once, and rethrow() raises it once the
12// region has closed. The skipped iterations leave their outputs unwritten,
13// so a caller that catches the rethrow must not read them. The handler
14// itself cannot throw: a lock there would be one more std::terminate.
16public:
17 template <class F> void run(F &&body) noexcept {
18 if (failed_.load(std::memory_order_relaxed)) {
19 return;
20 }
21 try {
22 body();
23 } catch (...) {
24 if (!failed_.exchange(true, std::memory_order_acq_rel)) {
25 first_ = std::current_exception();
26 }
27 }
28 }
29
30 bool failed() const noexcept { return failed_.load(std::memory_order_relaxed); }
31
32 // Call after the region, from the thread that opened it.
33 void rethrow() const {
34 if (first_) {
35 std::rethrow_exception(first_);
36 }
37 }
38
39private:
40 std::atomic<bool> failed_{false};
41 std::exception_ptr first_;
42};
43
44} // namespace seams
void run(F &&body) noexcept
bool failed() const noexcept