rippled
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 <ripple/protocol/Protocol.h>
24 #include <condition_variable>
25 #include <map>
26 #include <mutex>
27 
28 namespace ripple {
29 
37 {
38 private:
39  std::mutex mutable mutex_;
42 
43 public:
50  bool
52  {
53  std::lock_guard lock(mutex_);
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  {
76  std::lock_guard lock(mutex_);
77 
78  map_.erase(seq);
80  }
81 
83  bool
85  {
86  std::lock_guard lock(mutex_);
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 
137  getSnapshot() const
138  {
139  std::lock_guard lock(mutex_);
140 
141  return map_;
142  }
143 };
144 
145 } // namespace ripple
146 
147 #endif
ripple::PendingSaves::pending
bool pending(LedgerIndex seq)
Return true if a ledger is in the progress of being saved.
Definition: PendingSaves.h:84
std::lock_guard
STL class.
ripple::PendingSaves
Keeps track of which ledgers haven't been fully saved.
Definition: PendingSaves.h:36
ripple::PendingSaves::mutex_
std::mutex mutex_
Definition: PendingSaves.h:39
ripple::PendingSaves::startWork
bool startWork(LedgerIndex seq)
Start working on a ledger.
Definition: PendingSaves.h:51
std::unique_lock
STL class.
ripple::PendingSaves::await_
std::condition_variable await_
Definition: PendingSaves.h:41
ripple::PendingSaves::finishWork
void finishWork(LedgerIndex seq)
Finish working on a ledger.
Definition: PendingSaves.h:74
std::uint32_t
std::condition_variable::wait
T wait(T... args)
map
ripple::PendingSaves::shouldWork
bool shouldWork(LedgerIndex seq, bool isSynchronous)
Check if a ledger should be dispatched.
Definition: PendingSaves.h:99
ripple::PendingSaves::getSnapshot
std::map< LedgerIndex, bool > getSnapshot() const
Get a snapshot of the pending saves.
Definition: PendingSaves.h:137
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
condition_variable
mutex
ripple::PendingSaves::map_
std::map< LedgerIndex, bool > map_
Definition: PendingSaves.h:40
std::condition_variable::notify_all
T notify_all(T... args)