Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
cetlib
assert_only_one_thread.h
Go to the documentation of this file.
1
#ifndef cetlib_assert_only_one_thread_h
2
#define cetlib_assert_only_one_thread_h
3
4
// ===================================================================
5
// CET_ASSERT_ONLY_ONE_THREAD()
6
//
7
// This macro is a utility that can be called wherever it is expected
8
// for only one thread to be accessing a particular block of code.
9
//
10
// It has similar semantics to the standard 'assert' macro:
11
//
12
// - It is disabled if NDEBUG is defined
13
// - If more than one thread is accessing that block of code at one
14
// time, std::abort is called.
15
// - It is encouraged to be used whenever single-threaded execution
16
// of a code block is a pre-condition.
17
//
18
// If the std::abort() is called, file, line-number, and function-name
19
// information will be provided.
20
// ===================================================================
21
22
#include <atomic>
23
#include <iostream>
24
#include <mutex>
25
26
namespace
cet
{
27
namespace
detail {
28
29
class
ThreadCounter
{
30
public
:
31
explicit
ThreadCounter
(
char
const
*
filename
,
32
unsigned
const
linenum,
33
char
const
* funcname)
34
:
filename_
{filename},
linenum_
{linenum},
funcname_
{funcname}
35
{}
36
37
class
Sentry
;
// Only the sentry can access the members
38
39
private
:
40
std::atomic<unsigned>
counter_
{0
u
};
41
std::string
const
filename_
;
42
unsigned
const
linenum_
;
43
std::string
const
funcname_
;
44
};
45
46
class
ThreadCounter::Sentry
{
47
public
:
48
Sentry
(
ThreadCounter
& tc,
bool
const
terminate =
true
)
49
: tc_{tc}, terminate_{terminate}
50
{
51
if
(++tc_.counter_ != 1
u
) {
52
// Do not guard the abort!
53
{
54
std::lock_guard<decltype(m_)> hold{m_};
55
std::cerr
56
<<
"Failed assert--more than one thread accessing location:\n"
57
<<
" "
<< tc_.filename_ <<
':'
<< tc_.linenum_ <<
'\n'
58
<<
" function: "
<< tc_.funcname_ <<
'\n'
;
59
}
60
if
(terminate_) {
61
std::abort();
62
}
63
}
64
}
65
66
~Sentry
() noexcept { --tc_.counter_; }
67
68
private
:
69
ThreadCounter
&
tc_
;
70
bool
const
terminate_
;
71
std::mutex
m_{};
72
};
73
}
74
}
75
76
#define CONCATENATE_HIDDEN(a, b) a##b
77
#define CONCATENATE(a, b) CONCATENATE_HIDDEN(a, b)
78
79
#ifndef NDEBUG
80
#define CET_ASSERT_ONLY_ONE_THREAD() \
81
static cet::detail::ThreadCounter CONCATENATE(s, __LINE__){ \
82
__FILE__, __LINE__, __func__}; \
83
cet::detail::ThreadCounter::Sentry CONCATENATE(hold, __LINE__) \
84
{ \
85
CONCATENATE(s, __LINE__) \
86
}
87
#else
88
#define CET_ASSERT_ONLY_ONE_THREAD() ((void)0)
89
#endif
90
91
#endif
/* cetlib_assert_only_one_thread_h */
92
93
// Local variables:
94
// mode: c++
95
// End:
cet::detail::ThreadCounter::Sentry::Sentry
Sentry(ThreadCounter &tc, bool const terminate=true)
Definition:
assert_only_one_thread.h:48
cet::detail::ThreadCounter::Sentry::terminate_
bool const terminate_
Definition:
assert_only_one_thread.h:70
cet::detail::ThreadCounter::Sentry::tc_
ThreadCounter & tc_
Definition:
assert_only_one_thread.h:69
cet::detail::ThreadCounter::Sentry::~Sentry
~Sentry() noexcept
Definition:
assert_only_one_thread.h:66
cet::detail::ThreadCounter::funcname_
std::string const funcname_
Definition:
assert_only_one_thread.h:43
om::cerr
OStream cerr
Definition:
OStream.cxx:7
shutoffs.filename
string filename
Definition:
shutoffs.py:106
cet
Definition:
get_LibraryInfoCollection.h:10
submit_hadd.u
u
Definition:
submit_hadd.py:84
generate_CCQE_events.mutex
mutex
Definition:
generate_CCQE_events.py:53
cet::detail::ThreadCounter::filename_
std::string const filename_
Definition:
assert_only_one_thread.h:41
cet::detail::ThreadCounter::linenum_
unsigned const linenum_
Definition:
assert_only_one_thread.h:42
cet::detail::ThreadCounter::Sentry
Definition:
assert_only_one_thread.h:46
cet::detail::ThreadCounter::ThreadCounter
ThreadCounter(char const *filename, unsigned const linenum, char const *funcname)
Definition:
assert_only_one_thread.h:31
cet::detail::ThreadCounter::counter_
std::atomic< unsigned > counter_
Definition:
assert_only_one_thread.h:40
cet::detail::ThreadCounter
Definition:
assert_only_one_thread.h:29
string
enum BeamMode string
Generated on Sun Feb 28 2021 14:14:04 for NOvA by
1.8.11