mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
0d0b14de29
Drop the requirement of having only one writer and one reader, as the name of the ring-buffer does not indicate any limitation on the thread-safety. The two-threads-one-buffer kind of ring buffer can be reintroduced with a different name.
91 lines
1.7 KiB
C
91 lines
1.7 KiB
C
/*
|
|
* 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>
|
|
*
|
|
* @}
|
|
*/
|
|
|
|
#include "irq.h"
|
|
#include "tsrb.h"
|
|
|
|
static void _push(tsrb_t *rb, uint8_t c)
|
|
{
|
|
rb->buf[rb->writes++ & (rb->size - 1)] = c;
|
|
}
|
|
|
|
static uint8_t _pop(tsrb_t *rb)
|
|
{
|
|
return rb->buf[rb->reads++ & (rb->size - 1)];
|
|
}
|
|
|
|
int tsrb_get_one(tsrb_t *rb)
|
|
{
|
|
int retval = -1;
|
|
unsigned irq_state = irq_disable();
|
|
if (!tsrb_empty(rb)) {
|
|
retval = _pop(rb);
|
|
}
|
|
irq_restore(irq_state);
|
|
return retval;
|
|
}
|
|
|
|
int tsrb_get(tsrb_t *rb, uint8_t *dst, size_t n)
|
|
{
|
|
size_t tmp = n;
|
|
unsigned irq_state = irq_disable();
|
|
while (tmp && !tsrb_empty(rb)) {
|
|
*dst++ = _pop(rb);
|
|
tmp--;
|
|
}
|
|
irq_restore(irq_state);
|
|
return (n - tmp);
|
|
}
|
|
|
|
int tsrb_drop(tsrb_t *rb, size_t n)
|
|
{
|
|
size_t tmp = n;
|
|
unsigned irq_state = irq_disable();
|
|
while (tmp && !tsrb_empty(rb)) {
|
|
_pop(rb);
|
|
tmp--;
|
|
}
|
|
irq_restore(irq_state);
|
|
return (n - tmp);
|
|
}
|
|
|
|
int tsrb_add_one(tsrb_t *rb, uint8_t c)
|
|
{
|
|
int retval = -1;
|
|
unsigned irq_state = irq_disable();
|
|
if (!tsrb_full(rb)) {
|
|
_push(rb, c);
|
|
retval = 0;
|
|
}
|
|
irq_restore(irq_state);
|
|
return retval;
|
|
}
|
|
|
|
int tsrb_add(tsrb_t *rb, const uint8_t *src, size_t n)
|
|
{
|
|
size_t tmp = n;
|
|
unsigned irq_state = irq_disable();
|
|
while (tmp && !tsrb_full(rb)) {
|
|
_push(rb, *src++);
|
|
tmp--;
|
|
}
|
|
irq_restore(irq_state);
|
|
return (n - tmp);
|
|
}
|