$treeview $search $mathjax $extrastylesheet
otsdaq
v2_03_00
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #ifndef _ots_CircularBuffer_h_ 00002 #define _ots_CircularBuffer_h_ 00003 00004 #include "otsdaq-core/DataManager/BufferImplementation.h" 00005 #include "otsdaq-core/DataManager/CircularBufferBase.h" 00006 00007 #include "otsdaq-core/Macros/CoutMacros.h" 00008 #include "otsdaq-core/MessageFacility/MessageFacility.h" 00009 00010 #include <atomic> 00011 #include <iostream> 00012 #include <map> 00013 #include <string> 00014 00015 namespace ots 00016 { 00017 template<class D, class H> 00018 class CircularBuffer : public CircularBufferBase 00019 { 00020 public: 00021 CircularBuffer(const std::string& dataBufferId); 00022 virtual ~CircularBuffer(void); 00023 00024 void reset(void); // This DOES NOT reset the consumer list 00025 void resetConsumerList(void); 00026 bool isEmpty(void) const; 00027 unsigned int getTotalNumberOfSubBuffers(void) const; 00028 unsigned int getProducerBufferSize(const std::string& producerID) const; 00029 00030 inline int read(D& buffer, const std::string& consumerID) 00031 { 00032 H dummyHeader; 00033 return read(buffer, dummyHeader, consumerID); 00034 } 00035 00036 inline int read(D& buffer, H& header, const std::string& consumerID) 00037 { 00038 setNextProducerBuffer(consumerID); 00039 unsigned int readCounter = theBuffer_.size() - 1; 00040 // ++megaCounter_; 00041 // if(megaCounter_%10000000 == 0) 00042 // std::cout << __COUT_HDR_FL__ << __COUT_HDR_FL__ 00043 // << "Consumer: " << consumerID 00044 // << " Reading producer: " << lastReadBuffer_[consumerID]->first 00045 // << " Buffer empty? " << lastReadBuffer_[consumerID]->second.isEmpty() 00046 // << " written buffers: " << 00047 // lastReadBuffer_[consumerID]->second.numberOfWrittenBuffers() 00048 // << std::endl; 00049 int readReturnVal; 00050 while((readReturnVal = lastReadBuffer_[consumerID]->second.read( 00051 buffer, header, consumerID)) < 0 && 00052 readCounter > 0) 00053 { 00054 setNextProducerBuffer(consumerID); 00055 --readCounter; 00056 } 00057 return readReturnVal; 00058 } 00059 00060 int read(D*& buffer, H*& header, const std::string& consumerID) 00061 { 00062 setNextProducerBuffer(consumerID); 00063 unsigned int readCounter = theBuffer_.size() - 1; 00064 // ++megaCounter_; 00065 // if(megaCounter_%10000000 == 0) 00066 // std::cout << __COUT_HDR_FL__ << __COUT_HDR_FL__ 00067 // << "Consumer: " << consumerID 00068 // << " Reading producer: " << lastReadBuffer_[consumerID]->first 00069 // << " Buffer empty? " << lastReadBuffer_[consumerID]->second.isEmpty() 00070 // << " written buffers: " << 00071 // lastReadBuffer_[consumerID]->second.numberOfWrittenBuffers() 00072 // << std::endl; 00073 int readReturnVal; 00074 while((readReturnVal = lastReadBuffer_[consumerID]->second.read( 00075 buffer, header, consumerID)) < 0 && 00076 readCounter > 0) 00077 { 00078 setNextProducerBuffer(consumerID); 00079 --readCounter; 00080 } 00081 return readReturnVal; 00082 } 00083 00084 BufferImplementation<D, H>& getLastReadBuffer(const std::string& consumerID) 00085 { 00086 return lastReadBuffer_[consumerID]->second; 00087 } 00088 BufferImplementation<D, H>& getBuffer(const std::string& producerID) 00089 { 00090 __COUTV__(producerID); 00091 __COUTV__(int(theBuffer_.find(producerID) == theBuffer_.end())); 00092 return theBuffer_[producerID]; 00093 } 00094 00095 // void unregisterConsumer (const std::string& consumerID); 00096 // void unregisterProducer (const std::string& producerID); 00097 00098 private: 00099 std::map<std::string /*producer id*/, 00100 BufferImplementation<D, H> /*one producer, many consumers*/> 00101 theBuffer_; 00102 00103 void registerProducer(const std::string& producerID, 00104 unsigned int numberOfSubBuffers = 100); 00105 void registerConsumer(const std::string& consumerID, 00106 CircularBufferBase::ConsumerPriority priority); 00107 00108 void setNextProducerBuffer(const std::string& consumer); 00109 00110 std::map<std::string /*consumer id*/, 00111 /*iterator within producer map*/ typename std:: 00112 map<std::string, BufferImplementation<D, H>>::iterator> 00113 lastReadBuffer_; 00114 00115 std::map<std::string, CircularBufferBase::ConsumerPriority> consumers_; 00116 }; 00117 #include "otsdaq-core/DataManager/CircularBuffer.icc" 00118 00119 } // namespace ots 00120 #endif