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