CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SingleConsumerQ.h
Go to the documentation of this file.
1 #ifndef FWCore_Utilities_SingleConsumerQ_h
2 #define FWCore_Utilities_SingleConsumerQ_h
3 
4 // -*- C++ -*-
5 
6 /*
7  A bounded queue for use in a multi-threaded producer/consumer application.
8  This is a simple design. It is only meant to be used where there is
9  one consumer and one or more producers using the a queue instance.
10 
11  The problem with multiple consumers is the separate front/pop
12  member functions. If they are pulled together into one function,
13  multiple consumers may be possible, but exception safety would then
14  be a problem - popping an item off the queue to be held as a local
15  variable, followed by its removal from the queue. Having fixed size
16  buffers within a fixed size pool and using a circular buffer as the
17  queue alleviates most of this problem because exceptions will not
18  occur during manipulation. The only problem left to be checked is
19  how (or if) the boost mutex manipulation can throw and when.
20 
21  Note: the current implementation has no protection again unsigned int
22  overflows
23 
24  Missing:
25  - the ring buffer is really not used to its fullest extent
26  - the buffer sizes are fixed and cannot grow
27  - a simple Buffer object is returned that has the pointer and len
28  separate. The length should be stored as the first word of the
29  buffer itself
30  - timeouts for consumer
31  - good way to signal to consumer to end
32  - keeping the instance of this thing around until all using threads are
33  done with it
34 
35 */
36 
37 #include <vector>
38 #include <mutex>
39 #include <condition_variable>
40 
41 namespace edm {
42 
44  {
45  public:
46  struct Buffer
47  {
48  Buffer():ptr_(),len_() { }
49  Buffer(void* p,int len):ptr_(p),len_(len) { }
50 
51  void* ptr_;
52  int len_;
53  };
54 
57 
58  struct ConsumerType
59  {
61  { return b.getConsumerBuffer(); }
62  static void release(SingleConsumerQ& b, void* v)
63  { b.releaseConsumerBuffer(v); }
64  static void commit(SingleConsumerQ& b, void* v,int size)
65  { b.commitConsumerBuffer(v,size); }
66  };
67  struct ProducerType
68  {
70  { return b.getProducerBuffer(); }
71  static void release(SingleConsumerQ& b, void* v)
72  { b.releaseProducerBuffer(v); }
73  static void commit(SingleConsumerQ& b, void* v,int size)
74  { b.commitProducerBuffer(v,size); }
75  };
76 
77  template <class T>
79  {
80  public:
82  b_(b),v_(T::get(b)),committed_(false) { }
84  { if(!committed_) T::release(b_,v_.ptr_); }
85 
86  void* buffer() const { return v_.ptr_; }
87  int size() const { return v_.len_; }
88  void commit(int theSize=0) { T::commit(b_, v_.ptr_, theSize); committed_=true; }
89 
90  private:
93  bool committed_;
94  };
95 
98 
100  void releaseProducerBuffer(void*);
101  void commitProducerBuffer(void*,int);
102 
104  void releaseConsumerBuffer(void*);
105  void commitConsumerBuffer(void*,int);
106 
107  int maxEventSize() const { return max_event_size_; }
108  int maxQueueDepth() const { return max_queue_depth_; }
109 
110  private:
111  // no copy
113 
114  // the memory for the buffers
115  typedef std::vector<char> ByteArray;
116  // the pool of buffers
117  typedef std::vector<void*> Pool;
118  // the queue
119  typedef std::vector<Buffer> Queue;
120 
123  int pos_; // use pool as stack of avaiable buffers
127  unsigned int fpos_, bpos_; // positions for queue - front and back
128 
131  std::condition_variable pool_cond_;
132  std::condition_variable pop_cond_;
133  std::condition_variable push_cond_;
134 
135  };
136 
137 
138 }
139 #endif
std::condition_variable pop_cond_
static void commit(SingleConsumerQ &b, void *v, int size)
void releaseConsumerBuffer(void *)
static void release(SingleConsumerQ &b, void *v)
std::vector< void * > Pool
static boost::mutex mutex
Definition: LHEProxy.cc:11
std::vector< Buffer > Queue
void releaseProducerBuffer(void *)
std::condition_variable pool_cond_
OperateBuffer< ProducerType > ProducerBuffer
OperateBuffer< ConsumerType > ConsumerBuffer
std::condition_variable push_cond_
static void commit(SingleConsumerQ &b, void *v, int size)
std::vector< char > ByteArray
int maxEventSize() const
double b
Definition: hdecay.h:120
void commitProducerBuffer(void *, int)
void commitConsumerBuffer(void *, int)
volatile std::atomic< bool > shutdown_flag false
int maxQueueDepth() const
long double T
tuple size
Write out results.
T get(const Candidate &c)
Definition: component.h:55
static void release(SingleConsumerQ &b, void *v)
SingleConsumerQ(int max_event_size, int max_queue_depth)