2015-12-13 17:58:29 +01:00
|
|
|
// Copyright (c) 2012-2015 The Bitcoin Core developers
|
2014-10-31 01:43:19 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-12-01 23:04:14 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#ifndef BITCOIN_CHECKQUEUE_H
|
|
|
|
#define BITCOIN_CHECKQUEUE_H
|
2012-12-01 23:04:14 +01:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
#include <boost/foreach.hpp>
|
2012-12-01 23:04:14 +01:00
|
|
|
#include <boost/thread/condition_variable.hpp>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <boost/thread/locks.hpp>
|
|
|
|
#include <boost/thread/mutex.hpp>
|
2012-12-01 23:04:14 +01:00
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
template <typename T>
|
|
|
|
class CCheckQueueControl;
|
2012-12-01 23:04:14 +01:00
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
/**
|
|
|
|
* Queue for verifications that have to be performed.
|
2012-12-01 23:04:14 +01:00
|
|
|
* The verifications are represented by a type T, which must provide an
|
|
|
|
* operator(), returning a bool.
|
|
|
|
*
|
|
|
|
* One thread (the master) is assumed to push batches of verifications
|
|
|
|
* onto the queue, where they are processed by N-1 worker threads. When
|
|
|
|
* the master is done adding work, it temporarily joins the worker pool
|
|
|
|
* as an N'th worker, until all jobs are done.
|
|
|
|
*/
|
2014-09-19 19:21:46 +02:00
|
|
|
template <typename T>
|
|
|
|
class CCheckQueue
|
|
|
|
{
|
2012-12-01 23:04:14 +01:00
|
|
|
private:
|
2014-10-31 01:43:19 +01:00
|
|
|
//! Mutex to protect the inner state
|
2012-12-01 23:04:14 +01:00
|
|
|
boost::mutex mutex;
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! Worker threads block on this when out of work
|
2012-12-01 23:04:14 +01:00
|
|
|
boost::condition_variable condWorker;
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! Master thread blocks on this when out of work
|
2012-12-01 23:04:14 +01:00
|
|
|
boost::condition_variable condMaster;
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! The queue of elements to be processed.
|
|
|
|
//! As the order of booleans doesn't matter, it is used as a LIFO (stack)
|
2012-12-01 23:04:14 +01:00
|
|
|
std::vector<T> queue;
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! The number of workers (including the master) that are idle.
|
2012-12-01 23:04:14 +01:00
|
|
|
int nIdle;
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! The total number of workers (including the master).
|
2012-12-01 23:04:14 +01:00
|
|
|
int nTotal;
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! The temporary evaluation result.
|
2012-12-01 23:04:14 +01:00
|
|
|
bool fAllOk;
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
/**
|
|
|
|
* Number of verifications that haven't completed yet.
|
2015-04-28 16:48:28 +02:00
|
|
|
* This includes elements that are no longer queued, but still in the
|
2014-10-31 01:43:19 +01:00
|
|
|
* worker's own batches.
|
|
|
|
*/
|
2012-12-01 23:04:14 +01:00
|
|
|
unsigned int nTodo;
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! Whether we're shutting down.
|
2012-12-01 23:04:14 +01:00
|
|
|
bool fQuit;
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! The maximum number of elements to be processed in one batch
|
2012-12-01 23:04:14 +01:00
|
|
|
unsigned int nBatchSize;
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
/** Internal function that does bulk of the verification work. */
|
2014-09-19 19:21:46 +02:00
|
|
|
bool Loop(bool fMaster = false)
|
|
|
|
{
|
|
|
|
boost::condition_variable& cond = fMaster ? condMaster : condWorker;
|
2012-12-01 23:04:14 +01:00
|
|
|
std::vector<T> vChecks;
|
|
|
|
vChecks.reserve(nBatchSize);
|
|
|
|
unsigned int nNow = 0;
|
|
|
|
bool fOk = true;
|
|
|
|
do {
|
|
|
|
{
|
|
|
|
boost::unique_lock<boost::mutex> lock(mutex);
|
|
|
|
// first do the clean-up of the previous loop run (allowing us to do it in the same critsect)
|
|
|
|
if (nNow) {
|
|
|
|
fAllOk &= fOk;
|
|
|
|
nTodo -= nNow;
|
|
|
|
if (nTodo == 0 && !fMaster)
|
2015-04-28 16:48:28 +02:00
|
|
|
// We processed the last element; inform the master it can exit and return the result
|
2012-12-01 23:04:14 +01:00
|
|
|
condMaster.notify_one();
|
|
|
|
} else {
|
|
|
|
// first iteration
|
|
|
|
nTotal++;
|
|
|
|
}
|
|
|
|
// logically, the do loop starts here
|
|
|
|
while (queue.empty()) {
|
|
|
|
if ((fMaster || fQuit) && nTodo == 0) {
|
|
|
|
nTotal--;
|
|
|
|
bool fRet = fAllOk;
|
|
|
|
// reset the status for new work later
|
|
|
|
if (fMaster)
|
|
|
|
fAllOk = true;
|
|
|
|
// return the current status
|
|
|
|
return fRet;
|
|
|
|
}
|
|
|
|
nIdle++;
|
|
|
|
cond.wait(lock); // wait
|
|
|
|
nIdle--;
|
|
|
|
}
|
|
|
|
// Decide how many work units to process now.
|
|
|
|
// * Do not try to do everything at once, but aim for increasingly smaller batches so
|
|
|
|
// all workers finish approximately simultaneously.
|
|
|
|
// * Try to account for idle jobs which will instantly start helping.
|
|
|
|
// * Don't do batches smaller than 1 (duh), or larger than nBatchSize.
|
|
|
|
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++) {
|
2014-09-19 19:21:46 +02:00
|
|
|
// 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();
|
2012-12-01 23:04:14 +01:00
|
|
|
}
|
|
|
|
// Check whether we need to do work at all
|
|
|
|
fOk = fAllOk;
|
|
|
|
}
|
|
|
|
// execute work
|
2014-09-19 19:21:46 +02:00
|
|
|
BOOST_FOREACH (T& check, vChecks)
|
2012-12-01 23:04:14 +01:00
|
|
|
if (fOk)
|
|
|
|
fOk = check();
|
|
|
|
vChecks.clear();
|
2014-09-19 19:21:46 +02:00
|
|
|
} while (true);
|
2012-12-01 23:04:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-01-09 17:13:37 +01:00
|
|
|
//! Mutex to ensure only one concurrent CCheckQueueControl
|
|
|
|
boost::mutex ControlMutex;
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! Create a new check queue
|
2014-09-19 19:21:46 +02:00
|
|
|
CCheckQueue(unsigned int nBatchSizeIn) : nIdle(0), nTotal(0), fAllOk(true), nTodo(0), fQuit(false), nBatchSize(nBatchSizeIn) {}
|
2012-12-01 23:04:14 +01:00
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! Worker thread
|
2014-09-19 19:21:46 +02:00
|
|
|
void Thread()
|
|
|
|
{
|
2012-12-01 23:04:14 +01:00
|
|
|
Loop();
|
|
|
|
}
|
|
|
|
|
2015-04-28 16:48:28 +02:00
|
|
|
//! Wait until execution finishes, and return whether all evaluations were successful.
|
2014-09-19 19:21:46 +02:00
|
|
|
bool Wait()
|
|
|
|
{
|
2012-12-01 23:04:14 +01:00
|
|
|
return Loop(true);
|
|
|
|
}
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! Add a batch of checks to the queue
|
2014-09-19 19:21:46 +02:00
|
|
|
void Add(std::vector<T>& vChecks)
|
|
|
|
{
|
2012-12-01 23:04:14 +01:00
|
|
|
boost::unique_lock<boost::mutex> lock(mutex);
|
2014-09-19 19:21:46 +02:00
|
|
|
BOOST_FOREACH (T& check, vChecks) {
|
2012-12-01 23:04:14 +01:00
|
|
|
queue.push_back(T());
|
|
|
|
check.swap(queue.back());
|
|
|
|
}
|
|
|
|
nTodo += vChecks.size();
|
|
|
|
if (vChecks.size() == 1)
|
|
|
|
condWorker.notify_one();
|
|
|
|
else if (vChecks.size() > 1)
|
|
|
|
condWorker.notify_all();
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
~CCheckQueue()
|
|
|
|
{
|
2013-02-16 17:58:45 +01:00
|
|
|
}
|
|
|
|
|
2012-12-01 23:04:14 +01:00
|
|
|
};
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
/**
|
|
|
|
* RAII-style controller object for a CCheckQueue that guarantees the passed
|
|
|
|
* queue is finished before continuing.
|
2012-12-01 23:04:14 +01:00
|
|
|
*/
|
2014-09-19 19:21:46 +02:00
|
|
|
template <typename T>
|
|
|
|
class CCheckQueueControl
|
|
|
|
{
|
2012-12-01 23:04:14 +01:00
|
|
|
private:
|
2017-01-09 17:13:37 +01:00
|
|
|
CCheckQueue<T> * const pqueue;
|
2012-12-01 23:04:14 +01:00
|
|
|
bool fDone;
|
|
|
|
|
|
|
|
public:
|
2017-01-09 17:13:37 +01:00
|
|
|
CCheckQueueControl() = delete;
|
|
|
|
CCheckQueueControl(const CCheckQueueControl&) = delete;
|
|
|
|
CCheckQueueControl& operator=(const CCheckQueueControl&) = delete;
|
|
|
|
explicit CCheckQueueControl(CCheckQueue<T> * const pqueueIn) : pqueue(pqueueIn), fDone(false)
|
2014-09-19 19:21:46 +02:00
|
|
|
{
|
2012-12-01 23:04:14 +01:00
|
|
|
// passed queue is supposed to be unused, or NULL
|
|
|
|
if (pqueue != NULL) {
|
2017-01-09 17:13:37 +01:00
|
|
|
ENTER_CRITICAL_SECTION(pqueue->ControlMutex);
|
2012-12-01 23:04:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
bool Wait()
|
|
|
|
{
|
2012-12-01 23:04:14 +01:00
|
|
|
if (pqueue == NULL)
|
|
|
|
return true;
|
|
|
|
bool fRet = pqueue->Wait();
|
|
|
|
fDone = true;
|
|
|
|
return fRet;
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
void Add(std::vector<T>& vChecks)
|
|
|
|
{
|
2012-12-01 23:04:14 +01:00
|
|
|
if (pqueue != NULL)
|
|
|
|
pqueue->Add(vChecks);
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
~CCheckQueueControl()
|
|
|
|
{
|
2012-12-01 23:04:14 +01:00
|
|
|
if (!fDone)
|
|
|
|
Wait();
|
2017-01-09 17:13:37 +01:00
|
|
|
if (pqueue != NULL) {
|
|
|
|
LEAVE_CRITICAL_SECTION(pqueue->ControlMutex);
|
|
|
|
}
|
2012-12-01 23:04:14 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#endif // BITCOIN_CHECKQUEUE_H
|