otsdaq  3.11.00
CircularBuffer.h
1 #ifndef _ots_CircularBuffer_h_
2 #define _ots_CircularBuffer_h_
3 
4 #include "otsdaq/DataManager/BufferImplementation.h"
5 #include "otsdaq/DataManager/CircularBufferBase.h"
6 
7 #include "otsdaq/Macros/CoutMacros.h"
8 #include "otsdaq/MessageFacility/MessageFacility.h"
9 
10 #include <atomic>
11 #include <iostream>
12 #include <map>
13 #include <string>
14 
15 namespace ots
16 {
17 template<class D, class H>
19 {
20  public:
21  CircularBuffer(const std::string& dataBufferId);
22  virtual ~CircularBuffer(void);
23 
24  void reset(void);
25  void resetConsumerList(void);
26  bool isEmpty(void) const;
27  unsigned int getTotalNumberOfSubBuffers(void) const;
28  unsigned int getProducerBufferSize(const std::string& producerID) const;
29  unsigned int getProducerWrittenBuffers(const std::string& producerID) const;
30 
31  inline int read(D& buffer, const std::string& consumerID)
32  {
33  H dummyHeader;
34  return read(buffer, dummyHeader, consumerID);
35  }
36 
37  inline int read(D& buffer, H& header, const std::string& consumerID)
38  {
39  setNextProducerBuffer(consumerID);
40  unsigned int readCounter = theBuffer_.size() - 1;
41  // ++megaCounter_;
42  // if(megaCounter_%10000000 == 0)
43  // std::cout << __COUT_HDR_FL__ << __COUT_HDR_FL__
44  // << "Consumer: " << consumerID
45  // << " Reading producer: " << lastReadBuffer_[consumerID]->first
46  // << " Buffer empty? " << lastReadBuffer_[consumerID]->second.isEmpty()
47  // << " written buffers: " <<
48  // lastReadBuffer_[consumerID]->second.numberOfWrittenBuffers()
49  // << std::endl;
50  int readReturnVal;
51  while((readReturnVal = lastReadBuffer_[consumerID]->second.read(
52  buffer, header, consumerID)) < 0 &&
53  readCounter > 0)
54  {
55  setNextProducerBuffer(consumerID);
56  --readCounter;
57  }
58  return readReturnVal;
59  }
60 
61  int read(D*& buffer, H*& header, const std::string& consumerID)
62  {
63  setNextProducerBuffer(consumerID);
64  unsigned int readCounter = theBuffer_.size() - 1;
65  // ++megaCounter_;
66  // if(megaCounter_%10000000 == 0)
67  // std::cout << __COUT_HDR_FL__ << __COUT_HDR_FL__
68  // << "Consumer: " << consumerID
69  // << " Reading producer: " << lastReadBuffer_[consumerID]->first
70  // << " Buffer empty? " << lastReadBuffer_[consumerID]->second.isEmpty()
71  // << " written buffers: " <<
72  // lastReadBuffer_[consumerID]->second.numberOfWrittenBuffers()
73  // << std::endl;
74  int readReturnVal;
75  while((readReturnVal = lastReadBuffer_[consumerID]->second.read(
76  buffer, header, consumerID)) < 0 &&
77  readCounter > 0)
78  {
79  setNextProducerBuffer(consumerID);
80  --readCounter;
81  }
82  return readReturnVal;
83  }
84 
85  BufferImplementation<D, H>& getLastReadBuffer(const std::string& consumerID)
86  {
87  return lastReadBuffer_[consumerID]->second;
88  }
89  BufferImplementation<D, H>& getBuffer(const std::string& producerID)
90  {
91  // __COUTV__(producerID);
92  // __COUTV__(int(theBuffer_.find(producerID) == theBuffer_.end()));
93  return theBuffer_[producerID];
94  }
95 
96  // void unregisterConsumer (const std::string& consumerID);
97  // void unregisterProducer (const std::string& producerID);
98 
99  private:
100  std::map<std::string /*producer id*/,
101  BufferImplementation<D, H> /*one producer, many consumers*/>
102  theBuffer_;
103 
104  void registerProducer(const std::string& producerID,
105  unsigned int numberOfSubBuffers = 100);
106  void registerConsumer(const std::string& consumerID,
108 
109  void setNextProducerBuffer(const std::string& consumer);
110 
111  std::map<std::string /*consumer id*/,
112  /*iterator within producer map*/ typename std::
113  map<std::string, BufferImplementation<D, H>>::iterator>
114  lastReadBuffer_;
115 
116  std::map<std::string, CircularBufferBase::ConsumerPriority> consumers_;
117 };
118 #include "otsdaq/DataManager/CircularBuffer.icc"
119 
120 } // namespace ots
121 #endif
unsigned int getProducerWrittenBuffers(const std::string &producerID) const
sub-buffers written but not yet read by every consumer
void reset(void)
This DOES NOT reset the consumer list.
defines used also by OtsConfigurationWizardSupervisor