SEQC(9)
π¦ SEQC(9) β lockless read algorithm π¦
okay bestie, welcome to the SEQC(9) page from the Kernel Developerβs Manual β weβre doing lockless reads today and itβs honestly iconic. no cap.
πΈ NAME πΈ
These absolute main characters β seqc_consistent, seqc_read, seqc_write_begin, and seqc_write_end β are the lockless read algorithm. periodt.
π SYNOPSIS π
#include <sys/seqc.h>
void
seqc_write_begin(seqc_t *seqcp);
void
seqc_write_end(seqc_t *seqcp);
seqc_t
seqc_read(seqc_t *seqcp);
seqc_t
seqc_consistent(const seqc_t *seqcp, seqc_t oldseqc);
π DESCRIPTION π
okay so the seqc lets zero or more readers and zero or one writer concurrently access an object, and it gives readers a consistent snapshot of that object. the way that works is lowkey elegant: no mutual exclusion between readers and writers is required, but bestie, readers may be starved indefinitely by writers. yes, thatβs the tradeoff. π
Hereβs the function vibe check:
seqc_write_begin()andseqc_write_end()β these are for the writer. theyβre used to create a transaction for the writer and notify the readers that the object will be modified.seqc_read()β returns the current sequence number. if a writer has started a transaction, this function will spin until the transaction has ended.seqc_consistent()β compares the sequence number with a previously fetched value. theoldseqcvariable should contain a sequence number from the beginning of the read transaction.
At the end of a transaction, the reader checks if the sequence number has changed. if the sequence number didnβt change, the object wasnβt modified, and the fetched variables are valid. if the sequence number changed, the object was modified and the fetch should be repeated. and in the case when the sequence number is odd, the object change is in progress and the reader will wait until the write will the sequence number will become even. fr fr, thatβs the whole dance. π
β¨ EXAMPLES β¨
The following example for a writer changes the var1 and var2 variables in the obj structure:
lock_exclusive(&obj->lock);
seqc_write_begin(&obj->seqc);
obj->var1 = 1;
obj->var2 = 2;
seqc_write_end(&obj->seqc);
unlock_exclusive(&obj->lock);
The following example for a reader reads the var1 and var2 variables from the obj structure. In the case where the sequence number was changed, it restarts the whole process:
int var1, var2;
seqc_t seqc;
for (;;) {
seqc = seqc_read(&obj->seqc);
var1 = obj->var1;
var2 = obj->var2;
if (seqc_consistent(&obj->seqc, seqc))
break;
}
π AUTHORS π
The seqc functions was implemented by Mateusz Guzik <mjg@FreeBSD.org>. This manual page was written by Mariusz Zaborski <oshogbo@FreeBSD.org>. we love a credited icon. π«Ά
π CAVEATS π
β οΈ No progress guarantee for readers. There is no guarantee of progress for readers. In case when there are a lot of writers the reader can be starved. This concern may be solved by returning error after a few attempts. not me warning you about starvation in a cute font, but here we are. π
π The theoretical overflow jumpscare: Theoretically if reading takes a very long time, and when there are many writers the counter may overflow and wrap around to the same value. In that case the reader will not notice that the object was changed. Given that this needs4 billiontransactional writes across a single contended reader, it is unlikely to ever happen. This could be avoided by extending the interface to allow64-bitcounters. like, 4 billion? bestie, thatβs not a vibe check, thatβs a whole apocalypse. π
Debian Β· July 29, 2019 Β· SEQC(9) β bye bestie, slay your lockless reads π
π«Ά
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (C) 2019 Mariusz Zaborski <oshogbo@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms