Bitcoin ABC  0.26.3
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 
11 #include <cassert>
12 
34 class LOCKABLE Epoch {
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  private:
51  uint64_t m_marker = 0;
52 
53  // only allow modification via Epoch member functions
54  friend class Epoch;
55  Marker &operator=(const Marker &) = delete;
56 
57  public:
58  Marker() = default;
59  Marker(const Marker &) = default;
60  Marker(Marker &&) = delete;
61  Marker &operator=(Marker &&) = delete;
62  ~Marker() = default;
63  };
64 
66  private:
68 
69  public:
70  explicit Guard(Epoch &epoch) EXCLUSIVE_LOCK_FUNCTION(epoch)
71  : m_epoch(epoch) {
72  assert(!m_epoch.m_guarded);
73  ++m_epoch.m_raw_epoch;
74  m_epoch.m_guarded = true;
75  }
77  assert(m_epoch.m_guarded);
78  // ensure clear separation between epochs
79  ++m_epoch.m_raw_epoch;
80  m_epoch.m_guarded = false;
81  }
82  };
83 
84  bool visited(Marker &marker) const EXCLUSIVE_LOCKS_REQUIRED(*this) {
85  assert(m_guarded);
86  if (marker.m_marker < m_raw_epoch) {
87  // marker is from a previous epoch, so this is its first visit
88  marker.m_marker = m_raw_epoch;
89  return false;
90  } else {
91  return true;
92  }
93  }
94 };
95 
96 #define WITH_FRESH_EPOCH(epoch) \
97  const Epoch::Guard PASTE2(epoch_guard_, __COUNTER__)(epoch)
98 
99 #endif // BITCOIN_UTIL_EPOCHGUARD_H
Epoch & m_epoch
Definition: epochguard.h:67
~Guard() UNLOCK_FUNCTION()
Definition: epochguard.h:76
Guard(Epoch &epoch) EXCLUSIVE_LOCK_FUNCTION(epoch)
Definition: epochguard.h:70
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:34
bool m_guarded
Definition: epochguard.h:37
Epoch(const Epoch &)=delete
~Epoch()=default
Epoch & operator=(const Epoch &)=delete
bool guarded() const
Definition: epochguard.h:47
uint64_t m_raw_epoch
Definition: epochguard.h:36
bool visited(Marker &marker) const EXCLUSIVE_LOCKS_REQUIRED(*this)
Definition: epochguard.h:84
Epoch(Epoch &&)=delete
Epoch()=default
Epoch & operator=(Epoch &&)=delete
#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())