Skip to content

Commit 4a4344a

Browse files
polldofacchinm
authored andcommitted
PluggableUSBSerial - fix race condition on rx_buffer
1 parent 4046059 commit 4a4344a

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

cores/arduino/USB/PluggableUSBSerial.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,27 @@ class USBSerial: public USBCDC, public ::mbed::Stream, public HardwareSerial {
221221
}
222222

223223
int available(void) {
224+
USBCDC::lock();
224225
onInterrupt();
225-
return rx_buffer.available();
226+
auto ret = rx_buffer.available();
227+
USBCDC::unlock();
228+
return ret;
226229
}
227230

228231
int peek(void) {
232+
USBCDC::lock();
229233
onInterrupt();
230-
return rx_buffer.peek();
234+
auto ret = rx_buffer.peek();
235+
USBCDC::unlock();
236+
return ret;
231237
}
232238

233239
int read(void) {
240+
USBCDC::lock();
234241
onInterrupt();
235-
return rx_buffer.read_char();
242+
auto ret = rx_buffer.read_char();
243+
USBCDC::unlock();
244+
return ret;
236245
}
237246

238247
void flush(void) {}

0 commit comments

Comments
 (0)