-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.h
116 lines (103 loc) · 2.35 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#ifndef QUEUE_H
#define QUEUE_H
#include <stdbool.h>
#include <stddef.h>
#include <pthread.h>
/**
* Thread-safe queue
*/
struct queue {
char *data;
size_t item_size;
size_t size;
pthread_mutex_t mutex;
pthread_cond_t not_empty;
volatile bool terminated;
volatile size_t head;
volatile size_t tail;
};
/**
* Initialize a queue
*
* @param q queue
* @param item_size item size in bytes
* @param max_items maximum number of items in the queue
*
* @return false if memory allocation failed
*/
bool queue_init(struct queue *q, size_t item_size, size_t max_items);
/**
* Resize the queue
*
* @param q queue
* @param q queue
* @param item_size item size in bytes
* @param max_items maximum number of items in the queue
*
* @return false if memory allocation failed
*/
bool queue_resize(struct queue *q, size_t max_items);
/**
* Destroy the queue and allocated resources. Call only after all threads have
* stopped using the queue
*
* @param q queue
*/
void queue_deinit(struct queue *q);
/**
* Terminate the queue. This will cause all blocking operations to return
*
* @param q queue
*/
void queue_terminate(struct queue *q);
/**
* Check if the queue is full
*
* @param q queue
*/
bool queue_full(struct queue *q);
/**
* Check if the queue is empty
*
* @param q queue
*/
bool queue_empty(struct queue *q);
/**
* Push an item to the queue. This function will block if the queue is full
*
* @param q queue
* @param v item to push
* @param timeout_ms timeout in milliseconds, 0 will block forever
*
* @return true on success
*/
bool queue_push(struct queue *q, void *v, unsigned int timeout_ms);
/**
* Push an item to the queue (non-blocking).
*
* @param q queue
* @param v item to push
*
* @return false if the queue is full
*/
bool queue_push_noblock(struct queue *q, void *v);
/**
* Pop an item from the queue. This function will block if the queue is empty
*
* @param q queue
* @param v pointer to store the item
* @param timeout_ms timeout in milliseconds, 0 will block forever
*
* @return true if valid item was popped
*/
bool queue_pop(struct queue *q, void *v, unsigned int timeout_ms);
/**
* Pop an item from the queue (non-blocking)
*
* @param q queue
* @param v pointer to store the item
*
* @return false if the queue is empty
*/
bool queue_pop_noblock(struct queue *q, void *v);
#endif // QUEUE_H