This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 627d825014fe30931e52c2034ed8616180f5eb4d Author: zhanghongyu <[email protected]> AuthorDate: Tue Nov 10 19:19:46 2026 +0800 drivers/pipes: add pipecommon_writev() with an opt-in atomic mode POLLIN is raised per write, so a message written in chunks can be seen half-written: the reader consumes the length prefix, finds no payload and returns -EAGAIN without putting the bytes back. Register pipecommon_writev(), byte-stream by default. PIPEIOC_ATOMICWRITE makes a request land in full under one d_bflock or fail, with -EMSGSIZE if it exceeds the buffer and so could never be admitted. Signed-off-by: zhanghongyu <[email protected]> --- drivers/pipes/fifo.c | 2 +- drivers/pipes/pipe.c | 4 +- drivers/pipes/pipe_common.c | 306 ++++++++++++++++++++++++++++++++++++++++++++ drivers/pipes/pipe_common.h | 8 ++ include/nuttx/fs/ioctl.h | 6 + 5 files changed, 324 insertions(+), 2 deletions(-) diff --git a/drivers/pipes/fifo.c b/drivers/pipes/fifo.c index f96f23b6525..f98bf59a622 100644 --- a/drivers/pipes/fifo.c +++ b/drivers/pipes/fifo.c @@ -53,7 +53,7 @@ static const struct file_operations g_fifo_fops = NULL, /* truncate */ pipecommon_poll, /* poll */ NULL, /* readv */ - NULL /* writev */ + pipecommon_writev /* writev */ #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS , pipecommon_unlink /* unlink */ #endif diff --git a/drivers/pipes/pipe.c b/drivers/pipes/pipe.c index 97e4a8e682a..dc074f8b26f 100644 --- a/drivers/pipes/pipe.c +++ b/drivers/pipes/pipe.c @@ -67,7 +67,9 @@ static const struct file_operations g_pipe_fops = pipecommon_ioctl, /* ioctl */ pipe_mmap, /* mmap */ NULL, /* truncate */ - pipecommon_poll /* poll */ + pipecommon_poll, /* poll */ + NULL, /* readv */ + pipecommon_writev /* writev */ }; static mutex_t g_pipelock = NXMUTEX_INITIALIZER; diff --git a/drivers/pipes/pipe_common.c b/drivers/pipes/pipe_common.c index f96a574f335..150e99f5bef 100644 --- a/drivers/pipes/pipe_common.c +++ b/drivers/pipes/pipe_common.c @@ -45,6 +45,7 @@ #include <nuttx/semaphore.h> #include <nuttx/fs/fs.h> #include <nuttx/fs/ioctl.h> +#include <nuttx/fs/uio.h> #include "pipe_common.h" @@ -666,6 +667,296 @@ ssize_t pipecommon_write(FAR struct file *filep, FAR const char *buffer, } } +/**************************************************************************** + * Name: pipecommon_writev + * + * Description: + * Gather write. In the default byte-stream mode this behaves like a + * sequence of single-buffer writes: each segment is copied in order, + * short writes and blocking happen exactly as they would for a plain + * write(), and a request larger than the whole buffer is admitted a + * piece at a time as the reader drains it. When PIPE_FLAG_ATOMIC is set, + * the whole request is admitted all-or-nothing: no byte becomes visible + * to the reader until every segment has been copied while holding + * d_bflock. A single writev() then carries one complete message (a + * datagram whose header and payload live in separate segments), so a + * reader never observes a partial frame; a request that cannot ever fit + * the buffer is rejected with -EMSGSIZE instead of being written in part. + * + ****************************************************************************/ + +ssize_t pipecommon_writev(FAR struct file *filep, FAR struct uio *uio) +{ + FAR struct inode *inode = filep->f_inode; + FAR struct pipe_dev_s *dev = inode->i_private; + FAR const struct iovec *iov; + size_t offset; + size_t total; + int iovcnt; + int ret; + + DEBUGASSERT(dev); + DEBUGASSERT(up_interrupt_context() == false); + + total = uio->uio_resid; + if (total == 0) + { + return 0; + } + + if (!PIPE_IS_ATOMIC(dev->d_flags)) + { + ssize_t ntotal = 0; + ssize_t last; + + /* Byte-stream gather write: copy each segment in order, applying the + * same short-write, blocking and error reporting rules a plain + * write() would. A request larger than the buffer is written a piece + * at a time as the reader drains it, so there is no upper bound here. + */ + + iov = uio->uio_iov; + iovcnt = uio->uio_iovcnt; + offset = uio->uio_offset_in_iov; + + ret = nxrmutex_lock(&dev->d_bflock); + if (ret < 0) + { + return ret; + } + + last = 0; + while (iovcnt > 0) + { + FAR const uint8_t *base = (FAR const uint8_t *)iov->iov_base; + size_t len = iov->iov_len - offset; + + if (len == 0) + { + offset = 0; + iov++; + iovcnt--; + continue; + } + + /* REVISIT: "If all file descriptors referring to the read end of + * a pipe have been closed, then a write will cause a SIGPIPE + * signal to be generated for the calling process. If the calling + * process is ignoring this signal, then write(2) fails with the + * error EPIPE." + */ + + if (dev->d_nreaders <= 0 && PIPE_IS_POLICY_0(dev->d_flags)) + { + nxrmutex_unlock(&dev->d_bflock); + if (ntotal == 0) + { + return -EPIPE; + } + + uio_advance(uio, ntotal); + return ntotal; + } + + /* Would the next write overflow the circular buffer? */ + + if (!circbuf_is_full(&dev->d_buffer)) + { + size_t nwritten; + + nwritten = circbuf_write(&dev->d_buffer, base + offset, len); + ntotal += nwritten; + offset += nwritten; + + if (nwritten == len) + { + offset = 0; + iov++; + iovcnt--; + } + + if (iovcnt == 0) + { + /* Notify all poll/select waiters that they can read from + * the FIFO when buffer used exceeds poll threshold. + */ + + if (circbuf_used(&dev->d_buffer) > dev->d_pollinthrd) + { + poll_notify(dev->d_fds, CONFIG_DEV_PIPE_NPOLLWAITERS, + POLLIN); + } + + pipecommon_wakeup(&dev->d_rdsem); + + nxrmutex_unlock(&dev->d_bflock); + uio_advance(uio, ntotal); + return ntotal; + } + } + else + { + /* There is not enough room for the next byte. Was anything + * written in this pass? + */ + + if (last < ntotal) + { + poll_notify(dev->d_fds, CONFIG_DEV_PIPE_NPOLLWAITERS, + POLLIN); + pipecommon_wakeup(&dev->d_rdsem); + } + + last = ntotal; + + /* If O_NONBLOCK was set, then return partial bytes written or + * EAGAIN. + */ + + if (filep->f_oflags & O_NONBLOCK) + { + nxrmutex_unlock(&dev->d_bflock); + if (ntotal == 0) + { + return -EAGAIN; + } + + uio_advance(uio, ntotal); + return ntotal; + } + + /* There is more to be written.. wait for data to be removed + * from the pipe. + */ + + nxrmutex_unlock(&dev->d_bflock); + ret = nxsem_wait(&dev->d_wrsem); + if (ret < 0) + { + /* nxsem_wait may fail because a signal was received or + * because the task was canceled. + */ + + if (ntotal == 0) + { + return (ssize_t)ret; + } + + uio_advance(uio, ntotal); + return ntotal; + } + + ret = nxrmutex_lock(&dev->d_bflock); + if (ret < 0) + { + if (ntotal == 0) + { + return (ssize_t)ret; + } + + uio_advance(uio, ntotal); + return ntotal; + } + } + } + + nxrmutex_unlock(&dev->d_bflock); + uio_advance(uio, ntotal); + return ntotal; + } + + /* A request bigger than the whole buffer can never be admitted + * atomically, so reporting -EAGAIN would make the caller retry forever + * and blocking would wait for space that cannot appear. + */ + + if (total > dev->d_bufsize) + { + return -EMSGSIZE; + } + + ret = nxrmutex_lock(&dev->d_bflock); + if (ret < 0) + { + return ret; + } + + for (; ; ) + { + if (dev->d_nreaders <= 0 && PIPE_IS_POLICY_0(dev->d_flags)) + { + nxrmutex_unlock(&dev->d_bflock); + return -EPIPE; + } + + if (circbuf_space(&dev->d_buffer) >= total) + { + break; + } + + if (filep->f_oflags & O_NONBLOCK) + { + nxrmutex_unlock(&dev->d_bflock); + return -EAGAIN; + } + + /* Wait for the reader to free enough room for the whole request. + * Nothing has been written yet, so there is no partial state to + * publish or to roll back here. + */ + + nxrmutex_unlock(&dev->d_bflock); + ret = nxsem_wait(&dev->d_wrsem); + if (ret < 0) + { + return ret; + } + + ret = nxrmutex_lock(&dev->d_bflock); + if (ret < 0) + { + return ret; + } + } + + /* Room for the whole request is guaranteed from here to the notify + * below, and d_bflock is held throughout, so the reader observes the + * request as a single unit. Only uio_iov[0] carries an offset. + */ + + iov = uio->uio_iov; + iovcnt = uio->uio_iovcnt; + offset = uio->uio_offset_in_iov; + + while (iovcnt-- > 0) + { + size_t len = iov->iov_len - offset; + + if (len > 0) + { + pipe_dumpbuffer("To PIPE:", + (FAR uint8_t *)iov->iov_base + offset, len); + circbuf_write(&dev->d_buffer, + (FAR const uint8_t *)iov->iov_base + offset, len); + } + + offset = 0; + iov++; + } + + if (circbuf_used(&dev->d_buffer) > dev->d_pollinthrd) + { + poll_notify(dev->d_fds, CONFIG_DEV_PIPE_NPOLLWAITERS, POLLIN); + } + + pipecommon_wakeup(&dev->d_rdsem); + + nxrmutex_unlock(&dev->d_bflock); + + uio_advance(uio, total); + return total; +} + /**************************************************************************** * Name: pipecommon_poll ****************************************************************************/ @@ -832,6 +1123,21 @@ int pipecommon_ioctl(FAR struct file *filep, int cmd, unsigned long arg) } break; + case PIPEIOC_ATOMICWRITE: + { + if (arg != 0) + { + PIPE_ATOMIC_1(dev->d_flags); + } + else + { + PIPE_ATOMIC_0(dev->d_flags); + } + + ret = OK; + } + break; + case PIPEIOC_POLLINTHRD: { pipe_ndx_t threshold = (pipe_ndx_t)arg; diff --git a/drivers/pipes/pipe_common.h b/drivers/pipes/pipe_common.h index fdf0c26698a..d4bb8eaabf8 100644 --- a/drivers/pipes/pipe_common.h +++ b/drivers/pipes/pipe_common.h @@ -87,6 +87,8 @@ #define PIPE_FLAG_POLICY (1 << 0) /* Bit 0: Policy=Free buffer when empty */ #define PIPE_FLAG_UNLINKED (1 << 1) /* Bit 1: The driver has been unlinked */ +#define PIPE_FLAG_ATOMIC (1 << 2) /* Bit 2: writev admits a request as a + * single atomic frame */ #define PIPE_POLICY_0(f) do { (f) &= ~PIPE_FLAG_POLICY; } while (0) #define PIPE_POLICY_1(f) do { (f) |= PIPE_FLAG_POLICY; } while (0) @@ -96,6 +98,10 @@ #define PIPE_UNLINK(f) do { (f) |= PIPE_FLAG_UNLINKED; } while (0) #define PIPE_IS_UNLINKED(f) (((f) & PIPE_FLAG_UNLINKED) != 0) +#define PIPE_ATOMIC_0(f) do { (f) &= ~PIPE_FLAG_ATOMIC; } while (0) +#define PIPE_ATOMIC_1(f) do { (f) |= PIPE_FLAG_ATOMIC; } while (0) +#define PIPE_IS_ATOMIC(f) (((f) & PIPE_FLAG_ATOMIC) != 0) + /**************************************************************************** * Public Types ****************************************************************************/ @@ -150,6 +156,7 @@ extern "C" struct file; /* Forward reference */ struct inode; /* Forward reference */ +struct uio; /* Forward reference */ FAR struct pipe_dev_s *pipecommon_allocdev(size_t bufsize); void pipecommon_freedev(FAR struct pipe_dev_s *dev); @@ -157,6 +164,7 @@ int pipecommon_open(FAR struct file *filep); int pipecommon_close(FAR struct file *filep); ssize_t pipecommon_read(FAR struct file *, FAR char *, size_t); ssize_t pipecommon_write(FAR struct file *, FAR const char *, size_t); +ssize_t pipecommon_writev(FAR struct file *filep, FAR struct uio *uio); int pipecommon_ioctl(FAR struct file *filep, int cmd, unsigned long arg); int pipecommon_poll(FAR struct file *filep, FAR struct pollfd_s *fds, bool setup); diff --git a/include/nuttx/fs/ioctl.h b/include/nuttx/fs/ioctl.h index 5eafc7b2cc2..adeb5906c76 100644 --- a/include/nuttx/fs/ioctl.h +++ b/include/nuttx/fs/ioctl.h @@ -561,6 +561,12 @@ * IN: None * OUT: int */ +#define PIPEIOC_ATOMICWRITE _PIPEIOC(0x0007) /* Set atomic writev mode + * IN: unsigned long (0 to + * disable, non-zero to + * enable) + * OUT: None */ + /* RTC driver ioctl definitions *********************************************/ /* (see nuttx/include/rtc.h */
