CK_RWLOCK(3)
π ck_rwlock β centralized write-biased reader-writer locks π
okay bestie, sit down, grab your matcha π, because we're talking about ck_rwlock β the little reader-writer lock from Concurrency Kit (aka libck, link it with -lck) that is genuinely, unironically, the moment. β¨
This page covers a whole girl-group of functions: ck_rwlock_init, ck_rwlock_write_lock, ck_rwlock_write_unlock, ck_rwlock_write_trylock, ck_rwlock_write_downgrade, ck_rwlock_locked_writer, ck_rwlock_read_lock, ck_rwlock_read_trylock, ck_rwlock_read_unlock, ck_rwlock_locked_reader, ck_rwlock_recursive_write_lock, ck_rwlock_recursive_write_trylock, ck_rwlock_recurisve_write_unlock, ck_rwlock_recursive_read_lock, ck_rwlock_recursive_read_trylock, and ck_rwlock_recursive_read_unlock. yes, that's a lot of names. they all ate. π
π LIBRARY π
Concurrency Kit β that's libck, and you link it with -lck. slay. π«Ά
β¨ SYNOPSIS β¨
#include <ck_rwlock.h>
ck_rwlock_t lock = CK_RWLOCK_INITIALIZER;
void
ck_rwlock_init(ck_rwlock_t *lock);
void
ck_rwlock_write_lock(ck_rwlock_t *lock);
void
ck_rwlock_write_unlock(ck_rwlock_t *lock);
bool
ck_rwlock_write_trylock(ck_rwlock_t *lock);
bool
ck_rwlock_write_downgrade(ck_rwlock_t *lock);
bool
ck_rwlock_locked_writer(ck_rwlock_t *lock);
void
ck_rwlock_read_lock(ck_rwlock_t *lock);
bool
ck_rwlock_read_trylock(ck_rwlock_t *lock);
void
ck_rwlock_read_unlock(ck_rwlock_t *lock);
bool
ck_rwlock_locked_reader(ck_rwlock_t *lock);
ck_rwlock_recursive_t lock = CK_RWLOCK_RECURSIVE_INITIALIZER;
void
ck_rwlock_recursive_write_lock(ck_rwlock_recursive_t *lock,
unsigned int tid);
bool
ck_rwlock_recursive_write_trylock(ck_rwlock_recursive_t *lock,
unsigned int tid);
void
ck_rwlock_recurisve_write_unlock(ck_rwlock_recursive_t *lock);
void
ck_rwlock_recursive_read_lock(ck_rwlock_recursive_t *lock);
bool
ck_rwlock_recursive_read_trylock(ck_rwlock_recursive_t *lock);
void
ck_rwlock_recursive_read_unlock(ck_rwlock_recursive_t *lock);
copy that block exactly, bestie, i'm not kidding π«Ά
πΈ DESCRIPTION πΈ
okay so. this is a centralized write-biased reader-writer lock. it's giving minimal overhead, maximal lewk: it needs very little space overhead, and it has a low latency fast path. obsessed. π₯
here's the tea though β write-side recursion requires usage of ck_rwlock_recursive. and read-side recursion? disallowed. not "discouraged", not "maybe", disallowed. π«
and then there's ck_rwlock_write_downgrade() β the main character of the whole page, honestly. it degrades the caller's write-side acquisition to a read-side acquisition without forfeit of current critical section. like, you keep your spot in the critical section but you're just reading now. iconic behaviour. π
π EXAMPLE π
here's the vibe in code form. reader girlie and writer girlie, both in for (;;) loops, both thriving:
#include <ck_rwlock.h>
static ck_rwlock_t lock = CK_RWLOCK_INITIALIZER;
static void
reader(void)
{
for (;;) {
ck_rwlock_read_lock(&lock);
/* Read-side critical section. */
ck_rwlock_read_unlock(&lock);
if (ck_rwlock_read_trylock(&lock) == true) {
/* Read-side critical section. */
ck_rwlock_read_unlock(&lock);
}
}
return;
}
static void
writer(void)
{
for (;;) {
ck_rwlock_write_lock(&lock);
/* Write-side critical section. */
ck_rwlock_write_unlock(&lock);
if (ck_rwlock_write_trylock(&lock, 1) == true) {
/* Write-side critical section. */
ck_rwlock_write_unlock(&lock);
}
}
return;
}
π real talk: that's the example exactly as it's written in the page, character for character. don't @ me, i just report the facts.
π¦ SEE ALSO π¦
and if you want the full lore drop, it's all at http://concurrencykit.org/ β go read it, iykyk. π
and that's the page, bestie!! this one's dated July 26, 2013, and it lives at ck_rwlock(3) in the manual. go forth and lock responsibly πβ¨π
π¦ 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