proxygen
folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom > Class Template Reference

#include <DynamicBoundedQueue.h>

Public Member Functions

 DynamicBoundedQueue (Weight capacity)
 
 ~DynamicBoundedQueue ()
 
FOLLY_ALWAYS_INLINE void enqueue (const T &v)
 Enqueue functions. More...
 
FOLLY_ALWAYS_INLINE void enqueue (T &&v)
 
FOLLY_ALWAYS_INLINE bool try_enqueue (const T &v)
 
FOLLY_ALWAYS_INLINE bool try_enqueue (T &&v)
 
template<typename Clock , typename Duration >
FOLLY_ALWAYS_INLINE bool try_enqueue_until (const T &v, const std::chrono::time_point< Clock, Duration > &deadline)
 
template<typename Clock , typename Duration >
FOLLY_ALWAYS_INLINE bool try_enqueue_until (T &&v, const std::chrono::time_point< Clock, Duration > &deadline)
 
template<typename Rep , typename Period >
FOLLY_ALWAYS_INLINE bool try_enqueue_for (const T &v, const std::chrono::duration< Rep, Period > &duration)
 
template<typename Rep , typename Period >
FOLLY_ALWAYS_INLINE bool try_enqueue_for (T &&v, const std::chrono::duration< Rep, Period > &duration)
 
FOLLY_ALWAYS_INLINE void dequeue (T &elem)
 Dequeue functions. More...
 
FOLLY_ALWAYS_INLINE bool try_dequeue (T &elem)
 
template<typename Clock , typename Duration >
FOLLY_ALWAYS_INLINE bool try_dequeue_until (T &elem, const std::chrono::time_point< Clock, Duration > &deadline)
 
template<typename Rep , typename Period >
FOLLY_ALWAYS_INLINE bool try_dequeue_for (T &elem, const std::chrono::duration< Rep, Period > &duration)
 
void reset_capacity (Weight capacity) noexcept
 Secondary functions. More...
 
Weight weight () const noexcept
 
size_t size () const noexcept
 
bool empty () const noexcept
 

Private Types

enum  WaitingState : uint32_t { NOTWAITING = 0, WAITING = 1 }
 
using Weight = uint64_t
 

Private Member Functions

constexpr Weight threshold (Weight capacity) const noexcept
 Private functions ///. More...
 
template<typename Arg >
FOLLY_ALWAYS_INLINE void enqueueImpl (Arg &&v)
 
template<typename Arg >
FOLLY_ALWAYS_INLINE bool tryEnqueueImpl (Arg &&v)
 
template<typename Clock , typename Duration , typename Arg >
FOLLY_ALWAYS_INLINE bool tryEnqueueUntilImpl (Arg &&v, const std::chrono::time_point< Clock, Duration > &deadline)
 
template<typename Rep , typename Period , typename Arg >
FOLLY_ALWAYS_INLINE bool tryEnqueueForImpl (Arg &&v, const std::chrono::duration< Rep, Period > &duration)
 
FOLLY_ALWAYS_INLINE bool tryAddDebit (Weight weight) noexcept
 
FOLLY_ALWAYS_INLINE Weight getCapacity () const noexcept
 
FOLLY_ALWAYS_INLINE Weight fetchAddDebit (Weight weight) noexcept
 
FOLLY_ALWAYS_INLINE Weight getDebit () const noexcept
 
FOLLY_ALWAYS_INLINE void addCredit (Weight weight) noexcept
 
FOLLY_ALWAYS_INLINE Weight fetchAddCredit (Weight weight) noexcept
 
FOLLY_ALWAYS_INLINE Weight getCredit () const noexcept
 
FOLLY_ALWAYS_INLINE Weight getThreshold () const noexcept
 
void subDebit (Weight weight) noexcept
 
template<typename Clock , typename Duration , typename Arg >
bool tryEnqueueUntilSlow (Arg &&v, const std::chrono::time_point< Clock, Duration > &deadline)
 
template<typename Clock , typename Duration >
bool canEnqueue (const std::chrono::time_point< Clock, Duration > &deadline, Weight weight) noexcept
 
bool canBlock (Weight weight, Weight capacity) noexcept
 
bool tryReduceDebit () noexcept
 
Weight takeTransfer () noexcept
 
Weight getTransfer () const noexcept
 
void transferCredit () noexcept
 
Weight takeCredit () noexcept
 

Private Attributes

Atom< Weightdebit_
 Data members. More...
 
Atom< Weightcapacity_
 
Atom< Weightcredit_
 
Atom< Weightthreshold_
 
Atom< Weighttransfer_
 
detail::Futex< Atomwaiting_
 
UnboundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, Atomq_
 

Static Private Attributes

static constexpr bool SPSC = SingleProducer && SingleConsumer
 
static constexpr size_t Align = 1u << LgAlign
 

Detailed Description

template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
class folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >

Definition at line 287 of file DynamicBoundedQueue.h.

Member Typedef Documentation

template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
using folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::Weight = uint64_t
private

Definition at line 288 of file DynamicBoundedQueue.h.

Member Enumeration Documentation

template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
enum folly::DynamicBoundedQueue::WaitingState : uint32_t
private
Enumerator
NOTWAITING 
WAITING 

Definition at line 290 of file DynamicBoundedQueue.h.

Constructor & Destructor Documentation

template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::DynamicBoundedQueue ( Weight  capacity)
inlineexplicit

constructor

Definition at line 328 of file DynamicBoundedQueue.h.

329  : debit_(0),
330  capacity_(capacity + threshold(capacity)), // capacity slack
331  credit_(0),
332  threshold_(threshold(capacity)),
333  transfer_(0),
334  waiting_(0) {}
Atom< Weight > debit_
Data members.
constexpr Weight threshold(Weight capacity) const noexcept
Private functions ///.
detail::Futex< Atom > waiting_
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::~DynamicBoundedQueue ( )
inline

destructor

Definition at line 337 of file DynamicBoundedQueue.h.

337 {}

Member Function Documentation

template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
FOLLY_ALWAYS_INLINE void folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::addCredit ( Weight  weight)
inlineprivatenoexcept

Definition at line 535 of file DynamicBoundedQueue.h.

535  {
536  Weight before = fetchAddCredit(weight);
537  Weight thresh = getThreshold();
538  if (before + weight >= thresh && before < thresh) {
539  transferCredit();
540  }
541  }
Weight weight() const noexcept
FOLLY_ALWAYS_INLINE Weight fetchAddCredit(Weight weight) noexcept
FOLLY_ALWAYS_INLINE Weight getThreshold() const noexcept
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::canBlock ( Weight  weight,
Weight  capacity 
)
inlineprivatenoexcept

Definition at line 612 of file DynamicBoundedQueue.h.

612  {
613  waiting_.store(WAITING, std::memory_order_relaxed);
614  std::atomic_thread_fence(std::memory_order_seq_cst);
615  tryReduceDebit();
616  Weight debit = getDebit();
617  return debit + weight > capacity;
618  }
Weight weight() const noexcept
FOLLY_ALWAYS_INLINE Weight getDebit() const noexcept
detail::Futex< Atom > waiting_
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
template<typename Clock , typename Duration >
bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::canEnqueue ( const std::chrono::time_point< Clock, Duration > &  deadline,
Weight  weight 
)
inlineprivatenoexcept

Definition at line 589 of file DynamicBoundedQueue.h.

References folly::asm_volatile_pause(), folly::detail::futexWaitUntil(), max, and now().

591  {
592  Weight capacity = getCapacity();
593  while (true) {
594  tryReduceDebit();
595  Weight debit = getDebit();
596  if ((debit + weight <= capacity) && tryAddDebit(weight)) {
597  return true;
598  }
599  if (deadline < Clock::time_point::max() && Clock::now() >= deadline) {
600  return false;
601  }
602  if (MayBlock) {
603  if (canBlock(weight, capacity)) {
605  }
606  } else {
608  }
609  }
610  }
Weight weight() const noexcept
LogLevel max
Definition: LogLevel.cpp:31
std::chrono::steady_clock::time_point now()
FOLLY_ALWAYS_INLINE Weight getCapacity() const noexcept
bool canBlock(Weight weight, Weight capacity) noexcept
FOLLY_ALWAYS_INLINE Weight getDebit() const noexcept
FutexResult futexWaitUntil(const Futex *futex, uint32_t expected, std::chrono::time_point< Clock, Duration > const &deadline, uint32_t waitMask)
Definition: Futex-inl.h:112
detail::Futex< Atom > waiting_
FOLLY_ALWAYS_INLINE bool tryAddDebit(Weight weight) noexcept
void asm_volatile_pause()
Definition: Asm.h:37
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
FOLLY_ALWAYS_INLINE void folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::dequeue ( T elem)
inline

Dequeue functions.

dequeue

Definition at line 392 of file DynamicBoundedQueue.h.

Referenced by enq_deq_test().

392  {
393  q_.dequeue(elem);
394  addCredit(WeightFn()(elem));
395  }
FOLLY_ALWAYS_INLINE void addCredit(Weight weight) noexcept
UnboundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, Atom > q_
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::empty ( ) const
inlinenoexcept

empty

Definition at line 453 of file DynamicBoundedQueue.h.

453  {
454  return q_.empty();
455  }
UnboundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, Atom > q_
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
FOLLY_ALWAYS_INLINE void folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::enqueue ( const T v)
inline

Enqueue functions.

enqueue

Definition at line 342 of file DynamicBoundedQueue.h.

Referenced by enq_deq_test().

342  {
343  enqueueImpl(v);
344  }
FOLLY_ALWAYS_INLINE void enqueueImpl(Arg &&v)
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
FOLLY_ALWAYS_INLINE void folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::enqueue ( T &&  v)
inline

Definition at line 346 of file DynamicBoundedQueue.h.

References folly::gen::move.

346  {
348  }
FOLLY_ALWAYS_INLINE void enqueueImpl(Arg &&v)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
template<typename Arg >
FOLLY_ALWAYS_INLINE void folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::enqueueImpl ( Arg &&  v)
inlineprivate

Definition at line 469 of file DynamicBoundedQueue.h.

References max.

469  {
471  std::forward<Arg>(v), std::chrono::steady_clock::time_point::max());
472  }
LogLevel max
Definition: LogLevel.cpp:31
FOLLY_ALWAYS_INLINE bool tryEnqueueUntilImpl(Arg &&v, const std::chrono::time_point< Clock, Duration > &deadline)
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
FOLLY_ALWAYS_INLINE Weight folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::fetchAddCredit ( Weight  weight)
inlineprivatenoexcept

Definition at line 543 of file DynamicBoundedQueue.h.

543  {
544  Weight before;
545  if (SingleConsumer) {
546  before = getCredit();
547  credit_.store(before + weight, std::memory_order_relaxed);
548  } else {
549  before = credit_.fetch_add(weight, std::memory_order_acq_rel);
550  }
551  return before;
552  }
Weight weight() const noexcept
FOLLY_ALWAYS_INLINE Weight getCredit() const noexcept
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
FOLLY_ALWAYS_INLINE Weight folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::fetchAddDebit ( Weight  weight)
inlineprivatenoexcept

Definition at line 518 of file DynamicBoundedQueue.h.

518  {
519  Weight before;
520  if (SingleProducer) {
521  before = getDebit();
522  debit_.store(before + weight, std::memory_order_relaxed);
523  } else {
524  before = debit_.fetch_add(weight, std::memory_order_acq_rel);
525  }
526  return before;
527  }
Weight weight() const noexcept
Atom< Weight > debit_
Data members.
FOLLY_ALWAYS_INLINE Weight getDebit() const noexcept
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
FOLLY_ALWAYS_INLINE Weight folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::getCapacity ( ) const
inlineprivatenoexcept

Definition at line 514 of file DynamicBoundedQueue.h.

514  {
515  return capacity_.load(std::memory_order_acquire);
516  }
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
FOLLY_ALWAYS_INLINE Weight folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::getCredit ( ) const
inlineprivatenoexcept

Definition at line 554 of file DynamicBoundedQueue.h.

554  {
555  return credit_.load(std::memory_order_acquire);
556  }
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
FOLLY_ALWAYS_INLINE Weight folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::getDebit ( ) const
inlineprivatenoexcept

Definition at line 529 of file DynamicBoundedQueue.h.

529  {
530  return debit_.load(std::memory_order_acquire);
531  }
Atom< Weight > debit_
Data members.
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
FOLLY_ALWAYS_INLINE Weight folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::getThreshold ( ) const
inlineprivatenoexcept

Definition at line 558 of file DynamicBoundedQueue.h.

558  {
559  return threshold_.load(std::memory_order_acquire);
560  }
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
Weight folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::getTransfer ( ) const
inlineprivatenoexcept

Definition at line 636 of file DynamicBoundedQueue.h.

636  {
637  return transfer_.load(std::memory_order_acquire);
638  }
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
void folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::reset_capacity ( Weight  capacity)
inlinenoexcept

Secondary functions.

reset_capacity

Definition at line 433 of file DynamicBoundedQueue.h.

433  {
434  Weight thresh = threshold(capacity);
435  capacity_.store(capacity + thresh, std::memory_order_release);
436  threshold_.store(thresh, std::memory_order_release);
437  }
constexpr Weight threshold(Weight capacity) const noexcept
Private functions ///.
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
size_t folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::size ( ) const
inlinenoexcept

size

Definition at line 448 of file DynamicBoundedQueue.h.

448  {
449  return q_.size();
450  }
UnboundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, Atom > q_
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
void folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::subDebit ( Weight  weight)
inlineprivatenoexcept

Functions called infrequently by producers

Definition at line 564 of file DynamicBoundedQueue.h.

564  {
565  Weight before;
566  if (SingleProducer) {
567  before = getDebit();
568  debit_.store(before - weight, std::memory_order_relaxed);
569  } else {
570  before = debit_.fetch_sub(weight, std::memory_order_acq_rel);
571  }
572  DCHECK_GE(before, weight);
573  }
Weight weight() const noexcept
Atom< Weight > debit_
Data members.
FOLLY_ALWAYS_INLINE Weight getDebit() const noexcept
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
Weight folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::takeCredit ( )
inlineprivatenoexcept

Definition at line 652 of file DynamicBoundedQueue.h.

References folly::T.

652  {
653  Weight credit;
654  if (SingleConsumer) {
655  credit = credit_.load(std::memory_order_relaxed);
656  credit_.store(0, std::memory_order_relaxed);
657  } else {
658  credit = credit_.exchange(0, std::memory_order_acq_rel);
659  }
660  return credit;
661  }
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
Weight folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::takeTransfer ( )
inlineprivatenoexcept

Definition at line 628 of file DynamicBoundedQueue.h.

628  {
629  Weight w = getTransfer();
630  if (w > 0) {
631  w = transfer_.exchange(0, std::memory_order_acq_rel);
632  }
633  return w;
634  }
Weight getTransfer() const noexcept
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
constexpr Weight folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::threshold ( Weight  capacity) const
inlineprivatenoexcept

Private functions ///.

Definition at line 462 of file DynamicBoundedQueue.h.

462  {
463  return capacity / 10;
464  }
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
void folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::transferCredit ( )
inlineprivatenoexcept

Functions called infrequently by consumers

Definition at line 642 of file DynamicBoundedQueue.h.

References folly::detail::futexWake().

642  {
643  Weight credit = takeCredit();
644  transfer_.fetch_add(credit, std::memory_order_acq_rel);
645  if (MayBlock) {
646  std::atomic_thread_fence(std::memory_order_seq_cst);
647  waiting_.store(NOTWAITING, std::memory_order_relaxed);
649  }
650  }
detail::Futex< Atom > waiting_
int futexWake(const Futex *futex, int count, uint32_t wakeMask)
Definition: Futex-inl.h:107
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
FOLLY_ALWAYS_INLINE bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::try_dequeue ( T elem)
inline

try_dequeue

Definition at line 398 of file DynamicBoundedQueue.h.

Referenced by enq_deq_test().

398  {
399  if (q_.try_dequeue(elem)) {
400  addCredit(WeightFn()(elem));
401  return true;
402  }
403  return false;
404  }
FOLLY_ALWAYS_INLINE void addCredit(Weight weight) noexcept
UnboundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, Atom > q_
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
template<typename Rep , typename Period >
FOLLY_ALWAYS_INLINE bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::try_dequeue_for ( T elem,
const std::chrono::duration< Rep, Period > &  duration 
)
inline

try_dequeue_for

Definition at line 420 of file DynamicBoundedQueue.h.

Referenced by enq_deq_test().

422  {
423  if (q_.try_dequeue_for(elem, duration)) {
424  addCredit(WeightFn()(elem));
425  return true;
426  }
427  return false;
428  }
FOLLY_ALWAYS_INLINE void addCredit(Weight weight) noexcept
UnboundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, Atom > q_
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
template<typename Clock , typename Duration >
FOLLY_ALWAYS_INLINE bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::try_dequeue_until ( T elem,
const std::chrono::time_point< Clock, Duration > &  deadline 
)
inline

try_dequeue_until

Definition at line 408 of file DynamicBoundedQueue.h.

410  {
411  if (q_.try_dequeue_until(elem, deadline)) {
412  addCredit(WeightFn()(elem));
413  return true;
414  }
415  return false;
416  }
FOLLY_ALWAYS_INLINE void addCredit(Weight weight) noexcept
UnboundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, Atom > q_
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
FOLLY_ALWAYS_INLINE bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::try_enqueue ( const T v)
inline

try_enqueue

Definition at line 351 of file DynamicBoundedQueue.h.

Referenced by enq_deq_test().

351  {
352  return tryEnqueueImpl(v);
353  }
FOLLY_ALWAYS_INLINE bool tryEnqueueImpl(Arg &&v)
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
FOLLY_ALWAYS_INLINE bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::try_enqueue ( T &&  v)
inline

Definition at line 355 of file DynamicBoundedQueue.h.

References folly::gen::move.

355  {
356  return tryEnqueueImpl(std::move(v));
357  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
FOLLY_ALWAYS_INLINE bool tryEnqueueImpl(Arg &&v)
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
template<typename Rep , typename Period >
FOLLY_ALWAYS_INLINE bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::try_enqueue_for ( const T v,
const std::chrono::duration< Rep, Period > &  duration 
)
inline

try_enqueue_for

Definition at line 376 of file DynamicBoundedQueue.h.

Referenced by enq_deq_test().

378  {
379  return tryEnqueueForImpl(v, duration);
380  }
FOLLY_ALWAYS_INLINE bool tryEnqueueForImpl(Arg &&v, const std::chrono::duration< Rep, Period > &duration)
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
template<typename Rep , typename Period >
FOLLY_ALWAYS_INLINE bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::try_enqueue_for ( T &&  v,
const std::chrono::duration< Rep, Period > &  duration 
)
inline

Definition at line 383 of file DynamicBoundedQueue.h.

References folly::gen::move.

385  {
386  return tryEnqueueForImpl(std::move(v), duration);
387  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
FOLLY_ALWAYS_INLINE bool tryEnqueueForImpl(Arg &&v, const std::chrono::duration< Rep, Period > &duration)
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
template<typename Clock , typename Duration >
FOLLY_ALWAYS_INLINE bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::try_enqueue_until ( const T v,
const std::chrono::time_point< Clock, Duration > &  deadline 
)
inline

try_enqueue_until

Definition at line 361 of file DynamicBoundedQueue.h.

363  {
364  return tryEnqueueUntilImpl(v, deadline);
365  }
FOLLY_ALWAYS_INLINE bool tryEnqueueUntilImpl(Arg &&v, const std::chrono::time_point< Clock, Duration > &deadline)
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
template<typename Clock , typename Duration >
FOLLY_ALWAYS_INLINE bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::try_enqueue_until ( T &&  v,
const std::chrono::time_point< Clock, Duration > &  deadline 
)
inline

Definition at line 368 of file DynamicBoundedQueue.h.

References folly::gen::move.

370  {
371  return tryEnqueueUntilImpl(std::move(v), deadline);
372  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
FOLLY_ALWAYS_INLINE bool tryEnqueueUntilImpl(Arg &&v, const std::chrono::time_point< Clock, Duration > &deadline)
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
FOLLY_ALWAYS_INLINE bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::tryAddDebit ( Weight  weight)
inlineprivatenoexcept

Definition at line 503 of file DynamicBoundedQueue.h.

References LIKELY.

503  {
504  Weight capacity = getCapacity();
505  Weight before = fetchAddDebit(weight);
506  if (LIKELY(before + weight <= capacity)) {
507  return true;
508  } else {
509  subDebit(weight);
510  return false;
511  }
512  }
FOLLY_ALWAYS_INLINE Weight fetchAddDebit(Weight weight) noexcept
Weight weight() const noexcept
#define LIKELY(x)
Definition: Likely.h:47
FOLLY_ALWAYS_INLINE Weight getCapacity() const noexcept
void subDebit(Weight weight) noexcept
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
template<typename Rep , typename Period , typename Arg >
FOLLY_ALWAYS_INLINE bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::tryEnqueueForImpl ( Arg &&  v,
const std::chrono::duration< Rep, Period > &  duration 
)
inlineprivate

Definition at line 493 of file DynamicBoundedQueue.h.

References LIKELY, and now().

495  {
496  if (LIKELY(tryEnqueueImpl(std::forward<Arg>(v)))) {
497  return true;
498  }
499  auto deadline = std::chrono::steady_clock::now() + duration;
500  return tryEnqueueUntilSlow(std::forward<Arg>(v), deadline);
501  }
std::chrono::steady_clock::time_point now()
#define LIKELY(x)
Definition: Likely.h:47
FOLLY_ALWAYS_INLINE bool tryEnqueueImpl(Arg &&v)
bool tryEnqueueUntilSlow(Arg &&v, const std::chrono::time_point< Clock, Duration > &deadline)
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
template<typename Arg >
FOLLY_ALWAYS_INLINE bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::tryEnqueueImpl ( Arg &&  v)
inlineprivate

Definition at line 475 of file DynamicBoundedQueue.h.

References min.

475  {
476  return tryEnqueueUntilImpl(
477  std::forward<Arg>(v), std::chrono::steady_clock::time_point::min());
478  }
LogLevel min
Definition: LogLevel.cpp:30
FOLLY_ALWAYS_INLINE bool tryEnqueueUntilImpl(Arg &&v, const std::chrono::time_point< Clock, Duration > &deadline)
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
template<typename Clock , typename Duration , typename Arg >
FOLLY_ALWAYS_INLINE bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::tryEnqueueUntilImpl ( Arg &&  v,
const std::chrono::time_point< Clock, Duration > &  deadline 
)
inlineprivate

Definition at line 481 of file DynamicBoundedQueue.h.

References LIKELY, and v.

483  {
484  Weight weight = WeightFn()(std::forward<Arg>(v));
485  if (LIKELY(tryAddDebit(weight))) {
486  q_.enqueue(std::forward<Arg>(v));
487  return true;
488  }
489  return tryEnqueueUntilSlow(std::forward<Arg>(v), deadline);
490  }
auto v
Weight weight() const noexcept
#define LIKELY(x)
Definition: Likely.h:47
bool tryEnqueueUntilSlow(Arg &&v, const std::chrono::time_point< Clock, Duration > &deadline)
UnboundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, Atom > q_
FOLLY_ALWAYS_INLINE bool tryAddDebit(Weight weight) noexcept
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
template<typename Clock , typename Duration , typename Arg >
bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::tryEnqueueUntilSlow ( Arg &&  v,
const std::chrono::time_point< Clock, Duration > &  deadline 
)
inlineprivate

Definition at line 576 of file DynamicBoundedQueue.h.

References v.

578  {
579  Weight weight = WeightFn()(std::forward<Arg>(v));
580  if (canEnqueue(deadline, weight)) {
581  q_.enqueue(std::forward<Arg>(v));
582  return true;
583  } else {
584  return false;
585  }
586  }
auto v
Weight weight() const noexcept
UnboundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, Atom > q_
bool canEnqueue(const std::chrono::time_point< Clock, Duration > &deadline, Weight weight) noexcept
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::tryReduceDebit ( )
inlineprivatenoexcept

Definition at line 620 of file DynamicBoundedQueue.h.

620  {
621  Weight w = takeTransfer();
622  if (w > 0) {
623  subDebit(w);
624  }
625  return w > 0;
626  }
void subDebit(Weight weight) noexcept
template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
Weight folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::weight ( ) const
inlinenoexcept

weight

Definition at line 440 of file DynamicBoundedQueue.h.

References c, and folly::pushmi::detail::t.

440  {
441  auto d = getDebit();
442  auto c = getCredit();
443  auto t = getTransfer();
444  return d > (c + t) ? d - (c + t) : 0;
445  }
FOLLY_ALWAYS_INLINE Weight getDebit() const noexcept
Weight getTransfer() const noexcept
FOLLY_ALWAYS_INLINE Weight getCredit() const noexcept
char c

Member Data Documentation

template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
constexpr size_t folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::Align = 1u << LgAlign
staticprivate

Definition at line 296 of file DynamicBoundedQueue.h.

template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
Atom<Weight> folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::capacity_
private

Definition at line 304 of file DynamicBoundedQueue.h.

template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
Atom<Weight> folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::credit_
private

Definition at line 307 of file DynamicBoundedQueue.h.

template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
Atom<Weight> folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::debit_
private

Data members.

Definition at line 298 of file DynamicBoundedQueue.h.

template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
UnboundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, Atom> folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::q_
private

Definition at line 324 of file DynamicBoundedQueue.h.

template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
constexpr bool folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::SPSC = SingleProducer && SingleConsumer
staticprivate

Definition at line 295 of file DynamicBoundedQueue.h.

template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
Atom<Weight> folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::threshold_
private

Definition at line 308 of file DynamicBoundedQueue.h.

template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
Atom<Weight> folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::transfer_
private

Definition at line 312 of file DynamicBoundedQueue.h.

template<typename T, bool SingleProducer, bool SingleConsumer, bool MayBlock, size_t LgSegmentSize = 8, size_t LgAlign = 7, typename WeightFn = DefaultWeightFn<T>, template< typename > class Atom = std::atomic>
detail::Futex<Atom> folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::waiting_
private

Definition at line 313 of file DynamicBoundedQueue.h.


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