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#include <condition_variable>
25#include <map>
26#include <mutex>
27
28namespace ripple {
29
37{
38private:
42
43public:
50 bool
52 {
54
55 auto it = map_.find(seq);
56
57 if ((it == map_.end()) || it->second)
58 {
59 // Work done or another thread is doing it
60 return false;
61 }
62
63 it->second = true;
64 return true;
65 }
66
73 void
75 {
77
78 map_.erase(seq);
80 }
81
83 bool
85 {
87 return map_.find(seq) != map_.end();
88 }
89
98 bool
99 shouldWork(LedgerIndex seq, bool isSynchronous)
100 {
102 do
103 {
104 auto it = map_.find(seq);
105
106 if (it == map_.end())
107 {
108 map_.emplace(seq, false);
109 return true;
110 }
111
112 if (!isSynchronous)
113 {
114 // Already dispatched
115 return false;
116 }
117
118 if (!it->second)
119 {
120 // Scheduled, but not dispatched
121 return true;
122 }
123
124 // Already in progress, just need to wait
125 await_.wait(lock);
126
127 } while (true);
128 }
129
138 {
140
141 return map_;
142 }
143};
144
145} // namespace ripple
146
147#endif
Keeps track of which ledgers haven't been fully saved.
Definition: PendingSaves.h:37
void finishWork(LedgerIndex seq)
Finish working on a ledger.
Definition: PendingSaves.h:74
std::map< LedgerIndex, bool > getSnapshot() const
Get a snapshot of the pending saves.
Definition: PendingSaves.h:137
bool startWork(LedgerIndex seq)
Start working on a ledger.
Definition: PendingSaves.h:51
bool shouldWork(LedgerIndex seq, bool isSynchronous)
Check if a ledger should be dispatched.
Definition: PendingSaves.h:99
std::map< LedgerIndex, bool > map_
Definition: PendingSaves.h:40
std::condition_variable await_
Definition: PendingSaves.h:41
bool pending(LedgerIndex seq)
Return true if a ledger is in the progress of being saved.
Definition: PendingSaves.h:84
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26