From 65bb956e5eb921bbe7b1c4beed18979d7c725703 Mon Sep 17 00:00:00 2001 From: spiriou Date: Mon, 21 Sep 2020 11:41:20 +0200 Subject: [PATCH] pipe: check for writers first in case of pipe empty --- drivers/pipes/pipe_common.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/pipes/pipe_common.c b/drivers/pipes/pipe_common.c index e21f169943..0bf6095eb3 100644 --- a/drivers/pipes/pipe_common.c +++ b/drivers/pipes/pipe_common.c @@ -426,14 +426,6 @@ ssize_t pipecommon_read(FAR struct file *filep, FAR char *buffer, size_t len) while (dev->d_wrndx == dev->d_rdndx) { - /* If O_NONBLOCK was set, then return EGAIN */ - - if (filep->f_oflags & O_NONBLOCK) - { - nxsem_post(&dev->d_bfsem); - return -EAGAIN; - } - /* If there are no writers on the pipe, then return end of file */ if (dev->d_nwriters <= 0) @@ -442,6 +434,14 @@ ssize_t pipecommon_read(FAR struct file *filep, FAR char *buffer, size_t len) return 0; } + /* If O_NONBLOCK was set, then return EGAIN */ + + if (filep->f_oflags & O_NONBLOCK) + { + nxsem_post(&dev->d_bfsem); + return -EAGAIN; + } + /* Otherwise, wait for something to be written to the pipe */ sched_lock();