WORKQUEUE(9)
πΈ NAME πΈ
okay so this is workqueue(9) from the Kernel Developer's Manual, and the whole vibe is simple do-it-in-thread-context framework. like, it's the little setup that lets you shove work off to a thread instead of doing it right this second. bestie, it's giving "I'll handle it in thread context" energy, and honestly? iconic. π
β¨
β¨ SYNOPSIS β¨
#include <sys/workqueue.h>
int
workqueue_create(struct workqueue **wqp, const char *name,
void (*func)(struct work *, void *), void *arg, pri_t prio, int ipl,
int flags);
void
workqueue_enqueue(struct workqueue *wq, struct work *wk,
struct cpu_info *ci);
void
workqueue_wait(struct workqueue *wq, struct work *wk);
void
workqueue_destroy(struct workqueue *wq);
π DESCRIPTION π
the workqueue utility routines are here to defer work that needs to be processed in a thread context. no cap. like, you don't do it inline β you hand it to the workqueue framework and it gets handled by a worker thread. periodt. β¨π«Ά
π workqueue_create() π
workqueue_create() creates a workqueue. it takes these arguments, bestie:
wqpβ where to store the created workqueue. like, that's the spot it gets filled into. π«Άnameβ the name of the workqueue. label it cute so you know what it is.funcβ the function to be called for each work. this is the callback that does the actual thing.argβ an argument to be passed as the second argument offunc. literally that.prioβ the priority level for the worker threads.iplβ the highest IPL at which this workqueue is used. highkey important.flagsβ the value0indicates a standard create operation, but you can bitwise OR these flags together:WQ_FPUβ specifies that the kthread must be allowed to use any machine-dependent per-CPU floating-point units or SIMD vector units, as inkthread_fpu_enter(9)/kthread_fpu_exit(9), when it executes the worker function. like, FPU/SIMD permission slip activated. π₯WQ_MPSAFEβ specifies that the workqueue is multiprocessor safe and does its own locking; otherwise the kernel lock will be held while processing work. so either it locks itself or the kernel lock is held β no in-between. πWQ_PERCPUβ specifies that the workqueue should have a separate queue for each CPU, thus the work could be enqueued on concrete CPUs. one queue per CPU, slay. π
π workqueue_enqueue() π
workqueue_enqueue() enqueues the work wk into the workqueue wq. simple. β¨
if the WQ_PERCPU flag was set on workqueue creation, the ci argument may be used to specify the CPU on which the work should be enqueued. also it may be NULL, and then work will be enqueued on the current CPU. if the WQ_PERCPU flag was not set, ci must be NULL. okay? don't play with it. π
the enqueued work will be processed in a thread context. and a work must not be enqueued again until the callback is called by the workqueue framework. that's a hard rule, bestie β no double-enqueue. π
π¦ workqueue_wait() π¦
workqueue_wait() waits for a specified work wk on the workqueue wq to finish. it's literally "wait for this one to be done". the caller must ensure that wk will not be enqueued to the workqueue again until after workqueue_wait() returns. so no sneaky re-enqueue while waiting. periodt. βπ
π§ workqueue_destroy() π§
workqueue_destroy() destroys a workqueue and frees associated resources. the caller should ensure that the workqueue has no work enqueued beforehand. like, clean it out first β don't just nuke it with pending work. that's the vibe check. π«ΆπΈ
π₯ RETURN VALUES π₯
workqueue_create() returns 0 on success. otherwise, it returns an errno(2). so if it's not 0, check your errno, bestie. fr fr. π
π¦ CODE REFERENCES π¦
the workqueue subsystem is implemented within the file sys/kern/subr_workqueue.c. that's where the magic lives. β¨
π SEE ALSO π
callout(9)condvar(9)kthread(9)softint(9)
okay that's the whole page, mwah π β this Debian footer says December 28, 2017 and WORKQUEUE(9), so that's the source/version and date, and I'm obsessed with how clean that is. πΈπ
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c)2005 YAMAMOTO Takashi
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms