From 37db018c7019bb4d4af0d67292713cca3f6c8018 Mon Sep 17 00:00:00 2001 From: Todd Fleming Date: Tue, 10 Jan 2017 13:29:46 -0500 Subject: [PATCH] Race in VCOM_lib fifo --- VCOM_lib/serial_fifo.c | 13 +++++++++---- VCOM_lib/serial_fifo.h | 7 ++++--- VCOM_lib/usbSerial.c | 4 ++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/VCOM_lib/serial_fifo.c b/VCOM_lib/serial_fifo.c index e547502..ec13d24 100644 --- a/VCOM_lib/serial_fifo.c +++ b/VCOM_lib/serial_fifo.c @@ -38,24 +38,27 @@ void fifo_init(fifo_t *fifo, U8 *buf) } +// TBF: only non-isr may call this BOOL fifo_put(fifo_t *fifo, U8 c) { - int next; + int tail = fifo->tail; + int head = fifo->head; // check if FIFO has room - next = (fifo->head + 1) % VCOM_FIFO_SIZE; - if (next == fifo->tail) { + int next = (head + 1) % VCOM_FIFO_SIZE; + if (next == tail) { // full return FALSE; } - fifo->buf[fifo->head] = c; + fifo->buf[head] = c; fifo->head = next; return TRUE; } +// TBF: only USB isr may call this BOOL fifo_get(fifo_t *fifo, U8 *pc) { int next; @@ -74,12 +77,14 @@ BOOL fifo_get(fifo_t *fifo, U8 *pc) } +// TBF: only USB isr may call this int fifo_avail(fifo_t *fifo) { return (VCOM_FIFO_SIZE + fifo->head - fifo->tail) % VCOM_FIFO_SIZE; } +// TBF: only USB isr may call this int fifo_free(fifo_t *fifo) { return (VCOM_FIFO_SIZE - 1 - fifo_avail(fifo)); diff --git a/VCOM_lib/serial_fifo.h b/VCOM_lib/serial_fifo.h index 43b998d..34a01fa 100644 --- a/VCOM_lib/serial_fifo.h +++ b/VCOM_lib/serial_fifo.h @@ -26,13 +26,14 @@ */ #include "lpcusb_type.h" +#include #define VCOM_FIFO_SIZE 512 typedef struct { - int head; - int tail; - U8 *buf; + std::atomic head; + std::atomic tail; + U8 *buf; } fifo_t; void fifo_init(fifo_t *fifo, U8 *buf); diff --git a/VCOM_lib/usbSerial.c b/VCOM_lib/usbSerial.c index ceed39e..1bddc1e 100644 --- a/VCOM_lib/usbSerial.c +++ b/VCOM_lib/usbSerial.c @@ -261,7 +261,7 @@ static void BulkIn(U8 bEP, U8 bEPStatus) } } iLen = i; - + // send over USB if (iLen > 0) { USBHwEPWrite(bEP, abBulkBuf, iLen); @@ -435,7 +435,7 @@ void VCOM_puts(const char* str) { while(*str != '\0') { - VCOM_putchar(*str++); + VCOM_putc(*str++); } } void VCOM_putc(char c)