blob: 9de62ab544782a11221fb481b00ad29ed313e9d2 (
plain)
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
|
#pragma once
#include <stdint.h>
#include <pthread.h>
struct queue_entry {
struct queue_entry *next;
};
/// @brief a FIFO queue that holds a limited number of elements.
///
/// The queue is blocking, i.e. any attempt to push to a full
/// queue or read from an empty one will block indefinitely.
///
/// When tearing down the queue, blocked processes may be
/// unblocked by calling bounded_queue_kill(), which will
/// cause all blocked processes to return with error status.
struct bounded_queue {
struct queue_entry *head;
struct queue_entry *tail;
uint32_t count;
uint32_t limit;
int killed;
pthread_mutex_t mutex;
pthread_cond_t cond;
};
int bounded_queue_init (struct bounded_queue *q, uint32_t limit);
int bounded_queue_push (struct bounded_queue *q, struct queue_entry *e);
struct queue_entry *bounded_queue_pop (struct bounded_queue *q);
int bounded_queue_kill (struct bounded_queue *q);
|