-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRingsQueue.c
139 lines (117 loc) · 3.68 KB
/
RingsQueue.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <malloc.h>
#include <pthread.h>
#include <stdatomic.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "HazardPointer.h"
#include "RingsQueue.h"
struct RingsQueueNode;
typedef struct RingsQueueNode RingsQueueNode;
struct RingsQueueNode {
_Atomic(RingsQueueNode*) next;
Value buffer[RING_SIZE];
int push_idx;
int pop_idx;
_Atomic int free_slots;
};
Value getValue(RingsQueueNode* node) {
Value val = node->buffer[node->pop_idx];
node->pop_idx = ((node->pop_idx + 1) % RING_SIZE);
atomic_fetch_add(&(node->free_slots), 1);
return val;
}
void pushValue(RingsQueueNode* node, Value val) {
node->buffer[node->push_idx] = val;
node->push_idx = ((node->push_idx + 1) % RING_SIZE);
atomic_fetch_sub(&(node->free_slots), 1);
}
RingsQueueNode* RingsQueueNode_new() {
RingsQueueNode* node = (RingsQueueNode*)malloc(sizeof(RingsQueueNode));
assert(node != NULL);
node->push_idx = 0;
node->pop_idx = 0;
atomic_init(&node->free_slots, RING_SIZE);
atomic_init(&(node->next), NULL);
return node;
}
struct RingsQueue {
RingsQueueNode* head;
RingsQueueNode* tail;
pthread_mutex_t pop_mtx;
pthread_mutex_t push_mtx;
};
RingsQueue* RingsQueue_new(void) {
RingsQueue* queue = (RingsQueue*)malloc(sizeof(RingsQueue));
RingsQueueNode* node = RingsQueueNode_new();
queue->head = node;
queue->tail = node;
pthread_mutex_init(&queue->pop_mtx, NULL);
pthread_mutex_init(&queue->push_mtx, NULL);
return queue;
}
void RingsQueue_delete(RingsQueue* queue) {
pthread_mutex_destroy(&queue->pop_mtx);
pthread_mutex_destroy(&queue->push_mtx);
RingsQueueNode* node = queue->head;
while(node != NULL) {
RingsQueueNode* next = atomic_load(&node->next);
free(node);
node = next;
}
free(queue);
}
RingsQueueNode* RingsQueueNode_new_with_value(Value val) {
RingsQueueNode* node = (RingsQueueNode*)malloc(sizeof(RingsQueueNode));
assert(node != NULL);
node->buffer[0] = val;
node->push_idx = 1;
node->pop_idx = 0;
atomic_init(&node->free_slots, RING_SIZE - 1);
atomic_init(&(node->next), NULL);
return node;
}
void RingsQueue_push(RingsQueue* queue, Value item) {
pthread_mutex_lock(&queue->push_mtx);
if (atomic_load(&queue->tail->free_slots) > 0) {
pushValue(queue->tail, item);
}
//Last node full.
else {
RingsQueueNode* new_tail = RingsQueueNode_new_with_value(item);
atomic_store(&queue->tail->next, new_tail);
queue->tail = new_tail;
}
pthread_mutex_unlock(&queue->push_mtx);
}
Value RingsQueue_pop(RingsQueue* queue) {
Value val = EMPTY_VALUE;
pthread_mutex_lock(&(queue->pop_mtx));
RingsQueueNode* head = queue->head;
//When head empty and has next node.
if (atomic_load(&head->next) != NULL &&
atomic_load(&head->free_slots) == RING_SIZE) {
RingsQueueNode* new_head = atomic_load(&head->next);
//Take the first element from node (new head).
free(head);
queue->head = new_head;
val = getValue(new_head);
}
//Head not empty.
else if (atomic_load(&head->free_slots) < RING_SIZE) {
val = getValue(head);
}
//Head empty and no next node - return empty value.
pthread_mutex_unlock(&(queue->pop_mtx));
return val;
}
bool RingsQueue_is_empty(RingsQueue* queue) {
bool empty = true;
pthread_mutex_lock(&(queue->pop_mtx));
RingsQueueNode* head = queue->head;
if (atomic_load(&head->free_slots) < RING_SIZE || atomic_load(&head->next) != NULL) {
empty = false;
}
pthread_mutex_unlock(&(queue->pop_mtx));
return empty;
}