Limit the iteration count when reading from a fifo

This commit is contained in:
Maxime Coste 2014-08-26 22:11:23 +01:00
parent f96fa66a41
commit 114d33c7f8

View File

@ -85,6 +85,10 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll)
auto watcher = new FDWatcher(fd, [buffer, scroll](FDWatcher& watcher) { auto watcher = new FDWatcher(fd, [buffer, scroll](FDWatcher& watcher) {
constexpr size_t buffer_size = 2048; constexpr size_t buffer_size = 2048;
// if we read data slower than it arrives in the fifo, limiting the
// iteration number allows us to go back go back to the event loop and
// handle other events sources (such as input)
size_t loops = 16;
char data[buffer_size]; char data[buffer_size];
const int fifo = watcher.fd(); const int fifo = watcher.fd();
timeval tv{ 0, 0 }; timeval tv{ 0, 0 };
@ -113,7 +117,8 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll)
FD_ZERO(&rfds); FD_ZERO(&rfds);
FD_SET(fifo, &rfds); FD_SET(fifo, &rfds);
} }
while (count > 0 and select(fifo+1, &rfds, nullptr, nullptr, &tv) == 1); while (--loops and count > 0 and
select(fifo+1, &rfds, nullptr, nullptr, &tv) == 1);
if (count <= 0) if (count <= 0)
{ {