diff options
Diffstat (limited to 'src/checkqueue.h')
-rw-r--r-- | src/checkqueue.h | 59 |
1 files changed, 36 insertions, 23 deletions
diff --git a/src/checkqueue.h b/src/checkqueue.h index c2c7d8ca24..b2a713e646 100644 --- a/src/checkqueue.h +++ b/src/checkqueue.h @@ -13,7 +13,8 @@ #include <boost/thread/locks.hpp> #include <boost/thread/mutex.hpp> -template<typename T> class CCheckQueueControl; +template <typename T> +class CCheckQueueControl; /** Queue for verifications that have to be performed. * The verifications are represented by a type T, which must provide an @@ -24,7 +25,9 @@ template<typename T> class CCheckQueueControl; * the master is done adding work, it temporarily joins the worker pool * as an N'th worker, until all jobs are done. */ -template<typename T> class CCheckQueue { +template <typename T> +class CCheckQueue +{ private: // Mutex to protect the inner state boost::mutex mutex; @@ -60,8 +63,9 @@ private: unsigned int nBatchSize; // Internal function that does bulk of the verification work. - bool Loop(bool fMaster = false) { - boost::condition_variable &cond = fMaster ? condMaster : condWorker; + bool Loop(bool fMaster = false) + { + boost::condition_variable& cond = fMaster ? condMaster : condWorker; std::vector<T> vChecks; vChecks.reserve(nBatchSize); unsigned int nNow = 0; @@ -103,41 +107,43 @@ private: nNow = std::max(1U, std::min(nBatchSize, (unsigned int)queue.size() / (nTotal + nIdle + 1))); vChecks.resize(nNow); for (unsigned int i = 0; i < nNow; i++) { - // We want the lock on the mutex to be as short as possible, so swap jobs from the global - // queue to the local batch vector instead of copying. - vChecks[i].swap(queue.back()); - queue.pop_back(); + // We want the lock on the mutex to be as short as possible, so swap jobs from the global + // queue to the local batch vector instead of copying. + vChecks[i].swap(queue.back()); + queue.pop_back(); } // Check whether we need to do work at all fOk = fAllOk; } // execute work - BOOST_FOREACH(T &check, vChecks) + BOOST_FOREACH (T& check, vChecks) if (fOk) fOk = check(); vChecks.clear(); - } while(true); + } while (true); } public: // Create a new check queue - CCheckQueue(unsigned int nBatchSizeIn) : - nIdle(0), nTotal(0), fAllOk(true), nTodo(0), fQuit(false), nBatchSize(nBatchSizeIn) {} + CCheckQueue(unsigned int nBatchSizeIn) : nIdle(0), nTotal(0), fAllOk(true), nTodo(0), fQuit(false), nBatchSize(nBatchSizeIn) {} // Worker thread - void Thread() { + void Thread() + { Loop(); } // Wait until execution finishes, and return whether all evaluations where succesful. - bool Wait() { + bool Wait() + { return Loop(true); } // Add a batch of checks to the queue - void Add(std::vector<T> &vChecks) { + void Add(std::vector<T>& vChecks) + { boost::unique_lock<boost::mutex> lock(mutex); - BOOST_FOREACH(T &check, vChecks) { + BOOST_FOREACH (T& check, vChecks) { queue.push_back(T()); check.swap(queue.back()); } @@ -148,7 +154,8 @@ public: condWorker.notify_all(); } - ~CCheckQueue() { + ~CCheckQueue() + { } friend class CCheckQueueControl<T>; @@ -157,13 +164,16 @@ public: /** RAII-style controller object for a CCheckQueue that guarantees the passed * queue is finished before continuing. */ -template<typename T> class CCheckQueueControl { +template <typename T> +class CCheckQueueControl +{ private: - CCheckQueue<T> *pqueue; + CCheckQueue<T>* pqueue; bool fDone; public: - CCheckQueueControl(CCheckQueue<T> *pqueueIn) : pqueue(pqueueIn), fDone(false) { + CCheckQueueControl(CCheckQueue<T>* pqueueIn) : pqueue(pqueueIn), fDone(false) + { // passed queue is supposed to be unused, or NULL if (pqueue != NULL) { assert(pqueue->nTotal == pqueue->nIdle); @@ -172,7 +182,8 @@ public: } } - bool Wait() { + bool Wait() + { if (pqueue == NULL) return true; bool fRet = pqueue->Wait(); @@ -180,12 +191,14 @@ public: return fRet; } - void Add(std::vector<T> &vChecks) { + void Add(std::vector<T>& vChecks) + { if (pqueue != NULL) pqueue->Add(vChecks); } - ~CCheckQueueControl() { + ~CCheckQueueControl() + { if (!fDone) Wait(); } |