serial: make ringbuf write operations atomic

This commit is contained in:
Felix Kopp 2020-11-29 20:34:57 +01:00
parent a25ab04b0d
commit 43add13fea
No known key found for this signature in database
GPG key ID: C478BA0A85F75728
2 changed files with 16 additions and 6 deletions

View file

@ -31,7 +31,7 @@ size_t ringbuf_read(void *dest, struct ringbuf *buf, size_t len)
{
uint8_t *tmp = dest;
while (len-- > 0 && buf->len > 0) {
while (len-- && buf->len) {
*tmp++ = buf->data[buf->rpos++];
buf->len--;
@ -46,7 +46,7 @@ size_t ringbuf_write(struct ringbuf *buf, const void *src, size_t len)
{
const uint8_t *tmp = src;
while (len-- > 0 && buf->len < buf->capacity) {
while (len-- && buf->len != buf->capacity) {
buf->data[buf->wpos++] = *tmp++;
buf->len++;

View file

@ -5,6 +5,7 @@
#include <ardix/serial.h>
#include <arch/serial.h>
#include <arch/sched.h>
#include <stddef.h>
@ -46,14 +47,23 @@ void serial_exit(struct serial_interface *interface)
ssize_t serial_read(void *dest, struct serial_interface *interface, size_t len)
{
return (ssize_t)ringbuf_read(dest, interface->rx, len);
ssize_t ret;
sched_atomic_enter();
ret = (ssize_t)ringbuf_read(dest, interface->rx, len);
sched_atomic_leave();
return ret;
}
ssize_t serial_write(struct serial_interface *interface, const void *data, size_t len)
{
ssize_t ret = (ssize_t)ringbuf_write(interface->tx, data, len);
if (ret > 0)
arch_serial_notify(interface);
ssize_t ret;
sched_atomic_enter();
ret = (ssize_t)ringbuf_write(interface->tx, data, len);
sched_atomic_leave();
return ret;
}