The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
pipe.h File Reference

Go to the source code of this file.

Data Structures

struct  pipe_buf
 a pipe. More...

Macros

#define PIPE_CAPACITY   16384
 pipes: a ring buffer shared by a read end and a write end.
#define PIPE_BUF   4096

Typedefs

typedef struct pipe_buf pipe_buf_t
 a pipe.

Enumerations

enum  pipe_dir_t { PIPE_READ = 0 , PIPE_WRITE = 1 }
 pipe endpoint direction. More...

Functions

pipe_buf_t * pipe_alloc (void)
 allocate a pipe with one open read end and one open write end.
void pipe_ref (pipe_buf_t *pipe, pipe_dir_t dir)
 open another end of a pipe (dup2 or fd inheritance).
void pipe_unref (pipe_buf_t *pipe, pipe_dir_t dir)
 close one end of a pipe.
int32_t pipe_read (pipe_buf_t *pipe, char *buf, uint32_t count, bool nonblock)
 read from a pipe, sleeping while it is empty and a write end is open.
int32_t pipe_write (pipe_buf_t *pipe, const char *buf, uint32_t count, bool nonblock)
 write every byte to a pipe, sleeping whenever it is full.
uint32_t pipe_poll (pipe_buf_t *pipe, pipe_dir_t dir)
 the readiness of one end of a pipe: the read end has POLLIN when there is data or no writer is left (POLLHUP then too); the write end has POLLOUT when PIPE_BUF bytes fit, and POLLERR when no reader is left.

Macro Definition Documentation

◆ PIPE_CAPACITY

#define PIPE_CAPACITY   16384

pipes: a ring buffer shared by a read end and a write end.

each pipe and its buffer come from the kernel heap, so the number of pipes is limited only by memory (and by the fds that hold their ends).

a write of at most PIPE_BUF bytes is atomic: it waits until there is room for all of it, so its bytes are never mixed with another writer's. larger writes can be split.

every field of a pipe is guarded by its spinlock, so reads, writes and reference changes are atomic against preemption (syscalls run with interrupts enabled). readers and writers that must wait sleep on the pipe's wait queues with wait_queue_block_locked(), which releases the lock atomically, so a wakeup between the check and the sleep is never lost.

◆ PIPE_BUF

#define PIPE_BUF   4096

Typedef Documentation

◆ pipe_buf_t

typedef struct pipe_buf pipe_buf_t

a pipe.

shared between all of its read and write ends.

Enumeration Type Documentation

◆ pipe_dir_t

enum pipe_dir_t

pipe endpoint direction.

Enumerator
PIPE_READ 
PIPE_WRITE 

Function Documentation

◆ pipe_alloc()

pipe_buf_t * pipe_alloc ( void )

allocate a pipe with one open read end and one open write end.

Returns
the new pipe, or NULL if the heap is out of memory.

◆ pipe_ref()

void pipe_ref ( pipe_buf_t * pipe,
pipe_dir_t dir )

open another end of a pipe (dup2 or fd inheritance).

Parameters
pipethe pipe.
dirwhich end the new reference is.

◆ pipe_unref()

void pipe_unref ( pipe_buf_t * pipe,
pipe_dir_t dir )

close one end of a pipe.

closing the last read end wakes blocked writers (they then see a broken pipe); closing the last write end wakes blocked readers (they then see EOF). the pipe is freed once both ends are fully closed.

Parameters
pipethe pipe.
dirwhich end is being closed.

◆ pipe_read()

int32_t pipe_read ( pipe_buf_t * pipe,
char * buf,
uint32_t count,
bool nonblock )

read from a pipe, sleeping while it is empty and a write end is open.

Parameters
pipethe pipe.
bufdestination buffer.
countmaximum number of bytes to read (> 0).
nonblockreturn -EAGAIN instead of sleeping.
Returns
bytes read; 0 only at EOF (empty and no write end open); or -EAGAIN, or -ERESTARTSYS if a signal came while it waited.

◆ pipe_write()

int32_t pipe_write ( pipe_buf_t * pipe,
const char * buf,
uint32_t count,
bool nonblock )

write every byte to a pipe, sleeping whenever it is full.

a write of at most PIPE_BUF bytes goes in as one piece.

Parameters
pipethe pipe.
bufsource buffer.
countnumber of bytes to write (> 0).
nonblockstop instead of sleeping when the pipe is full.
Returns
bytes written (less than count if every read end closed, or the pipe filled up in nonblocking mode, or a signal came); -EPIPE if no read end is open; -EAGAIN if nonblocking and nothing fitted; -ERESTARTSYS if a signal came before anything was written.

◆ pipe_poll()

uint32_t pipe_poll ( pipe_buf_t * pipe,
pipe_dir_t dir )

the readiness of one end of a pipe: the read end has POLLIN when there is data or no writer is left (POLLHUP then too); the write end has POLLOUT when PIPE_BUF bytes fit, and POLLERR when no reader is left.

Parameters
pipethe pipe.
dirwhich end.
Returns
POLL* events (kernel/vfs/poll.h).