2015-09-24 21:51:00 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup sys
|
|
|
|
* @{
|
|
|
|
* @file
|
|
|
|
* @brief thread-safe ringbuffer implementation
|
|
|
|
*
|
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2020-10-13 10:32:41 +02:00
|
|
|
#include "irq.h"
|
2015-09-24 21:51:00 +02:00
|
|
|
#include "tsrb.h"
|
|
|
|
|
2019-06-05 12:25:12 +02:00
|
|
|
static void _push(tsrb_t *rb, uint8_t c)
|
2015-09-24 21:51:00 +02:00
|
|
|
{
|
|
|
|
rb->buf[rb->writes++ & (rb->size - 1)] = c;
|
|
|
|
}
|
|
|
|
|
2019-06-05 12:25:12 +02:00
|
|
|
static uint8_t _pop(tsrb_t *rb)
|
2015-09-24 21:51:00 +02:00
|
|
|
{
|
|
|
|
return rb->buf[rb->reads++ & (rb->size - 1)];
|
|
|
|
}
|
|
|
|
|
2021-11-22 11:46:04 +01:00
|
|
|
static uint8_t _peek(tsrb_t *rb, unsigned int idx)
|
|
|
|
{
|
|
|
|
return rb->buf[(rb->reads + idx) & (rb->size - 1)];
|
|
|
|
}
|
|
|
|
|
2015-09-24 21:51:00 +02:00
|
|
|
int tsrb_get_one(tsrb_t *rb)
|
|
|
|
{
|
2020-10-13 10:32:41 +02:00
|
|
|
int retval = -1;
|
|
|
|
unsigned irq_state = irq_disable();
|
2015-09-24 21:51:00 +02:00
|
|
|
if (!tsrb_empty(rb)) {
|
2020-10-13 10:32:41 +02:00
|
|
|
retval = _pop(rb);
|
2015-09-24 21:51:00 +02:00
|
|
|
}
|
2020-10-13 10:32:41 +02:00
|
|
|
irq_restore(irq_state);
|
|
|
|
return retval;
|
2015-09-24 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2021-11-22 11:46:04 +01:00
|
|
|
int tsrb_peek_one(tsrb_t *rb)
|
|
|
|
{
|
|
|
|
int retval = -1;
|
|
|
|
unsigned irq_state = irq_disable();
|
|
|
|
if (!tsrb_empty(rb)) {
|
|
|
|
retval = _peek(rb, 0);
|
|
|
|
}
|
|
|
|
irq_restore(irq_state);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2019-06-05 12:25:12 +02:00
|
|
|
int tsrb_get(tsrb_t *rb, uint8_t *dst, size_t n)
|
2015-09-24 21:51:00 +02:00
|
|
|
{
|
|
|
|
size_t tmp = n;
|
2020-10-13 10:32:41 +02:00
|
|
|
unsigned irq_state = irq_disable();
|
2015-09-24 21:51:00 +02:00
|
|
|
while (tmp && !tsrb_empty(rb)) {
|
|
|
|
*dst++ = _pop(rb);
|
|
|
|
tmp--;
|
|
|
|
}
|
2020-10-13 10:32:41 +02:00
|
|
|
irq_restore(irq_state);
|
2015-09-24 21:51:00 +02:00
|
|
|
return (n - tmp);
|
|
|
|
}
|
|
|
|
|
2021-11-22 11:46:04 +01:00
|
|
|
int tsrb_peek(tsrb_t *rb, uint8_t *dst, size_t n)
|
|
|
|
{
|
|
|
|
size_t idx = 0;
|
|
|
|
unsigned irq_state = irq_disable();
|
|
|
|
unsigned int avail = tsrb_avail(rb);
|
|
|
|
while (idx < n && idx != avail) {
|
|
|
|
*dst++ = _peek(rb, idx++);
|
|
|
|
}
|
|
|
|
irq_restore(irq_state);
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2018-08-31 10:35:02 +02:00
|
|
|
int tsrb_drop(tsrb_t *rb, size_t n)
|
|
|
|
{
|
|
|
|
size_t tmp = n;
|
2020-10-13 10:32:41 +02:00
|
|
|
unsigned irq_state = irq_disable();
|
2018-08-31 10:35:02 +02:00
|
|
|
while (tmp && !tsrb_empty(rb)) {
|
|
|
|
_pop(rb);
|
|
|
|
tmp--;
|
|
|
|
}
|
2020-10-13 10:32:41 +02:00
|
|
|
irq_restore(irq_state);
|
2018-08-31 10:35:02 +02:00
|
|
|
return (n - tmp);
|
|
|
|
}
|
|
|
|
|
2019-06-05 12:25:12 +02:00
|
|
|
int tsrb_add_one(tsrb_t *rb, uint8_t c)
|
2015-09-24 21:51:00 +02:00
|
|
|
{
|
2020-10-13 10:32:41 +02:00
|
|
|
int retval = -1;
|
|
|
|
unsigned irq_state = irq_disable();
|
2015-09-24 21:51:00 +02:00
|
|
|
if (!tsrb_full(rb)) {
|
|
|
|
_push(rb, c);
|
2020-10-13 10:32:41 +02:00
|
|
|
retval = 0;
|
2015-09-24 21:51:00 +02:00
|
|
|
}
|
2020-10-13 10:32:41 +02:00
|
|
|
irq_restore(irq_state);
|
|
|
|
return retval;
|
2015-09-24 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2019-06-05 12:25:12 +02:00
|
|
|
int tsrb_add(tsrb_t *rb, const uint8_t *src, size_t n)
|
2015-09-24 21:51:00 +02:00
|
|
|
{
|
|
|
|
size_t tmp = n;
|
2020-10-13 10:32:41 +02:00
|
|
|
unsigned irq_state = irq_disable();
|
2015-09-24 21:51:00 +02:00
|
|
|
while (tmp && !tsrb_full(rb)) {
|
|
|
|
_push(rb, *src++);
|
|
|
|
tmp--;
|
|
|
|
}
|
2020-10-13 10:32:41 +02:00
|
|
|
irq_restore(irq_state);
|
2015-09-24 21:51:00 +02:00
|
|
|
return (n - tmp);
|
|
|
|
}
|