5 #ifndef BITCOIN_CHECKQUEUE_H 6 #define BITCOIN_CHECKQUEUE_H 13 #include <boost/thread/condition_variable.hpp> 14 #include <boost/thread/mutex.hpp> 63 bool Loop(
bool fMaster =
false) {
65 std::vector<T> vChecks;
66 vChecks.reserve(nBatchSize);
67 unsigned int nNow = 0;
71 boost::unique_lock<boost::mutex> lock(mutex);
77 if (nTodo == 0 && !fMaster) {
80 condMaster.notify_one();
87 while (queue.empty()) {
88 if (fMaster && nTodo == 0) {
109 1U, std::min(nBatchSize, (
unsigned int)queue.size() /
110 (nTotal + nIdle + 1)));
111 vChecks.resize(nNow);
112 for (
unsigned int i = 0; i < nNow; i++) {
116 vChecks[i].swap(queue.back());
123 for (T &check : vChecks) {
138 : nIdle(0), nTotal(0), fAllOk(true), nTodo(0),
139 nBatchSize(nBatchSizeIn) {}
149 void Add(std::vector<T> &vChecks) {
150 boost::unique_lock<boost::mutex> lock(mutex);
151 for (T &check : vChecks) {
152 queue.push_back(T());
153 check.swap(queue.back());
155 nTodo += vChecks.size();
156 if (vChecks.size() == 1) {
157 condWorker.notify_one();
158 }
else if (vChecks.size() > 1) {
159 condWorker.notify_all();
180 : pqueue(pqueueIn), fDone(false) {
182 if (pqueue !=
nullptr) {
188 if (pqueue ==
nullptr) {
191 bool fRet = pqueue->
Wait();
196 void Add(std::vector<T> &vChecks) {
197 if (pqueue !=
nullptr) {
198 pqueue->
Add(vChecks);
206 if (pqueue !=
nullptr) {
212 #endif // BITCOIN_CHECKQUEUE_H
void Add(std::vector< T > &vChecks)
boost::condition_variable condWorker
Worker threads block on this when out of work.
boost::mutex mutex
Mutex to protect the inner state.
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.
CCheckQueueControl(CCheckQueue< T > *const pqueueIn)
void Thread()
Worker thread.
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
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)
int nTotal
The total number of workers (including the master).
Queue for verifications that have to be performed.
CCheckQueue< T > *const pqueue
#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.
boost::mutex ControlMutex
Mutex to ensure only one concurrent CCheckQueueControl.