$treeview $search $mathjax $extrastylesheet
otsdaq
v2_03_00
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #ifndef _ots_BufferImplementation_h_ 00002 #define _ots_BufferImplementation_h_ 00003 00004 #include "otsdaq-core/DataManager/CircularBufferBase.h" 00005 #include "otsdaq-core/Macros/CoutMacros.h" 00006 #include "otsdaq-core/Macros/StringMacros.h" 00007 #include "otsdaq-core/MessageFacility/MessageFacility.h" 00008 00009 #include <atomic> 00010 #include <iostream> 00011 #include <map> 00012 #include <string> 00013 #include <vector> 00014 00015 namespace ots 00016 { 00017 template<class D, class H> 00018 class BufferImplementation 00019 { 00020 struct ConsumerStruct 00021 { 00022 ConsumerStruct() 00023 : priority_(CircularBufferBase::LowConsumerPriority) 00024 , readPointer_(0) 00025 , subBuffersStatus_(nullptr) 00026 { 00027 } 00028 00029 CircularBufferBase::ConsumerPriority priority_; 00030 int readPointer_; 00031 std::atomic_bool* subBuffersStatus_; // Status of the Circular Buffer: 00032 }; 00033 00034 public: 00035 BufferImplementation(const std::string& producerName = "", 00036 unsigned int numberOfSubBuffers = 100); 00037 BufferImplementation(const BufferImplementation<D, H>& toCopy); 00038 BufferImplementation<D, H>& operator=(const BufferImplementation<D, H>& toCopy); 00039 virtual ~BufferImplementation(void); 00040 00041 void init(void); 00042 void reset(void); 00043 void resetConsumerList(void); 00044 void registerConsumer(const std::string& name, 00045 CircularBufferBase::ConsumerPriority priority); 00046 // void unregisterConsumer (const std::string& name); 00047 int attachToEmptySubBuffer(D*& data, H*& header); 00048 int setWrittenSubBuffer(void); 00049 int write(const D& buffer, const H& header = H()); 00050 int read(D& buffer, const std::string& consumer); 00051 int read(D& buffer, H& header, const std::string& consumer); 00052 int read(D*& buffer, H*& header, const std::string& consumer); 00053 int setReadSubBuffer(const std::string& consumer); // Must be used in conjunction 00054 // with attachToEmptySubBuffer 00055 // because it attach to the 00056 // nextWritePointer buffer 00057 00058 bool isEmpty(void) const; 00059 unsigned int bufferSize(void) const { return numberOfSubBuffers_; } 00060 unsigned int numberOfWrittenBuffers(void) const; 00061 00062 const std::map<std::string, ConsumerStruct>& getConsumers(void) const 00063 { 00064 return consumers_; 00065 }; 00066 00067 void dumpStatus(std::ostream* out = (std::ostream*)&(std::cout)) const; 00068 00069 protected: 00070 const std::string mfSubject_; 00071 00072 private: 00073 enum 00074 { 00075 ErrorBufferFull = -1, 00076 ErrorBufferLocked = -2, 00077 ErrorBufferNotAvailable = -3, 00078 ErrorReadBufferOutOfSync = -4 00079 }; 00080 00081 const std::string producerName_; 00082 unsigned int numberOfSubBuffers_; 00083 std::map<std::string, ConsumerStruct> 00084 consumers_; // Pointers to the blocks which the consumers are reading 00085 int writePointer_; // Pointer to the available free buffer, -1 means no free buffers! 00086 std::atomic_bool* subBuffersStatus_; // Status of the Circular Buffer: 00087 std::vector<H> headers_; // Buffer Header 00088 std::vector<D> subBuffers_; // Buffers filled with data 00089 const bool bufferFree_; 00090 00091 unsigned int nextWritePointer(void); 00092 unsigned int nextReadPointer(const std::string& consumer); 00093 int getFreeBufferIndex(void); // can return -1 if there are no free buffers! 00094 unsigned int getReadPointer(const std::string& consumer); 00095 void setWritten(unsigned int subBuffer); 00096 void setFree(unsigned int subBuffer, const std::string& consumer); 00097 std::atomic_bool& isFree(unsigned int subBuffer) const; 00098 std::atomic_bool& isFree(unsigned int subBuffer, const std::string& consumer) const; 00099 00100 H& getHeader(unsigned int subBuffer); 00101 D& getSubBuffer(unsigned int subBuffer); 00102 void writeSubBuffer(unsigned int subBuffer, const D& buffer, const H& header); 00103 }; 00104 #include "otsdaq-core/DataManager/BufferImplementation.icc" 00105 00106 } // namespace ots 00107 #endif