Bitcoin Core  24.99.0
P2P Digital Currency
epochguard.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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 
35 {
36 private:
37  uint64_t m_raw_epoch = 0;
38  bool m_guarded = false;
39 
40 public:
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  {
52  private:
53  uint64_t m_marker = 0;
54 
55  // only allow modification via Epoch member functions
56  friend class Epoch;
57  Marker& operator=(const Marker&) = delete;
58 
59  public:
60  Marker() = default;
61  Marker(const Marker&) = default;
62  Marker(Marker&&) = delete;
63  Marker& operator=(Marker&&) = delete;
64  ~Marker() = default;
65  };
66 
68  {
69  private:
71 
72  public:
73  explicit Guard(Epoch& epoch) EXCLUSIVE_LOCK_FUNCTION(epoch) : m_epoch(epoch)
74  {
75  assert(!m_epoch.m_guarded);
76  ++m_epoch.m_raw_epoch;
77  m_epoch.m_guarded = true;
78  }
80  {
81  assert(m_epoch.m_guarded);
82  ++m_epoch.m_raw_epoch; // ensure clear separation between epochs
83  m_epoch.m_guarded = false;
84  }
85  };
86 
87  bool visited(Marker& marker) const EXCLUSIVE_LOCKS_REQUIRED(*this)
88  {
89  assert(m_guarded);
90  if (marker.m_marker < m_raw_epoch) {
91  // marker is from a previous epoch, so this is its first visit
92  marker.m_marker = m_raw_epoch;
93  return false;
94  } else {
95  return true;
96  }
97  }
98 };
99 
100 #define WITH_FRESH_EPOCH(epoch) const Epoch::Guard UNIQUE_NAME(epoch_guard_)(epoch)
101 
102 #endif // BITCOIN_UTIL_EPOCHGUARD_H
Epoch & m_epoch
Definition: epochguard.h:70
~Guard() UNLOCK_FUNCTION()
Definition: epochguard.h:79
Guard(Epoch &epoch) EXCLUSIVE_LOCK_FUNCTION(epoch)
Definition: epochguard.h:73
Marker()=default
Marker(const Marker &)=default
Marker & operator=(Marker &&)=delete
~Marker()=default
Marker(Marker &&)=delete
Marker & operator=(const 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=(const 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:87
Epoch(Epoch &&)=delete
Epoch()=default
Epoch & operator=(Epoch &&)=delete
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
#define EXCLUSIVE_LOCK_FUNCTION(...)
Definition: threadsafety.h:42
#define SCOPED_LOCKABLE
Definition: threadsafety.h:37
#define LOCKABLE
Definition: threadsafety.h:36
#define UNLOCK_FUNCTION(...)
Definition: threadsafety.h:46
assert(!tx.IsCoinBase())