.\" $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 .In sys/proc.h .Ft struct cpu_info * .Fn curcpu "void" .Vt struct proc *curproc ; .Vt struct lwp *curlwp ; .In sys/cpu.h .Ft bool .Fn curcpu_stable "void" .Sh DESCRIPTION The following retrieve the current CPU, process, and thread .Pq lightweight process, or Tn LWP , respectively: .Bl -tag -width Dv .It Fn curcpu Returns a pointer to the .Vt "struct cpu_info" structure representing the CPU that the code calling it is running on. .Pp 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 .Pq Xr spl 9 , Xr mutex 9 , by disabling preemption .Pq Xr kpreempt_disable 9 , or by binding the thread to its CPU .Pq Xr curlwp_bind 9 . .Pp The function .Fn curcpu_stable can be used in assertions .Pq 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 . .It Dv curproc Yields a pointer to the .Vt "struct proc" structure representing the currently running process. .Pp 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. .It Dv curlwp Yields a pointer to the .Vt "struct lwp" structure representing the currently running thread. .Pp 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 .Pa machine/cpu.h . .Pp The .Dv curproc macro is defined in .Pa sys/lwp.h . .Pp The .Dv curlwp macro has a machine-independent definition in .Pa sys/lwp.h , but it may be overridden by .Pa machine/cpu.h , and must be overridden on architectures supporting multiprocessing and kernel preemption. .Pp The .Fn curcpu_stable function is defined in .Pa kern/subr_cpu.c . .Sh SEE ALSO .Xr cpu_number 9 , .Xr proc_find 9