-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleQueue.c
More file actions
90 lines (74 loc) · 2.12 KB
/
SimpleQueue.c
File metadata and controls
90 lines (74 loc) · 2.12 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
81
82
83
84
85
86
87
88
89
90
#include <malloc.h>
#include <pthread.h>
#include <stdatomic.h>
#include "SimpleQueue.h"
struct SimpleQueueNode;
typedef struct SimpleQueueNode SimpleQueueNode;
struct SimpleQueueNode {
_Atomic(SimpleQueueNode*) next;
Value item;
};
SimpleQueueNode* SimpleQueueNode_new(Value item)
{
SimpleQueueNode* node = (SimpleQueueNode*)malloc(sizeof(SimpleQueueNode));
atomic_init(&node->next, NULL);
node->item = item;
return node;
}
struct SimpleQueue {
SimpleQueueNode* head;
SimpleQueueNode* tail;
pthread_mutex_t head_mtx;
pthread_mutex_t tail_mtx;
};
SimpleQueue* SimpleQueue_new(void)
{
SimpleQueue* queue = (SimpleQueue*)malloc(sizeof(SimpleQueue));
pthread_mutex_init(&queue->head_mtx, NULL);
pthread_mutex_init(&queue->tail_mtx, NULL);
SimpleQueueNode *node = SimpleQueueNode_new(EMPTY_VALUE);
queue->head = node;
queue->tail = node;
return queue;
}
void SimpleQueue_delete(SimpleQueue* queue)
{
pthread_mutex_destroy(&queue->head_mtx);
pthread_mutex_destroy(&queue->tail_mtx);
SimpleQueueNode *node = queue->head;
while (node != NULL) {
SimpleQueueNode *next = atomic_load(&node->next);
free(node);
node = next;
};
free(queue);
}
void SimpleQueue_push(SimpleQueue* queue, Value item)
{
SimpleQueueNode *new_node = SimpleQueueNode_new(item);
pthread_mutex_lock(&queue->tail_mtx);
atomic_store(&queue->tail->next, new_node);
queue->tail = new_node;
pthread_mutex_unlock(&queue->tail_mtx);
}
Value SimpleQueue_pop(SimpleQueue* queue)
{
pthread_mutex_lock(&queue->head_mtx);
SimpleQueueNode *begin = atomic_load(&queue->head->next);
if (begin == NULL) {
pthread_mutex_unlock(&queue->head_mtx);
return EMPTY_VALUE;
}
Value val = begin->item;
free(queue->head);
queue->head = begin;
pthread_mutex_unlock(&queue->head_mtx);
return val;
}
bool SimpleQueue_is_empty(SimpleQueue* queue)
{
pthread_mutex_lock(&queue->head_mtx);
SimpleQueueNode *next = atomic_load(&queue->head->next);
pthread_mutex_unlock(&queue->head_mtx);
return (next == NULL);
}