proxygen
folly::ProducerConsumerQueue< T > Struct Template Reference

#include <ProducerConsumerQueue.h>

Public Types

typedef T value_type
 

Public Member Functions

 ProducerConsumerQueue (const ProducerConsumerQueue &)=delete
 
ProducerConsumerQueueoperator= (const ProducerConsumerQueue &)=delete
 
 ProducerConsumerQueue (uint32_t size)
 
 ~ProducerConsumerQueue ()
 
template<class... Args>
bool write (Args &&...recordArgs)
 
bool read (T &record)
 
TfrontPtr ()
 
void popFront ()
 
bool isEmpty () const
 
bool isFull () const
 
size_t sizeGuess () const
 
size_t capacity () const
 

Private Types

using AtomicIndex = std::atomic< unsigned int >
 

Private Attributes

char pad0_ [hardware_destructive_interference_size]
 
const uint32_t size_
 
T *const records_
 
AtomicIndex readIndex_
 
AtomicIndex writeIndex_
 
char pad1_ [hardware_destructive_interference_size-sizeof(AtomicIndex)]
 

Detailed Description

template<class T>
struct folly::ProducerConsumerQueue< T >

Definition at line 39 of file ProducerConsumerQueue.h.

Member Typedef Documentation

template<class T>
using folly::ProducerConsumerQueue< T >::AtomicIndex = std::atomic<unsigned int>
private

Definition at line 175 of file ProducerConsumerQueue.h.

template<class T>
typedef T folly::ProducerConsumerQueue< T >::value_type

Definition at line 40 of file ProducerConsumerQueue.h.

Constructor & Destructor Documentation

template<class T>
folly::ProducerConsumerQueue< T >::ProducerConsumerQueue ( const ProducerConsumerQueue< T > &  )
delete
template<class T>
folly::ProducerConsumerQueue< T >::ProducerConsumerQueue ( uint32_t  size)
inlineexplicit

Definition at line 50 of file ProducerConsumerQueue.h.

References folly::ProducerConsumerQueue< T >::records_.

51  : size_(size),
52  records_(static_cast<T*>(std::malloc(sizeof(T) * size))),
53  readIndex_(0),
54  writeIndex_(0) {
55  assert(size >= 2);
56  if (!records_) {
57  throw std::bad_alloc();
58  }
59  }
folly::std T
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
template<class T>
folly::ProducerConsumerQueue< T >::~ProducerConsumerQueue ( )
inline

Definition at line 61 of file ProducerConsumerQueue.h.

References testing::Args(), bm::free(), folly::ProducerConsumerQueue< T >::readIndex_, folly::ProducerConsumerQueue< T >::records_, folly::ProducerConsumerQueue< T >::size_, value, and folly::ProducerConsumerQueue< T >::writeIndex_.

61  {
62  // We need to destruct anything that may still exist in our queue.
63  // (No real synchronization needed at destructor time: only one
64  // thread can be doing this.)
66  size_t readIndex = readIndex_;
67  size_t endIndex = writeIndex_;
68  while (readIndex != endIndex) {
69  records_[readIndex].~T();
70  if (++readIndex == size_) {
71  readIndex = 0;
72  }
73  }
74  }
75 
77  }
static const char *const value
Definition: Conv.cpp:50
void free()

Member Function Documentation

template<class T>
size_t folly::ProducerConsumerQueue< T >::capacity ( ) const
inline

Definition at line 170 of file ProducerConsumerQueue.h.

References folly::ProducerConsumerQueue< T >::size_.

Referenced by TEST().

170  {
171  return size_ - 1;
172  }
template<class T>
T* folly::ProducerConsumerQueue< T >::frontPtr ( )
inline

Definition at line 116 of file ProducerConsumerQueue.h.

References folly::ProducerConsumerQueue< T >::readIndex_, folly::ProducerConsumerQueue< T >::records_, and folly::ProducerConsumerQueue< T >::writeIndex_.

116  {
117  auto const currentRead = readIndex_.load(std::memory_order_relaxed);
118  if (currentRead == writeIndex_.load(std::memory_order_acquire)) {
119  // queue is empty
120  return nullptr;
121  }
122  return &records_[currentRead];
123  }
template<class T>
bool folly::ProducerConsumerQueue< T >::isEmpty ( ) const
inline

Definition at line 138 of file ProducerConsumerQueue.h.

References folly::ProducerConsumerQueue< T >::readIndex_, and folly::ProducerConsumerQueue< T >::writeIndex_.

Referenced by TEST().

138  {
139  return readIndex_.load(std::memory_order_acquire) ==
140  writeIndex_.load(std::memory_order_acquire);
141  }
template<class T>
bool folly::ProducerConsumerQueue< T >::isFull ( ) const
inline

Definition at line 143 of file ProducerConsumerQueue.h.

References folly::ProducerConsumerQueue< T >::readIndex_, folly::ProducerConsumerQueue< T >::size_, and folly::ProducerConsumerQueue< T >::writeIndex_.

Referenced by TEST().

143  {
144  auto nextRecord = writeIndex_.load(std::memory_order_acquire) + 1;
145  if (nextRecord == size_) {
146  nextRecord = 0;
147  }
148  if (nextRecord != readIndex_.load(std::memory_order_acquire)) {
149  return false;
150  }
151  // queue is full
152  return true;
153  }
template<class T>
ProducerConsumerQueue& folly::ProducerConsumerQueue< T >::operator= ( const ProducerConsumerQueue< T > &  )
delete
template<class T>
void folly::ProducerConsumerQueue< T >::popFront ( )
inline

Definition at line 126 of file ProducerConsumerQueue.h.

References folly::ProducerConsumerQueue< T >::readIndex_, folly::ProducerConsumerQueue< T >::records_, folly::ProducerConsumerQueue< T >::size_, and folly::ProducerConsumerQueue< T >::writeIndex_.

126  {
127  auto const currentRead = readIndex_.load(std::memory_order_relaxed);
128  assert(currentRead != writeIndex_.load(std::memory_order_acquire));
129 
130  auto nextRecord = currentRead + 1;
131  if (nextRecord == size_) {
132  nextRecord = 0;
133  }
134  records_[currentRead].~T();
135  readIndex_.store(nextRecord, std::memory_order_release);
136  }
template<class T>
bool folly::ProducerConsumerQueue< T >::read ( T record)
inline

Definition at line 97 of file ProducerConsumerQueue.h.

References folly::gen::move, folly::ProducerConsumerQueue< T >::readIndex_, folly::ProducerConsumerQueue< T >::records_, folly::ProducerConsumerQueue< T >::size_, and folly::ProducerConsumerQueue< T >::writeIndex_.

Referenced by TEST(), and PCQ< T >::try_dequeue().

97  {
98  auto const currentRead = readIndex_.load(std::memory_order_relaxed);
99  if (currentRead == writeIndex_.load(std::memory_order_acquire)) {
100  // queue is empty
101  return false;
102  }
103 
104  auto nextRecord = currentRead + 1;
105  if (nextRecord == size_) {
106  nextRecord = 0;
107  }
108  record = std::move(records_[currentRead]);
109  records_[currentRead].~T();
110  readIndex_.store(nextRecord, std::memory_order_release);
111  return true;
112  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
template<class T>
size_t folly::ProducerConsumerQueue< T >::sizeGuess ( ) const
inline

Definition at line 160 of file ProducerConsumerQueue.h.

References folly::ProducerConsumerQueue< T >::readIndex_, folly::ProducerConsumerQueue< T >::size_, and folly::ProducerConsumerQueue< T >::writeIndex_.

Referenced by TEST().

160  {
161  int ret = writeIndex_.load(std::memory_order_acquire) -
162  readIndex_.load(std::memory_order_acquire);
163  if (ret < 0) {
164  ret += size_;
165  }
166  return ret;
167  }
template<class T>
template<class... Args>
bool folly::ProducerConsumerQueue< T >::write ( Args &&...  recordArgs)
inline

Definition at line 80 of file ProducerConsumerQueue.h.

References folly::ProducerConsumerQueue< T >::readIndex_, folly::ProducerConsumerQueue< T >::records_, folly::ProducerConsumerQueue< T >::size_, folly::T, and folly::ProducerConsumerQueue< T >::writeIndex_.

Referenced by PCQ< T >::enqueue(), TEST(), and PCQ< T >::try_enqueue().

80  {
81  auto const currentWrite = writeIndex_.load(std::memory_order_relaxed);
82  auto nextRecord = currentWrite + 1;
83  if (nextRecord == size_) {
84  nextRecord = 0;
85  }
86  if (nextRecord != readIndex_.load(std::memory_order_acquire)) {
87  new (&records_[currentWrite]) T(std::forward<Args>(recordArgs)...);
88  writeIndex_.store(nextRecord, std::memory_order_release);
89  return true;
90  }
91 
92  // queue is full
93  return false;
94  }
folly::std T

Member Data Documentation

template<class T>
char folly::ProducerConsumerQueue< T >::pad0_[hardware_destructive_interference_size]
private

Definition at line 177 of file ProducerConsumerQueue.h.

Definition at line 184 of file ProducerConsumerQueue.h.


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