MADNESS 0.10.1
madness::DQueue< T > Class Template Reference

A thread safe, fast but simple doubled-ended queue. More...

#include <dqueue.h>

Inheritance diagram for madness::DQueue< T >:
Collaboration diagram for madness::DQueue< T >:

Public Member Functions

 DQueue (size_t hint=200000)
virtual ~DQueue ()
bool empty () const
const DQStats & get_stats () const
void lock_and_flush_prebuf ()
std::pair< T, bool > pop_front (bool wait)
 Pop value off the front of queue.
int pop_front (int nmax, T *r, bool wait)
 Pop multiple values off the front of queue ... returns number popped ... might be zero.
void push_back (const T &value, int ncopy=1)
 Insert element at back of queue (default is just one copy).
void push_front (const T &value)
 Insert value at front of queue.
template<typename opT>
void scan (opT &op)
void set_wait_policy (WaitPolicy p, int us=0)
size_t size () const

Private Member Functions

size_t n __attribute__ ((aligned(64)))
 Number of elements in the buffer.
void flush_prebuf ()
void flush_prebufhi ()
void flush_prebuflo ()
void grow ()
void push_back_with_lock (const T &value)
void push_front_with_lock (const T &value)
void sanity_check () const
Private Member Functions inherited from madness::PthreadConditionVariable
 PthreadConditionVariable ()
virtual ~PthreadConditionVariable ()
void broadcast () const
pthread_mutex_t & get_pthread_mutex ()
void lock () const
void signal () const
void unlock () const
void wait () const
 You should have acquired the mutex before entering here.

Private Attributes

int _back
 Index of element at back of buffer.
int _front
 Index of element at front of buffer.
T * buf
 Actual buffer.
char pad [64]
 To put the lock and the data in separate cache lines.
DQStats stats
size_t sz
 Current capacity.

Detailed Description

template<typename T>
class madness::DQueue< T >

A thread safe, fast but simple doubled-ended queue.

Since the point is speed, the implementation is a circular buffer rather than a linked list so as to avoid the new/del overhead. It will grow as needed, but presently will not shrink. Had to modify STL API to make things thread safe.

It is now rather heavily specialized to its only use.

Constructor & Destructor Documentation

◆ DQueue()

template<typename T>
madness::DQueue< T >::DQueue ( size_t hint = 200000)
inline

References _back, _front, buf, and sz.

◆ ~DQueue()

template<typename T>
virtual madness::DQueue< T >::~DQueue ( )
inlinevirtual

References buf.

Member Function Documentation

◆ __attribute__()

template<typename T>
size_t n madness::DQueue< T >::__attribute__ ( (aligned(64)) )
private

Number of elements in the buffer.

◆ empty()

template<typename T>
bool madness::DQueue< T >::empty ( ) const

◆ flush_prebuf()

template<typename T>
void madness::DQueue< T >::flush_prebuf ( )
inlineprivate

◆ flush_prebufhi()

template<typename T>
void madness::DQueue< T >::flush_prebufhi ( )
inlineprivate

References push_front_with_lock().

Referenced by flush_prebuf(), and push_front().

◆ flush_prebuflo()

template<typename T>
void madness::DQueue< T >::flush_prebuflo ( )
inlineprivate

References push_back_with_lock().

Referenced by flush_prebuf().

◆ get_stats()

template<typename T>
const DQStats & madness::DQueue< T >::get_stats ( ) const
inline

References stats.

◆ grow()

template<typename T>
void madness::DQueue< T >::grow ( )
inlineprivate

◆ lock_and_flush_prebuf()

template<typename T>
void madness::DQueue< T >::lock_and_flush_prebuf ( )

References flush_prebuf(), and MADNESS_ASSERT.

◆ pop_front() [1/2]

template<typename T>
std::pair< T, bool > madness::DQueue< T >::pop_front ( bool wait)
inline

Pop value off the front of queue.

References pop_front(), and madness::PthreadConditionVariable::wait().

◆ pop_front() [2/2]

template<typename T>
int madness::DQueue< T >::pop_front ( int nmax,
T * r,
bool wait )
inline

Pop multiple values off the front of queue ... returns number popped ... might be zero.

r must refer to an array of dimension at least nmax ... you are presently given no more than max(size()/64,1) values ... arbitrary choice.

multi-threaded tasks might cause fewer tasks to be taken

References _front, buf, madness::f, flush_prebuf(), stats, sz, and madness::PthreadConditionVariable::wait().

Referenced by pop_front().

◆ push_back()

template<typename T>
void madness::DQueue< T >::push_back ( const T & value,
int ncopy = 1 )

Insert element at back of queue (default is just one copy).

References flush_prebuf(), madness::Future< void >::value, madness::is_madness_thread(), MADNESS_ASSERT, and push_back_with_lock().

◆ push_back_with_lock()

template<typename T>
void madness::DQueue< T >::push_back_with_lock ( const T & value)
inlineprivate

◆ push_front()

template<typename T>
void madness::DQueue< T >::push_front ( const T & value)

◆ push_front_with_lock()

template<typename T>
void madness::DQueue< T >::push_front_with_lock ( const T & value)
inlineprivate

◆ sanity_check()

template<typename T>
void madness::DQueue< T >::sanity_check ( ) const
inlineprivate

References _back, _front, MADNESS_ASSERT, and sz.

◆ scan()

template<typename T>
template<typename opT>
void madness::DQueue< T >::scan ( opT & op)
inline

References _front, buf, madness::f, op(), p(), size(), and sz.

◆ set_wait_policy()

template<typename T>
void madness::DQueue< T >::set_wait_policy ( WaitPolicy p,
int us = 0 )
inline

◆ size()

template<typename T>
size_t madness::DQueue< T >::size ( ) const
inline

Referenced by scan().

Member Data Documentation

◆ _back

template<typename T>
int madness::DQueue< T >::_back
private

Index of element at back of buffer.

Referenced by DQueue(), grow(), push_back_with_lock(), and sanity_check().

◆ _front

template<typename T>
int madness::DQueue< T >::_front
private

Index of element at front of buffer.

Referenced by DQueue(), grow(), pop_front(), push_front_with_lock(), sanity_check(), and scan().

◆ buf

template<typename T>
T* madness::DQueue< T >::buf
private

◆ pad

template<typename T>
char madness::DQueue< T >::pad[64]
private

To put the lock and the data in separate cache lines.

◆ stats

template<typename T>
DQStats madness::DQueue< T >::stats
private

◆ sz

template<typename T>
size_t madness::DQueue< T >::sz
private

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