Race in VCOM_lib fifo

This commit is contained in:
Todd Fleming 2017-01-10 13:29:46 -05:00
parent 408d820b9f
commit 37db018c70
3 changed files with 15 additions and 9 deletions

View File

@ -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) BOOL fifo_put(fifo_t *fifo, U8 c)
{ {
int next; int tail = fifo->tail;
int head = fifo->head;
// check if FIFO has room // check if FIFO has room
next = (fifo->head + 1) % VCOM_FIFO_SIZE; int next = (head + 1) % VCOM_FIFO_SIZE;
if (next == fifo->tail) { if (next == tail) {
// full // full
return FALSE; return FALSE;
} }
fifo->buf[fifo->head] = c; fifo->buf[head] = c;
fifo->head = next; fifo->head = next;
return TRUE; return TRUE;
} }
// TBF: only USB isr may call this
BOOL fifo_get(fifo_t *fifo, U8 *pc) BOOL fifo_get(fifo_t *fifo, U8 *pc)
{ {
int next; 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) int fifo_avail(fifo_t *fifo)
{ {
return (VCOM_FIFO_SIZE + fifo->head - fifo->tail) % VCOM_FIFO_SIZE; return (VCOM_FIFO_SIZE + fifo->head - fifo->tail) % VCOM_FIFO_SIZE;
} }
// TBF: only USB isr may call this
int fifo_free(fifo_t *fifo) int fifo_free(fifo_t *fifo)
{ {
return (VCOM_FIFO_SIZE - 1 - fifo_avail(fifo)); return (VCOM_FIFO_SIZE - 1 - fifo_avail(fifo));

View File

@ -26,13 +26,14 @@
*/ */
#include "lpcusb_type.h" #include "lpcusb_type.h"
#include <atomic>
#define VCOM_FIFO_SIZE 512 #define VCOM_FIFO_SIZE 512
typedef struct { typedef struct {
int head; std::atomic<int> head;
int tail; std::atomic<int> tail;
U8 *buf; U8 *buf;
} fifo_t; } fifo_t;
void fifo_init(fifo_t *fifo, U8 *buf); void fifo_init(fifo_t *fifo, U8 *buf);

View File

@ -261,7 +261,7 @@ static void BulkIn(U8 bEP, U8 bEPStatus)
} }
} }
iLen = i; iLen = i;
// send over USB // send over USB
if (iLen > 0) { if (iLen > 0) {
USBHwEPWrite(bEP, abBulkBuf, iLen); USBHwEPWrite(bEP, abBulkBuf, iLen);
@ -435,7 +435,7 @@ void VCOM_puts(const char* str)
{ {
while(*str != '\0') while(*str != '\0')
{ {
VCOM_putchar(*str++); VCOM_putc(*str++);
} }
} }
void VCOM_putc(char c) void VCOM_putc(char c)