-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathmqueue.c
More file actions
300 lines (243 loc) · 7.26 KB
/
Copy pathmqueue.c
File metadata and controls
300 lines (243 loc) · 7.26 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* Copyright (c) 2014, Zhiyong Liu <Neesenk at gmail dot com>
* All rights reserved.
*/
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include "mqueue.h"
#define container_of(ptr, type, member) (type *)((char *)(ptr) - offsetof(type,member))
// 平台相关函数
#define likely(e) __builtin_expect((e), 1)
#define unlikely(e) __builtin_expect((e), 0)
#define compare_and_swap(ptr, old, new) __atomic_compare_exchange_n(ptr, &(old), new, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED)
#define wmb() __atomic_thread_fence(__ATOMIC_RELEASE)
#define rmb() __atomic_thread_fence(__ATOMIC_ACQUIRE)
#define mb() __atomic_thread_fence(__ATOMIC_SEQ_CST)
#define atomic_load(n) __atomic_load_n(&(n), __ATOMIC_RELAXED)
#define atomic_fetch_add(n, num) __atomic_fetch_add(&(n), (num), __ATOMIC_RELAXED)
#define cpu_relax() do { asm volatile("pause\n":::"memory"); } while (0)
#define get_cpunum() get_nprocs()
#define CACHE_LINE_SIZE 64
#define ITEM(q, i) ((struct item *)((q)->items + (i) * (q)->size))
#define ITEM_IDX(q, ptr) (((char *)(ptr) - (q)->items) / (q)->size)
#define TAIL_IDX 0xFFFFFFFF
#define UNUSED_FLAG 0xFFFFFFFE
typedef uint32_t __attribute__ ((aligned((CACHE_LINE_SIZE * 2)))) aligned_uint32_t;
typedef uint64_t __attribute__ ((aligned((CACHE_LINE_SIZE * 2)))) aligned_uint64_t;
struct item {
volatile uint32_t next;
char content[];
};
struct mqueue {
size_t nmemb; // 队列大小
size_t size; // 队列每一项的占内存大小
volatile aligned_uint32_t head; // 队列的头部
// free高32位用来表示在数组中的下标,低32位表示设置free的序号
// 这样做的是为了防止ABA问题产生
volatile aligned_uint64_t free; // 未使用的元素的个数
char items[];
};
struct mqueuebatch {
int wseq; // writer加入的次数
int qnum; // queues的大小
struct mqueue *queues[];
};
void mqueuebatch_reader_init(struct mqueuebatch_reader *reader, struct mqueuebatch *qs)
{
reader->queueid = 0;
reader->queuebatch = qs;
}
void mqueuebatch_writer_init(struct mqueuebatch_writer *writer, struct mqueuebatch *qs)
{
int seq = atomic_fetch_add(qs->wseq, 1);
writer->queue = qs->queues[seq % qs->qnum];
writer->queuebatch = qs;
}
struct mqueue *mqueue_create(size_t nmemb, size_t size)
{
size_t i = 0;
struct mqueue *q = 0;
size = ((size + sizeof(struct item) + 7) / 8) * 8; // 8字节对齐
q = (struct mqueue*)calloc(1, sizeof(*q) + size * nmemb);
if (q == NULL)
return NULL;
q->nmemb= nmemb;
q->free = 0;
q->head = TAIL_IDX;
q->size = size;
for (i = 0; i < nmemb - 1; i++)
ITEM(q, i)->next = i + 1;
ITEM(q, i)->next = TAIL_IDX;
mb();
return q;
}
void mqueue_destroy(struct mqueue *q)
{
free(q);
}
void mqueuebatch_destroy(struct mqueuebatch *qs)
{
size_t i = 0;
for (i = 0; i < qs->qnum; i++) {
if (qs->queues[i])
mqueue_destroy(qs->queues[i]);
}
free(qs);
}
struct mqueuebatch *mqueuebatch_create(size_t nmemb, size_t size)
{
size_t i = 0;
size_t qnum = get_cpunum();
struct mqueuebatch *qs = (struct mqueuebatch*)calloc(1, sizeof(*qs) + sizeof(struct mqueue *) * qnum);
if (qs == NULL)
return NULL;
assert(qnum > 0);
nmemb = (nmemb + qnum - 1) / qnum;
qs->qnum = qnum;
for (i = 0; i < qnum; i++) {
if ((qs->queues[i] = mqueue_create(nmemb, size)) == NULL) {
mqueuebatch_destroy(qs);
return NULL;
}
}
return qs;
}
void *mqueue_writer_parpare(struct mqueue *q)
{
struct item *ret = NULL;
uint64_t oldf, newf;
uint32_t freen, seq;
for (;;) {
rmb();
oldf = atomic_load(q->free);
freen = oldf >> 32;
seq = (oldf & 0xFFFFFFFF) + 1;
if (unlikely(freen == TAIL_IDX))
return NULL;
ret = ITEM(q, freen);
newf = ((uint64_t)ret->next << 32) | seq;
if (compare_and_swap(&q->free, oldf, newf)) {
ret->next = UNUSED_FLAG;
return ret->content;
}
cpu_relax();
}
return NULL;
}
void *mqueuebatch_writer_parpare(struct mqueuebatch_writer *writer)
{
return mqueue_writer_parpare(writer->queue);
}
void mqueue_writer_commit(struct mqueue *q, void *ptr)
{
uint32_t head = 0;
struct item *item = container_of(ptr, struct item, content);
uint32_t idx = ITEM_IDX(q, item);
assert(item->next == UNUSED_FLAG && idx < q->nmemb);
for (;;) {
rmb();
head = item->next = atomic_load(q->head);
wmb();
if (compare_and_swap(&q->head, head, idx))
return;
cpu_relax();
}
}
void mqueuebatch_writer_commit(struct mqueuebatch_writer *writer, void *ptr)
{
mqueue_writer_commit(writer->queue, ptr);
}
void *mqueue_reader_next(struct reader_result *res)
{
if (res->curr == NULL) {
res->curr = res->header;
return res->curr->content;
}
if (res->curr->next == TAIL_IDX) {
assert(res->curr == res->tail);
return NULL;
}
assert(res->curr->next < res->q->nmemb);
res->curr = ITEM(res->q, res->curr->next);
return res->curr->content;
}
void *mqueuebatch_reader_next(struct reader_result *res)
{
return mqueue_reader_next(res);
}
size_t mqueue_reader_parpare(struct mqueue *q, struct reader_result *ret)
{
uint32_t head = 0, prev = TAIL_IDX, next = 0;
struct item *item = NULL, *tail = NULL;
size_t n = 1;
for (;;) {
rmb();
head = atomic_load(q->head);
if (head == TAIL_IDX)
return 0;
if (compare_and_swap(&q->head, head, TAIL_IDX))
break;
cpu_relax();
}
assert(head < q->nmemb);
// 反转链表
tail = item = ITEM(q, head);
while (item->next != TAIL_IDX) {
next = item->next;
item->next = prev;
prev = head;
head = next;
item = ITEM(q, head);
n++;
}
item->next = prev;
ret->nmemb = n;
ret->q = q;
ret->header= item;
ret->tail = tail;
ret->curr = NULL;
return n;
}
size_t mqueuebatch_reader_parpare(struct mqueuebatch_reader *reader, struct reader_result *ret)
{
size_t i = 0, n = 0;
size_t qnum = reader->queuebatch->qnum;
for (i = 0; i < qnum; i++) {
struct mqueue *q = reader->queuebatch->queues[(reader->queueid++) % qnum];
n = mqueue_reader_parpare(q, ret);
if (n > 0)
return n;
}
return 0;
}
void mqueue_reader_commit(struct mqueue *q, struct reader_result *res)
{
uint32_t freen, seq;
uint64_t oldf, newf;
uint32_t idx = ITEM_IDX(q, res->header);
assert(idx < q->nmemb);
assert(res->q == q);
for (;;) {
rmb();
oldf = atomic_load(q->free);
freen = oldf >> 32;
seq = (oldf & 0xFFFFFFFF) + 1;
newf = ((uint64_t)idx << 32) | seq;
res->tail->next = freen;
wmb();
if (compare_and_swap(&q->free, oldf, newf))
return;
cpu_relax();
}
}
void mqueuebatch_reader_commit(struct mqueuebatch_reader *reader, struct reader_result *res)
{
return mqueue_reader_commit(res->q, res);
}