xrpld
Loading...
Searching...
No Matches
PendingSaves.h
1#pragma once
2
3#include <xrpl/protocol/Protocol.h>
4
5#include <condition_variable>
6#include <map>
7#include <mutex>
8
9namespace xrpl {
10
19{
20private:
24
25public:
33 bool
35 {
36 std::scoped_lock const lock(mutex_);
37
38 auto it = map_.find(seq);
39
40 if ((it == map_.end()) || it->second)
41 {
42 // Work done or another thread is doing it
43 return false;
44 }
45
46 it->second = true;
47 return true;
48 }
49
57 void
59 {
60 std::scoped_lock const lock(mutex_);
61
62 map_.erase(seq);
63 await_.notify_all();
64 }
65
69 bool
71 {
72 std::scoped_lock const lock(mutex_);
73 return map_.contains(seq);
74 }
75
85 bool
86 shouldWork(LedgerIndex seq, bool isSynchronous)
87 {
89 do
90 {
91 auto it = map_.find(seq);
92
93 if (it == map_.end())
94 {
95 map_.emplace(seq, false);
96 return true;
97 }
98
99 if (!isSynchronous)
100 {
101 // Already dispatched
102 return false;
103 }
104
105 if (!it->second)
106 {
107 // Scheduled, but not dispatched
108 return true;
109 }
110
111 // Already in progress, just need to wait
112 await_.wait(lock);
113
114 } while (true);
115 }
116
126 {
127 std::scoped_lock const lock(mutex_);
128
129 return map_;
130 }
131};
132
133} // namespace xrpl
Keeps track of which ledgers haven't been fully saved.
std::condition_variable await_
std::map< LedgerIndex, bool > getSnapshot() const
Get a snapshot of the pending saves.
void finishWork(LedgerIndex seq)
Finish working on a ledger.
bool shouldWork(LedgerIndex seq, bool isSynchronous)
Check if a ledger should be dispatched.
bool startWork(LedgerIndex seq)
Start working on a ledger.
bool pending(LedgerIndex seq)
Return true if a ledger is in the progress of being saved.
std::map< LedgerIndex, bool > map_
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::uint32_t LedgerIndex
A ledger index.
Definition Protocol.h:370