CK_TFLOCK(3)

πŸ’– ck_tflock(3) β€” centralized task-fair reader-writer locks πŸ’–

okay bestie, sit down, hydrate, and let me tell you about ck_tflock β€” that's the page you're on, ck_tflock(3), the centralized task-fair reader-writer lock from Concurrency Kit. it's giving locks but make it fair. let's go πŸ’…


✨ NAME ✨

ck_tflock_ticket_init, ck_tflock_ticket_write_lock, ck_tflock_ticket_write_unlock, ck_tflock_ticket_read_lock, ck_tflock_ticket_read_unlock β€” centralized task-fair reader-writer locks

like, that's the whole squad. five functions, one lock, zero drama 🫢


πŸ“š LIBRARY πŸ“š

Concurrency Kit (libck, -lck)

so when you link, you link against libck with -lck. that's the whole vibe. πŸŽ€


🌸 SYNOPSIS 🌸

#include <ck_tflock.h>

ck_tflock_ticket_t lock = CK_TFLOCK_TICKET_INITIALIZER;

void
ck_tflock_ticket_init(ck_tflock_ticket_t *lock);

void
ck_tflock_ticket_write_lock(ck_tflock_ticket_t *lock);

void
ck_tflock_ticket_write_unlock(ck_tflock_ticket_t *lock);

void
ck_tflock_ticket_read_lock(ck_tflock_ticket_t *lock);

void
ck_tflock_ticket_read_unlock(ck_tflock_ticket_t *lock);

okay so the glow-up here: you got your type ck_tflock_ticket_t, you got a static initializer CK_TFLOCK_TICKET_INITIALIZER so your lock is ready before main even wakes up, and you got five functions β€” one init, and a lock/unlock pair for readers plus a lock/unlock pair for writers. every single one returns void and takes exactly one ck_tflock_ticket_t *lock. no extra args, no surprises. ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

so what is this thing? it's a centralized task-fair reader-writer lock. periodt.

what that means in bestie terms: readers and writers both get their turn, and the fairness is the whole point β€” nobody's getting starved while somebody else hogs the lock forever. that's the "task-fair" part and it's lowkey the main character of this entire manpage. πŸ‘‘

and the tea? it requires little space overhead and it has a low latency fast path. so it's small and it's quick when things are chill. it's giving efficient. it's giving iconic. πŸŽ€


πŸ”₯ EXAMPLE πŸ”₯

here's the whole thing in action, copy it exactly as it's written, bestie:

#include <ck_tflock.h>

static ck_tflock_ticket_t lock = CK_TFLOCK_INITIALIZER;

static void
reader(void)
{

        for (;;) {
                ck_tflock_ticket_read_lock(&lock);
                /* Read-side critical section. */
                ck_tflock_ticket_read_unlock(&lock);
        }

        return;
}

static void
writer(void)
{

        for (;;) {
                ck_tflock_ticket_write_lock(&lock);
                /* Write-side critical section. */
                ck_tflock_ticket_write_unlock(&lock);
        }

        return;
}

see how the reader grabs ck_tflock_ticket_read_lock(&lock), does its little read-side critical section, then ck_tflock_ticket_read_unlock(&lock)? and the writer does the exact same dance but with ck_tflock_ticket_write_lock(&lock) and ck_tflock_ticket_write_unlock(&lock). both of them loop forever with for (;;) and both return; at the end. that's the pattern. that's it. you ate. πŸ’…

little note bestie: the initializer in this example is spelled CK_TFLOCK_INITIALIZER, and in the SYNOPSIS it's CK_TFLOCK_TICKET_INITIALIZER. write each one exactly the way it appears in the block you're copying from. do not freestyle the spelling, i am begging πŸ™ˆ

πŸ‘€ SEE ALSO πŸ‘€

and if you want even more, the additional information lives at http://concurrencykit.org/ 🌈


and that's the page!! April 22, 2014. β€” that's when this beauty got dated, from the ck_tflock(3) manual page itself. mwah. go forth and lock responsibly, bestie πŸ«ΆπŸ’–βœ¨

πŸ“¦ where this page comes from