Repository navigation
Expand file tree
/
Copy pathCASLockFreeQueue.c
More file actions
208 lines (166 loc) · 3.86 KB
/
Copy pathCASLockFreeQueue.c
File metadata and controls
208 lines (166 loc) · 3.86 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* ---------------------------------------------------------------------------
* UnBounded LockFree Queue
*
* "Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms" by M. Michael and M. Scott
* http://www.cs.rochester.edu/u/scott/papers/1996_PODC_queues.pdf
*
* author: suzuki hironobu (hironobu@interdb.jp) 2009.Dec.03
* Copyright (C) 2009-2014 suzuki hironobu
*
* ---------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include "CASLockFreeQueue.h"
static node_t *create_node(const val_t);
static void free_node(node_t *);
static inline bool_t
#ifdef _X86_64_
cas(volatile pointer_t * addr, pointer_t oldp, const pointer_t newp)
{
char result;
__asm__ __volatile__("lock; cmpxchg16b %0; setz %1":"=m"(*addr),
"=q"(result)
:"m"(*addr), "a"(oldp.count), "d"(oldp.ptr),
"b"(newp.count), "c"(newp.ptr)
:"memory");
return (((int)result == 0) ? false:true);
}
#else
cas(volatile pointer_t * addr, const pointer_t oldp, const pointer_t newp)
{
char result;
__asm__ __volatile__("lock; cmpxchg8b %0; setz %1":"=m"(*addr),
"=q"(result)
:"m"(*addr), "a"(oldp.count), "d"(oldp.ptr),
"b"(newp.count), "c"(newp.ptr)
:"memory");
return (((int)result == 0) ? false:true);
}
#endif
static node_t *create_node(const val_t val)
{
node_t *node;
if ((node = (node_t *) calloc(1, sizeof(node_t))) == NULL) {
elog("calloc error");
return NULL;
}
node->val = val;
node->next.ptr = NULL;
node->next.count = 0;
return node;
}
static void free_node(node_t * node)
{
#ifdef _FREE_
free(node);
#endif
}
queue_t *init_queue(void)
{
queue_t *q;
node_t *node;
if ((q = (queue_t *) calloc(1, sizeof(queue_t))) == NULL) {
elog("calloc error");
return NULL;
}
if ((node = create_node((val_t)NULL)) == NULL) {
elog("create_node() error");
abort();
}
q->head.ptr = node;
q->tail.ptr = node;
return q;
}
void free_queue(queue_t * q)
{
free(q);
}
bool_t enq(queue_t * q, const val_t val)
{
node_t *newNode;
pointer_t tail, next, tmp;
if ((newNode = create_node(val)) == NULL)
return false;
while (1) {
tail = q->tail;
next = tail.ptr->next;
if (tail.count == q->tail.count && tail.ptr == q->tail.ptr) {
if (next.ptr == NULL) {
tmp.ptr = newNode;
tmp.count = next.count + 1;
if (cas(&tail.ptr->next, next, tmp) == true) {
break;
}
}
else {
tmp.ptr = next.ptr;
tmp.count = tail.count + 1;
cas(&q->tail, tail, tmp);
}
}
}
tmp.ptr = newNode; tmp.count = tail.count + 1;
cas(&q->tail, tail, tmp);
return true;
}
bool_t deq(queue_t * q, val_t * val)
{
pointer_t head, tail, next, tmp;
while (1) {
head = q->head;
tail = q->tail;
next = head.ptr->next;
if (head.count == q->head.count && head.ptr == q->head.ptr) {
if (head.ptr == tail.ptr) {
if (next.ptr == NULL) {
return false;
}
tmp.ptr = next.ptr;
tmp.count = head.count + 1;
cas(&q->tail, tail, tmp);
}
else {
*val = next.ptr->val;
tmp.ptr = next.ptr;
tmp.count = head.count + 1;
if (cas(&q->head, head, tmp) == true) {
break;
}
}
}
}
free_node (head.ptr);
return true;
}
void show_queue(queue_t * q)
{
node_t *curr;
curr = q->head.ptr;
while ((curr = curr->next.ptr) != NULL) {
printf("[%d]", (int) curr->val);
}
printf("\n");
}
#ifdef _SINGLE_THREAD_
queue_t *q;
int main(int argc, char **argv)
{
int i;
val_t val;
int max = 10;
q = init_queue();
for (i = 0; i < max; i++) {
enq(q, i);
show_queue(q);
}
for (i = 0; i < max; i++) {
deq(q, &val);
show_queue(q);
}
free_queue(q);
return 0;
}
#endif