Skip to content

Commit

Permalink
Merge bitcoin#9497: CCheckQueue Unit Tests
Browse files Browse the repository at this point in the history
96c7f2c Add CheckQueue Tests (Jeremy Rubin)
e207342 Fix CCheckQueue IsIdle (potential) race condition and remove dangerous constructors. (Jeremy Rubin)

Tree-SHA512: 5989743ad0f8b08998335e7ca9256e168fa319053f91b9dece9dbb134885bef7753b567b591acc7135785f23d19799ed7e6375917f59fe0178d389e961633d62
  • Loading branch information
laanwj authored and andvgal committed Dec 25, 2018
1 parent c835030 commit abcda0e
Show file tree
Hide file tree
Showing 3 changed files with 455 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ BITCOIN_TESTS =\
test/bip39_tests.cpp \
test/bloom_tests.cpp \
test/bswap_tests.cpp \
test/checkqueue_tests.cpp \
test/cachemap_tests.cpp \
test/cachemultimap_tests.cpp \
test/coins_tests.cpp \
Expand Down
22 changes: 12 additions & 10 deletions src/checkqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class CCheckQueue
}

public:
//! Mutex to ensure only one concurrent CCheckQueueControl
boost::mutex ControlMutex;

//! Create a new check queue
CCheckQueue(unsigned int nBatchSizeIn) : nIdle(0), nTotal(0), fAllOk(true), nTodo(0), fQuit(false), nBatchSize(nBatchSizeIn) {}

Expand Down Expand Up @@ -162,12 +165,6 @@ class CCheckQueue
{
}

bool IsIdle()
{
boost::unique_lock<boost::mutex> lock(mutex);
return (nTotal == nIdle && nTodo == 0 && fAllOk == true);
}

};

/**
Expand All @@ -178,16 +175,18 @@ template <typename T>
class CCheckQueueControl
{
private:
CCheckQueue<T>* pqueue;
CCheckQueue<T> * const pqueue;
bool fDone;

public:
CCheckQueueControl(CCheckQueue<T>* pqueueIn) : pqueue(pqueueIn), fDone(false)
CCheckQueueControl() = delete;
CCheckQueueControl(const CCheckQueueControl&) = delete;
CCheckQueueControl& operator=(const CCheckQueueControl&) = delete;
explicit CCheckQueueControl(CCheckQueue<T> * const pqueueIn) : pqueue(pqueueIn), fDone(false)
{
// passed queue is supposed to be unused, or NULL
if (pqueue != NULL) {
bool isIdle = pqueue->IsIdle();
assert(isIdle);
ENTER_CRITICAL_SECTION(pqueue->ControlMutex);
}
}

Expand All @@ -210,6 +209,9 @@ class CCheckQueueControl
{
if (!fDone)
Wait();
if (pqueue != NULL) {
LEAVE_CRITICAL_SECTION(pqueue->ControlMutex);
}
}
};

Expand Down
Loading

0 comments on commit abcda0e

Please sign in to comment.