-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmpmc_queue.h
More file actions
executable file
·252 lines (227 loc) · 6.28 KB
/
Copy pathmpmc_queue.h
File metadata and controls
executable file
·252 lines (227 loc) · 6.28 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#ifndef _MCT_MPMC_QUEUE_H_
#define _MCT_MPMC_QUEUE_H_
/*
* MPMC_QUEUE:
* ==========
* Multiple Producers, Multiple Consumers lock-free queue.
*
* What's special?
* ---------------
* Two advantages over queue protected by lock (like the
* skb_queue_xxx APIs in Linux kernel, which is the core
* utility of Linux kernel networking subsystem):
* 1) Producer and comsumer works parallely
* 2) One producer never get blocked by other producer.
* Same for comsumers. This means lock-free. (for precise
* definition of lock-free/wait-free, please refer to
* "The Art of Multiprocessor Programming" by Herlihy and
* Shavit.
*
* Implementation options and concerns
* -----------------------------------
* To overcome ABA problem, DCAS(Double Compare and Swap) is
* used for x86. For RISC processors with LL/SC primitives,
* the ABA problem is naturally avoided.
*
* Credit and Copyleft
* -------------------
* Invented and published by Maged M. Michael and Michael L.
* Scott in the article "Simple, Fast, and Practical Non-Blocking
* and Blocking Concurrent Queue Algorithms", PODC96.
*
* Implemented and tested by:
* Hong Zhiguo <honkiko@gmail.com>
* 2012.01.18
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*/
#include "mct_adapt.h"
struct mpmcq_node;
struct versioned_pointer {
struct mpmcq_node *ptr;
unsigned long version;
} __attribute__ (( __aligned__( 16 ) ));
/* for comparation of 2 versioned_pointer */
#define vp_equal(x,y) \
( ((x).ptr == ACCESS_ONCE((y).ptr)) \
&& \
((x).version == ACCESS_ONCE((y).version)) \
)
#define vp_assign(x,y) \
({ \
(x).ptr = ACCESS_ONCE((y).ptr); \
(x).version = ACCESS_ONCE((y).version); \
})
struct mpmcq_node {
struct versioned_pointer next;
void *user_data;
};
struct mpmc_queue {
volatile struct versioned_pointer head;
#ifndef CONFIG_SAME_CPU
char pading[MCT_CACHELINE_BYTES - sizeof(struct versioned_pointer)];
#endif
volatile struct versioned_pointer tail;
#ifdef HAVE_Q_LEN
#ifndef CONFIG_SAME_CPU
char pading2[MCT_CACHELINE_BYTES - sizeof(struct versioned_pointer)];
#endif
atomic_t len;
#endif
};
static inline int
mpmc_queue_init(struct mpmc_queue *q)
{
struct mpmcq_node *node;
if (!q){
return -1;
}
node = (struct mpmcq_node *)MCT_MALLOC(sizeof(struct mpmcq_node));
if (!node){
return -1;
}
node->next.ptr = NULL;
node->next.version = 0;
q->head.ptr = node;
q->head.version = 0;
q->tail.ptr = node;
q->tail.version = 0;
#ifdef HAVE_Q_LEN
atomic_set(&q->len, 0);
#endif
return 0;
}
static inline int
mpmc_queue_empty(struct mpmc_queue *q)
{
if (!q){
return 1;
}
smp_rmb();
return q->head.ptr->next.ptr == NULL;
}
static inline int
mpmc_enqueue(struct mpmc_queue *q, void *data)
{
struct mpmcq_node *node;
unsigned char swapped;
struct versioned_pointer tail;
struct versioned_pointer next;
struct versioned_pointer pnode; /*pointer to node*/
if (!q){
return -1;
}
node = (struct mpmcq_node *)MCT_MALLOC(sizeof(struct mpmcq_node));
if (!node){
return -1;
}
node->user_data = data;
node->next.ptr = NULL;
node->next.version = 0;
while(1) {
//tail = ACCESS_ONCE(q->tail);
vp_assign(tail,q->tail);
//next = tail.ptr->next;
vp_assign(next, tail.ptr->next);
barrier();
if ( vp_equal(tail, q->tail)){
if (next.ptr == NULL){
pnode.ptr = node;
pnode.version = next.version + 1;
swapped = double_cmp_and_swap(&tail.ptr->next, &next, &pnode);
if (swapped) {
break;
}
} else {
pnode.ptr = next.ptr;
pnode.version = tail.version + 1;
double_cmp_and_swap(&q->tail, &tail, &pnode);
}
}
} /*while*/
pnode.ptr = node;
pnode.version = tail.version + 1;
double_cmp_and_swap(&q->tail, &tail, &pnode);
#ifdef HAVE_Q_LEN
atomic_inc(&q->len);
#endif
return 0;
}
/**different than spsc_queue: user_data is returned, instead of node */
static inline void *
mpmc_dequeue(struct mpmc_queue *q)
{
unsigned char swapped;
void * p_user_data;
struct versioned_pointer head;
struct versioned_pointer tail;
struct versioned_pointer next;
struct versioned_pointer pnode;
if (!q){
return NULL;
}
while (1) {
//head = ACCESS_ONCE(q->head);
vp_assign(head, q->head);
//tail = ACCESS_ONCE(q->tail);
vp_assign(tail, q->tail);
//next = head.ptr->next;
vp_assign(next, head.ptr->next);
if (! vp_equal(head, q->head)) {
continue;
}
if (head.ptr == tail.ptr) {
if (next.ptr == NULL) {
return NULL;
}
pnode.ptr = next.ptr;
pnode.version = tail.version + 1;
double_cmp_and_swap(&q->tail, &tail, &pnode);
} else {
p_user_data = next.ptr->user_data;
pnode.ptr = next.ptr;
pnode.version = head.version + 1;
swapped = double_cmp_and_swap(&q->head, &head, &pnode);
if (swapped) {
break;
}
} /*if (head.ptr == tail.ptr)*/
} /*while (1)*/
#ifdef HAVE_Q_LEN
atomic_dec(&q->len);
#endif
MCT_FREE(head.ptr);
return p_user_data;
}
/*mpmp_queue_destroy must be called exclusively*/
static inline void
mpmc_queue_destroy(struct mpmc_queue *q)
{
void *data;
if (!q){
return;
}
while (!mpmc_queue_empty(q)) {
data = mpmc_dequeue(q);
if (data){
MCT_FREE(data);
}
}
/*free the dead node*/
MCT_FREE(q->head.ptr);
}
#ifdef HAVE_Q_LEN
static inline long
mpmc_queue_len(struct mpmc_queue *q)
{
if (!q){
return 0;
}
return atomic_read(&q->len);
}
#endif
#endif