-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
78 lines (73 loc) · 1.81 KB
/
queue.c
File metadata and controls
78 lines (73 loc) · 1.81 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
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <assert.h>
#include "queue.h"
#include <stdbool.h>
// Queue Struct
typedef struct queue {
int in;
int out;
int size;
int count;
void **buffer;
pthread_mutex_t lock;
pthread_cond_t condition;
} queue_t;
// Allocation and Initialization
queue_t *queue_new(int size) {
queue_t *q = malloc(sizeof(queue_t));
assert(q != NULL);
// Initialize queue
q->in = 0;
q->out = 0;
q->size = size;
q->count = 0;
q->buffer = malloc(sizeof(void *) * size);
assert(q->buffer != NULL);
// pthread init
pthread_mutex_init(&q->lock, NULL);
pthread_cond_init(&q->condition, NULL);
return q;
}
// Free Memory
void queue_delete(queue_t **q) {
if (*q != NULL) {
free((*q)->buffer);
pthread_mutex_destroy(&(*q)->lock);
pthread_cond_destroy(&(*q)->condition);
free(*q);
*q = NULL;
}
}
// Add Element to Queue
bool queue_push(queue_t *q, void *elem) {
bool result = true;
pthread_mutex_lock(&q->lock);
while (q->count >= q->size) {
pthread_cond_wait(&q->condition, &q->lock);
}
q->buffer[q->in] = elem;
q->in = (q->in + 1) % q->size;
q->count++;
pthread_cond_broadcast(&q->condition);
pthread_mutex_unlock(&q->lock);
return result;
}
// Remove Element from Queue
bool queue_pop(queue_t *q, void **elem) {
bool result = false;
if (q != NULL && elem != NULL) {
pthread_mutex_lock(&q->lock);
while (q->count <= 0) {
pthread_cond_wait(&q->condition, &q->lock);
}
*elem = q->buffer[q->out];
q->out = (q->out + 1) % q->size;
q->count--;
pthread_cond_broadcast(&q->condition);
pthread_mutex_unlock(&q->lock);
result = true;
}
return result;
}