Bitcoin Core 28.99.0
P2P Digital Currency
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
CCheckQueue< T, R > Class Template Reference

Queue for verifications that have to be performed. More...

#include <checkqueue.h>

Collaboration diagram for CCheckQueue< T, R >:
[legend]

Public Member Functions

 CCheckQueue (unsigned int batch_size, int worker_threads_num)
 Create a new check queue. More...
 
 CCheckQueue (const CCheckQueue &)=delete
 
CCheckQueueoperator= (const CCheckQueue &)=delete
 
 CCheckQueue (CCheckQueue &&)=delete
 
CCheckQueueoperator= (CCheckQueue &&)=delete
 
std::optional< R > Complete () EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
 Join the execution until completion. More...
 
void Add (std::vector< T > &&vChecks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
 Add a batch of checks to the queue. More...
 
 ~CCheckQueue ()
 
bool HasThreads () const
 

Public Attributes

Mutex m_control_mutex
 Mutex to ensure only one concurrent CCheckQueueControl. More...
 

Private Member Functions

std::vector< T > queue GUARDED_BY (m_mutex)
 The queue of elements to be processed. More...
 
int nIdle GUARDED_BY (m_mutex)
 The number of workers (including the master) that are idle. More...
 
int nTotal GUARDED_BY (m_mutex)
 The total number of workers (including the master). More...
 
std::optional< R > m_result GUARDED_BY (m_mutex)
 The temporary evaluation result. More...
 
unsigned int nTodo GUARDED_BY (m_mutex)
 Number of verifications that haven't completed yet. More...
 
bool m_request_stop GUARDED_BY (m_mutex)
 
std::optional< R > Loop (bool fMaster) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
 Internal function that does bulk of the verification work. More...
 

Private Attributes

Mutex m_mutex
 Mutex to protect the inner state. More...
 
std::condition_variable m_worker_cv
 Worker threads block on this when out of work. More...
 
std::condition_variable m_master_cv
 Master thread blocks on this when out of work. More...
 
const unsigned int nBatchSize
 The maximum number of elements to be processed in one batch. More...
 
std::vector< std::thread > m_worker_threads
 

Detailed Description

template<typename T, typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
class CCheckQueue< T, R >

Queue for verifications that have to be performed.

The verifications are represented by a type T, which must provide an operator(), returning an std::optional<R>.

The overall result of the computation is std::nullopt if all invocations return std::nullopt, or one of the other results otherwise.

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.

Definition at line 33 of file checkqueue.h.

Constructor & Destructor Documentation

◆ CCheckQueue() [1/3]

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
CCheckQueue< T, R >::CCheckQueue ( unsigned int  batch_size,
int  worker_threads_num 
)
inlineexplicit

Create a new check queue.

Definition at line 144 of file checkqueue.h.

Here is the call graph for this function:

◆ CCheckQueue() [2/3]

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
CCheckQueue< T, R >::CCheckQueue ( const CCheckQueue< T, R > &  )
delete

◆ CCheckQueue() [3/3]

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
CCheckQueue< T, R >::CCheckQueue ( CCheckQueue< T, R > &&  )
delete

◆ ~CCheckQueue()

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
CCheckQueue< T, R >::~CCheckQueue ( )
inline

Definition at line 191 of file checkqueue.h.

Member Function Documentation

◆ Add()

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
void CCheckQueue< T, R >::Add ( std::vector< T > &&  vChecks)
inline

Add a batch of checks to the queue.

Definition at line 172 of file checkqueue.h.

Here is the caller graph for this function:

◆ Complete()

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
std::optional< R > CCheckQueue< T, R >::Complete ( )
inline

Join the execution until completion.

If at least one evaluation wasn't successful, return its error.

Definition at line 166 of file checkqueue.h.

Here is the caller graph for this function:

◆ GUARDED_BY() [1/6]

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
std::vector< T > queue CCheckQueue< T, R >::GUARDED_BY ( m_mutex  )
private

The queue of elements to be processed.

As the order of booleans doesn't matter, it is used as a LIFO (stack)

◆ GUARDED_BY() [2/6]

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
int nIdle CCheckQueue< T, R >::GUARDED_BY ( m_mutex  )
inlineprivate

The number of workers (including the master) that are idle.

Definition at line 50 of file checkqueue.h.

◆ GUARDED_BY() [3/6]

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
int nTotal CCheckQueue< T, R >::GUARDED_BY ( m_mutex  )
inlineprivate

The total number of workers (including the master).

Definition at line 53 of file checkqueue.h.

◆ GUARDED_BY() [4/6]

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
std::optional< R > m_result CCheckQueue< T, R >::GUARDED_BY ( m_mutex  )
private

The temporary evaluation result.

◆ GUARDED_BY() [5/6]

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
unsigned int nTodo CCheckQueue< T, R >::GUARDED_BY ( m_mutex  )
inlineprivate

Number of verifications that haven't completed yet.

This includes elements that are no longer queued, but still in the worker's own batches.

Definition at line 63 of file checkqueue.h.

◆ GUARDED_BY() [6/6]

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
bool m_request_stop CCheckQueue< T, R >::GUARDED_BY ( m_mutex  )
inlineprivate

Definition at line 69 of file checkqueue.h.

◆ HasThreads()

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
bool CCheckQueue< T, R >::HasThreads ( ) const
inline

Definition at line 200 of file checkqueue.h.

◆ Loop()

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
std::optional< R > CCheckQueue< T, R >::Loop ( bool  fMaster)
inlineprivate

Internal function that does bulk of the verification work.

If fMaster, return the final result.

Definition at line 72 of file checkqueue.h.

◆ operator=() [1/2]

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
CCheckQueue & CCheckQueue< T, R >::operator= ( CCheckQueue< T, R > &&  )
delete

◆ operator=() [2/2]

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
CCheckQueue & CCheckQueue< T, R >::operator= ( const CCheckQueue< T, R > &  )
delete

Member Data Documentation

◆ m_control_mutex

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
Mutex CCheckQueue< T, R >::m_control_mutex

Mutex to ensure only one concurrent CCheckQueueControl.

Definition at line 141 of file checkqueue.h.

◆ m_master_cv

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
std::condition_variable CCheckQueue< T, R >::m_master_cv
private

Master thread blocks on this when out of work.

Definition at line 43 of file checkqueue.h.

◆ m_mutex

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
Mutex CCheckQueue< T, R >::m_mutex
private

Mutex to protect the inner state.

Definition at line 37 of file checkqueue.h.

◆ m_worker_cv

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
std::condition_variable CCheckQueue< T, R >::m_worker_cv
private

Worker threads block on this when out of work.

Definition at line 40 of file checkqueue.h.

◆ m_worker_threads

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
std::vector<std::thread> CCheckQueue< T, R >::m_worker_threads
private

Definition at line 68 of file checkqueue.h.

◆ nBatchSize

template<typename T , typename R = std::remove_cvref_t<decltype(std::declval<T>()().value())>>
const unsigned int CCheckQueue< T, R >::nBatchSize
private

The maximum number of elements to be processed in one batch.

Definition at line 66 of file checkqueue.h.


The documentation for this class was generated from the following file: