mirror of
https://github.com/apache/nuttx.git
synced 2026-05-29 20:56:47 +08:00
drivers/pipe: Remove pipe from file system after open
to make the pipe as an anonymous object as soon as possible and simplify the life cycle management Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
+33
-86
@@ -39,12 +39,6 @@
|
|||||||
|
|
||||||
#if CONFIG_DEV_PIPE_SIZE > 0
|
#if CONFIG_DEV_PIPE_SIZE > 0
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Pre-processor Definitions
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
#define MAX_PIPES 32
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Types
|
* Private Types
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@@ -68,14 +62,10 @@ static const struct file_operations g_pipe_fops =
|
|||||||
NULL, /* seek */
|
NULL, /* seek */
|
||||||
pipecommon_ioctl, /* ioctl */
|
pipecommon_ioctl, /* ioctl */
|
||||||
pipecommon_poll /* poll */
|
pipecommon_poll /* poll */
|
||||||
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
||||||
, pipecommon_unlink /* unlink */
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static sem_t g_pipesem = SEM_INITIALIZER(1);
|
static sem_t g_pipesem = SEM_INITIALIZER(1);
|
||||||
static uint32_t g_pipeset = 0;
|
static int g_pipeno;
|
||||||
static uint32_t g_pipecreated = 0;
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Functions
|
* Private Functions
|
||||||
@@ -86,37 +76,23 @@ static uint32_t g_pipecreated = 0;
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static inline int pipe_allocate(void)
|
static inline int pipe_allocate(void)
|
||||||
{
|
|
||||||
int pipeno;
|
|
||||||
int ret = -ENFILE;
|
|
||||||
|
|
||||||
for (pipeno = 0; pipeno < MAX_PIPES; pipeno++)
|
|
||||||
{
|
|
||||||
if ((g_pipeset & (1 << pipeno)) == 0)
|
|
||||||
{
|
|
||||||
g_pipeset |= (1 << pipeno);
|
|
||||||
ret = pipeno;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Name: pipe_free
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
static inline void pipe_free(int pipeno)
|
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = nxsem_wait(&g_pipesem);
|
ret = nxsem_wait(&g_pipesem);
|
||||||
if (ret == OK)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
g_pipeset &= ~(1 << pipeno);
|
return ret;
|
||||||
nxsem_post(&g_pipesem);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ret = g_pipeno++;
|
||||||
|
if (g_pipeno < 0)
|
||||||
|
{
|
||||||
|
g_pipeno = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
nxsem_post(&g_pipesem);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@@ -138,7 +114,7 @@ static int pipe_close(FAR struct file *filep)
|
|||||||
{
|
{
|
||||||
/* Release the pipe when there are no further open references to it. */
|
/* Release the pipe when there are no further open references to it. */
|
||||||
|
|
||||||
pipe_free(dev->d_pipeno);
|
pipecommon_freedev(dev);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@@ -155,69 +131,34 @@ static int pipe_register(size_t bufsize, int flags,
|
|||||||
int pipeno;
|
int pipeno;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
/* Get exclusive access to the pipe allocation data */
|
|
||||||
|
|
||||||
ret = nxsem_wait(&g_pipesem);
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
goto errout;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Allocate a minor number for the pipe device */
|
/* Allocate a minor number for the pipe device */
|
||||||
|
|
||||||
pipeno = pipe_allocate();
|
pipeno = pipe_allocate();
|
||||||
if (pipeno < 0)
|
if (pipeno < 0)
|
||||||
{
|
{
|
||||||
ret = pipeno;
|
return pipeno;
|
||||||
goto errout_with_sem;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create a pathname to the pipe device */
|
/* Create a pathname to the pipe device */
|
||||||
|
|
||||||
snprintf(devname, namesize, CONFIG_DEV_PIPE_VFS_PATH"/%d", pipeno);
|
snprintf(devname, namesize, CONFIG_DEV_PIPE_VFS_PATH"/%d", pipeno);
|
||||||
|
|
||||||
/* Check if the pipe device has already been created */
|
/* Allocate and initialize a new device structure instance */
|
||||||
|
|
||||||
if ((g_pipecreated & (1 << pipeno)) == 0)
|
dev = pipecommon_allocdev(bufsize);
|
||||||
|
if (dev == NULL)
|
||||||
{
|
{
|
||||||
/* No.. Allocate and initialize a new device structure instance */
|
return -ENOMEM;
|
||||||
|
|
||||||
dev = pipecommon_allocdev(bufsize);
|
|
||||||
if (!dev)
|
|
||||||
{
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto errout_with_pipe;
|
|
||||||
}
|
|
||||||
|
|
||||||
dev->d_pipeno = pipeno;
|
|
||||||
|
|
||||||
/* Register the pipe device */
|
|
||||||
|
|
||||||
ret = register_driver(devname, &g_pipe_fops, 0666, (FAR void *)dev);
|
|
||||||
if (ret != 0)
|
|
||||||
{
|
|
||||||
nxsem_post(&g_pipesem);
|
|
||||||
goto errout_with_dev;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Remember that we created this device */
|
|
||||||
|
|
||||||
g_pipecreated |= (1 << pipeno);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nxsem_post(&g_pipesem);
|
/* Register the pipe device */
|
||||||
return OK;
|
|
||||||
|
|
||||||
errout_with_dev:
|
ret = register_driver(devname, &g_pipe_fops, 0666, (FAR void *)dev);
|
||||||
pipecommon_freedev(dev);
|
if (ret != 0)
|
||||||
|
{
|
||||||
|
pipecommon_freedev(dev);
|
||||||
|
}
|
||||||
|
|
||||||
errout_with_pipe:
|
|
||||||
pipe_free(pipeno);
|
|
||||||
|
|
||||||
errout_with_sem:
|
|
||||||
nxsem_post(&g_pipesem);
|
|
||||||
|
|
||||||
errout:
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,7 +192,7 @@ errout:
|
|||||||
|
|
||||||
int file_pipe(FAR struct file *filep[2], size_t bufsize, int flags)
|
int file_pipe(FAR struct file *filep[2], size_t bufsize, int flags)
|
||||||
{
|
{
|
||||||
char devname[16];
|
char devname[32];
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
/* Register a new pipe device */
|
/* Register a new pipe device */
|
||||||
@@ -278,6 +219,9 @@ int file_pipe(FAR struct file *filep[2], size_t bufsize, int flags)
|
|||||||
goto errout_with_wrfd;
|
goto errout_with_wrfd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Remove the pipe name from file system */
|
||||||
|
|
||||||
|
unregister_driver(devname);
|
||||||
return OK;
|
return OK;
|
||||||
|
|
||||||
errout_with_wrfd:
|
errout_with_wrfd:
|
||||||
@@ -290,7 +234,7 @@ errout_with_driver:
|
|||||||
|
|
||||||
int nx_pipe(int fd[2], size_t bufsize, int flags)
|
int nx_pipe(int fd[2], size_t bufsize, int flags)
|
||||||
{
|
{
|
||||||
char devname[16];
|
char devname[32];
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
/* Register a new pipe device */
|
/* Register a new pipe device */
|
||||||
@@ -319,6 +263,9 @@ int nx_pipe(int fd[2], size_t bufsize, int flags)
|
|||||||
goto errout_with_wrfd;
|
goto errout_with_wrfd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Remove the pipe name from file system */
|
||||||
|
|
||||||
|
unregister_driver(devname);
|
||||||
return OK;
|
return OK;
|
||||||
|
|
||||||
errout_with_wrfd:
|
errout_with_wrfd:
|
||||||
|
|||||||
@@ -37,9 +37,6 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
|
|
||||||
#ifdef CONFIG_DEBUG_FEATURES
|
|
||||||
# include <nuttx/arch.h>
|
|
||||||
#endif
|
|
||||||
#include <nuttx/kmalloc.h>
|
#include <nuttx/kmalloc.h>
|
||||||
#include <nuttx/semaphore.h>
|
#include <nuttx/semaphore.h>
|
||||||
#include <nuttx/fs/fs.h>
|
#include <nuttx/fs/fs.h>
|
||||||
|
|||||||
@@ -121,7 +121,6 @@ struct pipe_dev_s
|
|||||||
pipe_ndx_t d_bufsize; /* allocated size of d_buffer in bytes */
|
pipe_ndx_t d_bufsize; /* allocated size of d_buffer in bytes */
|
||||||
uint8_t d_nwriters; /* Number of reference counts for write access */
|
uint8_t d_nwriters; /* Number of reference counts for write access */
|
||||||
uint8_t d_nreaders; /* Number of reference counts for read access */
|
uint8_t d_nreaders; /* Number of reference counts for read access */
|
||||||
uint8_t d_pipeno; /* Pipe minor number */
|
|
||||||
uint8_t d_flags; /* See PIPE_FLAG_* definitions */
|
uint8_t d_flags; /* See PIPE_FLAG_* definitions */
|
||||||
uint8_t *d_buffer; /* Buffer allocated when device opened */
|
uint8_t *d_buffer; /* Buffer allocated when device opened */
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user