CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/IOPool/Streamer/interface/EventBuffer.h

Go to the documentation of this file.
00001 #ifndef IOPool_Streamer_EventBuffer_h
00002 #define IOPool_Streamer_EventBuffer_h
00003 
00004 // -*- C++ -*-
00005 
00006 /*
00007  A bounded queue for use in a multi-threaded producer/consumer application.
00008  This is a simple design.  It is only meant to be used where there is
00009  one consumer and one or more producers using the a queue instance.
00010 
00011  The problem with multiple consumers is the separate front/pop
00012  member functions.  If they are pulled together into one function,
00013  multiple consumers may be possible, but exception safety would then
00014  be a problem - popping an item off the queue to be held as a local
00015  variable, followed by its removal from the queue.  Having fixed size
00016  buffers within a fixed size pool and using a circular buffer as the
00017  queue alleviates most of this problem because exceptions will not
00018  occur during manipulation.  The only problem left to be checked is
00019  how (or if) the boost mutex manipulation can throw and when.
00020 
00021  Note: the current implementation has no protection again unsigned int
00022  overflows
00023 
00024  Missing:
00025   - the ring buffer is really not used to its fullest extent
00026   - the buffer sizes are fixed and cannot grow
00027   - a simple Buffer object is returned that has the pointer and len
00028     separate.  The length should be stored as the first word of the
00029     buffer itself
00030   - timeouts for consumer
00031   - good way to signal to consumer to end
00032   - keeping the instance of this thing around until all using threads are 
00033     done with it
00034 
00035 */
00036 
00037 #include <vector>
00038 #include "boost/thread/mutex.hpp"
00039 #include "boost/thread/condition.hpp"
00040 
00041 namespace edm {
00042 
00043   class EventBuffer
00044   {
00045   public:
00046     struct Buffer
00047     {
00048       Buffer():ptr_(),len_() { }
00049       Buffer(void* p,int len):ptr_(p),len_(len) { }
00050 
00051       void* ptr_;
00052       int len_;
00053     };
00054 
00055     EventBuffer(int max_event_size, int max_queue_depth);
00056     ~EventBuffer();
00057 
00058     struct ConsumerType
00059     {
00060       static EventBuffer::Buffer get(EventBuffer& b)
00061       { return b.getConsumerBuffer(); }
00062       static void release(EventBuffer& b, void* v)
00063       { b.releaseConsumerBuffer(v); }
00064       static void commit(EventBuffer& b, void* v,int size)
00065       { b.commitConsumerBuffer(v,size); }
00066     };
00067     struct ProducerType
00068     {
00069       static EventBuffer::Buffer get(EventBuffer& b)
00070       { return b.getProducerBuffer(); }
00071       static void release(EventBuffer& b, void* v)
00072       { b.releaseProducerBuffer(v); }
00073       static void commit(EventBuffer& b, void* v,int size)
00074       { b.commitProducerBuffer(v,size); }
00075     };
00076 
00077     template <class T>
00078     class OperateBuffer
00079     {
00080     public:
00081       explicit OperateBuffer(EventBuffer& b):
00082         b_(b),v_(T::get(b)),committed_(false) { }
00083       ~OperateBuffer()
00084       { if(!committed_) T::release(b_,v_.ptr_); }
00085 
00086       void* buffer() const { return v_.ptr_; }
00087       int size() const { return v_.len_; }
00088       void commit(int size=0) { T::commit(b_,v_.ptr_,size); committed_=true; }
00089 
00090     private:
00091       EventBuffer& b_;
00092       EventBuffer::Buffer v_;
00093       bool committed_;
00094     };
00095 
00096     typedef OperateBuffer<ConsumerType> ConsumerBuffer;
00097     typedef OperateBuffer<ProducerType> ProducerBuffer;
00098 
00099     Buffer getProducerBuffer();
00100     void releaseProducerBuffer(void*);
00101     void commitProducerBuffer(void*,int);
00102 
00103     Buffer getConsumerBuffer();
00104     void releaseConsumerBuffer(void*);
00105     void commitConsumerBuffer(void*,int);
00106 
00107     int maxEventSize() const { return max_event_size_; }
00108     int maxQueueDepth() const { return max_queue_depth_; }
00109 
00110   private:
00111     // no copy
00112     EventBuffer(const EventBuffer&) { }
00113 
00114     // the memory for the buffers
00115     typedef std::vector<char> ByteArray;
00116     // the pool of buffers
00117     typedef std::vector<void*> Pool;
00118     // the queue
00119     typedef std::vector<Buffer> Queue;
00120 
00121     int max_event_size_;
00122     int max_queue_depth_;
00123     int pos_; // use pool as stack of avaiable buffers
00124     ByteArray mem_;
00125     Pool buffer_pool_;
00126     Queue queue_;
00127     unsigned int fpos_, bpos_; // positions for queue - front and back 
00128 
00129     boost::mutex pool_lock_;
00130     boost::mutex queue_lock_;
00131     boost::condition pool_cond_;
00132     boost::condition pop_cond_;
00133     boost::condition push_cond_;
00134 
00135  public:
00136     bool empty() { boost::mutex::scoped_lock sl(queue_lock_); return (bpos_==fpos_); }
00137 
00138   };
00139 
00140 
00141 }
00142 #endif
00143 
00144 
00145