CURPROC(9)

πŸ“¦ version 11.0, in nixpkgs unstable

πŸ’… curproc(9) β€” current processor, thread, and process πŸ¦‹

okay so this is a page from the Kernel Developer's Manual, bestie, and it's literally about three little things that tell you where you are and who you are inside the kernel. main character energy fr πŸ’–


✨ NAME ✨

curcpu, curlwp, curproc β€” current processor, thread, and process 🌸


πŸŽ€ SYNOPSIS πŸŽ€

#include <sys/proc.h>

struct cpu_info *
curcpu(void);

struct proc *curproc;
struct lwp *curlwp;

#include <sys/cpu.h>

bool
curcpu_stable(void);

πŸ’… DESCRIPTION πŸ’…

okay so these three retrieve the current CPU, process, and thread (that's a lightweight process, aka LWP, iykyk) respectively β€” one for each, slay:

  • curcpu() β€” returns a pointer to the struct cpu_info structure that represents the CPU the code calling it is running on. ✨
⚠️ bestie listen β€” the value of curcpu() is unstable and may be stale as soon as it is read unless the caller prevents preemption by raising the IPL (spl(9), mutex(9)), by disabling preemption (kpreempt_disable(9)), or by binding the thread to its CPU (curlwp_bind(9)). like, it literally expires on contact 😭

The function curcpu_stable() can be used in assertions (KASSERT(9)) to verify that curcpu() is stable in the current context. curcpu_stable() MUST NOT be used to make dynamic decisions about whether to query curcpu(). 🫢 like, assert with it, don't decide with it β€” that's the whole vibe check.

  • curproc β€” yields a pointer to the struct proc structure representing the currently running process. πŸ‘‘

The value of curproc is stable and does not change during execution except in machine-dependent logic to perform context switches, so it works like a global constant, not like a stateful procedure. no cap, it just is πŸ’œ

  • curlwp β€” yields a pointer to the struct lwp structure representing the currently running thread. 🧚

The value of curlwp is stable and does not change during execution except in machine-dependent logic to perform context switches, so it works like a global constant, not like a stateful procedure. same energy, iconic behaviour 🌈


πŸ¦‹ SOURCE REFERENCES πŸ¦‹

  • the curcpu() macro is defined in the machine-independent machine/cpu.h ✨
  • the curproc macro is defined in sys/lwp.h
  • the curlwp macro has a machine-independent definition in sys/lwp.h, but it may be overridden by machine/cpu.h, and must be overridden on architectures supporting multiprocessing and kernel preemption πŸ”₯ (so on those archs, sys/lwp.h's version is not the final word β€” periodt)
  • the curcpu_stable() function is defined in kern/subr_cpu.c πŸ“

πŸ‘€ SEE ALSO πŸ‘€


okay that's the whole page, go off πŸ’… and yeah β€” this cutie came from Debian, dated July 8, 2023. mwah πŸ«ΆπŸ’–

πŸ“¦ where this page comes from