Classes | Public Types | Public Member Functions | Private Types | Private Attributes | List of all members
mf::SingleConsumerQ Class Reference

#include <SingleConsumerQ.h>

Classes

struct  Buffer
 
struct  ConsumerType
 
class  OperateBuffer
 
struct  ProducerType
 

Public Types

using ConsumerBuffer = OperateBuffer< ConsumerType >
 
using ProducerBuffer = OperateBuffer< ProducerType >
 

Public Member Functions

 SingleConsumerQ (int const max_event_size, int const max_queue_depth)
 
Buffer getProducerBuffer ()
 
void releaseProducerBuffer (void *)
 
void commitProducerBuffer (void *, int)
 
Buffer getConsumerBuffer ()
 
void releaseConsumerBuffer (void *)
 
void commitConsumerBuffer (void *, int)
 
int maxEventSize () const
 
int maxQueueDepth () const
 
 SingleConsumerQ (SingleConsumerQ const &)=delete
 
SingleConsumerQ operator= (SingleConsumerQ const &)=delete
 

Private Types

using ByteArray = std::vector< char >
 
using Pool = std::vector< void * >
 
using Queue = std::vector< Buffer >
 

Private Attributes

int max_event_size_
 
int max_queue_depth_
 
int pos_
 
ByteArray mem_
 
Pool buffer_pool_ {}
 
Queue queue_
 
unsigned int fpos_ {}
 
unsigned int bpos_ {}
 
std::mutex pool_mutex_ {}
 
std::mutex queue_mutex_ {}
 
std::condition_variable pool_cond_ {}
 
std::condition_variable pop_cond_ {}
 
std::condition_variable push_cond_ {}
 

Detailed Description

Definition at line 39 of file SingleConsumerQ.h.

Member Typedef Documentation

using mf::SingleConsumerQ::ByteArray = std::vector<char>
private

Definition at line 149 of file SingleConsumerQ.h.

Definition at line 122 of file SingleConsumerQ.h.

using mf::SingleConsumerQ::Pool = std::vector<void*>
private

Definition at line 151 of file SingleConsumerQ.h.

Definition at line 123 of file SingleConsumerQ.h.

using mf::SingleConsumerQ::Queue = std::vector<Buffer>
private

Definition at line 153 of file SingleConsumerQ.h.

Constructor & Destructor Documentation

mf::SingleConsumerQ::SingleConsumerQ ( int const  max_event_size,
int const  max_queue_depth 
)

Definition at line 5 of file SingleConsumerQ.cc.

6  : max_event_size_(max_event_size)
7  , max_queue_depth_(max_queue_depth)
8  , pos_(max_queue_depth - 1)
9  , mem_(max_event_size * max_queue_depth)
10  , queue_(max_queue_depth)
11  {
12  // throw if event size 0 or queue depth 0
13 
14  for (char* i = &mem_[0]; i < &mem_[mem_.size()]; i += max_event_size)
15  buffer_pool_.push_back(i);
16  }
mf::SingleConsumerQ::SingleConsumerQ ( SingleConsumerQ const &  )
delete

Member Function Documentation

void mf::SingleConsumerQ::commitConsumerBuffer ( void *  v,
int   
)

Definition at line 89 of file SingleConsumerQ.cc.

90  {
92  }
void releaseProducerBuffer(void *)
void mf::SingleConsumerQ::commitProducerBuffer ( void *  v,
int  len 
)

Definition at line 43 of file SingleConsumerQ.cc.

44  {
45  // get lock
46  std::unique_lock<std::mutex> sl(queue_mutex_);
47  // if full, wait for item to be removed
48  while ((bpos_ + max_queue_depth_) == fpos_) {
49  push_cond_.wait(sl);
50  }
51 
52  // put buffer into queue
53  queue_[fpos_ % max_queue_depth_] = Buffer(v, len);
54  ++fpos_;
55  // signal consumer
56  pop_cond_.notify_all();
57  }
std::condition_variable push_cond_
std::condition_variable pop_cond_
SingleConsumerQ::Buffer mf::SingleConsumerQ::getConsumerBuffer ( )

Definition at line 60 of file SingleConsumerQ.cc.

61  {
62  // get lock
63  std::unique_lock<std::mutex> sl(queue_mutex_);
64  // if empty, wait for item to appear
65  while (bpos_ == fpos_) {
66  pop_cond_.wait(sl);
67  }
68  // get a buffer from the queue and return it
69  Buffer v = queue_[bpos_ % max_queue_depth_];
70  ++bpos_;
71  // note that these operations cannot throw
72  // signal producer
73  push_cond_.notify_all();
74  return v;
75  }
std::condition_variable push_cond_
std::condition_variable pop_cond_
SingleConsumerQ::Buffer mf::SingleConsumerQ::getProducerBuffer ( )

Definition at line 19 of file SingleConsumerQ.cc.

20  {
21  // get lock
22  std::unique_lock<std::mutex> sl(pool_mutex_);
23  // wait for buffer to appear
24  while (pos_ < 0) {
25  pool_cond_.wait(sl);
26  }
27  void* v = buffer_pool_[pos_];
28  --pos_;
29  return Buffer(v, max_event_size_);
30  }
std::condition_variable pool_cond_
int mf::SingleConsumerQ::maxEventSize ( ) const
inline

Definition at line 134 of file SingleConsumerQ.h.

135  {
136  return max_event_size_;
137  }
int mf::SingleConsumerQ::maxQueueDepth ( ) const
inline

Definition at line 139 of file SingleConsumerQ.h.

140  {
141  return max_queue_depth_;
142  }
SingleConsumerQ mf::SingleConsumerQ::operator= ( SingleConsumerQ const &  )
delete
void mf::SingleConsumerQ::releaseConsumerBuffer ( void *  v)

Definition at line 78 of file SingleConsumerQ.cc.

79  {
80  // should the buffer be placed back onto the queue and not released?
81  // we got here because a commit did to occur in the consumer.
82  // we will allow consumers to call or not call commit for now, meaning
83  // that we cannot distinguish between exception conditions and normal
84  // return. The buffer will always be released
86  }
void releaseProducerBuffer(void *)
void mf::SingleConsumerQ::releaseProducerBuffer ( void *  v)

Definition at line 33 of file SingleConsumerQ.cc.

34  {
35  // get lock
36  std::lock_guard<std::mutex> sl(pool_mutex_);
37  ++pos_;
38  buffer_pool_[pos_] = v;
39  pool_cond_.notify_all();
40  }
std::condition_variable pool_cond_

Member Data Documentation

unsigned int mf::SingleConsumerQ::bpos_ {}
private

Definition at line 161 of file SingleConsumerQ.h.

Pool mf::SingleConsumerQ::buffer_pool_ {}
private

Definition at line 159 of file SingleConsumerQ.h.

unsigned int mf::SingleConsumerQ::fpos_ {}
private

Definition at line 161 of file SingleConsumerQ.h.

int mf::SingleConsumerQ::max_event_size_
private

Definition at line 155 of file SingleConsumerQ.h.

int mf::SingleConsumerQ::max_queue_depth_
private

Definition at line 156 of file SingleConsumerQ.h.

ByteArray mf::SingleConsumerQ::mem_
private

Definition at line 158 of file SingleConsumerQ.h.

std::condition_variable mf::SingleConsumerQ::pool_cond_ {}
private

Definition at line 165 of file SingleConsumerQ.h.

std::mutex mf::SingleConsumerQ::pool_mutex_ {}
private

Definition at line 163 of file SingleConsumerQ.h.

std::condition_variable mf::SingleConsumerQ::pop_cond_ {}
private

Definition at line 166 of file SingleConsumerQ.h.

int mf::SingleConsumerQ::pos_
private

Definition at line 157 of file SingleConsumerQ.h.

std::condition_variable mf::SingleConsumerQ::push_cond_ {}
private

Definition at line 167 of file SingleConsumerQ.h.

Queue mf::SingleConsumerQ::queue_
private

Definition at line 160 of file SingleConsumerQ.h.

std::mutex mf::SingleConsumerQ::queue_mutex_ {}
private

Definition at line 164 of file SingleConsumerQ.h.


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