1 // Copyright (c) 2012-2015 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_CHECKQUEUE_H
6 #define BITCOIN_CHECKQUEUE_H
13 #include <boost/foreach.hpp>
14 #include <boost/thread/condition_variable.hpp>
15 #include <boost/thread/locks.hpp>
16 #include <boost/thread/mutex.hpp>
19 class CCheckQueueControl
;
22 * Queue for verifications that have to be performed.
23 * The verifications are represented by a type T, which must provide an
24 * operator(), returning a bool.
26 * One thread (the master) is assumed to push batches of verifications
27 * onto the queue, where they are processed by N-1 worker threads. When
28 * the master is done adding work, it temporarily joins the worker pool
29 * as an N'th worker, until all jobs are done.
35 //! Mutex to protect the inner state
38 //! Worker threads block on this when out of work
39 boost::condition_variable condWorker
;
41 //! Master thread blocks on this when out of work
42 boost::condition_variable condMaster
;
44 //! The queue of elements to be processed.
45 //! As the order of booleans doesn't matter, it is used as a LIFO (stack)
48 //! The number of workers (including the master) that are idle.
51 //! The total number of workers (including the master).
54 //! The temporary evaluation result.
58 * Number of verifications that haven't completed yet.
59 * This includes elements that are no longer queued, but still in the
60 * worker's own batches.
64 //! Whether we're shutting down.
67 //! The maximum number of elements to be processed in one batch
68 unsigned int nBatchSize
;
70 /** Internal function that does bulk of the verification work. */
71 bool Loop(bool fMaster
= false)
73 boost::condition_variable
& cond
= fMaster
? condMaster
: condWorker
;
74 std::vector
<T
> vChecks
;
75 vChecks
.reserve(nBatchSize
);
76 unsigned int nNow
= 0;
80 boost::unique_lock
<boost::mutex
> lock(mutex
);
81 // first do the clean-up of the previous loop run (allowing us to do it in the same critsect)
85 if (nTodo
== 0 && !fMaster
)
86 // We processed the last element; inform the master it can exit and return the result
87 condMaster
.notify_one();
92 // logically, the do loop starts here
93 while (queue
.empty()) {
94 if ((fMaster
|| fQuit
) && nTodo
== 0) {
97 // reset the status for new work later
100 // return the current status
104 cond
.wait(lock
); // wait
107 // Decide how many work units to process now.
108 // * Do not try to do everything at once, but aim for increasingly smaller batches so
109 // all workers finish approximately simultaneously.
110 // * Try to account for idle jobs which will instantly start helping.
111 // * Don't do batches smaller than 1 (duh), or larger than nBatchSize.
112 nNow
= std::max(1U, std::min(nBatchSize
, (unsigned int)queue
.size() / (nTotal
+ nIdle
+ 1)));
113 vChecks
.resize(nNow
);
114 for (unsigned int i
= 0; i
< nNow
; i
++) {
115 // We want the lock on the mutex to be as short as possible, so swap jobs from the global
116 // queue to the local batch vector instead of copying.
117 vChecks
[i
].swap(queue
.back());
120 // Check whether we need to do work at all
124 BOOST_FOREACH (T
& check
, vChecks
)
132 //! Mutex to ensure only one concurrent CCheckQueueControl
133 boost::mutex ControlMutex
;
135 //! Create a new check queue
136 CCheckQueue(unsigned int nBatchSizeIn
) : nIdle(0), nTotal(0), fAllOk(true), nTodo(0), fQuit(false), nBatchSize(nBatchSizeIn
) {}
144 //! Wait until execution finishes, and return whether all evaluations were successful.
150 //! Add a batch of checks to the queue
151 void Add(std::vector
<T
>& vChecks
)
153 boost::unique_lock
<boost::mutex
> lock(mutex
);
154 BOOST_FOREACH (T
& check
, vChecks
) {
155 queue
.push_back(T());
156 check
.swap(queue
.back());
158 nTodo
+= vChecks
.size();
159 if (vChecks
.size() == 1)
160 condWorker
.notify_one();
161 else if (vChecks
.size() > 1)
162 condWorker
.notify_all();
172 * RAII-style controller object for a CCheckQueue that guarantees the passed
173 * queue is finished before continuing.
175 template <typename T
>
176 class CCheckQueueControl
179 CCheckQueue
<T
> * const pqueue
;
183 CCheckQueueControl() = delete;
184 CCheckQueueControl(const CCheckQueueControl
&) = delete;
185 CCheckQueueControl
& operator=(const CCheckQueueControl
&) = delete;
186 explicit CCheckQueueControl(CCheckQueue
<T
> * const pqueueIn
) : pqueue(pqueueIn
), fDone(false)
188 // passed queue is supposed to be unused, or NULL
189 if (pqueue
!= NULL
) {
190 ENTER_CRITICAL_SECTION(pqueue
->ControlMutex
);
198 bool fRet
= pqueue
->Wait();
203 void Add(std::vector
<T
>& vChecks
)
206 pqueue
->Add(vChecks
);
209 ~CCheckQueueControl()
213 if (pqueue
!= NULL
) {
214 LEAVE_CRITICAL_SECTION(pqueue
->ControlMutex
);
219 #endif // BITCOIN_CHECKQUEUE_H