-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.hpp
More file actions
29 lines (27 loc) · 809 Bytes
/
Copy pathqueue.hpp
File metadata and controls
29 lines (27 loc) · 809 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#pragma once
#include <stdint.h>
template <typename VALUE_TYPE, unsigned int N_QUEUE>
struct Queue_t {
volatile uint32_t head = 0;
volatile uint32_t tail = 0;
static_assert(
(N_QUEUE && ((N_QUEUE & (N_QUEUE - 1)) == 0)),
"queue size have to be a 2^N."
);
volatile VALUE_TYPE queue[N_QUEUE];
inline bool is_empty(){return head == tail;}
inline bool is_full(){return ((head+1)&(N_QUEUE -1)) == tail;}
inline bool append(VALUE_TYPE const e){
if( is_full() ) return true;
queue[head] = e;
head = ((head+1)&(N_QUEUE -1));
return false;
}
inline VALUE_TYPE pop(){
VALUE_TYPE tail_value = queue[tail];
tail = ((tail+1)&(N_QUEUE -1));
return tail_value;
}
inline void clear(){tail = head;}
inline uint32_t size(){return (head - tail) & (N_QUEUE-1);}
};