-
Notifications
You must be signed in to change notification settings - Fork 0
/
rb-lf.c
164 lines (144 loc) · 4.78 KB
/
rb-lf.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#include <string.h>
#include "system.h"
#include "ringbuffer.h"
#include "util.h"
#define BITS 16
#define MASK_USED ((1 << BITS) - 1)
#define MASK_TAIL (MASK_USED << BITS)
#define MASK_HEAD (MASK_TAIL << BITS)
typedef struct _ringbuffer {
void **data;
uintptr_t head, tail_used;
size_t capacity;
int deleted;
sem_t empty_sem, filled_sem;
} RB_LF;
RB_LF *rb_lf_init(size_t capacity) {
if (capacity < 1 || capacity > (1 << BITS )) return NULL;
LOGGER(LOG_INFO, " rb_init()");
RB_LF *rb = malloc(sizeof(RB_LF));
rb->capacity = capacity;
rb->data = calloc(capacity, sizeof(void *));
rb->tail_used = 0;
rb->head = 0;
rb->deleted = 0;
sem_init(&rb->filled_sem, 0, 0);
sem_init(&rb->empty_sem, 0, capacity);
return rb;
}
int rb_lf_isfull(RB_LF *rb) {
if (rb == NULL || rb->deleted) return -1;
return (rb->tail_used & MASK_USED) >= rb->capacity;
}
int rb_lf_isempty(RB_LF *rb) {
if (rb == NULL || rb->deleted) return -1;
return (rb->tail_used & MASK_USED) == 0;
}
int rb_lf_size(RB_LF *rb) {
if (rb == NULL || rb->deleted) return -1;
return (rb->tail_used & MASK_USED);
}
int rb_lf_pushback(RB_LF *rb, void *data) {
if (rb == NULL || rb->deleted) return -1;
uintptr_t current, updated, tail, used;
int success = 0;
int ret;
while (rb && !rb->deleted) {
pthread_testcancel();
ret = sem_wait(&rb->empty_sem);
if (ret == -1 && errno == EINTR) continue;
do {
current = rb->tail_used;
tail = (((current & MASK_TAIL) >> BITS) + 1) % rb->capacity;
used = ((current & MASK_USED) + 1);
updated = (tail << BITS) | used;
} while (!__sync_bool_compare_and_swap(&rb->tail_used, current, updated));
// we got it
rb->data[(current & MASK_TAIL)>>BITS] = data;
sem_post(&rb->filled_sem);
return 0;
}
return -1;
}
void *rb_lf_popfront(RB_LF *rb) {
if (rb == NULL || rb->deleted) return NULL;
void *result;
uintptr_t current, updated, head, used, newhead;
int success = 0;
int ret;
while (rb && !rb->deleted) {
pthread_testcancel();
ret = sem_wait(&rb->filled_sem);
if (ret == -1 && errno == EINTR) continue;
do {
head = rb->head;
updated = (head + 1) % rb->capacity;
} while (!__sync_bool_compare_and_swap(&rb->head, head, updated));
// we now have reserved the former head node
result = rb->data[head];
__sync_synchronize();
// now its safe to release the prior node by decrementing used
do {
current = rb->tail_used;
updated = current - 1;
} while (!__sync_bool_compare_and_swap(&rb->tail_used, current, updated));
sem_post(&rb->empty_sem);
return result;
}
return NULL;
}
int rb_lf_drain(RB_LF *rb, void **dest, size_t max) {
uintptr_t count, current, updated, head, used, newhead, available;
int ret;
while (rb && !rb->deleted) {
pthread_testcancel();
ret = sem_wait(&rb->filled_sem);
if (ret == -1 && errno == EINTR) continue;
// we got one, try for more up to max
count = 1;
while (count < max) {
ret = sem_trywait(&rb->filled_sem);
if (ret == -1 && errno == EINTR) continue;
if (ret == -1 && errno == EAGAIN) break;
count++;
}
do {
available = rb->tail_used & MASK_USED;
if (available < count) count = available;
head = rb->head;
updated = (head + count) % rb->capacity;
} while (!__sync_bool_compare_and_swap(&rb->head, head, updated));
// we now have reserved count nodes at the head
if (updated > head) {
// not split across end of buffer - a single memcpy
memcpy(dest, rb->data + head, count*sizeof(void *));
} else {
// two memcpy required
size_t first = rb->capacity - head;
memcpy(dest, rb->data + head, first*sizeof(void *));
memcpy(dest + first, rb->data, (count - first)*sizeof(void *));
}
__sync_synchronize();
// now safe to release the nodes
do {
current = rb->tail_used;
updated = current - count;
} while (!__sync_bool_compare_and_swap(&rb->tail_used, current, updated));
for (int i = 0; i < count; i++) sem_post(&rb->empty_sem);
return count;
}
return -1;
}
void rb_lf_free(RB_LF *rb) {
if (rb == NULL) return;
rb->deleted = 1;
free(rb->data);
sem_destroy(&rb->filled_sem);
free(rb);
}