xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
ledger
PendingSaves.h
1
#pragma once
2
3
#include <xrpl/protocol/Protocol.h>
4
5
#include <
condition_variable
>
6
#include <
map
>
7
#include <
mutex
>
8
9
namespace
xrpl
{
10
18
class
PendingSaves
19
{
20
private
:
21
std::mutex
mutable
mutex_
;
22
std::map<LedgerIndex, bool>
map_
;
23
std::condition_variable
await_
;
24
25
public
:
33
bool
34
startWork
(
LedgerIndex
seq)
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
58
finishWork
(
LedgerIndex
seq)
59
{
60
std::scoped_lock
const
lock(
mutex_
);
61
62
map_
.erase(seq);
63
await_
.notify_all();
64
}
65
69
bool
70
pending
(
LedgerIndex
seq)
71
{
72
std::scoped_lock
const
lock(
mutex_
);
73
return
map_
.contains(seq);
74
}
75
85
bool
86
shouldWork
(
LedgerIndex
seq,
bool
isSynchronous)
87
{
88
std::unique_lock<std::mutex>
lock(
mutex_
);
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
124
std::map<LedgerIndex, bool>
125
getSnapshot
()
const
126
{
127
std::scoped_lock
const
lock(
mutex_
);
128
129
return
map_
;
130
}
131
};
132
133
}
// namespace xrpl
xrpl::PendingSaves
Keeps track of which ledgers haven't been fully saved.
Definition
PendingSaves.h:19
xrpl::PendingSaves::await_
std::condition_variable await_
Definition
PendingSaves.h:23
xrpl::PendingSaves::getSnapshot
std::map< LedgerIndex, bool > getSnapshot() const
Get a snapshot of the pending saves.
Definition
PendingSaves.h:125
xrpl::PendingSaves::finishWork
void finishWork(LedgerIndex seq)
Finish working on a ledger.
Definition
PendingSaves.h:58
xrpl::PendingSaves::mutex_
std::mutex mutex_
Definition
PendingSaves.h:21
xrpl::PendingSaves::shouldWork
bool shouldWork(LedgerIndex seq, bool isSynchronous)
Check if a ledger should be dispatched.
Definition
PendingSaves.h:86
xrpl::PendingSaves::startWork
bool startWork(LedgerIndex seq)
Start working on a ledger.
Definition
PendingSaves.h:34
xrpl::PendingSaves::pending
bool pending(LedgerIndex seq)
Return true if a ledger is in the progress of being saved.
Definition
PendingSaves.h:70
xrpl::PendingSaves::map_
std::map< LedgerIndex, bool > map_
Definition
PendingSaves.h:22
condition_variable
map
mutex
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::LedgerIndex
std::uint32_t LedgerIndex
A ledger index.
Definition
Protocol.h:382
std::scoped_lock
std::unique_lock
Generated by
1.17.0