CK_RING_TRYDEQUEUE_SPMC(3)

๐Ÿ’– CK_RING_TRYDEQUEUE_SPMC ๐Ÿ’–

okay bestie, welcome to ck_ring_trydequeue_spmc(3) โ€” yes that's the whole manual name, say it three times fast, i dare you ๐Ÿซถ it lives in the Concurrency Kit corner of the universe, aka libck, link it with -lck.


โœจ NAME โœจ

ck_ring_trydequeue_spmc โ€” dequeue from a bounded FIFO, and it's allowed to just. fail. spuriously. no drama, that's the whole bit ๐Ÿ’…


๐Ÿ“š LIBRARY ๐Ÿ“š

Concurrency Kit (libck, -lck) โ€” iconic, legendary, lowkey the moment.


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

#include <ck_ring.h>

bool
ck_ring_trydequeue_spmc(ck_ring_t *ring, ck_ring_buffer_t *buffer,
    void *result);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

so. ck_ring_trydequeue_spmc(3) tries to dequeue one pointer from the bounded buffer that ring points at, and it does it FIFO style โ€” first in, first out, oldest pointer first, no cutting the line ๐Ÿ™…โ€โ™€๏ธ that pointer lands in the pointer that result points at.

the buffer that buffer points at has to be unique to ring โ€” no sharing, not even a little โ€” and it must point to an array of ck_ring_buffer_t that's long enough (long enough meaning: according to the power-of-2 elements in the buffer, so do the math, bestie ๐Ÿงฎโœจ).

why is the ring decoupled from the buffer at all?? because use-cases like multiple address spaces and DMA (among others!!) needed it, and honestly? she ate. ๐Ÿ’…

if you're on non-POSIX platforms or you simply want strict compliance with C, then the recommendation is to pass a pointer of type void ** for result. noted, respected, do it.

concurrency tea time โ˜•: this function is safe to call without locking for UINT_MAX concurrent ck_ring_dequeue_spmc(3) or ck_ring_trydequeue_spmc(3) invocations, plus up to one concurrent ck_ring_enqueue_spmc(3) or ck_ring_tryenqueue_spmc(3) invocation. that's it, that's the budget. one enqueuer, bestie, don't get greedy ๐Ÿซถ

this operation will always complete in a bounded number of steps. no infinite spirals, no waiting forever, we respect your time.

and now the part that makes people scream: it is possible for the function to return false even if ring is non-empty. yes. literally that. spurious failure is not a bug, it's a lifestyle. ๐Ÿ˜ญ

also the original manpage just ends that paragraph with the word "This" and nothing else ๐Ÿ’€ not me getting left on read by a documentation file. we keep it, we honor it, we move on.

๐ŸŒธ EXAMPLE ๐ŸŒธ

#include <ck_ring.h>

/* This ring was previously initialized with ck_ring_init. */
ck_ring_t ring;

/* The ring was initialized for 1023 elements. */
ck_ring_buffer_t buffer[1024];

void
dequeue(void)
{
        void *result;

        /* Dequeue from ring until contention is actively observed. */
        while (ck_ring_trydequeue_spmc(&ring, &buffer, &result) == true) {
                /*
                 * Results contains the oldest pointer in ring
                 * since the dequeue operation returned true.
                 */
                operation(result);
        }

        /* An empty ring was encountered, leave. */
        return;
}

look at that loop go ๐Ÿ”ฅ keep dequeuing until you actually observe contention, and the second it returns false, you leave. main character exit. ๐Ÿšชโœจ


๐Ÿ’€ RETURN VALUES ๐Ÿ’€

  • the function returns true if the dequeue operation completely successfully goes through in a bounded number of steps โ€” and the result of the dequeue is stored in the value that result points to. slay. ๐Ÿ‘‘
  • otherwise the function returns false if the buffer was empty, or if the operation could not be completed in a bounded number of steps.
โš ๏ธ if the function returns false, then the contents of result are undefined. do not read it. do not peek. do not look at it and hope. undefined is undefined, bestie, respect the boundary ๐Ÿ™ˆ

๐Ÿซถ SEE ALSO ๐Ÿซถ

additional information available at http://concurrencykit.org/ โ€” go, read, obsess ๐Ÿฆ‹


and that's the whole page, babes ๐Ÿ’– this was CK_RING_TRYDEQUEUE_SPMC(3), dated April 20, 2013, and it's giving timeless. no cap. periodt. ๐ŸŒˆโญ๐Ÿ’œ

๐Ÿ“ฆ where this page comes from