CK_BRLOCK(3)
π ck_brlock β big-reader locks, bestie edition π
okay so. welcome to ck_brlock(3), the manual page for big-reader locks in the Concurrency Kit. if you've ever wanted readers to just glide through while writers wait their turn, this is your moment. let's get into it π¦
π LIBRARY π
Concurrency Kit (libck, -lck) β that's the whole library, she's iconic, she contains multitudes π
β¨ SYNOPSIS β¨
first, the includes and the globals, exactly as they are, no notes:
#include <ck_brlock.h>
ck_brlock_t brlock = CK_BRLOCK_INITIALIZER;
ck_brlock_reader_t reader = CK_BRLOCK_READER_INITIALIZER;
and now the whole function lineup, all nine of them, in order, because we do not skip β:
void
ck_brlock_init(ck_brlock_t *br);
void
ck_brlock_write_lock(ck_brlock_t *br);
void
ck_brlock_write_unlock(ck_brlock_t *br);
bool
ck_brlock_write_trylock(ck_brlock_t *br, unsigned int factor);
void
ck_brlock_read_register(ck_brlock_t *br, ck_brlock_reader_t *reader);
void
ck_brlock_read_unregister(ck_brlock_t *br, ck_brlock_reader_t *reader);
void
ck_brlock_read_lock(ck_brlock_t *br, ck_brlock_reader_t *reader);
bool
ck_brlock_read_trylock(ck_brlock_t *br, ck_brlock_reader_t *reader,
unsigned int factor);
void
ck_brlock_read_unlock(ck_brlock_reader_t *reader);
π DESCRIPTION π
okay so here's the tea. big reader locks are distributed reader-writer locks, and their whole thing is that reader acquisition is low latency and constant time β and that's with respect to the number of concurrent readers, bestie. ten readers, ten thousand readers, the read side literally does not care. she ate. π
the tradeoff though?? writer acquisitions are a relatively expensive O(n) operation. nope, not a typo. the writers are the ones waiting in line at the club while the readers get walked straight in. π
and one more thing, super important: this is a write-biased lock. write that down, iykyk. βοΈ
π EXAMPLE π
here's the full example, verbatim, don't touch a single character:
static ck_brlock_t lock = CK_BRLOCK_INITIALIZER;
static __thread ck_brlock_reader_t reader;
static void
reader(void)
{
/* Add our thread as a lock participant. */
ck_brlock_read_register(&lock, &reader);
for (;;) {
ck_brlock_read_lock(&lock, &reader);
/* Read-side critical section. */
ck_brlock_read_unlock(&reader);
if (ck_brlock_read_trylock(&lock, &reader, 1) == true) {
/* Read-side critical section. */
ck_brlock_read_unlock(&reader);
}
}
return;
}
static void
writer(void)
{
for (;;) {
ck_brlock_write_lock(&lock);
/* Write-side critical section. */
ck_brlock_write_unlock(&lock);
if (ck_brlock_write_trylock(&lock, 1) == true) {
/* Write-side critical section. */
ck_brlock_write_unlock(&lock);
}
}
return;
}
notice how the reader thread registers itself first? yeah. ck_brlock_read_register(&lock, &reader) β that's the "add our thread as a lock participant" step, and it's the vibe check before anything else happens. π
π SEE ALSO π
ck_bytelock(3)ck_rwlock(3)
and if you want more, more is available, bestie: http://concurrencykit.org/ π
lowkey obsessed with how the readers get in and out in constant time while the writers are out here doing O(n) cardio. it's giving main character energy for readers only, no cap.
okay that's the whole page, go forth and lock responsibly πβ¨ππ¦πΈπ π«Ά
signing off from the ck_brlock(3) manual page, dated July 26, 2013 β yes she's an older queen but she still slays πβππ
π¦ where this page comes from
- alpine 3.24: ck-doc 0.7.2-r1 Β· BSD-2-Clause AND Apache-2.0
- debian bookworm: libck-dev 0.7.1-10 Β· BSD-2-Clause
- debian trixie: libck-dev 0.7.2-6 Β· BSD-2-Clause
- epel 10: ck-devel 0.7.2-1.el10_1 Β· BSD-2-clause AND Apache-2.0 AND BSD-3-clause
- epel 9: ck-devel 0.7.0-10.el9 Β· BSD
- fedora 43: ck-devel 0.7.2-2.fc43 Β· BSD-2-clause AND Apache-2.0 AND BSD-3-clause
- fedora 43: ck-devel 0.7.2-2.fc43 Β· BSD-2-clause AND Apache-2.0 AND BSD-3-clause
- fedora 44: ck-devel 0.7.2-4.fc44 Β· BSD-2-clause AND Apache-2.0 AND BSD-3-clause
- fedora 44: ck-devel 0.7.2-4.fc44 Β· BSD-2-clause AND Apache-2.0 AND BSD-3-clause
- nixos 26.05: ck 0.7.2
- nixpkgs unstable: ck 0.7.2
- opensuse-leap 16.0: ck-devel 0.7.2-bp160.1.9 Β· Apache-2.0 AND BSD-2-Clause
- opensuse-leap 16.0: ck-devel 0.7.2-bp160.1.9 Β· Apache-2.0 AND BSD-2-Clause
- opensuse-leap 16.0: ck-devel 0.7.2-bp160.1.9 Β· Apache-2.0 AND BSD-2-Clause
- ubuntu jammy: libck-dev 0.7.1-6ubuntu2 Β· BSD-2-Clause
- ubuntu noble: libck-dev 0.7.1-13build1 Β· BSD-2-Clause
- ubuntu questing: libck-dev 0.7.2-5ubuntu1
- ubuntu resolute: libck-dev 0.7.2-5ubuntu2 Β· BSD-2-Clause
- ubuntu stonking: libck-dev 0.7.2-11 Β· BSD-2-Clause
- Copyright 2013 Samy Al Bahra
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms