Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions src/resources/uart_esp32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -311,8 +306,6 @@ PRIMITIVE(read) {
return process->allocate_string_or_error("broken UART read");
}

data->resize_external(process, read);

return data;
}

Expand Down