|
| 1 | +//! An in-kernel, unidirectional byte pipe. |
| 2 | +//! |
| 3 | +//! A pipe couples a read end ([`PipeReceiver`]) and a write end |
| 4 | +//! ([`PipeSender`]) through a single shared, bounded ring buffer. Both |
| 5 | +//! endpoints are ordinary [`Fd`](crate::fd::Fd) objects, so they live |
| 6 | +//! behind `Arc<RwLock<Fd>>` in the per-process object map. `fork` clones |
| 7 | +//! those `Arc`s into the child's object map, which means a pipe created |
| 8 | +//! before the fork is transparently shared between parent and child — |
| 9 | +//! the classic Unix way for two processes to communicate. |
| 10 | +//! |
| 11 | +//! Lifetime of the endpoints is tracked by the `Arc` refcount of the |
| 12 | +//! shared state: once the last reference to an endpoint is dropped, its |
| 13 | +//! `Drop` impl marks the corresponding side as closed and wakes the |
| 14 | +//! opposite side. A reader then observes end-of-file (`read` returns 0), |
| 15 | +//! a writer observes a broken pipe ([`Errno::Pipe`]). |
| 16 | +
|
| 17 | +use alloc::collections::vec_deque::VecDeque; |
| 18 | +use alloc::sync::Arc; |
| 19 | +use alloc::vec::Vec; |
| 20 | +use core::future; |
| 21 | +use core::task::{Poll, Waker}; |
| 22 | + |
| 23 | +use hermit_sync::InterruptTicketMutex; |
| 24 | + |
| 25 | +use crate::errno::Errno; |
| 26 | +use crate::fd::{ObjectInterface, PollEvent, StatusFlags}; |
| 27 | +use crate::io; |
| 28 | + |
| 29 | +/// Capacity of the pipe's ring buffer in bytes. |
| 30 | +/// |
| 31 | +/// Mirrors the 64 KiB default Linux gives a pipe. A write blocks (or, in |
| 32 | +/// non-blocking mode, returns [`Errno::Again`]) once the buffer is full. |
| 33 | +const PIPE_CAPACITY: usize = 64 * 1024; |
| 34 | + |
| 35 | +/// State shared between the two endpoints of a pipe. |
| 36 | +#[derive(Debug)] |
| 37 | +struct PipeState { |
| 38 | + /// FIFO byte buffer, capped at [`PIPE_CAPACITY`]. |
| 39 | + buffer: VecDeque<u8>, |
| 40 | + /// Set once every read end has been dropped. |
| 41 | + reader_closed: bool, |
| 42 | + /// Set once every write end has been dropped. |
| 43 | + writer_closed: bool, |
| 44 | + /// Tasks blocked in `read`/readable `poll`. |
| 45 | + read_wakers: Vec<Waker>, |
| 46 | + /// Tasks blocked in `write`/writable `poll`. |
| 47 | + write_wakers: Vec<Waker>, |
| 48 | +} |
| 49 | + |
| 50 | +impl PipeState { |
| 51 | + fn new() -> Self { |
| 52 | + Self { |
| 53 | + buffer: VecDeque::new(), |
| 54 | + reader_closed: false, |
| 55 | + writer_closed: false, |
| 56 | + read_wakers: Vec::new(), |
| 57 | + write_wakers: Vec::new(), |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// Wake everyone waiting for the pipe to become readable. |
| 62 | + /// |
| 63 | + /// All wakers are drained and woken: readability is level-triggered |
| 64 | + /// and the `poll`-based fd multiplexing re-registers a fresh waker on |
| 65 | + /// each poll, so the queue accumulates stale wakers — waking only one |
| 66 | + /// could wake a dead waker and miss the live one. |
| 67 | + fn wake_readers(&mut self) { |
| 68 | + for waker in self.read_wakers.drain(..) { |
| 69 | + waker.wake(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// Wake everyone waiting for the pipe to become writable. |
| 74 | + fn wake_writers(&mut self) { |
| 75 | + for waker in self.write_wakers.drain(..) { |
| 76 | + waker.wake(); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/// Allocate a fresh pipe, returning its read and write endpoints. |
| 82 | +pub(crate) fn pipe() -> (PipeReceiver, PipeSender) { |
| 83 | + let state = Arc::new(InterruptTicketMutex::new(PipeState::new())); |
| 84 | + ( |
| 85 | + PipeReceiver { |
| 86 | + state: state.clone(), |
| 87 | + status_flags: StatusFlags::empty(), |
| 88 | + }, |
| 89 | + PipeSender { |
| 90 | + state, |
| 91 | + status_flags: StatusFlags::empty(), |
| 92 | + }, |
| 93 | + ) |
| 94 | +} |
| 95 | + |
| 96 | +/// The read end of a pipe. |
| 97 | +#[derive(Debug)] |
| 98 | +pub(crate) struct PipeReceiver { |
| 99 | + state: Arc<InterruptTicketMutex<PipeState>>, |
| 100 | + status_flags: StatusFlags, |
| 101 | +} |
| 102 | + |
| 103 | +impl Drop for PipeReceiver { |
| 104 | + fn drop(&mut self) { |
| 105 | + let mut state = self.state.lock(); |
| 106 | + state.reader_closed = true; |
| 107 | + // A blocked writer must return `EPIPE` now that nobody reads. |
| 108 | + state.wake_writers(); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl ObjectInterface for PipeReceiver { |
| 113 | + async fn read(&self, buf: &mut [u8]) -> io::Result<usize> { |
| 114 | + let nonblock = self.status_flags.contains(StatusFlags::O_NONBLOCK); |
| 115 | + |
| 116 | + future::poll_fn(|cx| { |
| 117 | + let mut state = self.state.lock(); |
| 118 | + |
| 119 | + if !state.buffer.is_empty() { |
| 120 | + let len = buf.len().min(state.buffer.len()); |
| 121 | + for byte in buf.iter_mut().take(len) { |
| 122 | + *byte = state.buffer.pop_front().unwrap(); |
| 123 | + } |
| 124 | + // Freed buffer space: a blocked writer can make progress. |
| 125 | + state.wake_writers(); |
| 126 | + Poll::Ready(Ok(len)) |
| 127 | + } else if state.writer_closed { |
| 128 | + // End of file: all write ends are gone. |
| 129 | + Poll::Ready(Ok(0)) |
| 130 | + } else if nonblock { |
| 131 | + Poll::Ready(Err(Errno::Again)) |
| 132 | + } else { |
| 133 | + state.read_wakers.push(cx.waker().clone()); |
| 134 | + Poll::Pending |
| 135 | + } |
| 136 | + }) |
| 137 | + .await |
| 138 | + } |
| 139 | + |
| 140 | + async fn poll(&self, event: PollEvent) -> io::Result<PollEvent> { |
| 141 | + future::poll_fn(|cx| { |
| 142 | + let mut state = self.state.lock(); |
| 143 | + |
| 144 | + let mut available = PollEvent::empty(); |
| 145 | + if !state.buffer.is_empty() { |
| 146 | + available.insert(PollEvent::POLLIN | PollEvent::POLLRDNORM); |
| 147 | + } |
| 148 | + if state.writer_closed { |
| 149 | + // EOF is reported as a readable, non-error condition. |
| 150 | + available.insert(PollEvent::POLLIN | PollEvent::POLLRDNORM | PollEvent::POLLHUP); |
| 151 | + } |
| 152 | + |
| 153 | + let ret = event & available; |
| 154 | + if ret.is_empty() && !state.writer_closed { |
| 155 | + state.read_wakers.push(cx.waker().clone()); |
| 156 | + Poll::Pending |
| 157 | + } else { |
| 158 | + Poll::Ready(Ok(ret)) |
| 159 | + } |
| 160 | + }) |
| 161 | + .await |
| 162 | + } |
| 163 | + |
| 164 | + async fn status_flags(&self) -> io::Result<StatusFlags> { |
| 165 | + Ok(self.status_flags) |
| 166 | + } |
| 167 | + |
| 168 | + async fn set_status_flags(&mut self, status_flags: StatusFlags) -> io::Result<()> { |
| 169 | + self.status_flags = status_flags; |
| 170 | + Ok(()) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +/// The write end of a pipe. |
| 175 | +#[derive(Debug)] |
| 176 | +pub(crate) struct PipeSender { |
| 177 | + state: Arc<InterruptTicketMutex<PipeState>>, |
| 178 | + status_flags: StatusFlags, |
| 179 | +} |
| 180 | + |
| 181 | +impl Drop for PipeSender { |
| 182 | + fn drop(&mut self) { |
| 183 | + let mut state = self.state.lock(); |
| 184 | + state.writer_closed = true; |
| 185 | + // A blocked reader must observe end-of-file now. |
| 186 | + state.wake_readers(); |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +impl ObjectInterface for PipeSender { |
| 191 | + async fn write(&self, buf: &[u8]) -> io::Result<usize> { |
| 192 | + if buf.is_empty() { |
| 193 | + return Ok(0); |
| 194 | + } |
| 195 | + let nonblock = self.status_flags.contains(StatusFlags::O_NONBLOCK); |
| 196 | + |
| 197 | + future::poll_fn(|cx| { |
| 198 | + let mut state = self.state.lock(); |
| 199 | + |
| 200 | + if state.reader_closed { |
| 201 | + // Writing to a pipe with no readers is a broken pipe. |
| 202 | + return Poll::Ready(Err(Errno::Pipe)); |
| 203 | + } |
| 204 | + |
| 205 | + let free = PIPE_CAPACITY - state.buffer.len(); |
| 206 | + if free > 0 { |
| 207 | + let len = buf.len().min(free); |
| 208 | + state.buffer.extend(buf[..len].iter().copied()); |
| 209 | + // New data: a blocked reader can make progress. |
| 210 | + state.wake_readers(); |
| 211 | + Poll::Ready(Ok(len)) |
| 212 | + } else if nonblock { |
| 213 | + Poll::Ready(Err(Errno::Again)) |
| 214 | + } else { |
| 215 | + state.write_wakers.push(cx.waker().clone()); |
| 216 | + Poll::Pending |
| 217 | + } |
| 218 | + }) |
| 219 | + .await |
| 220 | + } |
| 221 | + |
| 222 | + async fn poll(&self, event: PollEvent) -> io::Result<PollEvent> { |
| 223 | + future::poll_fn(|cx| { |
| 224 | + let mut state = self.state.lock(); |
| 225 | + |
| 226 | + let mut available = PollEvent::empty(); |
| 227 | + if state.reader_closed { |
| 228 | + // A vanished reader is an error/hangup condition for the writer. |
| 229 | + available.insert(PollEvent::POLLERR); |
| 230 | + } else if state.buffer.len() < PIPE_CAPACITY { |
| 231 | + available.insert(PollEvent::POLLOUT | PollEvent::POLLWRNORM); |
| 232 | + } |
| 233 | + |
| 234 | + let ret = event & available; |
| 235 | + if ret.is_empty() && !state.reader_closed { |
| 236 | + state.write_wakers.push(cx.waker().clone()); |
| 237 | + Poll::Pending |
| 238 | + } else { |
| 239 | + Poll::Ready(Ok(ret)) |
| 240 | + } |
| 241 | + }) |
| 242 | + .await |
| 243 | + } |
| 244 | + |
| 245 | + async fn status_flags(&self) -> io::Result<StatusFlags> { |
| 246 | + Ok(self.status_flags) |
| 247 | + } |
| 248 | + |
| 249 | + async fn set_status_flags(&mut self, status_flags: StatusFlags) -> io::Result<()> { |
| 250 | + self.status_flags = status_flags; |
| 251 | + Ok(()) |
| 252 | + } |
| 253 | +} |
0 commit comments