The manual
ginnOS
The reference: libc, the libraries and the kernel
Toggle main menu visibility
Loading...
Searching...
No Matches
pipe.h
Go to the documentation of this file.
1
#pragma once
2
3
/**
4
* pipes: a ring buffer shared by a read end and a write end. each pipe and
5
* its buffer come from the kernel heap, so the number of pipes is limited
6
* only by memory (and by the fds that hold their ends).
7
*
8
* a write of at most PIPE_BUF bytes is atomic: it waits until there is room
9
* for all of it, so its bytes are never mixed with another writer's. larger
10
* writes can be split.
11
*
12
* every field of a pipe is guarded by its spinlock, so reads, writes and
13
* reference changes are atomic against preemption (syscalls run with
14
* interrupts enabled). readers and writers that must wait sleep on the
15
* pipe's wait queues with wait_queue_block_locked(), which releases the
16
* lock atomically, so a wakeup between the check and the sleep is never
17
* lost.
18
*/
19
20
#include "
common/stdint.h
"
21
#include "
kernel/sync/spinlock.h
"
22
#include "
kernel/sync/wait_queue.h
"
23
24
// bytes a pipe can hold before a writer waits
25
#define PIPE_CAPACITY 16384
26
27
// largest write that is never split (POSIX PIPE_BUF; also in libc limits.h)
28
#define PIPE_BUF 4096
29
30
/**
31
* pipe endpoint direction.
32
*/
33
typedef
enum
34
{
35
PIPE_READ
= 0,
36
PIPE_WRITE
= 1,
37
}
pipe_dir_t
;
38
39
/**
40
* a pipe. shared between all of its read and write ends.
41
*/
42
typedef
struct
pipe_buf
43
{
44
char
*
data
;
// PIPE_CAPACITY bytes from the heap
45
uint32_t
read_pos
;
// next byte to read
46
uint32_t
write_pos
;
// next byte to write
47
uint32_t
count
;
// bytes currently in buffer
48
int32_t
read_refs
;
// number of open read ends
49
int32_t
write_refs
;
// number of open write ends
50
spinlock_t
lock
;
// guards every field above
51
52
wait_queue_t
readers
;
// readers blocked waiting for data
53
wait_queue_t
writers
;
// writers blocked waiting for free space
54
}
pipe_buf_t
;
55
56
/**
57
* allocate a pipe with one open read end and one open write end.
58
* @return the new pipe, or NULL if the heap is out of memory.
59
*/
60
pipe_buf_t
*
pipe_alloc
(
void
);
61
62
/**
63
* open another end of a pipe (dup2 or fd inheritance).
64
* @param pipe the pipe.
65
* @param dir which end the new reference is.
66
*/
67
void
pipe_ref
(
pipe_buf_t
*
pipe
,
pipe_dir_t
dir);
68
69
/**
70
* close one end of a pipe. closing the last read end wakes blocked writers
71
* (they then see a broken pipe); closing the last write end wakes blocked
72
* readers (they then see EOF). the pipe is freed once both ends are fully
73
* closed.
74
* @param pipe the pipe.
75
* @param dir which end is being closed.
76
*/
77
void
pipe_unref
(
pipe_buf_t
*
pipe
,
pipe_dir_t
dir);
78
79
/**
80
* read from a pipe, sleeping while it is empty and a write end is open.
81
* @param pipe the pipe.
82
* @param buf destination buffer.
83
* @param count maximum number of bytes to read (> 0).
84
* @param nonblock return -EAGAIN instead of sleeping.
85
* @return bytes read; 0 only at EOF (empty and no write end open); or
86
* -EAGAIN, or -ERESTARTSYS if a signal came while it waited.
87
*/
88
int32_t
pipe_read
(
pipe_buf_t
*
pipe
,
char
*buf,
uint32_t
count,
bool
nonblock);
89
90
/**
91
* write every byte to a pipe, sleeping whenever it is full. a write of at
92
* most PIPE_BUF bytes goes in as one piece.
93
* @param pipe the pipe.
94
* @param buf source buffer.
95
* @param count number of bytes to write (> 0).
96
* @param nonblock stop instead of sleeping when the pipe is full.
97
* @return bytes written (less than count if every read end closed, or the
98
* pipe filled up in nonblocking mode, or a signal came); -EPIPE if
99
* no read end is open; -EAGAIN if nonblocking and nothing fitted;
100
* -ERESTARTSYS if a signal came before anything was written.
101
*/
102
int32_t
pipe_write
(
pipe_buf_t
*
pipe
,
const
char
*buf,
uint32_t
count,
bool
nonblock);
103
104
/**
105
* the readiness of one end of a pipe: the read end has POLLIN when there
106
* is data or no writer is left (POLLHUP then too); the write end has
107
* POLLOUT when PIPE_BUF bytes fit, and POLLERR when no reader is left.
108
* @param pipe the pipe.
109
* @param dir which end.
110
* @return POLL* events (kernel/vfs/poll.h).
111
*/
112
uint32_t
pipe_poll
(
pipe_buf_t
*
pipe
,
pipe_dir_t
dir);
stdint.h
uint32_t
unsigned int uint32_t
Definition
fb.h:17
int32_t
int int32_t
Definition
stdint.h:14
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.
Definition
pipe.c:173
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 (...
Definition
pipe.c:227
pipe_unref
void pipe_unref(pipe_buf_t *pipe, pipe_dir_t dir)
close one end of a pipe.
Definition
pipe.c:98
pipe_ref
void pipe_ref(pipe_buf_t *pipe, pipe_dir_t dir)
open another end of a pipe (dup2 or fd inheritance).
Definition
pipe.c:82
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.
Definition
pipe.c:137
pipe_alloc
pipe_buf_t * pipe_alloc(void)
allocate a pipe with one open read end and one open write end.
Definition
pipe.c:58
pipe_buf_t
struct pipe_buf pipe_buf_t
a pipe.
pipe_dir_t
pipe_dir_t
pipe endpoint direction.
Definition
pipe.h:34
PIPE_WRITE
@ PIPE_WRITE
Definition
pipe.h:36
PIPE_READ
@ PIPE_READ
Definition
pipe.h:35
spinlock.h
pipe_buf
a pipe.
Definition
pipe.h:43
pipe_buf::read_pos
uint32_t read_pos
Definition
pipe.h:45
pipe_buf::readers
wait_queue_t readers
Definition
pipe.h:52
pipe_buf::write_refs
int32_t write_refs
Definition
pipe.h:49
pipe_buf::count
uint32_t count
Definition
pipe.h:47
pipe_buf::write_pos
uint32_t write_pos
Definition
pipe.h:46
pipe_buf::lock
spinlock_t lock
Definition
pipe.h:50
pipe_buf::writers
wait_queue_t writers
Definition
pipe.h:53
pipe_buf::data
char * data
Definition
pipe.h:44
pipe_buf::read_refs
int32_t read_refs
Definition
pipe.h:48
spinlock_t
kernel locking rules:
Definition
spinlock.h:67
wait_queue_t
a queue of blocked processes, linked through process_t::wait_next.
Definition
wait_queue.h:35
pipe
int pipe(int fds[2])
create a pipe (pair of connected file descriptors).
wait_queue.h
src
kernel
syscall
pipe.h
Generated by
1.18.0