ITHREAD(9)
π ITHREAD β kernel interrupt threads π
Section 9 of the Kernel Developer's Manual, bestie. This is the whole ithread_* family β the interrupt thread squad in the kernel. Pull up a chair, grab a boba, we're doing this. π§β¨
β¨ NAME β¨
ithread_add_handler, ithread_create, ithread_destroy, ithread_priority, ithread_remove_handler, ithread_schedule β kernel interrupt threads π§΅π₯
π SYNOPSIS π
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/interrupt.h>
int
ithread_add_handler(struct ithd *ithread, const char *name,
driver_intr_t handler, void *arg, u_char pri, enum intr_type flags,
void **cookiep);
int
ithread_create(struct ithd **ithread, int vector, int flags,
void (*disable)(int), void (*enable)(int), const char *fmt, ...);
int
ithread_destroy(struct ithd *ithread);
u_char
ithread_priority(enum intr_type flags);
int
ithread_remove_handler(void *cookie);
int
ithread_schedule(struct ithd *ithread, int do_switch);
π¦ DESCRIPTION π¦
Okay so. Interrupt threads are kernel threads that run a whole list of handlers whenever a hardware or software interrupt goes off. Main character energy, fr. π
Every single interrupt handler is packing:
- a
name - a
handlerfunction - a
handlerargument - a
priority - and various
flags
Each interrupt thread keeps its handler list sorted by priority, which means higher priority handlers get executed before lower priority ones. Periodt. π Each thread also takes on the priority of its highest priority handler as its process priority β or PRIO_MAX if it's got zero handlers, rip. π And every interrupt thread is tied to exactly one interrupt source, which is represented as a vector number.
ithread_create() β make a new thread, slay β¨
The ithread_create() function creates a new interrupt thread.
ithreadβ points to astruct ithdpointer that, on success, will point to the newly created thread. π«Άvectorβ the interrupt source this thread gets associated with.flagsβ a mask of properties for this thread. The only valid flag currently forithread_create()isIT_SOFT, which says this interrupt thread is a software interrupt.enableanddisableβ optional functions used to enable and disable this interrupt thread's interrupt source. They receive thevectorcorresponding to the thread's interrupt source as their only argument. π- the remaining arguments β they form a
printf(9)argument list used to build the base name of the new ithread.
The full name of an interrupt thread is made by concatenating the base name of the interrupt thread with the names of all of its interrupt handlers. It's giving collab. π€
ithread_destroy() β bye bye π
The ithread_destroy() function destroys a previously created interrupt thread by releasing its resources and arranging for the backing kernel thread to terminate.
Heads up bestie: an interrupt thread can only be destroyed if it has no handlers remaining. No handlers, no problem. Otherwise? Problem. π
ithread_add_handler() β new handler just dropped π
The ithread_add_handler() function adds a new handler to an existing interrupt thread specified by ithread.
nameβ a name for this handler.handlerandargβ the function to execute for this handler, and an argument to pass to it.priβ the priority of this handler. It's used both for sorting it against the other handlers for this thread and to specify the priority of the backing kernel thread.flagsβ properties of this handler, as defined in<sys/bus.h>.cookiepβ if it is notNULL, it gets assigned a cookie you can use later to remove this handler. πͺ
ithread_remove_handler() β yeet that handler π
The ithread_remove_handler() removes a handler from an interrupt thread. The cookie argument specifies the handler to remove from its thread.
ithread_schedule() β go go go π
The ithread_schedule() function schedules an interrupt thread to run. If the do_switch argument is non-zero and the interrupt thread is idle, then a context switch will be forced after putting the interrupt thread on the run queue.
ithread_priority() β translator β¨
The ithread_priority() function translates the INTR_TYPE_* interrupt flags into interrupt handler priorities.
the extra flags π©
The interrupt flags that are not related to the type of a particular interrupt (INTR_TYPE_*) can be used to specify additional properties of both hardware and software interrupt handlers:
INTR_EXCLβ says this handler cannot share an interrupt thread with another handler. Exclusive! πINTR_MPSAFEβ says this handler is MP safe, meaning it does not need theGiantmutex to be held while it executes.INTR_ENTROPYβ says the interrupt source this handler is tied to is a good source of entropy, so entropy should be gathered when an interrupt from the handler's source triggers. π Presently, theINTR_ENTROPYflag is not valid for software interrupt handlers.
π¨ REAL TALK: It is not permitted to sleep in an interrupt thread. So any memory or zone allocations in an interrupt thread should be specified with the M_NOWAIT flag set. And any allocation errors must be handled thereafter. Don't skip that part, I'm begging. π
β RETURN VALUES β
ithread_add_handler(),ithread_create(),ithread_destroy(),ithread_remove_handler(), andithread_schedule()all return zero on success and non-zero on failure. πithread_priority()returns a process priority corresponding to the passed in interrupt flags.
π EXAMPLES π
The swi_add() function demonstrates the use of ithread_create() and ithread_add_handler(). Look at her go:
int
swi_add(struct ithd **ithdp, const char *name, driver_intr_t handler,
void *arg, int pri, enum intr_type flags, void **cookiep)
{
struct proc *p;
struct ithd *ithd;
int error;
if (flags & INTR_ENTROPY)
return (EINVAL);
ithd = (ithdp != NULL) ? *ithdp : NULL;
if (ithd != NULL) {
if ((ithd->it_flags & IT_SOFT) == 0)
return(EINVAL);
} else {
error = ithread_create(&ithd, pri, IT_SOFT, NULL, NULL,
"swi%d:", pri);
if (error)
return (error);
if (ithdp != NULL)
*ithdp = ithd;
}
return (ithread_add_handler(ithd, name, handler, arg, pri + PI_SOFT,
flags, cookiep));
}
π ERRORS π
ithread_add_handler() will fail if:
[EINVAL]β Any of theithread,handler, ornamearguments areNULL.[EINVAL]β TheINTR_EXCLflag is specified and the interrupt threadithreadalready has at least one handler, or the interrupt threadithreadalready has an exclusive handler.[ENOMEM]β Could not allocate needed memory for this handler.
ithread_create() will fail if:
[EAGAIN]β The system-imposed limit on the total number of processes under execution would be exceeded. That limit is given by thesysctl(3)MIB variableKERN_MAXPROC.[EINVAL]β A flag other thanIT_SOFTwas specified in theflagsparameter.[ENOMEM]β Could not allocate needed memory for this interrupt thread.
ithread_destroy() will fail if:
[EINVAL]β Theithreadargument isNULL.[EINVAL]β The interrupt thread pointed to byithreadhas at least one handler.
ithread_remove_handler() will fail if:
[EINVAL]β Thecookieargument isNULL.
ithread_schedule() will fail if:
[EINVAL]β Theithreadargument isNULL.[EINVAL]β The interrupt thread pointed to byithreadhas no interrupt handlers.
π SEE ALSO π
kthread(9), malloc(9), swi(9), uma(9)
π HISTORY π
Interrupt threads and their corresponding API first appeared in FreeBSD 5.0. Vintage. π°οΈ
π BUGS π
Currently struct ithd represents both an interrupt source and an interrupt thread. There should be a separate struct isrc that contains a vector number, enable and disable functions, etc. that an ithread holds a reference to. Like, the design said "let's do both" and honestly? π It's a known thing.
And that's the whole page, bestie! This one came out of the Debian build of the Kernel Developer's Manual, dated August 25, 2006. Go forth and schedule some interrupts. πβ¨π¦
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2001 John H. Baldwin <jhb@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms