Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
checkqueue.h
Go to the documentation of this file.
1 // Copyright (c) 2012 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 #ifndef CHECKQUEUE_H
5 #define CHECKQUEUE_H
6 
7 #include <boost/thread/mutex.hpp>
8 #include <boost/thread/locks.hpp>
9 #include <boost/thread/condition_variable.hpp>
10 
11 #include <vector>
12 #include <algorithm>
13 
14 template<typename T> class CCheckQueueControl;
15 
25 template<typename T> class CCheckQueue {
26 private:
27  // Mutex to protect the inner state
28  boost::mutex mutex;
29 
30  // Worker threads block on this when out of work
31  boost::condition_variable condWorker;
32 
33  // Master thread blocks on this when out of work
34  boost::condition_variable condMaster;
35 
36  // The queue of elements to be processed.
37  // As the order of booleans doesn't matter, it is used as a LIFO (stack)
38  std::vector<T> queue;
39 
40  // The number of workers (including the master) that are idle.
41  int nIdle;
42 
43  // The total number of workers (including the master).
44  int nTotal;
45 
46  // The temporary evaluation result.
47  bool fAllOk;
48 
49  // Number of verifications that haven't completed yet.
50  // This includes elements that are not anymore in queue, but still in
51  // worker's own batches.
52  unsigned int nTodo;
53 
54  // Whether we're shutting down.
55  bool fQuit;
56 
57  // The maximum number of elements to be processed in one batch
58  unsigned int nBatchSize;
59 
60  // Internal function that does bulk of the verification work.
61  bool Loop(bool fMaster = false) {
62  boost::condition_variable &cond = fMaster ? condMaster : condWorker;
63  std::vector<T> vChecks;
64  vChecks.reserve(nBatchSize);
65  unsigned int nNow = 0;
66  bool fOk = true;
67  do {
68  {
69  boost::unique_lock<boost::mutex> lock(mutex);
70  // first do the clean-up of the previous loop run (allowing us to do it in the same critsect)
71  if (nNow) {
72  fAllOk &= fOk;
73  nTodo -= nNow;
74  if (nTodo == 0 && !fMaster)
75  // We processed the last element; inform the master he can exit and return the result
76  condMaster.notify_one();
77  } else {
78  // first iteration
79  nTotal++;
80  }
81  // logically, the do loop starts here
82  while (queue.empty()) {
83  if ((fMaster || fQuit) && nTodo == 0) {
84  nTotal--;
85  bool fRet = fAllOk;
86  // reset the status for new work later
87  if (fMaster)
88  fAllOk = true;
89  // return the current status
90  return fRet;
91  }
92  nIdle++;
93  cond.wait(lock); // wait
94  nIdle--;
95  }
96  // Decide how many work units to process now.
97  // * Do not try to do everything at once, but aim for increasingly smaller batches so
98  // all workers finish approximately simultaneously.
99  // * Try to account for idle jobs which will instantly start helping.
100  // * Don't do batches smaller than 1 (duh), or larger than nBatchSize.
101  nNow = std::max(1U, std::min(nBatchSize, (unsigned int)queue.size() / (nTotal + nIdle + 1)));
102  vChecks.resize(nNow);
103  for (unsigned int i = 0; i < nNow; i++) {
104  // We want the lock on the mutex to be as short as possible, so swap jobs from the global
105  // queue to the local batch vector instead of copying.
106  vChecks[i].swap(queue.back());
107  queue.pop_back();
108  }
109  // Check whether we need to do work at all
110  fOk = fAllOk;
111  }
112  // execute work
113  BOOST_FOREACH(T &check, vChecks)
114  if (fOk)
115  fOk = check();
116  vChecks.clear();
117  } while(true);
118  }
119 
120 public:
121  // Create a new check queue
122  CCheckQueue(unsigned int nBatchSizeIn) :
123  nIdle(0), nTotal(0), fAllOk(true), nTodo(0), fQuit(false), nBatchSize(nBatchSizeIn) {}
124 
125  // Worker thread
126  void Thread() {
127  Loop();
128  }
129 
130  // Wait until execution finishes, and return whether all evaluations where succesful.
131  bool Wait() {
132  return Loop(true);
133  }
134 
135  // Add a batch of checks to the queue
136  void Add(std::vector<T> &vChecks) {
137  boost::unique_lock<boost::mutex> lock(mutex);
138  BOOST_FOREACH(T &check, vChecks) {
139  queue.push_back(T());
140  check.swap(queue.back());
141  }
142  nTodo += vChecks.size();
143  if (vChecks.size() == 1)
144  condWorker.notify_one();
145  else if (vChecks.size() > 1)
146  condWorker.notify_all();
147  }
148 
150  }
151 
152  friend class CCheckQueueControl<T>;
153 };
154 
158 template<typename T> class CCheckQueueControl {
159 private:
161  bool fDone;
162 
163 public:
164  CCheckQueueControl(CCheckQueue<T> *pqueueIn) : pqueue(pqueueIn), fDone(false) {
165  // passed queue is supposed to be unused, or NULL
166  if (pqueue != NULL) {
167  assert(pqueue->nTotal == pqueue->nIdle);
168  assert(pqueue->nTodo == 0);
169  assert(pqueue->fAllOk == true);
170  }
171  }
172 
173  bool Wait() {
174  if (pqueue == NULL)
175  return true;
176  bool fRet = pqueue->Wait();
177  fDone = true;
178  return fRet;
179  }
180 
181  void Add(std::vector<T> &vChecks) {
182  if (pqueue != NULL)
183  pqueue->Add(vChecks);
184  }
185 
187  if (!fDone)
188  Wait();
189  }
190 };
191 
192 #endif
boost::condition_variable condWorker
Definition: checkqueue.h:31
boost::mutex mutex
Definition: checkqueue.h:28
boost::condition_variable condMaster
Definition: checkqueue.h:34
bool Loop(bool fMaster=false)
Definition: checkqueue.h:61
void Thread()
Definition: checkqueue.h:126
CCheckQueue(unsigned int nBatchSizeIn)
Definition: checkqueue.h:122
std::vector< T > queue
Definition: checkqueue.h:38
bool fAllOk
Definition: checkqueue.h:47
CCheckQueue< T > * pqueue
Definition: checkqueue.h:160
CCheckQueueControl(CCheckQueue< T > *pqueueIn)
Definition: checkqueue.h:164
Queue for verifications that have to be performed.
Definition: checkqueue.h:25
bool Wait()
Definition: checkqueue.h:131
bool fQuit
Definition: checkqueue.h:55
unsigned int nTodo
Definition: checkqueue.h:52
void Add(std::vector< T > &vChecks)
Definition: checkqueue.h:136
unsigned int nBatchSize
Definition: checkqueue.h:58
void Add(std::vector< T > &vChecks)
Definition: checkqueue.h:181
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
Definition: checkqueue.h:14