Crates.io | scroll-ring |
lib.rs | scroll-ring |
version | 0.1.2 |
source | src |
created_at | 2023-02-10 10:08:49.20684 |
updated_at | 2024-09-13 11:29:33.841639 |
description | An MPSC overwriting ring buffer tuned for character data scrollback |
homepage | |
repository | https://gitlab.com/chrysn/scroll-ring |
max_upload_size | |
id | 781760 |
size | 23,238 |
Structures for MPSC text streaming
The goal of the data structure provided by this crate is to cater for the needs of stdout-style text streaming over a lossy network, which results in thee requirements:
Overflowing data may not even hit the buffer, for example when a slice larger than the buffer is written, or when (large amounts of) data is to be written while a read happens, or while a write from a slow process happens -- either way, the written bytes are counted, they're just not available for reading (just as they would have been if the reader were merely too slow).
This is the minimal implementation that solely on "overflow as little as possible" to get a waaaay easier implementation. Better options are:
A perfect implementation would have atomics everywhere, and as long as there are no reads (or no writes from threads with such low priority that higher priority writes wrap around before the low priority write is done memcpy'ing), there would be no overflow. That'd be ideal, but is really hard.
An intermediate implementation would allow parallel writes, but have a brief period of locking the administrative part of the data structure when operations start and stop. (Thus, overflows might happen merely because an interrupt triggers at an inopportune time).
Both these versions would probably need a bitfield of valid bytes (or a list of busy writers), because due to variable thread priorities concurrent writes don't finish in the opposite order of being started.
What is currently provided is about the dumbest possible version. For the whole duration of reading or writing, the data structure stays locked, and any overflows are recorded into an atomic field next to the locked structure.
The [ringbuf] data structures are used in single-threaded mode here; there is a multi-threaded mode that could go some way implementing the others.
License: MIT OR Apache-2.0