-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringbuffer.c
426 lines (386 loc) · 9.22 KB
/
ringbuffer.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
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
/*
* Project: ringbuffer - File: ringbuffer.c
* Copyright (C) 2019 - Tania Hagn - tania@df9ry.de
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ringbuffer/ringbuffer.h"
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <assert.h>
struct _ringbuffer {
size_t size;
size_t used;
size_t tail;
size_t lost;
pthread_spinlock_t spinlock;
pthread_mutex_t rd_mutex;
pthread_mutex_t rd_cond_lock;
pthread_cond_t rd_cond;
pthread_mutex_t wr_mutex;
pthread_mutex_t wr_cond_lock;
pthread_cond_t wr_cond;
uint8_t data[0];
};
static inline void _cond_wait(pthread_cond_t *cond, pthread_mutex_t *lock)
{
int erc;
erc = pthread_mutex_lock(lock);
assert(erc == 0);
erc = pthread_cond_wait(cond, lock);
assert(erc == 0);
erc = pthread_mutex_unlock(lock);
assert(erc == 0);
}
static inline void _cond_signal(pthread_cond_t *cond, pthread_mutex_t *lock)
{
int erc;
erc = pthread_mutex_lock(lock);
assert(erc == 0);
erc = pthread_cond_signal(cond);
assert(erc == 0);
erc = pthread_mutex_unlock(lock);
assert(erc == 0);
}
static inline size_t _free(struct _ringbuffer *_rb)
{
return _rb->size - _rb->used;
}
static inline size_t _used(struct _ringbuffer *_rb)
{
return _rb->used;
}
static inline size_t _put(struct _ringbuffer *_rb, uint8_t *po, size_t co)
{
size_t __free = _rb->size - _rb->used;
size_t __head = (_rb->tail + _rb->used) % _rb->size;
size_t new_head, i;
if (co > __free)
co = __free;
new_head = (__head + co) % _rb->size;
if (new_head <= _rb->size) {
memcpy(&_rb->data[__head], po, co);
} else {
i = _rb->size - __head;
memcpy(&_rb->data[__head], po, i);
new_head -= _rb->size;
memcpy(_rb->data, &po[i], new_head);
}
_rb->used += co;
return co;
}
static inline size_t _get(struct _ringbuffer *_rb, uint8_t *po, size_t co)
{
size_t new_tail;
if (co > _rb->used)
co = _rb->used;
if (!co)
return 0;
new_tail = (_rb->tail + co) % _rb->size;
if (new_tail > _rb->tail) {
memcpy(po, &_rb->data[_rb->tail], co);
_rb->tail = new_tail;
} else {
co = _rb->size - _rb->tail;
memcpy(po, &_rb->data[_rb->tail], co);
_rb->tail = 0;
}
_rb->used -= co;
return co;
}
int rb_init(ringbuffer_t *rb, size_t size)
{
int erc;
assert(rb);
rb->_rb = malloc(sizeof(struct _ringbuffer) + size);
if (!rb->_rb)
return ENOMEM;
erc = pthread_spin_init(&rb->_rb->spinlock, PTHREAD_PROCESS_PRIVATE);
if (erc)
goto undo_spinlock;
erc = pthread_mutex_init(&rb->_rb->rd_mutex, NULL);
if (erc)
goto undo_rd_mutex;
erc = pthread_mutex_init(&rb->_rb->rd_cond_lock, NULL);
if (erc)
goto undo_rd_cond_lock;
erc = pthread_cond_init(&rb->_rb->rd_cond, NULL);
if (erc)
goto undo_rd_cond;
erc = pthread_mutex_init(&rb->_rb->wr_mutex, NULL);
if (erc)
goto undo_wr_mutex;
erc = pthread_mutex_init(&rb->_rb->wr_cond_lock, NULL);
if (erc)
goto undo_wr_cond_lock;
erc = pthread_cond_init(&rb->_rb->wr_cond, NULL);
if (erc)
goto undo_wr_cond;
rb->_rb->used = rb->_rb->tail = rb->_rb->lost = 0;
rb->_rb->size = size;
return 0;
undo_wr_cond:
pthread_mutex_destroy(&rb->_rb->wr_cond_lock);
undo_wr_cond_lock:
pthread_mutex_destroy(&rb->_rb->wr_mutex);
undo_wr_mutex:
pthread_cond_destroy(&rb->_rb->rd_cond);
undo_rd_cond:
pthread_mutex_destroy(&rb->_rb->rd_cond_lock);
undo_rd_cond_lock:
pthread_mutex_destroy(&rb->_rb->rd_mutex);
undo_rd_mutex:
pthread_spin_destroy(&rb->_rb->spinlock);
undo_spinlock:
return erc;
}
int rb_destroy(ringbuffer_t *rb)
{
assert(rb);
if (!rb->_rb)
return EFAULT;
pthread_cond_destroy(&rb->_rb->wr_cond);
pthread_mutex_destroy(&rb->_rb->wr_cond_lock);
pthread_mutex_destroy(&rb->_rb->wr_mutex);
pthread_cond_destroy(&rb->_rb->rd_cond);
pthread_mutex_destroy(&rb->_rb->rd_cond_lock);
pthread_mutex_destroy(&rb->_rb->rd_mutex);
pthread_spin_destroy(&rb->_rb->spinlock);
free(rb->_rb);
rb->_rb = NULL;
return 0;
}
int rb_write_block(ringbuffer_t *rb, uint8_t *po, size_t co)
{
struct _ringbuffer *_rb;
int erc, res = 0, written = 0;
assert(rb);
_rb = rb->_rb;
if (!rb->_rb)
return -EFAULT;
if (co == 0)
return 0;
erc = pthread_mutex_lock(&_rb->wr_mutex);
assert(erc == 0);
while (co > 0) {
erc = pthread_spin_lock(&_rb->spinlock); /*----v*/
assert(erc == 0);
res = _put(_rb, po, co);
erc = pthread_spin_unlock(&_rb->spinlock); /*--^*/
assert(erc == 0);
po += res;
co -= res;
written += res;
if (res > 0)
_cond_signal(&_rb->wr_cond, &_rb->wr_cond_lock);
if (co > 0)
_cond_wait(&_rb->rd_cond, &_rb->rd_cond_lock);
} /* end while */
erc = pthread_mutex_unlock(&_rb->wr_mutex);
assert(erc == 0);
return written;
}
int rb_write_nonblock(ringbuffer_t *rb, uint8_t *po, size_t co)
{
struct _ringbuffer *_rb;
int erc, res;
size_t written = 0;
assert(rb);
_rb = rb->_rb;
if (!rb->_rb)
return -EFAULT;
if (co > _rb->size)
return -E2BIG;
erc = pthread_spin_lock(&_rb->spinlock); /*----v*/
assert(erc == 0);
if (co <= _free(_rb)) {
while (co > 0) {
res = _put(_rb, po, co);
po += res;
co -= res;
written += res;
_cond_signal(&_rb->wr_cond, &_rb->wr_cond_lock);
} /* end while */
res = written;
} else {
res = -ENOMEM;
}
erc = pthread_spin_unlock(&_rb->spinlock); /*--^*/
assert(erc == 0);
return res;
}
int rb_read_block(ringbuffer_t *rb, uint8_t *po, size_t co)
{
struct _ringbuffer *_rb;
int erc, res, got = 0;
assert(rb);
_rb = rb->_rb;
if (!rb->_rb)
return -EFAULT;
erc = pthread_mutex_lock(&_rb->rd_mutex);
assert(erc == 0);
while (co > 0) {
erc = pthread_spin_lock(&_rb->spinlock); /*----v*/
assert(erc == 0);
res = _get(_rb, po, co);
erc = pthread_spin_unlock(&_rb->spinlock); /*--^*/
assert(erc == 0);
if (res == 0) {
if (got == 0) {
_cond_wait(&_rb->wr_cond, &_rb->wr_cond_lock);
} else {
break;
}
} else {
po += res;
co -= res;
got += res;
} /* end while */
} /* end while */
erc = pthread_mutex_unlock(&_rb->rd_mutex);
assert(erc == 0);
_cond_signal(&_rb->rd_cond, &_rb->rd_cond_lock);
return got;
}
int rb_read_nonblock(ringbuffer_t *rb, uint8_t *po, size_t co)
{
struct _ringbuffer *_rb;
int erc, res;
size_t got = 0;
assert(rb);
_rb = rb->_rb;
if (!rb->_rb)
return -EFAULT;
if (co > _rb->size)
return -E2BIG;
erc = pthread_spin_lock(&_rb->spinlock); /*----v*/
assert(erc == 0);
if (co <= _used(_rb)) {
while (co > 0) {
res = _get(_rb, po, co);
po += res;
co -= res;
got += res;
_cond_signal(&_rb->rd_cond, &_rb->rd_cond_lock);
} /* end while */
res = got;
} else {
res = -EAGAIN;
}
erc = pthread_spin_unlock(&_rb->spinlock); /*--^*/
assert(erc == 0);
return res;
}
void rb_clear(ringbuffer_t *rb)
{
struct _ringbuffer *_rb;
int erc;
assert(rb);
_rb = rb->_rb;
assert(_rb);
erc = pthread_spin_lock(&_rb->spinlock); /*----v*/
assert(erc == 0);
_rb->used = _rb->tail = _rb->lost = 0;
erc = pthread_spin_unlock(&_rb->spinlock); /*--^*/
assert(erc == 0);
}
size_t rb_get_size(ringbuffer_t *rb)
{
struct _ringbuffer *_rb;
assert(rb);
_rb = rb->_rb;
assert(_rb);
return _rb->size;
}
size_t rb_get_used(ringbuffer_t *rb)
{
struct _ringbuffer *_rb;
size_t res;
int erc;
assert(rb);
_rb = rb->_rb;
assert(_rb);
erc = pthread_spin_lock(&_rb->spinlock); /*----v*/
assert(erc == 0);
res = _used(_rb);
erc = pthread_spin_unlock(&_rb->spinlock); /*--^*/
assert(erc == 0);
return res;
}
size_t rb_get_free(ringbuffer_t *rb)
{
struct _ringbuffer *_rb;
size_t res;
int erc;
assert(rb);
_rb = rb->_rb;
assert(_rb);
erc = pthread_spin_lock(&_rb->spinlock); /*----v*/
assert(erc == 0);
res = _free(_rb);
erc = pthread_spin_unlock(&_rb->spinlock); /*--^*/
assert(erc == 0);
return res;
}
size_t rb_get_lost(ringbuffer_t *rb)
{
struct _ringbuffer *_rb;
size_t lost;
int erc;
assert(rb);
_rb = rb->_rb;
if (!_rb)
return EFAULT;
erc = pthread_spin_lock(&_rb->spinlock); /*----v*/
assert(erc == 0);
lost = _rb->lost;
erc = pthread_spin_unlock(&_rb->spinlock); /*--^*/
assert(erc == 0);
return lost;
}
size_t rb_clear_lost(ringbuffer_t *rb)
{
struct _ringbuffer *_rb;
size_t lost;
int erc;
assert(rb);
_rb = rb->_rb;
if (!_rb)
return EFAULT;
erc = pthread_spin_lock(&_rb->spinlock); /*----v*/
assert(erc == 0);
lost = _rb->lost;
_rb->lost = 0;
erc = pthread_spin_unlock(&_rb->spinlock); /*--^*/
assert(erc == 0);
return lost;
}
size_t rb_loose(ringbuffer_t *rb, size_t loose)
{
struct _ringbuffer *_rb;
size_t lost;
int erc;
assert(rb);
_rb = rb->_rb;
if (!_rb)
return EFAULT;
erc = pthread_spin_lock(&_rb->spinlock); /*----v*/
assert(erc == 0);
_rb->lost += loose;
lost = _rb->lost;
erc = pthread_spin_unlock(&_rb->spinlock); /*--^*/
assert(erc == 0); /*--^*/
return lost;
}