5 #ifndef BITCOIN_CHECKQUEUE_H
6 #define BITCOIN_CHECKQUEUE_H
13 #include <boost/thread/condition_variable.hpp>
14 #include <boost/thread/mutex.hpp>
66 bool Loop(
bool fMaster =
false)
68 boost::condition_variable& cond = fMaster ? condMaster :
condWorker;
69 std::vector<T> vChecks;
70 vChecks.reserve(nBatchSize);
71 unsigned int nNow = 0;
75 boost::unique_lock<boost::mutex> lock(mutex);
80 if (nTodo == 0 && !fMaster)
82 condMaster.notify_one();
88 while (queue.empty()) {
89 if (fMaster && nTodo == 0) {
106 nNow = std::max(1U, std::min(nBatchSize, (
unsigned int)queue.size() / (nTotal + nIdle + 1)));
107 vChecks.resize(nNow);
108 for (
unsigned int i = 0; i < nNow; i++) {
111 vChecks[i].swap(queue.back());
118 for (T& check : vChecks)
130 explicit CCheckQueue(
unsigned int nBatchSizeIn) : nIdle(0), nTotal(0), fAllOk(true), nTodo(0), nBatchSize(nBatchSizeIn) {}
145 void Add(std::vector<T>& vChecks)
147 boost::unique_lock<boost::mutex> lock(mutex);
148 for (T& check : vChecks) {
149 queue.push_back(T());
150 check.swap(queue.back());
152 nTodo += vChecks.size();
153 if (vChecks.size() == 1)
154 condWorker.notify_one();
155 else if (vChecks.size() > 1)
156 condWorker.notify_all();
169 template <
typename T>
183 if (pqueue !=
nullptr) {
190 if (pqueue ==
nullptr)
192 bool fRet = pqueue->
Wait();
197 void Add(std::vector<T>& vChecks)
199 if (pqueue !=
nullptr)
200 pqueue->
Add(vChecks);
207 if (pqueue !=
nullptr) {
213 #endif // BITCOIN_CHECKQUEUE_H
boost::condition_variable condWorker
Worker threads block on this when out of work.
boost::mutex mutex
Mutex to protect the inner state.
CCheckQueueControl & operator=(const CCheckQueueControl &)=delete
CCheckQueueControl(CCheckQueue< T > *const pqueueIn)
boost::condition_variable condMaster
Master thread blocks on this when out of work.
bool Loop(bool fMaster=false)
Internal function that does bulk of the verification work.
void Thread()
Worker thread.
CCheckQueueControl()=delete
CCheckQueue(unsigned int nBatchSizeIn)
Create a new check queue.
std::vector< T > queue
The queue of elements to be processed.
bool fAllOk
The temporary evaluation result.
#define LEAVE_CRITICAL_SECTION(cs)
CCheckQueue< T > *const pqueue
int nTotal
The total number of workers (including the master).
Queue for verifications that have to be performed.
#define ENTER_CRITICAL_SECTION(cs)
bool Wait()
Wait until execution finishes, and return whether all evaluations were successful.
int nIdle
The number of workers (including the master) that are idle.
unsigned int nTodo
Number of verifications that haven't completed yet.
void Add(std::vector< T > &vChecks)
Add a batch of checks to the queue.
unsigned int nBatchSize
The maximum number of elements to be processed in one batch.
void Add(std::vector< T > &vChecks)
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
boost::mutex ControlMutex
Mutex to ensure only one concurrent CCheckQueueControl.