Repository navigation
Expand file tree
/
Copy pathqueue.c
More file actions
77 lines (64 loc) · 1.44 KB
/
Copy pathqueue.c
File metadata and controls
77 lines (64 loc) · 1.44 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
#include "queue.h"
Queue *queueInit(void) {
Queue *q = (Queue *)malloc(sizeof(Queue));
if (q == NULL) {
fprintf(stderr, "queue: queue init failed");
return NULL;
}
q->empty = 1;
q->full = 0;
q->head = 0;
q->tail = 0;
q->mut = (pthread_mutex_t *)malloc(sizeof (pthread_mutex_t));
if (q->mut == NULL) {
fprintf(stderr, "queue: mutex key init failed");
return NULL;
}
pthread_mutex_init(q->mut, NULL);
q->notFull = (pthread_cond_t *)malloc(sizeof (pthread_cond_t));
if (q == NULL) {
fprintf(stderr, "queue: notFull init failed");
return NULL;
}
pthread_cond_init (q->notFull, NULL);
q->notEmpty = (pthread_cond_t *)malloc(sizeof (pthread_cond_t));
if (q == NULL) {
fprintf(stderr, "queue: notEmpty init failed");
return NULL;
}
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, Job in) {
q->buf[q->tail] = in;
q->tail++;
if (q->tail == QUEUE_SIZE) {
q->tail = 0;
}
if (q->tail == q->head) {
q->full = 1;
}
q->empty = 0;
return;
}
void queuePop(Queue *q, Job *out) {
*out = q->buf[q->head];
q->head++;
if (q->head == QUEUE_SIZE) {
q->head = 0;
}
if (q->head == q->tail) {
q->empty = 1;
}
q->full = 0;
return;
}