5#ifndef BITCOIN_CHECKQUEUE_H
6#define BITCOIN_CHECKQUEUE_H
74 std::condition_variable& cond =
fMaster ? m_master_cv : m_worker_cv;
77 unsigned int nNow = 0;
91 m_master_cv.notify_one();
101 std::optional<R>
to_return = std::move(m_result);
103 m_result = std::nullopt;
126 do_work = !m_result.has_value();
147 LogInfo(
"Script verification uses %d additional threads", worker_threads_num);
148 m_worker_threads.reserve(worker_threads_num);
149 for (
int n = 0; n < worker_threads_num; ++n) {
150 m_worker_threads.emplace_back([
this, n]() {
185 m_worker_cv.notify_one();
187 m_worker_cv.notify_all();
194 m_worker_cv.notify_all();
195 for (std::thread&
t : m_worker_threads) {
200 bool HasThreads()
const {
return !m_worker_threads.empty(); }
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
CCheckQueueControl & operator=(const CCheckQueueControl &)=delete
CCheckQueueControl(const CCheckQueueControl &)=delete
CCheckQueueControl()=delete
~CCheckQueueControl() UNLOCK_FUNCTION()
UniqueLock< Mutex > m_lock
CCheckQueue< T, R > & m_queue
CCheckQueueControl(CCheckQueue< T > &queueIn) EXCLUSIVE_LOCK_FUNCTION(queueIn.m_control_mutex)
std::optional< R > Complete()
void Add(std::vector< T > &&vChecks)
Queue for verifications that have to be performed.
bool m_request_stop GUARDED_BY(m_mutex)
std::optional< R > Complete() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Join the execution until completion.
unsigned int nTodo GUARDED_BY(m_mutex)
Number of verifications that haven't completed yet.
Mutex m_control_mutex
Mutex to ensure only one concurrent CCheckQueueControl.
std::vector< T > queue GUARDED_BY(m_mutex)
The queue of elements to be processed.
CCheckQueue(const CCheckQueue &)=delete
void Add(std::vector< T > &&vChecks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Add a batch of checks to the queue.
int nIdle GUARDED_BY(m_mutex)
The number of workers (including the master) that are idle.
CCheckQueue & operator=(CCheckQueue &&)=delete
int nTotal GUARDED_BY(m_mutex)
The total number of workers (including the master).
CCheckQueue(unsigned int batch_size, int worker_threads_num)
Create a new check queue.
Mutex m_mutex
Mutex to protect the inner state.
std::optional< R > Loop(bool fMaster) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Internal function that does bulk of the verification work.
std::condition_variable m_worker_cv
Worker threads block on this when out of work.
std::vector< std::thread > m_worker_threads
const unsigned int nBatchSize
The maximum number of elements to be processed in one batch.
std::optional< R > m_result GUARDED_BY(m_mutex)
The temporary evaluation result.
CCheckQueue(CCheckQueue &&)=delete
std::condition_variable m_master_cv
Master thread blocks on this when out of work.
CCheckQueue & operator=(const CCheckQueue &)=delete
Wrapper around std::unique_lock style lock for MutexType.
#define T(expected, seed, data)
void ThreadRename(const std::string &)
Rename a thread both in terms of an internal (in-memory) name as well as its system thread name.
#define WAIT_LOCK(cs, name)
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define EXCLUSIVE_LOCK_FUNCTION(...)
#define UNLOCK_FUNCTION(...)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.