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
/*
queue_t is a basic thread-safe non-blocking fixed-size queue.
put_queue returns false if the queue is full.
get_queue returns NULL if the queue is empty.
Example usage:
// Initialization:
queue_t fooq;
init_queue(&fooq);
// Producer:
foo_t *item = new_foo();
if (!put_queue(&fooq, item))
delete_foo(item);
// Consumer:
foo_t *item = get_queue(&fooq);
if (item)
{
do_something_with_foo(item);
delete_foo(item);
}
*/
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <pthread.h>
#define MAX_QUEUE_SIZE 512
typedef struct queue_t
{
void *items[MAX_QUEUE_SIZE];
size_t read_index, write_index;
size_t num_items;
pthread_mutex_t mutex;
} queue_t;
void init_queue(queue_t *q);
void reset_queue(queue_t *q);
void *put_queue_replace(queue_t *q, void *item);
bool put_queue(queue_t *q, void *item);
void *get_queue(queue_t *q);
/* Put the given item back onto this queue at the head.
(The next call to put_queue would return this item.)
Return true if successful, false if the queue was full */
bool requeue(queue_t *q, void *item);
/* Remove the last item queued.
Return the item or NULL if the queue was empty */
void *unqueue(queue_t *q);
typedef bool queue_matcher_t(void *wanted, void *candidate);
/* Unqueue the most recently queued item for which `matcher(wanted, candidate)`
returns true where `candidate` is an item currently on the queue.
Look only at the last `max_depth` items on the queue, at most.
Returns the candidate item, or NULL if none matches */
void *unqueue_matching(queue_t *q, size_t max_depth, queue_matcher_t *matcher, void *wanted);