8 #include "STMDAQ-TestBeam/utils/queue.hh"
11 queue_buffer::queue_buffer()
14 for(
int chan = 0; chan < CHNUM; chan++)
23 occupancy[chan].store(0);
29 data_buffer[chan] =
new int16_t[RING_BUFFER_LEN];
34 int queue_buffer::try_push(
int chan, int16_t*
data,
int n)
37 uint64_t push_size = 0;
38 uint64_t push_len = 0;
41 current_tail[chan] = tail[chan].load();
50 write_num[chan] = occupancy[chan].load();
53 uint64_t space = RING_BUFFER_LEN - write_num[chan];
58 std::cout <<
"Space full" << std::endl;
70 push_size = push_len *
sizeof(int16_t);
95 if(increment(current_tail[chan], push_len) < current_tail[chan])
98 uint64_t end = RING_BUFFER_LEN - current_tail[chan];
100 memcpy(&data_buffer[chan][current_tail[chan]],
data, end *
sizeof(int16_t));
102 memcpy(&data_buffer[chan][0], &
data[end], (push_len - end) *
sizeof(int16_t));
108 memcpy(&data_buffer[chan][current_tail[chan]],
data, push_size);
112 occupancy[chan].fetch_add(push_len);
127 tail[chan].store(increment(current_tail[chan], push_len));
134 void queue_buffer::push(
int chan, int16_t*
data,
int n)
147 retval = try_push(chan,
data, n);
164 uint64_t queue_buffer::try_pull(
int chan, int16_t*&
data)
167 uint64_t pull_size = 0;
168 uint64_t pull_len = 0;
171 current_head[chan] = head[chan].load();
181 read_num[chan] = occupancy[chan].load();
187 if(read_num[chan] == 0)
190 return read_num[chan];
193 pull_len = read_num[chan];
194 pull_size = pull_len *
sizeof(int16_t);
217 if(increment(current_head[chan], pull_len) < current_head[chan])
220 uint64_t end = RING_BUFFER_LEN - current_head[chan];
223 memcpy(
data, &data_buffer[chan][current_head[chan]], end *
sizeof(int16_t));
225 memcpy(&
data[end], &data_buffer[chan][0], (pull_len - end) *
sizeof(int16_t));
231 memcpy(
data, &data_buffer[chan][current_head[chan]], pull_size);
235 occupancy[chan].fetch_sub(pull_len);
252 head[chan].store(increment(current_head[chan], pull_len));
260 uint64_t queue_buffer::pull(
bool* timeout,
int chan, int16_t*&
data)
264 uint64_t pull_len = 0;
266 while((pull_len = try_pull(chan,
data)) == 0 && !*timeout) {};