forked from jserv/hungry-birds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.h
69 lines (58 loc) · 2.29 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
#ifndef MY_QUEUE_H
#define MY_QUEUE_H
#include <stdint.h>
typedef struct __QueueInternal *queue_p;
typedef enum __QueueResult {
QUEUE_FALSE,
QUEUE_SUCCESS,
QUEUE_TRUE,
QUEUE_OUT_OF_MEMORY = -1,
} QueueResult;
/**
* \brief An unbounded lockless single consumer multiple producer FIFO Queue.
*/
extern struct __QUEUE_API__ {
/** Create a new Queue object.
* @param size the storage size in bytes.
*/
queue_p (*create)(size_t size);
/** Push an element to the back of the queue.
* Pushing supports copying and moving. Pushing is considered a producer
* operation. Any thread can safely execute this operation at any time.
* @param data the region where the value stored will be copied from.
* @return QUEUE_OUT_OF_MEMORY if the heap is exhausted.
*/
QueueResult (*push)(queue_p, void *data);
/** Check if the queue has any data.
* The methid is considered a consumer operation, and only one thread may
* safely execute this at one time.
* @return QUEUE_TRUE if there is a front.
* @return QUEUE_FALSE if there is not.
*/
QueueResult (*hasFront)(queue_p);
/** Get the value at the front of the queue.
* You should always check that there is data in queue before calling
* front as there is no built in check. If no data is in the queue when
* front is called, memory violation likely happens.
* Getting data is considered a consumer operation, only one thread may
* safely execute this at one time.
* @param data the destination where value stored will be copied to
*/
QueueResult (*front)(queue_p, void *data);
/** Remove the item at the front of the queue.
* You should always check that there is data in queue before popping as
* there is no built in check. If no data is in the queue when pop is
* called, memory violation likely happens.
* Popping is considered a consumer operation, and only one thread may
* safely execute this at one time.
*/
QueueResult (*pop)(queue_p);
/** Clear the entire queue.
* You should always clear it before deleting the Queue itself, otherwise
* you will leak memory.
*/
QueueResult (*clear)(queue_p);
/** Destroy the queue object */
QueueResult (*destroy)(queue_p);
} Queue;
#endif