-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqueue.h
More file actions
99 lines (82 loc) · 1.76 KB
/
queue.h
File metadata and controls
99 lines (82 loc) · 1.76 KB
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
/*
* fqueue.h
*
* Created on: Jan 27, 2013
* Author: mike
*/
#ifndef QUEUE_H_
#define QUEUE_H_
#include <pthread.h>
#include <semaphore.h>
/**
* A very simple linked list based item for our queue. The data can
* be whatever the user wants, but it must be freed by the caller
*/
struct queue_item {
/**
* The data in our item
*/
void *data;
/**
* Next item in our queue
*/
struct queue_item *next;
};
/**
* Thread-safe blocking queue with a maximum number of items. The
* queue will block when empty or when the maximum number of items
* is reached. When you're done with the queue just call fq_fin()
* to add a NULL element which will unblock the queue such that
* consumers can finish and we can join worker threads
*/
typedef struct _fqueue {
/**
* Our items
*/
struct queue_item *items;
/**
* Fist, last items
*/
struct queue_item *first, *last;
/**
* Maximum length of queue
*/
unsigned int max_len;
/**
* Length of our queue
*/
unsigned int len;
/**
* Mutex, for exclusive access to our queue
*/
pthread_mutex_t mutex;
/**
* Condition variable to block when we're full
*/
pthread_cond_t full;
/**
* Condition variable to block when we're empty
*/
pthread_cond_t empty;
} fqueue;
/**
* Initialize a queue with a maximum length
*/
int fq_init(fqueue *queue, size_t max_len);
/**
* Add an item to our queue
*/
int fq_add(fqueue *queue, void *data);
/**
* Get something from the queeue
*/
int fq_get(fqueue *queue, void **data);
/**
* Add a null element which will flag our queue as done
*/
int fq_fin(fqueue *queue);
/**
* Free a queue
*/
int fq_free(fqueue *queue);
#endif /* QUEUE_H_ */