CK_SWLOCK(3)

πŸ’– ck_swlock β€” bestie, it's giving lock πŸ’–

okay so this is ck_swlock(3) from the Concurrency Kit manual, and we're about to get so cozy with some locks. pull up a chair πŸŽ€


✨ NAME ✨

ck_swlock_init, ck_swlock_write_latch, ck_swlock_write_unlatch, ck_swlock_write_lock, ck_swlock_write_unlock, ck_swlock_write_trylock, ck_swlock_write_downgrade, ck_swlock_locked_writer, ck_swlock_read_lock, ck_swlock_read_trylock, ck_swlock_read_unlock, ck_swlock_locked_reader β€” centralized copy-safe write-biased single-writer read-write locks πŸ’…


🌸 LIBRARY 🌸

Concurrency Kit (libck, -lck) 🫢


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <ck_swlock.h>

ck_swlock_t lock = CK_SWLOCK_INITIALIZER;

void
ck_swlock_init(ck_swlock_t *lock);

void
ck_swlock_write_lock(ck_swlock_t *lock);

void
ck_swlock_write_unlock(ck_swlock_t *lock);

void
ck_swlatch_write_latch(ck_swlatch_t *latch);

void
ck_swlatch_write_unlatch(ck_swlatch_t *latch);

bool
ck_swlock_write_trylock(ck_swlock_t *lock);

bool
ck_swlock_write_downgrade(ck_swlock_t *lock);

bool
ck_swlock_locked_writer(ck_swlock_t *lock);

void
ck_swlock_read_lock(ck_swlock_t *lock);

bool
ck_swlock_read_trylock(ck_swlock_t *lock);

void
ck_swlock_read_unlock(ck_swlock_t *lock);

bool
ck_swlock_locked_reader(ck_swlock_t *lock);

πŸ’œ DESCRIPTION πŸ’œ

so basically? this is a centralized write-biased single-writer reader-writer lock. she is that girl. πŸ‘‘

the way she's built β€” she needs half the space that ck_rwlock does, and her fast path is low latency. skinny legend behavior fr fr. πŸ”₯

and she supports latch and unlatch operations, which is the whole reason she can be used in a copy-safe manner β€” meaning the reader-bits may be over-written safely. no cap. πŸ’…


πŸŽ€ EXAMPLE πŸŽ€

here's the whole thing, verbatim, because we do not play with code on this blog ✨

#include <ck_swlock.h>

static ck_swlock_t lock = CK_SWLOCK_INITIALIZER;

static void
reader(void)
{

        for (;;) {
                ck_swlock_read_lock(&lock);
                /* Read-side critical section. */
                ck_swlock_read_unlock(&lock);

                if (ck_swlock_read_trylock(&lock) == true) {
                        /* Read-side critical section. */
                        ck_swlock_read_unlock(&lock);
                }
        }

        return;
}

static void
writer(void)
{
        ck_swlock_t contrived;

        for (;;) {
                ck_swlock_write_lock(&lock);
                /* Write-side critical section. */
                ck_swlock_write_unlock(&lock);

                if (ck_swlock_write_trylock(&lock) == true) {
                        /* Write-side critical section. */
                        ck_swlock_write_unlock(&lock);
                }

                ck_swlock_write_latch(&lock);
                /* Write-side critical section. */

                /* This is safe to do with-in a latch. */
                contrived = lock;
                lock = contrived;
                ck_swlock_write_unlatch(&lock);
        }

        return;
}

like… the contrived variable is literally her doing a lil copy of the lock inside a latch and it's safe to do?? iconique. 😭🫢


🌈 SEE ALSO 🌈

and if you want additional information, it's available at http://concurrencykit.org/ ⭐


k that's the whole page bestie, go forth and lock responsibly πŸ’–

April 22, 2014 β€” Concurrency Kit, ck_swlock(3). mwah, love u, bye πŸŽ€πŸ’…βœ¨

πŸ“¦ where this page comes from