From aa321eb3f0d6bcc59c6f10380a3b52d17683f567 Mon Sep 17 00:00:00 2001
From: Kaspar Schleiser <kaspar@schleiser.de>
Date: Thu, 24 Sep 2015 21:51:23 +0200
Subject: [PATCH] sys: uart_stdio: use thread-safe ringbuffer

---
 Makefile.dep                |  4 ++++
 sys/uart_stdio/uart_stdio.c | 15 ++++++---------
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/Makefile.dep b/Makefile.dep
index 012c4a967b..093cf5fc6a 100644
--- a/Makefile.dep
+++ b/Makefile.dep
@@ -287,6 +287,10 @@ ifneq (,$(filter posix_sockets,$(USEMODULE)))
   USEMODULE += random
 endif
 
+ifneq (,$(filter uart_stdio,$(USEMODULE)))
+  USEMODULE += tsrb
+endif
+
 ifneq (,$(filter posix,$(USEMODULE)))
   USEMODULE += timex
   USEMODULE += vtimer
diff --git a/sys/uart_stdio/uart_stdio.c b/sys/uart_stdio/uart_stdio.c
index 08e1cda2a3..e58a8b2c91 100644
--- a/sys/uart_stdio/uart_stdio.c
+++ b/sys/uart_stdio/uart_stdio.c
@@ -26,7 +26,7 @@
 #include <stdio.h>
 
 #include "cpu_conf.h"
-#include "ringbuffer.h"
+#include "tsrb.h"
 #include "thread.h"
 #include "mutex.h"
 #include "irq.h"
@@ -56,7 +56,7 @@
  */
 static mutex_t _rx_mutex = MUTEX_INIT;
 static char _rx_buf_mem[STDIO_RX_BUFSIZE];
-static ringbuffer_t _rx_buf;
+static tsrb_t _rx_buf = TSRB_INIT(_rx_buf_mem);
 
 /**
  * @brief Receive a new character from the UART and put it into the receive buffer
@@ -64,25 +64,22 @@ static ringbuffer_t _rx_buf;
 void uart_stdio_rx_cb(void *arg, char data)
 {
     (void)arg;
-    ringbuffer_add_one(&_rx_buf, data);
+    tsrb_add_one(&_rx_buf, data);
     mutex_unlock(&_rx_mutex);
 }
 
 void uart_stdio_init(void)
 {
     mutex_lock(&_rx_mutex);
-    ringbuffer_init(&_rx_buf, _rx_buf_mem, STDIO_RX_BUFSIZE);
     uart_init(STDIO, STDIO_BAUDRATE, uart_stdio_rx_cb, 0, 0);
 }
 
 int uart_stdio_read(char* buffer, int count)
 {
     int res;
-    mutex_lock(&_rx_mutex);
-    unsigned state = disableIRQ();
-    count = (count < _rx_buf.avail) ? count : _rx_buf.avail;
-    res = ringbuffer_get(&_rx_buf, (char*)buffer, count);
-    restoreIRQ(state);
+    while (!(res = tsrb_get(&_rx_buf, buffer, count))) {
+        mutex_lock(&_rx_mutex);
+    }
     return res;
 }
 
-- 
GitLab