COND_INIT(9)
π cond_init(9) β the wait condition API, explained by your local Unix bestie β¨
okay so bestie, we're in cond_init(9) territory today β kernel developer's manual, wait-condition edition. this is the whole "make a thread nap until something's done" situation and honestly? it's giving synchronization slay π
β¨ SYNOPSIS β¨
#include <sys/systm.h>
void
cond_init(struct cond *c);
void
cond_wait(struct cond *c, const char *wmesg);
void
cond_signal(struct cond *c);
void
cond_signal_handler(void *c);
COND_INITIALIZER();
πΈ DESCRIPTION πΈ
so the wait condition API is literally the "go to sleep, queen, I'll wake you when it's done" of the kernel π it lets a thread sleep while it waits for a notification β aka a signal β that pending work has completed. no cap.
cond_init()β initialises the wait conditioncfor use. that's it, that's the vibe check βcond_wait()β this is the actual napping function π€ it sleeps on the wait conditioncuntil whatever the thread is waiting on callscond_signal()orcond_signal_handler(). andwmesg? it's a pointer to a character string that says why the thread is sleeping β like a little "do not disturb: tqbar" note on the door πcond_signal()andcond_signal_handler()β these two are the "wake up bestie, the work is finished, you may proceed" notification π and listen, they operate identically, the only difference is the type of argument they take.cond_signal_handler()takes avoid *argument, which is lowkey iconic because it means you can use it directly with APIs such astask_set(9)andtimeout_set(9)π«ΆCOND_INITIALIZER()β initialises a declaration of acondfor use π
π¦ CONTEXT π¦
where can you call these? good question, bestie:
cond_init(),cond_signal(), andcond_signal_handler()can be called during autoconf, from process context, or from interrupt context π₯ flexible queens.cond_wait()can be called from process context. that's the one.
π EXAMPLES π
not me about to show you real production code π taskq_barrier(9) is implemented using the wait condition API, and here's a commented copy of the implementation β fr fr, this is the actual source:
static void taskq_barrier_task(void *);
void
taskq_barrier(struct taskq *tq)
{
struct cond c;
struct task t;
/*
* any currently running work has to have finished
* before this new task can be run.
*/
cond_init(&c);
task_init(&t, taskq_barrier_task, &c);
task_add(tq, &t);
/* wait until the task runs and signals completion */
cond_wait(&c, "tqbar");
}
static void
taskq_barrier_task(void *p)
{
struct cond *c = p;
/*
* all previous tasks have run, signal the thread waiting
* in taskq_barrier
*/
cond_signal(c);
}
see how it all comes together? cond_init(&c) sets it up, cond_wait(&c, "tqbar") has the thread chilling until the task signals, and then cond_signal(c) in the task function is the "you're up, babe" π
main character energy from start to finish.
okay that's the whole page, bestie π this lil breakdown came to you from the Debian source, dated July 28, 2025 β go forth and synchronize your threads, you absolute icon π§β¨
π¦ where this page comes from
- nixpkgs unstable: cvs-export
- Copyright (c) 2017 David Gwynne <dlg@openbsd.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms