rippled
Loading...
Searching...
No Matches
PendingSaves.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2015 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_APP_PENDINGSAVES_H_INCLUDED
21#define RIPPLE_APP_PENDINGSAVES_H_INCLUDED
22
23#include <xrpl/protocol/Protocol.h>
24
25#include <condition_variable>
26#include <map>
27#include <mutex>
28
29namespace ripple {
30
38{
39private:
43
44public:
51 bool
53 {
55
56 auto it = map_.find(seq);
57
58 if ((it == map_.end()) || it->second)
59 {
60 // Work done or another thread is doing it
61 return false;
62 }
63
64 it->second = true;
65 return true;
66 }
67
74 void
76 {
78
79 map_.erase(seq);
81 }
82
84 bool
86 {
88 return map_.find(seq) != map_.end();
89 }
90
99 bool
100 shouldWork(LedgerIndex seq, bool isSynchronous)
101 {
103 do
104 {
105 auto it = map_.find(seq);
106
107 if (it == map_.end())
108 {
109 map_.emplace(seq, false);
110 return true;
111 }
112
113 if (!isSynchronous)
114 {
115 // Already dispatched
116 return false;
117 }
118
119 if (!it->second)
120 {
121 // Scheduled, but not dispatched
122 return true;
123 }
124
125 // Already in progress, just need to wait
126 await_.wait(lock);
127
128 } while (true);
129 }
130
139 {
141
142 return map_;
143 }
144};
145
146} // namespace ripple
147
148#endif
Keeps track of which ledgers haven't been fully saved.
Definition: PendingSaves.h:38
void finishWork(LedgerIndex seq)
Finish working on a ledger.
Definition: PendingSaves.h:75
std::map< LedgerIndex, bool > getSnapshot() const
Get a snapshot of the pending saves.
Definition: PendingSaves.h:138
bool startWork(LedgerIndex seq)
Start working on a ledger.
Definition: PendingSaves.h:52
bool shouldWork(LedgerIndex seq, bool isSynchronous)
Check if a ledger should be dispatched.
Definition: PendingSaves.h:100
std::map< LedgerIndex, bool > map_
Definition: PendingSaves.h:41
std::condition_variable await_
Definition: PendingSaves.h:42
bool pending(LedgerIndex seq)
Return true if a ledger is in the progress of being saved.
Definition: PendingSaves.h:85
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25