-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfifo.c
More file actions
471 lines (405 loc) · 9.71 KB
/
fifo.c
File metadata and controls
471 lines (405 loc) · 9.71 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <errno.h>
#include <atomic_ops.h>
#include <limits.h>
#include <stddef.h>
#include <string.h>
#include <stdint.h>
#include "fifo.h"
#define likely(cond) __builtin_expect((cond), 1)
#define unlikely(cond) __builtin_expect((cond), 0)
#ifndef FIFO_OVERRIDE
#define USE_EVENTFD 1
#define EVENTFD_NONBLOCKING 0
#define USE_EVENTFD_EMULATION 0
#endif
#define NO_LIBC_EVENTFD
#if USE_EVENTFD_EMULATION && USE_EVENTFD
char *fifo_implementation_type = "eventfd emulation via pipe(2)";
#elif USE_EVENTFD
#if EVENTFD_NONBLOCKING
char *fifo_implementation_type = "non-blocking eventfd(2)";
#else
char *fifo_implementation_type = "eventfd(2)";
#endif
#else
char *fifo_implementation_type = "futex(2)";
#endif
#if USE_EVENTFD
#include <poll.h>
#if USE_EVENTFD_EMULATION
typedef uint32_t eventfd_t;
#else /* USE_EVENTFD_EMULATION */
#ifdef NO_LIBC_EVENTFD
typedef uint64_t eventfd_t;
static
int eventfd(unsigned initval, int flags)
{
return syscall(__NR_eventfd, initval, flags);
}
#else
#include <sys/eventfd.h>
#endif /* ! NONLIBC_EVENTFD */
#endif /* ! USE_EVENTFD_EMULATION */
#else /* ! USE_EVENTFD */
#include <linux/futex.h>
#endif
#define SPIN_COUNT 256
__attribute__((aligned(64)))
int64_t fifo_reader_exchange_count;
int64_t fifo_writer_wake_count;
int64_t fifo_reader_wait_spins;
int64_t fifo_reader_wait_calls;
__attribute__((aligned(64)))
int64_t fifo_writer_exchange_count;
int64_t fifo_reader_wake_count;
int64_t fifo_writer_wait_spins;
int64_t fifo_writer_wait_calls;
#if USE_EVENTFD
static __attribute__((unused))
int file_flags_change(int fd, int and_mask, int or_mask)
{
int flags;
int err = fcntl(fd, F_GETFL, (long)&flags);
if (err < 0)
return err;
flags = (flags & and_mask) | or_mask;
return fcntl(fd, F_SETFL, (long) &flags);
}
#if USE_EVENTFD_EMULATION
static
int eventfd_create(struct shm_fifo_eventfd_storage *this)
{
int fds[2];
int rv;
rv = pipe(fds);
if (rv < 0)
return rv;
this->fd = fds[0];
this->write_side_fd = fds[1];
file_flags_change(this->write_side_fd, -1, O_NONBLOCK);
fprintf(stderr, "eventfd_emulation:eventfd_create:success\n");
return 0;
}
static
void eventfd_release(struct shm_fifo_eventfd_storage *this)
{
close(this->fd);
close(this->write_side_fd);
}
#else /* !USE_EVENTFD_EMULATION */
static
int eventfd_create(struct shm_fifo_eventfd_storage *this)
{
int fd = eventfd(0, 0);
if (fd < 0) {
perror("eventfd");
return fd;
}
#if EVENTFD_NONBLOCKING
file_flags_change(fd, -1, O_NONBLOCK);
#endif
this->fd = fd;
fprintf(stderr, "eventfd:eventfd_create:success\n");
return 0;
}
static
void eventfd_release(struct shm_fifo_eventfd_storage *this)
{
close(this->fd);
}
#endif /* !USE_EVENTFD_EMULATION */
#endif /* USE_EVENTFD */
int fifo_create(struct shm_fifo **ptr)
{
int err = posix_memalign((void **)ptr, 4096, FIFO_TOTAL_SIZE);
struct shm_fifo *fifo = *ptr;
if (!err) {
memset(fifo, 0, offsetof(struct shm_fifo, data));
#if USE_EVENTFD
int rv;
rv = eventfd_create(&fifo->head_eventfd);
if (rv)
goto out_free;
rv = eventfd_create(&fifo->tail_eventfd);
if (rv) {
eventfd_release(&fifo->head_eventfd);
out_free:
free(fifo);
return 0;
}
#else
fprintf(stderr, "fifo_create: using futex implementation\n");
#endif // USE_EVENTFD
}
fifo->head_wait = fifo->tail_wait = 0xffffffff;
return err;
}
static
int common_fifo_window_init(struct shm_fifo *fifo, struct fifo_window *window,
unsigned min_length, unsigned pull_length, int reader)
{
window->fifo = fifo;
window->reader = reader;
window->len = 0;
if (min_length > pull_length)
pull_length = min_length;
window->min_length = min_length;
window->pull_length = pull_length;
return 0;
}
int fifo_window_init_reader(struct shm_fifo *fifo, struct fifo_window *window,
unsigned min_length, unsigned pull_length)
{
window->start = fifo->tail % FIFO_SIZE;
return common_fifo_window_init(fifo, window, min_length, pull_length, 1);
}
int fifo_window_init_writer(struct shm_fifo *fifo, struct fifo_window *window,
unsigned min_length, unsigned pull_length)
{
window->start = fifo->head % FIFO_SIZE;
return common_fifo_window_init(fifo, window, min_length, pull_length, 0);
}
#if USE_EVENTFD && USE_EVENTFD_EMULATION
static
void eventfd_wait(struct shm_fifo_eventfd_storage *eventfd, unsigned *addr, unsigned wait_value)
{
AO_nop_full();
if (*addr != wait_value)
return;
eventfd_t buf[8];
int rv;
again:
rv = read(eventfd->fd, buf, sizeof(buf));
if (rv < 0) {
if (errno == EINTR)
goto again;
perror("eventfd_wait:read");
abort();
}
}
static
void eventfd_wake(struct shm_fifo_eventfd_storage *eventfd)
{
eventfd_t value = 1;
write(eventfd->write_side_fd, &value, sizeof(value));
}
#elif USE_EVENTFD
static
void eventfd_wait(struct shm_fifo_eventfd_storage *eventfd, unsigned *addr, unsigned wait_value)
{
AO_nop_full();
if (*addr != wait_value)
return;
int fd = eventfd->fd;
#if EVENTFD_NONBLOCKING
struct pollfd wait;
wait.fd = fd;
wait.events = POLLIN;
poll(&wait, 1, -1);
#endif
eventfd_t tmp;
read(fd, &tmp, sizeof(eventfd_t));
}
static
void eventfd_wake(struct shm_fifo_eventfd_storage *eventfd)
{
eventfd_t value = 1;
write(eventfd->fd, &value, sizeof(value));
}
#else /* !USE_EVENTFD */
static
int futex(int *uaddr, int op, int val, const struct timespec *timeout,
int *uaddr2, int val3)
{
return syscall(__NR_futex, uaddr, op, val, timeout, uaddr2, val3);
}
static
void futex_wait(void *addr, unsigned value)
{
int rv;
do {
rv = futex(addr, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, value, 0, 0, 0);
} while (rv && errno == EINTR);
if (rv) {
if (errno == EWOULDBLOCK)
return;
perror("futex_wait");
exit(1);
}
}
static
void futex_wake(void *addr)
{
int rv;
rv = futex(addr, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1, 0, 0, 0);
if (rv < 0) {
perror("futex_wake");
exit(1);
}
}
#endif
void fifo_window_reader_wait(struct fifo_window *window)
{
struct shm_fifo *fifo = window->fifo;
unsigned count;
unsigned tail;
unsigned head;
if (!window->reader)
abort();
tail = fifo->tail;
head = fifo->head;
if (head - tail != window->len)
return;
fifo_reader_wait_calls++;
for (count = SPIN_COUNT; count > 0; count--) {
AO_nop_full();
if (fifo->head != head) {
fifo_reader_wait_spins += SPIN_COUNT - count + 1;
return;
}
}
fifo_reader_wait_spins += SPIN_COUNT - count;
fifo->head_wait = head;
do {
#if USE_EVENTFD
eventfd_wait(&fifo->head_eventfd, &fifo->head, head);
#else
futex_wait(&fifo->head, head);
#endif
} while (head == fifo->head);
}
void fifo_window_writer_wait(struct fifo_window *window)
{
struct shm_fifo *fifo = window->fifo;
unsigned count;
unsigned tail;
unsigned head;
if (window->reader)
abort();
tail = fifo->tail;
head = fifo->head;
if (tail + FIFO_SIZE - head != window->len)
return;
fifo_writer_wait_calls++;
for (count = SPIN_COUNT; count > 0; count--) {
AO_nop_full();
if (fifo->tail != tail) {
fifo_writer_wait_spins += SPIN_COUNT - count + 1;
return;
}
}
fifo_writer_wait_spins += SPIN_COUNT - count;
fifo->tail_wait = tail;
do {
#if USE_EVENTFD
eventfd_wait(&fifo->tail_eventfd, &fifo->tail, tail);
#else
futex_wait(&fifo->tail, tail);
#endif
} while (tail == fifo->tail);
}
static
void shm_fifo_notify_reader(struct shm_fifo *fifo, unsigned old_head)
{
AO_nop_full();
if (fifo->head_wait == old_head) {
fifo_reader_wake_count++;
#if USE_EVENTFD
eventfd_wake(&fifo->head_eventfd);
#else
futex_wake(&fifo->head);
#endif
}
}
static
void shm_fifo_notify_writer(struct shm_fifo *fifo, unsigned old_tail)
{
AO_nop_full();
if (fifo->tail_wait == old_tail) {
fifo_writer_wake_count++;
#if USE_EVENTFD
eventfd_wake(&fifo->tail_eventfd);
#else
futex_wake(&fifo->tail);
#endif
}
}
static
void fifo_notify_invalid_window(struct fifo_window *window, int reader)
{
fprintf(stderr, "window %p (reader = %d) (arg-reader = %d) is invalid. len = %u\n", (void *)window, window->reader, reader, window->len);
__builtin_trap();
}
static inline
unsigned check_window_free_count(struct fifo_window *window, unsigned old_start, int reader)
{
unsigned start = window->start;
unsigned free_count = start - old_start;
if (unlikely(free_count > FIFO_SIZE)) {
fifo_notify_invalid_window(window, reader);
abort();
}
return free_count;
}
void fifo_window_exchange_reader(struct fifo_window *window)
{
unsigned len;
again:
len = window->len;
struct shm_fifo *fifo = window->fifo;
unsigned tail = fifo->tail;
unsigned free_count = check_window_free_count(window, tail, 1);
unsigned old_tail = tail;
tail += free_count;
fifo->tail = tail;
window->start = tail;
if (len < window->pull_length)
len = window->len = fifo->head - tail;
if (len > FIFO_SIZE) {
fifo_notify_invalid_window(window, 1);
fifo->tail = fifo->head;
window->len = 0;
window->start = tail % FIFO_SIZE;
}
shm_fifo_notify_writer(fifo, old_tail);
if (unlikely(len < window->min_length)) {
fifo_window_reader_wait(window);
goto again;
}
fifo_reader_exchange_count++;
}
void fifo_window_exchange_writer(struct fifo_window *window)
{
unsigned len;
again:
len = window->len;
struct shm_fifo *fifo = window->fifo;
unsigned head = fifo->head;
unsigned free_count = check_window_free_count(window, head, 0);
unsigned old_head = head;
head += free_count;
fifo->head = head;
window->start = head;
if (len < window->pull_length)
len = window->len = fifo->tail + FIFO_SIZE - head;
if (len > FIFO_SIZE) {
fifo_notify_invalid_window(window, 0);
fifo->head = fifo->tail;
window->len = FIFO_SIZE;
window->start = head % FIFO_SIZE;
}
shm_fifo_notify_reader(fifo, old_head);
if (unlikely(len < window->min_length)) {
fifo_window_writer_wait(window);
goto again;
}
fifo_writer_exchange_count++;
}