|
|
@@ -20,16 +20,17 @@
|
|
|
static int pipe_fops_open(struct dfs_fd *fd)
|
|
|
{
|
|
|
int rc = 0;
|
|
|
- rt_device_t device;
|
|
|
rt_pipe_t *pipe;
|
|
|
|
|
|
pipe = (rt_pipe_t *)fd->fnode->data;
|
|
|
- if (!pipe) return -1;
|
|
|
+ if (!pipe)
|
|
|
+ {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
|
|
|
- device = &(pipe->parent);
|
|
|
- rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
|
|
+ rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
|
|
|
|
|
|
- if (device->ref_count == 0)
|
|
|
+ if (fd->fnode->ref_count == 1)
|
|
|
{
|
|
|
pipe->fifo = rt_ringbuffer_create(pipe->bufsz);
|
|
|
if (pipe->fifo == RT_NULL)
|
|
|
@@ -39,23 +40,8 @@ static int pipe_fops_open(struct dfs_fd *fd)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- switch (fd->fnode->flags & O_ACCMODE)
|
|
|
- {
|
|
|
- case O_RDONLY:
|
|
|
- pipe->readers++;
|
|
|
- break;
|
|
|
- case O_WRONLY:
|
|
|
- pipe->writers++;
|
|
|
- break;
|
|
|
- case O_RDWR:
|
|
|
- pipe->readers++;
|
|
|
- pipe->writers++;
|
|
|
- break;
|
|
|
- }
|
|
|
- device->ref_count++;
|
|
|
-
|
|
|
__exit:
|
|
|
- rt_mutex_release(&(pipe->lock));
|
|
|
+ rt_mutex_release(&pipe->lock);
|
|
|
|
|
|
return rc;
|
|
|
}
|
|
|
@@ -66,46 +52,26 @@ static int pipe_fops_close(struct dfs_fd *fd)
|
|
|
rt_pipe_t *pipe;
|
|
|
|
|
|
pipe = (rt_pipe_t *)fd->fnode->data;
|
|
|
- if (!pipe) return -1;
|
|
|
-
|
|
|
- device = &(pipe->parent);
|
|
|
- rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
|
|
-
|
|
|
- switch (fd->fnode->flags & O_ACCMODE)
|
|
|
+ if (!pipe)
|
|
|
{
|
|
|
- case O_RDONLY:
|
|
|
- pipe->readers--;
|
|
|
- break;
|
|
|
- case O_WRONLY:
|
|
|
- pipe->writers--;
|
|
|
- break;
|
|
|
- case O_RDWR:
|
|
|
- pipe->readers--;
|
|
|
- pipe->writers--;
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- if (pipe->writers == 0)
|
|
|
- {
|
|
|
- rt_wqueue_wakeup(&(pipe->reader_queue), (void*)(POLLIN | POLLERR | POLLHUP));
|
|
|
+ return -1;
|
|
|
}
|
|
|
|
|
|
- if (pipe->readers == 0)
|
|
|
- {
|
|
|
- rt_wqueue_wakeup(&(pipe->writer_queue), (void*)(POLLOUT | POLLERR | POLLHUP));
|
|
|
- }
|
|
|
+ device = &pipe->parent;
|
|
|
+ rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
|
|
|
|
|
|
- if (device->ref_count == 1)
|
|
|
+ if (fd->fnode->ref_count == 1)
|
|
|
{
|
|
|
if (pipe->fifo != RT_NULL)
|
|
|
+ {
|
|
|
rt_ringbuffer_destroy(pipe->fifo);
|
|
|
+ }
|
|
|
pipe->fifo = RT_NULL;
|
|
|
}
|
|
|
- device->ref_count--;
|
|
|
|
|
|
- rt_mutex_release(&(pipe->lock));
|
|
|
+ rt_mutex_release(&pipe->lock);
|
|
|
|
|
|
- if (device->ref_count == 0 && pipe->is_named == RT_FALSE)
|
|
|
+ if (fd->fnode->ref_count == 1 && pipe->is_named == RT_FALSE)
|
|
|
{
|
|
|
/* delete the unamed pipe */
|
|
|
rt_pipe_delete(device->parent.name);
|
|
|
@@ -145,18 +111,10 @@ static int pipe_fops_read(struct dfs_fd *fd, void *buf, size_t count)
|
|
|
pipe = (rt_pipe_t *)fd->fnode->data;
|
|
|
|
|
|
/* no process has the pipe open for writing, return end-of-file */
|
|
|
- if (pipe->writers == 0)
|
|
|
- return 0;
|
|
|
-
|
|
|
- rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
|
|
+ rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
|
|
|
|
|
|
while (1)
|
|
|
{
|
|
|
- if (pipe->writers == 0)
|
|
|
- {
|
|
|
- goto out;
|
|
|
- }
|
|
|
-
|
|
|
len = rt_ringbuffer_get(pipe->fifo, buf, count);
|
|
|
|
|
|
if (len > 0)
|
|
|
@@ -165,21 +123,21 @@ static int pipe_fops_read(struct dfs_fd *fd, void *buf, size_t count)
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if (fd->fnode->flags & O_NONBLOCK)
|
|
|
+ if (fd->flags & O_NONBLOCK)
|
|
|
{
|
|
|
len = -EAGAIN;
|
|
|
goto out;
|
|
|
}
|
|
|
|
|
|
rt_mutex_release(&pipe->lock);
|
|
|
- rt_wqueue_wakeup(&(pipe->writer_queue), (void*)POLLOUT);
|
|
|
- rt_wqueue_wait(&(pipe->reader_queue), 0, -1);
|
|
|
- rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
|
|
+ rt_wqueue_wakeup(&pipe->writer_queue, (void*)POLLOUT);
|
|
|
+ rt_wqueue_wait(&pipe->reader_queue, 0, -1);
|
|
|
+ rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* wakeup writer */
|
|
|
- rt_wqueue_wakeup(&(pipe->writer_queue), (void*)POLLOUT);
|
|
|
+ rt_wqueue_wakeup(&pipe->writer_queue, (void*)POLLOUT);
|
|
|
|
|
|
out:
|
|
|
rt_mutex_release(&pipe->lock);
|
|
|
@@ -196,27 +154,16 @@ static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
|
|
|
|
|
|
pipe = (rt_pipe_t *)fd->fnode->data;
|
|
|
|
|
|
- if (pipe->readers == 0)
|
|
|
- {
|
|
|
- ret = -EPIPE;
|
|
|
- goto out;
|
|
|
- }
|
|
|
-
|
|
|
if (count == 0)
|
|
|
+ {
|
|
|
return 0;
|
|
|
+ }
|
|
|
|
|
|
pbuf = (uint8_t*)buf;
|
|
|
rt_mutex_take(&pipe->lock, -1);
|
|
|
|
|
|
while (1)
|
|
|
{
|
|
|
- if (pipe->readers == 0)
|
|
|
- {
|
|
|
- if (ret == 0)
|
|
|
- ret = -EPIPE;
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
len = rt_ringbuffer_put(pipe->fifo, pbuf, count - ret);
|
|
|
ret += len;
|
|
|
pbuf += len;
|
|
|
@@ -228,7 +175,7 @@ static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if (fd->fnode->flags & O_NONBLOCK)
|
|
|
+ if (fd->flags & O_NONBLOCK)
|
|
|
{
|
|
|
if (ret == 0)
|
|
|
{
|
|
|
@@ -240,19 +187,18 @@ static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
|
|
|
}
|
|
|
|
|
|
rt_mutex_release(&pipe->lock);
|
|
|
- rt_wqueue_wakeup(&(pipe->reader_queue), (void*)POLLIN);
|
|
|
+ rt_wqueue_wakeup(&pipe->reader_queue, (void*)POLLIN);
|
|
|
/* pipe full, waiting on suspended write list */
|
|
|
- rt_wqueue_wait(&(pipe->writer_queue), 0, -1);
|
|
|
+ rt_wqueue_wait(&pipe->writer_queue, 0, -1);
|
|
|
rt_mutex_take(&pipe->lock, -1);
|
|
|
}
|
|
|
rt_mutex_release(&pipe->lock);
|
|
|
|
|
|
if (wakeup)
|
|
|
{
|
|
|
- rt_wqueue_wakeup(&(pipe->reader_queue), (void*)POLLIN);
|
|
|
+ rt_wqueue_wakeup(&pipe->reader_queue, (void*)POLLIN);
|
|
|
}
|
|
|
|
|
|
-out:
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
@@ -263,10 +209,10 @@ static int pipe_fops_poll(struct dfs_fd *fd, rt_pollreq_t *req)
|
|
|
int mode = 0;
|
|
|
pipe = (rt_pipe_t *)fd->fnode->data;
|
|
|
|
|
|
- rt_poll_add(&(pipe->reader_queue), req);
|
|
|
- rt_poll_add(&(pipe->writer_queue), req);
|
|
|
+ rt_poll_add(&pipe->reader_queue, req);
|
|
|
+ rt_poll_add(&pipe->writer_queue, req);
|
|
|
|
|
|
- switch (fd->fnode->flags & O_ACCMODE)
|
|
|
+ switch (fd->flags & O_ACCMODE)
|
|
|
{
|
|
|
case O_RDONLY:
|
|
|
mode = 1;
|
|
|
@@ -285,10 +231,6 @@ static int pipe_fops_poll(struct dfs_fd *fd, rt_pollreq_t *req)
|
|
|
{
|
|
|
mask |= POLLIN;
|
|
|
}
|
|
|
- if (pipe->writers == 0)
|
|
|
- {
|
|
|
- mask |= POLLHUP;
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
if (mode & 2)
|
|
|
@@ -297,10 +239,6 @@ static int pipe_fops_poll(struct dfs_fd *fd, rt_pollreq_t *req)
|
|
|
{
|
|
|
mask |= POLLOUT;
|
|
|
}
|
|
|
- if (pipe->readers == 0)
|
|
|
- {
|
|
|
- mask |= POLLERR;
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
return mask;
|
|
|
@@ -330,8 +268,8 @@ rt_err_t rt_pipe_open (rt_device_t device, rt_uint16_t oflag)
|
|
|
ret = -RT_EINVAL;
|
|
|
goto __exit;
|
|
|
}
|
|
|
-
|
|
|
- rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
|
|
+
|
|
|
+ rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
|
|
|
|
|
|
if (pipe->fifo == RT_NULL)
|
|
|
{
|
|
|
@@ -342,7 +280,7 @@ rt_err_t rt_pipe_open (rt_device_t device, rt_uint16_t oflag)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- rt_mutex_release(&(pipe->lock));
|
|
|
+ rt_mutex_release(&pipe->lock);
|
|
|
|
|
|
__exit:
|
|
|
return ret;
|
|
|
@@ -352,16 +290,16 @@ rt_err_t rt_pipe_close (rt_device_t device)
|
|
|
{
|
|
|
rt_pipe_t *pipe = (rt_pipe_t *)device;
|
|
|
|
|
|
- if (device == RT_NULL) return -RT_EINVAL;
|
|
|
- rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
|
|
-
|
|
|
- if (device->ref_count == 1)
|
|
|
+ if (device == RT_NULL)
|
|
|
{
|
|
|
- rt_ringbuffer_destroy(pipe->fifo);
|
|
|
- pipe->fifo = RT_NULL;
|
|
|
+ return -RT_EINVAL;
|
|
|
}
|
|
|
+ rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
|
|
|
|
|
|
- rt_mutex_release(&(pipe->lock));
|
|
|
+ rt_ringbuffer_destroy(pipe->fifo);
|
|
|
+ pipe->fifo = RT_NULL;
|
|
|
+
|
|
|
+ rt_mutex_release(&pipe->lock);
|
|
|
|
|
|
return RT_EOK;
|
|
|
}
|
|
|
@@ -377,15 +315,21 @@ rt_size_t rt_pipe_read (rt_device_t device, rt_off_t pos, void *buffer, rt_siz
|
|
|
rt_set_errno(-EINVAL);
|
|
|
return 0;
|
|
|
}
|
|
|
- if (count == 0) return 0;
|
|
|
+ if (count == 0)
|
|
|
+ {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
|
|
|
pbuf = (uint8_t*)buffer;
|
|
|
- rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
|
|
+ rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
|
|
|
|
|
|
while (read_bytes < count)
|
|
|
{
|
|
|
int len = rt_ringbuffer_get(pipe->fifo, &pbuf[read_bytes], count - read_bytes);
|
|
|
- if (len <= 0) break;
|
|
|
+ if (len <= 0)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
read_bytes += len;
|
|
|
}
|
|
|
@@ -405,7 +349,10 @@ rt_size_t rt_pipe_write (rt_device_t device, rt_off_t pos, const void *buffer,
|
|
|
rt_set_errno(-EINVAL);
|
|
|
return 0;
|
|
|
}
|
|
|
- if (count == 0) return 0;
|
|
|
+ if (count == 0)
|
|
|
+ {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
|
|
|
pbuf = (uint8_t*)buffer;
|
|
|
rt_mutex_take(&pipe->lock, -1);
|
|
|
@@ -413,7 +360,10 @@ rt_size_t rt_pipe_write (rt_device_t device, rt_off_t pos, const void *buffer,
|
|
|
while (write_bytes < count)
|
|
|
{
|
|
|
int len = rt_ringbuffer_put(pipe->fifo, &pbuf[write_bytes], count - write_bytes);
|
|
|
- if (len <= 0) break;
|
|
|
+ if (len <= 0)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
write_bytes += len;
|
|
|
}
|
|
|
@@ -449,14 +399,14 @@ rt_pipe_t *rt_pipe_create(const char *name, int bufsz)
|
|
|
|
|
|
rt_memset(pipe, 0, sizeof(rt_pipe_t));
|
|
|
pipe->is_named = RT_TRUE; /* initialize as a named pipe */
|
|
|
- rt_mutex_init(&(pipe->lock), name, RT_IPC_FLAG_FIFO);
|
|
|
- rt_wqueue_init(&(pipe->reader_queue));
|
|
|
- rt_wqueue_init(&(pipe->writer_queue));
|
|
|
+ rt_mutex_init(&pipe->lock, name, RT_IPC_FLAG_FIFO);
|
|
|
+ rt_wqueue_init(&pipe->reader_queue);
|
|
|
+ rt_wqueue_init(&pipe->writer_queue);
|
|
|
|
|
|
RT_ASSERT(bufsz < 0xFFFF);
|
|
|
pipe->bufsz = bufsz;
|
|
|
|
|
|
- dev = &(pipe->parent);
|
|
|
+ dev = &pipe->parent;
|
|
|
dev->type = RT_Device_Class_Pipe;
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
|
|
dev->ops = &pipe_ops;
|
|
|
@@ -472,7 +422,7 @@ rt_pipe_t *rt_pipe_create(const char *name, int bufsz)
|
|
|
dev->rx_indicate = RT_NULL;
|
|
|
dev->tx_complete = RT_NULL;
|
|
|
|
|
|
- if (rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE) != 0)
|
|
|
+ if (rt_device_register(&pipe->parent, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE) != 0)
|
|
|
{
|
|
|
rt_free(pipe);
|
|
|
return RT_NULL;
|
|
|
@@ -496,18 +446,13 @@ int rt_pipe_delete(const char *name)
|
|
|
{
|
|
|
rt_pipe_t *pipe;
|
|
|
|
|
|
- if (device->ref_count != 0)
|
|
|
- {
|
|
|
- return -RT_EBUSY;
|
|
|
- }
|
|
|
-
|
|
|
pipe = (rt_pipe_t *)device;
|
|
|
|
|
|
- rt_mutex_detach(&(pipe->lock));
|
|
|
+ rt_mutex_detach(&pipe->lock);
|
|
|
rt_device_unregister(device);
|
|
|
|
|
|
/* close fifo ringbuffer */
|
|
|
- if (pipe->fifo)
|
|
|
+ if (pipe->fifo)
|
|
|
{
|
|
|
rt_ringbuffer_destroy(pipe->fifo);
|
|
|
pipe->fifo = RT_NULL;
|