CMS 3D CMS Logo

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  public:
45  struct Buffer {
46  Buffer() : ptr_(), len_() {}
47  Buffer(void* p, int len) : ptr_(p), len_(len) {}
48 
49  void* ptr_;
50  int len_;
51  };
52 
53  SingleConsumerQ(int max_event_size, int max_queue_depth);
55 
56  struct ConsumerType {
57  static SingleConsumerQ::Buffer get(SingleConsumerQ& b) { return b.getConsumerBuffer(); }
58  static void release(SingleConsumerQ& b, void* v) { b.releaseConsumerBuffer(v); }
59  static void commit(SingleConsumerQ& b, void* v, int size) { b.commitConsumerBuffer(v, size); }
60  };
61  struct ProducerType {
62  static SingleConsumerQ::Buffer get(SingleConsumerQ& b) { return b.getProducerBuffer(); }
63  static void release(SingleConsumerQ& b, void* v) { b.releaseProducerBuffer(v); }
64  static void commit(SingleConsumerQ& b, void* v, int size) { b.commitProducerBuffer(v, size); }
65  };
66 
67  template <class T>
68  class OperateBuffer {
69  public:
70  explicit OperateBuffer(SingleConsumerQ& b) : b_(b), v_(T::get(b)), committed_(false) {}
72  if (!committed_)
73  T::release(b_, v_.ptr_);
74  }
75 
76  void* buffer() const { return v_.ptr_; }
77  int size() const { return v_.len_; }
78  void commit(int theSize = 0) {
79  T::commit(b_, v_.ptr_, theSize);
80  committed_ = true;
81  }
82 
83  private:
86  bool committed_;
87  };
88 
91 
93  void releaseProducerBuffer(void*);
94  void commitProducerBuffer(void*, int);
95 
97  void releaseConsumerBuffer(void*);
98  void commitConsumerBuffer(void*, int);
99 
100  int maxEventSize() const { return max_event_size_; }
101  int maxQueueDepth() const { return max_queue_depth_; }
102 
103  private:
104  // no copy
105  SingleConsumerQ(const SingleConsumerQ&) = delete;
106 
107  // the memory for the buffers
108  typedef std::vector<char> ByteArray;
109  // the pool of buffers
110  typedef std::vector<void*> Pool;
111  // the queue
112  typedef std::vector<Buffer> Queue;
113 
116  int pos_; // use pool as stack of avaiable buffers
117  ByteArray mem_;
119  Queue queue_;
120  unsigned int fpos_, bpos_; // positions for queue - front and back
121 
124  std::condition_variable pool_cond_;
125  std::condition_variable pop_cond_;
126  std::condition_variable push_cond_;
127  };
128 
129 } // namespace edm
130 #endif
size
Write out results.
std::condition_variable pop_cond_
static boost::mutex mutex
Definition: Proxy.cc:11
static void commit(SingleConsumerQ &b, void *v, int size)
void releaseConsumerBuffer(void *)
static void release(SingleConsumerQ &b, void *v)
std::vector< void * > Pool
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
T const & get(Event const &event, InputTag const &tag)(false)
Definition: Event.h:658
double b
Definition: hdecay.h:120
void commitProducerBuffer(void *, int)
HLT enums.
void commitConsumerBuffer(void *, int)
int maxQueueDepth() const
long double T
static void release(SingleConsumerQ &b, void *v)
SingleConsumerQ(int max_event_size, int max_queue_depth)