/* * If called with *pipep = NULL, pipe_new will call pipe_alloc to allocate a * pipe control structure and set *pipep to its address. * pipe is locked, when pipe_new returns with no error. */ static int pipe_new( pipe_control_t **pipep ) { pipe_control_t *pipe; int err = 0; err = pipe_lock(); if (err) return err; pipe = *pipep; if (pipe == NULL) { err = pipe_alloc(&pipe); if (err) goto out; } if (! PIPE_LOCK(pipe)) err = -EINTR; if (*pipep == NULL) { if (err) pipe_free(pipe); else *pipep = pipe; } out: pipe_unlock(); return err; }
/* * If called with *pipep = NULL, pipe_new will call pipe_alloc to allocate a * pipe control structure and set *pipep to its address. * pipe is locked, when pipe_new returns with no error. */ static int pipe_new( pipe_control_t **pipep ) { pipe_control_t *pipe; int err = 0; if (rtems_semaphore_obtain(rtems_pipe_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT) != RTEMS_SUCCESSFUL) return -EINTR; pipe = *pipep; if (pipe == NULL) { err = pipe_alloc(&pipe); if (err) goto out; } if (! PIPE_LOCK(pipe)) err = -EINTR; if (*pipep == NULL) { if (err) pipe_free(pipe); else *pipep = pipe; } out: rtems_semaphore_release(rtems_pipe_semaphore); return err; }
int pipe2(int pipefd[2], int flags) { struct idesc_table *it; struct pipe *pipe; int res = 0; it = task_resource_idesc_table(task_self()); assert(it); pipe = pipe_alloc(); if (!pipe) { res = ENOMEM; goto out_err; } idesc_pipe_init(&pipe->read_desc, pipe, FS_MAY_READ); idesc_pipe_init(&pipe->write_desc, pipe, FS_MAY_WRITE); pipefd[0] = idesc_table_add(it, &pipe->read_desc.idesc, flags); pipefd[1] = idesc_table_add(it, &pipe->write_desc.idesc, flags); if (pipefd[0] < 0 || pipefd[1] < 0) { res = ENOMEM; goto out_err; } return 0; out_err: if (pipefd[1] >= 0) { idesc_table_del(it, pipefd[1]); } if (pipefd[0] >= 0) { idesc_table_del(it, pipefd[0]); } if (pipe) { pipe_free(pipe); } SET_ERRNO(res); return -1; }