$NetBSD: curproc.9,v 1.7 2023/07/08 13:59:05 riastradh Exp $

Copyright (c) 2002 The NetBSD Foundation, Inc.
All rights reserved.

This code is derived from software contributed to The NetBSD Foundation
by Gregory McGarry.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

.Dd July 8, 2023 .Dt CURPROC 9 .Os .Sh NAME .Nm curcpu , .Nm curlwp , .Nm curproc .Nd current processor, thread, and process .Sh SYNOPSIS n sys/proc.h .Ft struct cpu_info * .Fn curcpu "void" .Vt struct proc *curproc ; .Vt struct lwp *curlwp ; n sys/cpu.h .Ft bool .Fn curcpu_stable "void" .Sh DESCRIPTION The following retrieve the current CPU, process, and thread

q lightweight process, or Tn LWP , respectively: l -tag -width Dv t Fn curcpu Returns a pointer to the .Vt "struct cpu_info" structure representing the CPU that the code calling it is running on.

p The value of .Fn curcpu is unstable and may be stale as soon as it is read unless the caller prevents preemption by raising the IPL

q Xr spl 9 , Xr mutex 9 , by disabling preemption

q Xr kpreempt_disable 9 , or by binding the thread to its CPU

q Xr curlwp_bind 9 .

p The function .Fn curcpu_stable can be used in assertions

q Xr KASSERT 9 to verify that .Fn curcpu is stable in the current context. .Fn curcpu_stable MUST NOT be used to make dynamic decisions about whether to query .Fn curcpu . t Dv curproc Yields a pointer to the .Vt "struct proc" structure representing the currently running process.

p The value of .Dv 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. t Dv curlwp Yields a pointer to the .Vt "struct lwp" structure representing the currently running thread.

p The value of .Dv 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. .El .Sh SOURCE REFERENCES The .Fn curcpu macro is defined in the machine-independent

a machine/cpu.h .

p The .Dv curproc macro is defined in

a sys/lwp.h .

p The .Dv curlwp macro has a machine-independent definition in

a sys/lwp.h , but it may be overridden by

a machine/cpu.h , and must be overridden on architectures supporting multiprocessing and kernel preemption.

p The .Fn curcpu_stable function is defined in

a kern/subr_cpu.c . .Sh SEE ALSO .Xr cpu_number 9 , .Xr proc_find 9