5 #ifndef FABCOIN_CHECKQUEUE_H 6 #define FABCOIN_CHECKQUEUE_H 13 #include <boost/thread/condition_variable.hpp> 14 #include <boost/thread/mutex.hpp> 69 bool Loop(
bool fMaster =
false)
71 boost::condition_variable& cond = fMaster ? condMaster :
condWorker;
72 std::vector<T> vChecks;
73 vChecks.reserve(nBatchSize);
74 unsigned int nNow = 0;
78 boost::unique_lock<boost::mutex> lock(mutex);
83 if (nTodo == 0 && !fMaster)
85 condMaster.notify_one();
91 while (queue.empty()) {
92 if ((fMaster || fQuit) && nTodo == 0) {
110 nNow =
std::max(1U,
std::min(nBatchSize, (
unsigned int)queue.size() / (nTotal + nIdle + 1)));
111 vChecks.resize(nNow);
112 for (
unsigned int i = 0; i < nNow; i++) {
115 vChecks[i].swap(queue.back());
122 for (
T& check : vChecks)
134 CCheckQueue(
unsigned int nBatchSizeIn) : nIdle(0), nTotal(0), fAllOk(true), nTodo(0), fQuit(false), nBatchSize(nBatchSizeIn) {}
149 void Add(std::vector<T>& vChecks)
151 boost::unique_lock<boost::mutex> lock(mutex);
152 for (
T& check : vChecks) {
153 queue.push_back(
T());
154 check.swap(queue.back());
156 nTodo += vChecks.size();
157 if (vChecks.size() == 1)
158 condWorker.notify_one();
159 else if (vChecks.size() > 1)
160 condWorker.notify_all();
173 template <
typename T>
187 if (pqueue !=
nullptr) {
194 if (pqueue ==
nullptr)
196 bool fRet = pqueue->
Wait();
201 void Add(std::vector<T>& vChecks)
203 if (pqueue !=
nullptr)
204 pqueue->
Add(vChecks);
211 if (pqueue !=
nullptr) {
217 #endif // FABCOIN_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.
bool fQuit
Whether we're shutting down.
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.
dev::WithExisting max(dev::WithExisting _a, dev::WithExisting _b)
boost::mutex ControlMutex
Mutex to ensure only one concurrent CCheckQueueControl.