d-SEAMS
Install
Output
Methods
Docs
Engine & CLI
Python
Rust
Julia
R
Lua / Fennel
Fortran
PLUMED
Libraries
linkcell
Wiki
Cite
GitHub
seams-core
v2.14.3
libyodaLib, the C++ engine of d-SEAMS
Toggle main menu visibility
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
6
namespace
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.
15
class
RegionErrors
{
16
public
:
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
39
private
:
40
std::atomic<bool> failed_{
false
};
41
std::exception_ptr first_;
42
};
43
44
}
// namespace seams
seams::RegionErrors
Definition
region_errors.hpp:15
seams::RegionErrors::rethrow
void rethrow() const
Definition
region_errors.hpp:33
seams::RegionErrors::run
void run(F &&body) noexcept
Definition
region_errors.hpp:17
seams::RegionErrors::failed
bool failed() const noexcept
Definition
region_errors.hpp:30
seams
Definition
cpu_features.hpp:30
src
include
internal
region_errors.hpp