Bitcoin ABC 0.30.7
P2P Digital Currency
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:
71 explicit Guard(Epoch &epoch) EXCLUSIVE_LOCK_FUNCTION(epoch)
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
85 bool visited(Marker &marker) const EXCLUSIVE_LOCKS_REQUIRED(*this) {
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
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
#define EXCLUSIVE_LOCK_FUNCTION(...)
Definition: threadsafety.h:49
#define SCOPED_LOCKABLE
Definition: threadsafety.h:44
#define LOCKABLE
Definition: threadsafety.h:43
#define UNLOCK_FUNCTION(...)
Definition: threadsafety.h:53
assert(!tx.IsCoinBase())