MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / pipe_fops_open

Function pipe_fops_open

components/drivers/ipc/pipe.c:59–110  ·  view source on GitHub ↗

* @brief This function will open a pipe. * * @param fd is the file descriptor. * * @return Return the operation status. * When the return value is 0, it means the operation is successful. * When the return value is -1, it means the file descriptor is invalid. * When the return value is -RT_ENOMEM, it means insufficient memory allocation failed. */

Source from the content-addressed store, hash-verified

57 * When the return value is -RT_ENOMEM, it means insufficient memory allocation failed.
58 */
59static int pipe_fops_open(struct dfs_file *fd)
60{
61 int rc = 0;
62 rt_pipe_t *pipe;
63
64 pipe = (rt_pipe_t *)fd->vnode->data;
65 if (!pipe)
66 {
67 return -1;
68 }
69
70 rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER);
71
72 if ((fd->flags & O_ACCMODE) == O_RDONLY)
73 {
74 pipe->reader += 1;
75 }
76
77 if ((fd->flags & O_ACCMODE) == O_WRONLY)
78 {
79 pipe->writer += 1;
80 }
81 if (fd->vnode->ref_count == 1)
82 {
83 pipe->fifo = rt_ringbuffer_create(pipe->bufsz);
84 if (pipe->fifo == RT_NULL)
85 {
86 rc = -RT_ENOMEM;
87 goto __exit;
88 }
89 }
90
91 if ((fd->flags & O_ACCMODE) == O_RDONLY && !pipe->writer)
92 {
93 /* wait for partner */
94 rc = rt_condvar_timedwait(&pipe->waitfor_parter, &pipe->lock,
95 RT_INTERRUPTIBLE, RT_WAITING_FOREVER);
96 if (rc != 0)
97 {
98 pipe->reader--;
99 }
100 }
101 else if ((fd->flags & O_ACCMODE) == O_WRONLY)
102 {
103 rt_condvar_broadcast(&pipe->waitfor_parter);
104 }
105
106__exit:
107 rt_mutex_release(&pipe->lock);
108
109 return rc;
110}
111
112/**
113 * @brief This function will close a pipe.

Callers

nothing calls this directly

Calls 5

rt_mutex_takeFunction · 0.85
rt_ringbuffer_createFunction · 0.85
rt_condvar_timedwaitFunction · 0.85
rt_condvar_broadcastFunction · 0.85
rt_mutex_releaseFunction · 0.85

Tested by

no test coverage detected