forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntimeError.cxx
More file actions
105 lines (93 loc) · 2.68 KB
/
RuntimeError.cxx
File metadata and controls
105 lines (93 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "Framework/RuntimeError.h"
#include <cstdio>
#include <climits>
#include <atomic>
#include <cstdarg>
#include <cstring>
#include <unistd.h>
#include <cstdlib>
#include <cxxabi.h>
#include <execinfo.h>
#include <stdexcept>
namespace o2::framework
{
namespace
{
static RuntimeError gError[RuntimeError::MAX_RUNTIME_ERRORS];
static std::atomic<bool> gErrorBooking[RuntimeError::MAX_RUNTIME_ERRORS];
bool canDumpBacktrace()
{
#ifdef DPL_ENABLE_BACKTRACE
return true;
#else
return false;
#endif
}
} // namespace
void clean_all_runtime_errors()
{
for (auto& i : gErrorBooking) {
i.store(false);
}
}
void clean_runtime_error(int i)
{
gErrorBooking[i].store(false);
}
RuntimeError& error_from_ref(RuntimeErrorRef ref)
{
return gError[ref.index];
}
RuntimeErrorRef runtime_error_f(const char* format, ...)
{
int i = 0;
while (true) {
bool expected = false;
if (gErrorBooking[i].compare_exchange_strong(expected, true) == true) {
break;
}
++i;
if (i >= RuntimeError::MAX_RUNTIME_ERRORS) {
throw std::runtime_error("Too many o2::framework::runtime_error thrown without proper cleanup.");
}
}
va_list args;
va_start(args, format);
vsnprintf(gError[i].what, RuntimeError::MAX_RUNTIME_ERROR_SIZE, format, args);
va_end(args);
gError[i].maxBacktrace = canDumpBacktrace() ? backtrace(gError[i].backtrace, BacktraceHelpers::MAX_BACKTRACE_SIZE) : 0;
return RuntimeErrorRef{i};
}
RuntimeErrorRef runtime_error(const char* s)
{
int i = 0;
while (true) {
bool expected = false;
if (gErrorBooking[i].compare_exchange_strong(expected, true) == true) {
break;
}
++i;
if (i >= RuntimeError::MAX_RUNTIME_ERRORS) {
throw std::runtime_error("Too many o2::framework::runtime_error thrown without proper cleanup.");
}
}
strncpy(gError[i].what, s, RuntimeError::MAX_RUNTIME_ERROR_SIZE - 1);
gError[i].what[RuntimeError::MAX_RUNTIME_ERROR_SIZE - 1] = 0;
gError[i].maxBacktrace = canDumpBacktrace() ? backtrace(gError[i].backtrace, BacktraceHelpers::MAX_BACKTRACE_SIZE) : 0;
return RuntimeErrorRef{i};
}
void throw_error(RuntimeErrorRef ref)
{
throw ref;
}
} // namespace o2::framework