/
queue.h
56 lines (44 loc) · 1.25 KB
/
queue.h
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#ifndef queue_h_
#define queue_h_
#include <stdint.h>
#include <pthread.h>
/** Blocking queue data structure
*/
typedef struct __queue_t__ {
// backing buffer and size
uint8_t *buffer;
size_t size;
// backing buffer's memfd descriptor
int fd;
// read / write indices
size_t head;
size_t tail;
// sequence number of next consumable message
size_t head_seq;
// sequence number of last written message
size_t tail_seq;
// synchronization primitives
pthread_cond_t readable;
pthread_cond_t writeable;
pthread_mutex_t lock;
} queue_t;
/** Initialize a blocking queue *q* of size *s*
*/
void queue_init(queue_t *q, size_t s);
/** Destroy the blocking queue *q*
*/
void queue_destroy(queue_t *q);
/** Insert into queue *q* a message of *size* bytes from *buffer*
*
* Blocks until sufficient space is available in the queue.
*/
void queue_put(queue_t *q, uint8_t *buffer, size_t size);
/** Retrieves a message of at most *max* bytes from queue *q* and writes
* it to *buffer*.
*
* Blocks until a message of no more than *max* bytes is available.
*
* Returns the number of bytes in the written message.
*/
size_t queue_get(queue_t *q, uint8_t *buffer, size_t max);
#endif