otsdaq-mu2e-stm  5.02.01
queue.h
1 #ifndef testQueueMAIN_h_
2 #define testQueueMAIN_h_
3 
4 #include <iostream>
5 #include <thread>
6 #include <atomic>
7 #include <cinttypes>
8 #include <cstring>
9 #include <thread>
10 #include <mutex>
11 
12 static const int CHNUM = 2;
13 //static const int buffer_size = 65537; //65536; // power of 2 for efficient %
14 static const int buffer_size = 131072; //65536; // power of 2 for efficient %
15 static const int m_num = 100*buffer_size;
16 static const int push_max = 65536;
17 static const int packet_size = 8198;
18 static const int packet_len = packet_size/2;
19 template <typename T> class queue_buffer{
20 
21 public :
22  // Constructor
23  queue_buffer();
24  // Try to push data to queue
25  int try_push(int chan, T *data, int n, int index);
26  // Push data to queue
27  void push(int chan, T *data, int n);
28  // Try to pull data from queue
29  int try_pull(int chan, T *&data);
30  // Pull data from queue
31  // int pull(UDPsocket *udp, int chan, T *&data);
32  int pull(int chan, T *&data);
33  // Atomic write pointer
34  std::atomic<int64_t> write[CHNUM];
35  // Atomic read pointer
36  std::atomic<int64_t> read[CHNUM];
37  // Atomic memory checker
38  std::atomic<int64_t> numInBuffer[CHNUM];
39  // Current tail pointer
40  int64_t current_tail[CHNUM];
41  // Next tail pointer
42  int64_t next_tail[CHNUM];
43  // Current head pointer
44  int64_t current_head[CHNUM];
45  // number in push buffer
46  int64_t writeNum[CHNUM];
47  // number in pull buffer
48  int64_t readNum[CHNUM];
49  // Atomic fetch_add write pointer
50  int64_t writeNumInBuffer[CHNUM];
51  // Atomic fetch_sub read pointer
52  int64_t readNumInBuffer[CHNUM];
53  // The buffer size
54  static const int64_t size = buffer_size;
55  // The maximum number of datagrams to pull
56  int pull_max = m_num;
57  // Pull data pointer array
58  T pdata[CHNUM];
59  // The buffer
60  T* buffer[CHNUM];
61  // Increment function
62  int64_t increment(int n, int x){
63  return (n + x) % (size);
64  }
65  int64_t memPercent(int n, int x){
66  return n*(n / x);
67  }
68  bool startOfRun[CHNUM] = {true, true};
69 private :
70 };
71 
72 #endif
Definition: data.hh:4