raw_write_seqcount_barrier(9)
π raw_write_seqcount_barrier β Kernel API Manual β¨
okay bestie, so this whole page is about raw_write_seqcount_barrier(9), from the Kernel API Manual, and it's genuinely one of those tiny functions that does a lot of heavy lifting. lowkey iconic. let's get into it π«Ά
πΈ NAME πΈ
raw_write_seqcount_barrier β do a seqcount_t write barrier. that's it, that's the whole job, and she does it flawlessly π
β¨ SYNOPSIS β¨
raw_write_seqcount_barrier (s );
yep, one argument, no notes, main character energy π
π ARGUMENTS π
sβ a pointer to aseqcount_t, or literally any of theseqcount_LOCKNAME_tvariants. she's flexible like that π
π¦ DESCRIPTION π¦
so here's the vibe: this one gives you an ordering guarantee instead of the usual consistency guarantee. different flavor, same friend group. and it's one wmb cheaper than the alternative, because it can collapse the two back-to-back wmbs into one. frugal queen, we stan π
β οΈ okay important bit, don't skip this: the writes surrounding the barrier should be declared atomic (like, via WRITE_ONCE). why? two reasons, both real: a) so the writes actually become visible to other threads atomically β this dodges compiler optimizations doing sneaky little things behind your back π b) so you document which writes are meant to propagate to the reader's critical section. future you (and everyone else) will thank you, fr fr.
and this is necessary because neither the writes before nor the writes after the barrier are enclosed in a seq-writer critical section β the kind that would normally make sure readers know about ongoing writes. so like, nobody's got your back here except this barrier. no cap π
here's the example, copied exactly, do not touch:
seqcount_t seq;
bool X = true, Y = false;
void read(void)
{
bool x, y;
do {
int s = read_seqcount_begin(seq);
x = X; y = Y;
} while (read_seqcount_retry(seq, s));
BUG_ON(!x && !y);
}
void write(void)
{
WRITE_ONCE(Y, true);
raw_write_seqcount_barrier(seq);
WRITE_ONCE(X, false);
}
like, see how both WRITE_ONCEs are outside any seq-writer critical section? that's the whole point bestie, that's the assignment πβ¨
π SEE ALSO π
the kernel file that lives at ./include/linux/seqlock.h, plus this entire friend group β every single one, no skipping, we respect all of them equally π«Ά:
seqcount_init(9),SEQCNT_ZERO(9)__read_seqcount_begin(9),raw_read_seqcount_begin(9),read_seqcount_begin(9)raw_read_seqcount(9),raw_seqcount_try_begin(9),raw_seqcount_begin(9)__read_seqcount_retry(9),read_seqcount_retry(9)raw_write_seqcount_begin(9),raw_write_seqcount_end(9)write_seqcount_begin_nested(9),write_seqcount_begin(9),write_seqcount_end(9),write_seqcount_invalidate(9)SEQCNT_LATCH_ZERO(9),seqcount_latch_init(9)raw_read_seqcount_latch(9),read_seqcount_latch(9),raw_read_seqcount_latch_retry(9),read_seqcount_latch_retry(9)raw_write_seqcount_latch(9),write_seqcount_latch_begin(9),write_seqcount_latch(9),write_seqcount_latch_end(9)seqlock_init(9),DEFINE_SEQLOCK(9)read_seqbegin(9),read_seqretry(9)write_seqlock(9),write_sequnlock(9)write_seqlock_bh(9),write_sequnlock_bh(9)write_seqlock_irq(9),write_sequnlock_irq(9)write_seqlock_irqsave(9),write_sequnlock_irqrestore(9)read_seqlock_excl(9),read_sequnlock_excl(9)read_seqlock_excl_bh(9),read_sequnlock_excl_bh(9)read_seqlock_excl_irq(9),read_sequnlock_excl_irq(9)read_seqlock_excl_irqsave(9),read_sequnlock_excl_irqrestore(9)read_seqbegin_or_lock(9),need_seqretry(9),done_seqretry(9)read_seqbegin_or_lock_irqsave(9),done_seqretry_irqrestore(9)scoped_seqlock_read(9)
and that's the whole page, bestie!! π§ββοΈ this lil masterpiece came out of ./include/linux, dated October 2026, and it's page raw_write_seqcount_barrier(9) of the Kernel API Manual. go forth and barrier responsibly, ily, bye πππ
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms