summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2014-09-30 15:49:30 -0600
committerGregory Nutt <gnutt@nuttx.org>2014-09-30 15:49:30 -0600
commit2f34044505daa870a2d56ed466cdb94200641a97 (patch)
treecac0ee5c392b54d9efd94b26d1d78bdb20d56de9
parent11a8aa452a46b0ef9af14fed47a5cb6a4592d51e (diff)
downloadpx4-nuttx-2f34044505daa870a2d56ed466cdb94200641a97.tar.gz
px4-nuttx-2f34044505daa870a2d56ed466cdb94200641a97.tar.bz2
px4-nuttx-2f34044505daa870a2d56ed466cdb94200641a97.zip
Fix a cornercase problem in in the UART simulation
-rw-r--r--nuttx/arch/sim/src/up_simuart.c37
1 files changed, 22 insertions, 15 deletions
diff --git a/nuttx/arch/sim/src/up_simuart.c b/nuttx/arch/sim/src/up_simuart.c
index d3bd202b7..7d48d3987 100644
--- a/nuttx/arch/sim/src/up_simuart.c
+++ b/nuttx/arch/sim/src/up_simuart.c
@@ -232,30 +232,37 @@ int simuart_getc(void)
int index;
int ch;
+ /* Locking the scheduler should eliminate the race conditions in the
+ * unlikely case of mutliple reading threads.
+ */
+
+ sched_lock();
for (;;)
{
/* Wait for a byte to become available */
- simuart_wait();
-
- /* The UART buffer show be non-empty... check anyway */
-
- if (g_uarthead != g_uarttail)
+ while (g_uarthead == g_uarttail)
{
- /* Take the next byte from the tail of the buffer */
+ simuart_wait();
+ }
- index = g_uarttail;
- ch = (int)g_uartbuffer[index];
- /* Increment the tai index (with wrapping) */
+ /* The UART buffer is non-empty... Take the next byte from the tail
+ * of the buffer.
+ */
- if (++index >= SIMUART_BUFSIZE)
- {
- index = 0;
- }
+ index = g_uarttail;
+ ch = (int)g_uartbuffer[index];
+
+ /* Increment the tai index (with wrapping) */
- g_uarttail = index;
- return ch;
+ if (++index >= SIMUART_BUFSIZE)
+ {
+ index = 0;
}
+
+ g_uarttail = index;
+ sched_unlock();
+ return ch;
}
}