-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
83 lines (64 loc) · 1.75 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
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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
typedef struct queue_t {
struct linked_list_t *head;
struct linked_list_t* tail;
pthread_mutex_t mutex;
pthread_cond_t wakeup;
} queue_t;
typedef struct linked_list_t {
void *data;
struct linked_list_t* next;
} linked_list_t;
queue_t* queue_create() {
struct queue_t* queue = (struct queue_t*)malloc(sizeof(struct queue_t));
queue->head = NULL;
queue->tail = NULL;
queue->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
queue->wakeup = (pthread_cond_t)PTHREAD_COND_INITIALIZER;
return queue;
}
void queue_push(queue_t* queue, void* x) {
pthread_mutex_lock(&queue->mutex);
struct linked_list_t* new_node = (struct linked_list_t*)malloc(sizeof(struct linked_list_t));
new_node->data = x;
new_node->next = NULL;
if (queue->head == NULL && queue->tail == NULL) {
queue->head = queue->tail = new_node;
} else {
queue->tail->next = new_node;
queue->tail = new_node;
}
pthread_mutex_unlock(&queue->mutex);
pthread_cond_signal(&queue->wakeup);
}
void *queue_pop(queue_t* queue) {
pthread_mutex_lock(&queue->mutex);
while(queue->head == NULL) {
pthread_cond_wait(&queue->wakeup, &queue->mutex);
}
struct linked_list_t* current_head = queue->head;
void* data = current_head->data;
queue->head = current_head->next;
if (queue->head == NULL) {
// no head no tail
queue->tail == NULL;
}
free(current_head);
pthread_mutex_unlock(&queue->mutex);
return data;
}
int main() {
queue_t* my_queue = queue_create();
char* hello = "hello";
queue_push(my_queue, hello);
char* world = "world";
queue_push(my_queue, world);
char *result;
result = queue_pop(my_queue);
printf("Popped: %s\n", result);
result = queue_pop(my_queue);
printf("Popped: %s\n", result);
}