6 #ifndef RAVEN_CHECKQUEUE_H 7 #define RAVEN_CHECKQUEUE_H 14 #include <boost/thread/condition_variable.hpp> 15 #include <boost/thread/mutex.hpp> 70 bool Loop(
bool fMaster =
false)
73 std::vector<T> vChecks;
74 vChecks.reserve(nBatchSize);
75 unsigned int nNow = 0;
79 boost::unique_lock<boost::mutex> lock(mutex);
84 if (nTodo == 0 && !fMaster)
86 condMaster.notify_one();
92 while (queue.empty()) {
93 if ((fMaster || fQuit) && nTodo == 0) {
111 nNow = std::max(1U, std::min(nBatchSize, (
unsigned int)queue.size() / (nTotal + nIdle + 1)));
112 vChecks.resize(nNow);
113 for (
unsigned int i = 0; i < nNow; i++) {
116 vChecks[i].swap(queue.back());
123 for (T& check : vChecks)
135 explicit CCheckQueue(
unsigned int nBatchSizeIn) : nIdle(0), nTotal(0), fAllOk(true), nTodo(0), fQuit(false), nBatchSize(nBatchSizeIn) {}
150 void Add(std::vector<T>& vChecks)
152 boost::unique_lock<boost::mutex> lock(mutex);
153 for (T& check : vChecks) {
154 queue.push_back(T());
155 check.swap(queue.back());
157 nTodo += vChecks.size();
158 if (vChecks.size() == 1)
159 condWorker.notify_one();
160 else if (vChecks.size() > 1)
161 condWorker.notify_all();
174 template <
typename T>
188 if (pqueue !=
nullptr) {
195 if (pqueue ==
nullptr)
197 bool fRet = pqueue->
Wait();
202 void Add(std::vector<T>& vChecks)
204 if (pqueue !=
nullptr)
205 pqueue->
Add(vChecks);
212 if (pqueue !=
nullptr) {
218 #endif // RAVEN_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.
boost::mutex ControlMutex
Mutex to ensure only one concurrent CCheckQueueControl.