-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
56 lines (47 loc) · 1.25 KB
/
queue.c
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
#include "queue.h"
Queue *queueInit(int size) {
Queue *q;
q = (Queue *)malloc(sizeof(Queue));
if (q == NULL)
return NULL;
q->size = size;
q->buf = (WorkFunction *)malloc(q->size * sizeof(WorkFunction));
q->empty = 1;
q->full = 0;
q->head = 0;
q->tail = 0;
q->mut = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
pthread_mutex_init(q->mut, NULL);
q->notFull = (pthread_cond_t *)malloc(sizeof(pthread_cond_t));
pthread_cond_init(q->notFull, NULL);
q->notEmpty = (pthread_cond_t *)malloc(sizeof(pthread_cond_t));
pthread_cond_init(q->notEmpty, NULL);
return q;
}
void queueDelete(Queue *q) {
pthread_mutex_destroy (q->mut);
free (q->mut);
pthread_cond_destroy (q->notFull);
free (q->notFull);
pthread_cond_destroy (q->notEmpty);
free (q->notEmpty);
free (q);
}
void queueAdd(Queue *q, WorkFunction in) {
q->buf[q->tail] = in;
q->tail++;
if (q->tail == q->size)
q->tail = 0;
if (q->tail == q->head)
q->full = 1;
q->empty = 0;
}
void queueDel(Queue *q, WorkFunction *out) {
*out = q->buf[q->head];
q->head++;
if (q->head == q->size)
q->head = 0;
if (q->head == q->tail)
q->empty = 1;
q->full = 0;
}