Stop flserial's SerialThread spinning on an idle serial port

POSIX opens the port O_NONBLOCK with VMIN/VTIME=0, and fl_open skips
setTimeout on Linux/macOS, so the default all-zero Timeout makes
SerialThread's read() return immediately. With no traffic the reader
spins at 100% of a core for the whole session. Release builds and a
minimized window do not help because it is a native thread.

Set a 10 ms read/select timeout on POSIX. Data still wakes select
immediately; the timeout only bounds idle wakeups and queued-write
latency. Apply the same change to the macOS copy of flserial.

Follow-up to #557.

Measured with a PTY harness, idle: 99.9% of one core before, 0.2%
after. RX and TX still pass.
This commit is contained in:
Aleksandrs Sevastjanovs committed 2026-10-30 16:15:15 +00:00
1 parent ac28dac575
commit bb7a15f436
2 files changed
+12 -2

No files matched your search

+6 -1
View File
@@ -154,7 +154,12 @@ FFI_PLUGIN_EXPORT int fl_open(int flh, char *portname, int baudrate)
port->serialport = new serial::Serial();
#if !defined(__linux__) && !defined(__APPLE__)
port->serialport->setTimeout(serial::Timeout(0, 2, 0, 0, 0));
#endif // #if defined(__linux__)
#else
// With a zero read timeout, read() returns immediately on an idle
// O_NONBLOCK port and SerialThread spins. A short timeout lets select()
// sleep; arriving data still wakes it immediately.
port->serialport->setTimeout(10, 10, 0, 0, 0);
#endif
port->serialport->setPort(portname);
port->serialport->setBaudrate(baudrate);
+6 -1
View File
@@ -167,7 +167,12 @@ FFI_PLUGIN_EXPORT int fl_open(int flh, char *portname, int baudrate)
port->serialport = new serial::Serial();
#if !defined(__linux__) && !defined(__APPLE__)
port->serialport->setTimeout(serial::Timeout(0, 2, 0, 0, 0));
#endif // #if defined(__linux__)
#else
// With a zero read timeout, read() returns immediately on an idle
// O_NONBLOCK port and SerialThread spins. A short timeout lets select()
// sleep; arriving data still wakes it immediately.
port->serialport->setTimeout(10, 10, 0, 0, 0);
#endif
port->serialport->setPort(portname);
port->serialport->setBaudrate(baudrate);