Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
epochguard.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2020 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#include <util/macros.h>
11
12#include <cassert>
13
36private:
37 uint64_t m_raw_epoch = 0;
38 bool m_guarded = false;
39
40public:
41 Epoch() = default;
42 Epoch(const Epoch &) = delete;
43 Epoch &operator=(const Epoch &) = delete;
44 Epoch(Epoch &&) = delete;
45 Epoch &operator=(Epoch &&) = delete;
46 ~Epoch() = default;
47
48 bool guarded() const { return m_guarded; }
49
50 class Marker {
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
67 private:
69
70 public:
72 : m_epoch(epoch) {
73 assert(!m_epoch.m_guarded);
74 ++m_epoch.m_raw_epoch;
75 m_epoch.m_guarded = true;
76 }
78 assert(m_epoch.m_guarded);
79 // ensure clear separation between epochs
80 ++m_epoch.m_raw_epoch;
81 m_epoch.m_guarded = false;
82 }
83 };
84
86 assert(m_guarded);
87 if (marker.m_marker < m_raw_epoch) {
88 // marker is from a previous epoch, so this is its first visit
89 marker.m_marker = m_raw_epoch;
90 return false;
91 } else {
92 return true;
93 }
94 }
95};
96
97#define WITH_FRESH_EPOCH(epoch) \
98 const Epoch::Guard UNIQUE_NAME(epoch_guard_)(epoch)
99
100#endif // BITCOIN_UTIL_EPOCHGUARD_H
Epoch & m_epoch
Definition epochguard.h:68
~Guard() UNLOCK_FUNCTION()
Definition epochguard.h:77
Guard(Epoch &epoch) EXCLUSIVE_LOCK_FUNCTION(epoch)
Definition epochguard.h:71
Marker()=default
Marker & operator=(Marker &&)=delete
Marker & operator=(const Marker &)=delete
Marker(const Marker &)=default
~Marker()=default
Marker(Marker &&)=delete
Epoch: RAII-style guard for using epoch-based graph traversal algorithms.
Definition epochguard.h:35
bool m_guarded
Definition epochguard.h:38
Epoch(const Epoch &)=delete
~Epoch()=default
Epoch & operator=(Epoch &&)=delete
bool guarded() const
Definition epochguard.h:48
uint64_t m_raw_epoch
Definition epochguard.h:37
bool visited(Marker &marker) const EXCLUSIVE_LOCKS_REQUIRED(*this)
Definition epochguard.h:85
Epoch & operator=(const Epoch &)=delete
Epoch(Epoch &&)=delete
Epoch()=default
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define EXCLUSIVE_LOCK_FUNCTION(...)
#define SCOPED_LOCKABLE
#define LOCKABLE
#define UNLOCK_FUNCTION(...)
assert(!tx.IsCoinBase())