From 70ee8ca6492f1f67e8c04d1940cfb6269580fc17 Mon Sep 17 00:00:00 2001 From: Florian Loitsch Date: Fri, 22 Jul 2022 10:55:00 +0200 Subject: [PATCH] Remove work-around for uart_get_buffered_data_len The race condition has been fixed: https://github.com/espressif/esp-idf/issues/6397 --- src/resources/uart_esp32.cc | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/resources/uart_esp32.cc b/src/resources/uart_esp32.cc index 2f9b0ab4f..03aa5ac99 100644 --- a/src/resources/uart_esp32.cc +++ b/src/resources/uart_esp32.cc @@ -290,17 +290,12 @@ PRIMITIVE(read) { return Primitive::os_error(err, process); } - // The uart_get_buffered_data_len is not thread safe, so it's not guaranteed that - // we get an updated value. As false negatives can lead to deadlock, we don't trust - // when it returns 0. To work around this, we instead try to read 8 bytes, whenever - // we are suggested 0; it is always safe to try to read too much, so this is only a - // performance issue. - size_t capacity = Utils::max(available, (size_t)8); - Error* error = null; - ByteArray* data = process->allocate_byte_array(capacity, &error, /*force_external*/ true); + ByteArray* data = process->allocate_byte_array(available, &error, /*force_external*/ available != 0); if (data == null) return error; + if (available == 0) return data; + ByteArray::Bytes rx(data); int read = uart_read_bytes(uart->port(), rx.address(), rx.length(), 0); if (read == -1) { @@ -311,8 +306,6 @@ PRIMITIVE(read) { return process->allocate_string_or_error("broken UART read"); } - data->resize_external(process, read); - return data; }