-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayqueue.c
More file actions
80 lines (70 loc) · 1.38 KB
/
arrayqueue.c
File metadata and controls
80 lines (70 loc) · 1.38 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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include <assert.h>
#include "arrayqueue.h"
int
init_queue(queue_t *que, size_t max_length)
{
if ((que->ringbuf = (void **) calloc(max_length, sizeof(void *))) == NULL){
perror("malloc");
exit(0);
}
que->max_length = max_length;
que->length = 0;
que->head = que->tail = 0;
return 0;
}
int
delete_queue(queue_t *que)
{
free(que->ringbuf);
return 0;
}
int
queue_isempty(queue_t *que)
{
return (que->length == 0) ? 1 : 0;
}
int
queue_isfull(queue_t *que)
{
return (que->length == que->max_length) ? 1 : 0;
}
void
queue_push(queue_t *que, void *item)
{
assert(que->length < que->max_length);
que->ringbuf[que->head++] = item;
if (que->head == que->max_length) { que->head = 0; }
que->length++;
}
void *
queue_pop(queue_t *que)
{
void *item = NULL;
assert(que->length > 0);
item = que->ringbuf[que->tail++];
if (que->tail == que->max_length) { que->tail = 0; }
que->length--;
return item;
}
int
init_quecontrol(queue_control_t *ctrl)
{
pthread_mutex_init(&ctrl->mtx, NULL);
pthread_cond_init(&ctrl->more, NULL);
pthread_cond_init(&ctrl->less, NULL);
ctrl->active = 1;
return 0;
}
int
control_deactivate(queue_control_t *ctrl)
{
pthread_mutex_lock(&ctrl->mtx);
ctrl->active = 0;
pthread_cond_broadcast(&ctrl->more);
pthread_mutex_unlock(&ctrl->mtx);
return 0;
}