Draupnir is a fast, concurrent, thread-safe ring buffer with an std.Io.Reader/std.Io.Writer interface. It implements the magic ring buffer pattern to provide a single contiguous slice for io interface buffers. Reading from and writing to an interface buffer does not require holding a lock.
error: 'root.test.concurrent write and read' terminated with signal ABRT with stderr:
thread 345025 panic: start index 1024 is larger than end index 0
/home/inspectorboat/.local/share/zigup/0.17.0/files/lib/std/Io/Reader.zig:686:30: 0x1106845 in readSliceShort (std.zig)
const contents = r.buffer[r.seek..r.end];
^
/home/inspectorboat/.local/share/zigup/0.17.0/files/lib/std/Io/Reader.zig:671:33: 0x1106637 in readSliceAll (std.zig)
const n = try readSliceShort(r, buffer);
^
/home/inspectorboat/src/draupnir/src/root.zig:260:31: 0x1236a53 in doRead (root.zig)
try r.readSliceAll(&buf);
Not sure of the cause, it might be my user error (I’m definitely not an expert at this stuff) so I’m haven’t filed an issue yet.
Hey @InspectorBoat, I think the reason for the crash is that the test is issuing multiple concurrent reads/writes. I forgot to document it, but draupnir.Ring is a Single Producer Single Consumer (SPSC) queue. Multiple concurrent reads/writes can result in the crashes you are seeing. I modified this test to concurrently run a single read and single write and it no longer crashes.
AFAIK, there is no way to make a MPMC/SPMC/MPSC queue and keep the single Io.Reader/Io.Writer interface, but would love to be proven wrong on this. The closest I have come up with is to add reader(buffer: []u8) Io.Reader and writer(buffer: []u8) Io.Writer methods, but i think you give up serializability with all those independent buffers.
This one presents itself as a contiguous slice in virtual memory by creating two mappings that refer to the same memory:
[ ][ ]
^h ^t ^h ^t
In this example, a writer can write straight from the tail in the first mapping to the head in the second mapping. Normally, it would have to do two writes: from the head until the end of the buffer and from the beginning of the buffer to the head. You can do this with std.Deque, and I’ve written a few functions that help with this. But the Draupnir interface is nicer.