Skip to content

Commit bb6fcc7

Browse files
committed
refactor: Drop boost::thread stuff in CCheckQueue
1 parent 6784ac4 commit bb6fcc7

File tree

3 files changed

+25
-33
lines changed

3 files changed

+25
-33
lines changed

src/checkqueue.h

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
#include <algorithm>
1313
#include <vector>
1414

15-
#include <boost/thread/condition_variable.hpp>
16-
#include <boost/thread/mutex.hpp>
17-
1815
template <typename T>
1916
class CCheckQueueControl;
2017

@@ -33,58 +30,58 @@ class CCheckQueue
3330
{
3431
private:
3532
//! Mutex to protect the inner state
36-
boost::mutex mutex;
33+
Mutex m_mutex;
3734

3835
//! Worker threads block on this when out of work
39-
boost::condition_variable condWorker;
36+
std::condition_variable m_worker_cv;
4037

4138
//! Master thread blocks on this when out of work
42-
boost::condition_variable condMaster;
39+
std::condition_variable m_master_cv;
4340

4441
//! The queue of elements to be processed.
4542
//! As the order of booleans doesn't matter, it is used as a LIFO (stack)
46-
std::vector<T> queue;
43+
std::vector<T> queue GUARDED_BY(m_mutex);
4744

4845
//! The number of workers (including the master) that are idle.
49-
int nIdle{0};
46+
int nIdle GUARDED_BY(m_mutex){0};
5047

5148
//! The total number of workers (including the master).
52-
int nTotal{0};
49+
int nTotal GUARDED_BY(m_mutex){0};
5350

5451
//! The temporary evaluation result.
55-
bool fAllOk{true};
52+
bool fAllOk GUARDED_BY(m_mutex){true};
5653

5754
/**
5855
* Number of verifications that haven't completed yet.
5956
* This includes elements that are no longer queued, but still in the
6057
* worker's own batches.
6158
*/
62-
unsigned int nTodo{0};
59+
unsigned int nTodo GUARDED_BY(m_mutex){0};
6360

6461
//! The maximum number of elements to be processed in one batch
6562
const unsigned int nBatchSize;
6663

6764
std::vector<std::thread> m_worker_threads;
68-
bool m_request_stop{false};
65+
bool m_request_stop GUARDED_BY(m_mutex){false};
6966

7067
/** Internal function that does bulk of the verification work. */
7168
bool Loop(bool fMaster)
7269
{
73-
boost::condition_variable& cond = fMaster ? condMaster : condWorker;
70+
std::condition_variable& cond = fMaster ? m_master_cv : m_worker_cv;
7471
std::vector<T> vChecks;
7572
vChecks.reserve(nBatchSize);
7673
unsigned int nNow = 0;
7774
bool fOk = true;
7875
do {
7976
{
80-
boost::unique_lock<boost::mutex> lock(mutex);
77+
WAIT_LOCK(m_mutex, lock);
8178
// first do the clean-up of the previous loop run (allowing us to do it in the same critsect)
8279
if (nNow) {
8380
fAllOk &= fOk;
8481
nTodo -= nNow;
8582
if (nTodo == 0 && !fMaster)
8683
// We processed the last element; inform the master it can exit and return the result
87-
condMaster.notify_one();
84+
m_master_cv.notify_one();
8885
} else {
8986
// first iteration
9087
nTotal++;
@@ -115,7 +112,7 @@ class CCheckQueue
115112
nNow = std::max(1U, std::min(nBatchSize, (unsigned int)queue.size() / (nTotal + nIdle + 1)));
116113
vChecks.resize(nNow);
117114
for (unsigned int i = 0; i < nNow; i++) {
118-
// We want the lock on the mutex to be as short as possible, so swap jobs from the global
115+
// We want the lock on the m_mutex to be as short as possible, so swap jobs from the global
119116
// queue to the local batch vector instead of copying.
120117
vChecks[i].swap(queue.back());
121118
queue.pop_back();
@@ -133,7 +130,7 @@ class CCheckQueue
133130

134131
public:
135132
//! Mutex to ensure only one concurrent CCheckQueueControl
136-
boost::mutex ControlMutex;
133+
Mutex m_control_mutex;
137134

138135
//! Create a new check queue
139136
explicit CCheckQueue(unsigned int nBatchSizeIn)
@@ -145,7 +142,7 @@ class CCheckQueue
145142
void StartWorkerThreads(const int threads_num)
146143
{
147144
{
148-
boost::unique_lock<boost::mutex> lock(mutex);
145+
LOCK(m_mutex);
149146
nIdle = 0;
150147
nTotal = 0;
151148
fAllOk = true;
@@ -168,32 +165,28 @@ class CCheckQueue
168165
//! Add a batch of checks to the queue
169166
void Add(std::vector<T>& vChecks)
170167
{
171-
boost::unique_lock<boost::mutex> lock(mutex);
168+
LOCK(m_mutex);
172169
for (T& check : vChecks) {
173170
queue.push_back(T());
174171
check.swap(queue.back());
175172
}
176173
nTodo += vChecks.size();
177174
if (vChecks.size() == 1)
178-
condWorker.notify_one();
175+
m_worker_cv.notify_one();
179176
else if (vChecks.size() > 1)
180-
condWorker.notify_all();
177+
m_worker_cv.notify_all();
181178
}
182179

183180
//! Stop all of the worker threads.
184181
void StopWorkerThreads()
185182
{
186-
{
187-
boost::unique_lock<boost::mutex> lock(mutex);
188-
m_request_stop = true;
189-
}
190-
condWorker.notify_all();
183+
WITH_LOCK(m_mutex, m_request_stop = true);
184+
m_worker_cv.notify_all();
191185
for (std::thread& t : m_worker_threads) {
192186
t.join();
193187
}
194188
m_worker_threads.clear();
195-
boost::unique_lock<boost::mutex> lock(mutex);
196-
m_request_stop = false;
189+
WITH_LOCK(m_mutex, m_request_stop = false);
197190
}
198191

199192
~CCheckQueue()
@@ -222,7 +215,7 @@ class CCheckQueueControl
222215
{
223216
// passed queue is supposed to be unused, or nullptr
224217
if (pqueue != nullptr) {
225-
ENTER_CRITICAL_SECTION(pqueue->ControlMutex);
218+
ENTER_CRITICAL_SECTION(pqueue->m_control_mutex);
226219
}
227220
}
228221

@@ -246,7 +239,7 @@ class CCheckQueueControl
246239
if (!fDone)
247240
Wait();
248241
if (pqueue != nullptr) {
249-
LEAVE_CRITICAL_SECTION(pqueue->ControlMutex);
242+
LEAVE_CRITICAL_SECTION(pqueue->m_control_mutex);
250243
}
251244
}
252245
};

src/test/checkqueue_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
342342
}
343343
// Try to get control of the queue a bunch of times
344344
for (auto x = 0; x < 100 && !fails; ++x) {
345-
fails = queue->ControlMutex.try_lock();
345+
fails = queue->m_control_mutex.try_lock();
346346
}
347347
{
348348
// Unfreeze (we need lock n case of spurious wakeup)
@@ -405,7 +405,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks)
405405
cv.wait(l, [&](){return has_lock;});
406406
bool fails = false;
407407
for (auto x = 0; x < 100 && !fails; ++x) {
408-
fails = queue->ControlMutex.try_lock();
408+
fails = queue->m_control_mutex.try_lock();
409409
}
410410
has_tried = true;
411411
cv.notify_one();

test/lint/lint-includes.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ EXPECTED_BOOST_INCLUDES=(
6969
boost/signals2/signal.hpp
7070
boost/test/unit_test.hpp
7171
boost/thread/condition_variable.hpp
72-
boost/thread/mutex.hpp
7372
boost/thread/shared_mutex.hpp
7473
boost/thread/thread.hpp
7574
boost/variant.hpp

0 commit comments

Comments
 (0)