Bitcoin Core
22.99.0
P2P Digital Currency
src
util
epochguard.h
Go to the documentation of this file.
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-2021 The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#ifndef BITCOIN_UTIL_EPOCHGUARD_H
7
#define BITCOIN_UTIL_EPOCHGUARD_H
8
9
#include <
threadsafety.h
>
10
11
#include <cassert>
12
33
class
LOCKABLE
Epoch
34
{
35
private
:
36
uint64_t m_raw_epoch = 0;
37
bool
m_guarded =
false
;
38
39
public
:
40
Epoch
() =
default
;
41
Epoch
(
const
Epoch
&) =
delete
;
42
Epoch
& operator=(
const
Epoch
&) =
delete
;
43
Epoch
(
Epoch
&&) =
delete
;
44
Epoch
& operator=(
Epoch
&&) =
delete
;
45
~
Epoch
() =
default
;
46
47
bool
guarded
()
const
{
return
m_guarded; }
48
49
class
Marker
50
{
51
private
:
52
uint64_t m_marker = 0;
53
54
// only allow modification via Epoch member functions
55
friend
class
Epoch
;
56
Marker
&
operator=
(
const
Marker
&) =
delete
;
57
58
public
:
59
Marker
() =
default
;
60
Marker
(
const
Marker
&) =
default
;
61
Marker
(
Marker
&&) =
delete
;
62
Marker
&
operator=
(
Marker
&&) =
delete
;
63
~
Marker
() =
default
;
64
};
65
66
class
SCOPED_LOCKABLE
Guard
67
{
68
private
:
69
Epoch
&
m_epoch
;
70
71
public
:
72
explicit
Guard
(
Epoch
& epoch)
EXCLUSIVE_LOCK_FUNCTION
(epoch) : m_epoch(epoch)
73
{
74
assert
(!m_epoch.
m_guarded
);
75
++m_epoch.
m_raw_epoch
;
76
m_epoch.
m_guarded
=
true
;
77
}
78
~Guard
()
UNLOCK_FUNCTION
()
79
{
80
assert
(m_epoch.
m_guarded
);
81
++m_epoch.
m_raw_epoch
;
// ensure clear separation between epochs
82
m_epoch.
m_guarded
=
false
;
83
}
84
};
85
86
bool
visited
(
Marker
& marker)
const
EXCLUSIVE_LOCKS_REQUIRED
(*
this
)
87
{
88
assert
(m_guarded);
89
if
(marker.m_marker < m_raw_epoch) {
90
// marker is from a previous epoch, so this is its first visit
91
marker.m_marker = m_raw_epoch;
92
return
false
;
93
}
else
{
94
return
true
;
95
}
96
}
97
};
98
99
#define WITH_FRESH_EPOCH(epoch) const Epoch::Guard PASTE2(epoch_guard_, __COUNTER__)(epoch)
100
101
#endif // BITCOIN_UTIL_EPOCHGUARD_H
assert
assert(!tx.IsCoinBase())
Epoch::Guard::m_epoch
Epoch & m_epoch
Definition:
epochguard.h:69
EXCLUSIVE_LOCK_FUNCTION
#define EXCLUSIVE_LOCK_FUNCTION(...)
Definition:
threadsafety.h:42
UNLOCK_FUNCTION
#define UNLOCK_FUNCTION(...)
Definition:
threadsafety.h:46
Epoch::m_guarded
bool m_guarded
Definition:
epochguard.h:37
Epoch::visited
bool visited(Marker &marker) const EXCLUSIVE_LOCKS_REQUIRED(*this)
Definition:
epochguard.h:86
Epoch::operator=
Epoch & operator=(const Epoch &)=delete
SCOPED_LOCKABLE
#define SCOPED_LOCKABLE
Definition:
threadsafety.h:37
threadsafety.h
Epoch::Marker
Definition:
epochguard.h:49
LOCKABLE
#define LOCKABLE
Definition:
threadsafety.h:36
Epoch::m_raw_epoch
uint64_t m_raw_epoch
Definition:
epochguard.h:36
Epoch::Guard
Definition:
epochguard.h:66
Epoch::guarded
bool guarded() const
Definition:
epochguard.h:47
Epoch::Guard::~Guard
~Guard() UNLOCK_FUNCTION()
Definition:
epochguard.h:78
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition:
threadsafety.h:49
Epoch::Guard::Guard
Guard(Epoch &epoch) EXCLUSIVE_LOCK_FUNCTION(epoch)
Definition:
epochguard.h:72
Epoch
Epoch: RAII-style guard for using epoch-based graph traversal algorithms.
Definition:
epochguard.h:33
Generated on Fri Feb 18 2022 20:03:52 for Bitcoin Core by
1.8.17