CURPROC(9) Kernel Developer's Manual CURPROC(9)

curcpu, curlwp, curproccurrent processor, thread, and process

#include <sys/proc.h>

struct cpu_info *
curcpu(void);

struct proc *curproc;
struct lwp *curlwp;

#include <sys/cpu.h>

bool
curcpu_stable(void);

The following retrieve the current CPU, process, and thread (lightweight process, or LWP), respectively:

()
Returns a pointer to the struct cpu_info structure representing the CPU that the code calling it is running on.

The value of () 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)).

The function () 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().

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.

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.

The () 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.

The () function is defined in kern/subr_cpu.c.

cpu_number(9), proc_find(9)

July 8, 2023 NetBSD 11.0