Add mkfifo2() and pipe2() which are just like mkfifo() and pipe(), but allow control of the size of the underlying, in-memory circular buffer

This commit is contained in:
Gregory Nutt 2016-07-19 13:51:05 -06:00
parent 9a44f3017b
commit 671d7fae31
4 changed files with 90 additions and 22 deletions

View File

@ -74,6 +74,62 @@ static const struct file_operations fifo_fops =
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: mkfifo2
*
* Description:
* mkfifo() makes a FIFO device driver file with name 'pathname.' Unlike
* Linux, a NuttX FIFO is not a special file type but simply a device
* driver instance. 'mode' specifies the FIFO's permissions.
*
* Once the FIFO has been created by mkfifo(), any thread can open it for
* reading or writing, in the same way as an ordinary file. However, it
* must have been opened from both reading and writing before input or
* output can be performed. This FIFO implementation will block all
* attempts to open a FIFO read-only until at least one thread has opened
* the FIFO for writing.
*
* If all threads that write to the FIFO have closed, subsequent calls to
* read() on the FIFO will return 0 (end-of-file).
*
* NOTE: mkfifo2 is a special, non-standard, NuttX-only interface. Since
* the NuttX FIFOs are based in in-memory, circular buffers, the ability
* to control the size of those buffers is critical for system tuning.
*
* Inputs:
* pathname - The full path to the FIFO instance to attach to or to create
* (if not already created).
* mode - Ignored for now
* bufsize - The size of the in-memory, circular buffer in bytes.
*
* Return:
* 0 is returned on success; otherwise, -1 is returned with errno set
* appropriately.
*
****************************************************************************/
int mkfifo2(FAR const char *pathname, mode_t mode, size_t bufsize)
{
FAR struct pipe_dev_s *dev;
int ret;
/* Allocate and initialize a new device structure instance */
dev = pipecommon_allocdev(bufsize);
if (!dev)
{
return -ENOMEM;
}
ret = register_driver(pathname, &fifo_fops, mode, (FAR void *)dev);
if (ret != 0)
{
pipecommon_freedev(dev);
}
return ret;
}
/****************************************************************************
* Name: mkfifo
*
@ -105,24 +161,7 @@ static const struct file_operations fifo_fops =
int mkfifo(FAR const char *pathname, mode_t mode)
{
FAR struct pipe_dev_s *dev;
int ret;
/* Allocate and initialize a new device structure instance */
dev = pipecommon_allocdev(CONFIG_DEV_FIFO_SIZE);
if (!dev)
{
return -ENOMEM;
}
ret = register_driver(pathname, &fifo_fops, mode, (FAR void *)dev);
if (ret != 0)
{
pipecommon_freedev(dev);
}
return ret;
return mkfifo2(pathname, mode, CONFIG_DEV_FIFO_SIZE);
}
#endif /* CONFIG_DEV_FIFO_SIZE > 0 */

View File

@ -167,16 +167,21 @@ static int pipe_close(FAR struct file *filep)
****************************************************************************/
/****************************************************************************
* Name: pipe
* Name: pipe2
*
* Description:
* pipe() creates a pair of file descriptors, pointing to a pipe inode,
* and places them in the array pointed to by 'fd'. fd[0] is for reading,
* fd[1] is for writing.
*
* NOTE: mkfifo2 is a special, non-standard, NuttX-only interface. Since
* the NuttX FIFOs are based in in-memory, circular buffers, the ability
* to control the size of those buffers is critical for system tuning.
*
* Inputs:
* fd[2] - The user provided array in which to catch the pipe file
* descriptors
* bufsize - The size of the in-memory, circular buffer in bytes.
*
* Return:
* 0 is returned on success; otherwise, -1 is returned with errno set
@ -184,7 +189,7 @@ static int pipe_close(FAR struct file *filep)
*
****************************************************************************/
int pipe(int fd[2])
int pipe2(int fd[2], size_t bufsize)
{
FAR struct pipe_dev_s *dev = NULL;
char devname[16];
@ -222,7 +227,7 @@ int pipe(int fd[2])
{
/* No.. Allocate and initialize a new device structure instance */
dev = pipecommon_allocdev(CONFIG_DEV_PIPE_SIZE);
dev = pipecommon_allocdev(bufsize);
if (!dev)
{
(void)sem_post(&g_pipesem);
@ -289,4 +294,26 @@ errout:
return ERROR;
}
/****************************************************************************
* Name: pipe2
*
* Description:
* pipe() creates a pair of file descriptors, pointing to a pipe inode,
* and places them in the array pointed to by 'fd'. fd[0] is for reading,
* fd[1] is for writing.
*
* Inputs:
* fd[2] - The user provided array in which to catch the pipe file
* descriptors
*
* Return:
* 0 is returned on success; otherwise, -1 is returned with errno set
* appropriately.
*
****************************************************************************/
int pipe(int fd[2])
{
return pipe2(fd, CONFIG_DEV_PIPE_SIZE);
}
#endif /* CONFIG_DEV_PIPE_SIZE > 0 */

View File

@ -130,6 +130,7 @@ extern "C"
#endif
int mkdir(FAR const char *pathname, mode_t mode);
int mkfifo2(FAR const char *pathname, mode_t mode, size_t bufsize); /* NuttX only */
int mkfifo(FAR const char *pathname, mode_t mode);
int stat(const char *path, FAR struct stat *buf);
#if 0 /* Not yet supported */

View File

@ -1,7 +1,7 @@
/****************************************************************************
* include/unistd.h
*
* Copyright (C) 2007-2009, 2013-2014 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2009, 2013-2014, 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -174,6 +174,7 @@ FAR void *sbrk(intptr_t incr);
/* Special devices */
int pipe2(int fd[2], size_t bufsize); /* NuttX only */
int pipe(int fd[2]);
/* Working directory operations */