1.\" $NetBSD: curproc.9,v 1.7 2023/07/08 13:59:05 riastradh Exp $ 2.\" 3.\" Copyright (c) 2002 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Gregory McGarry. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd July 8, 2023 31.Dt CURPROC 9 32.Os 33.Sh NAME 34.Nm curcpu , 35.Nm curlwp , 36.Nm curproc 37.Nd current processor, thread, and process 38.Sh SYNOPSIS 39.In sys/proc.h 40.Ft struct cpu_info * 41.Fn curcpu "void" 42.Vt struct proc *curproc ; 43.Vt struct lwp *curlwp ; 44.In sys/cpu.h 45.Ft bool 46.Fn curcpu_stable "void" 47.Sh DESCRIPTION 48The following retrieve the current CPU, process, and thread 49.Pq lightweight process, or Tn LWP , 50respectively: 51.Bl -tag -width Dv 52.It Fn curcpu 53Returns a pointer to the 54.Vt "struct cpu_info" 55structure representing the CPU that the code calling it is running on. 56.Pp 57The value of 58.Fn curcpu 59is unstable and may be stale as soon as it is read unless the caller 60prevents preemption by raising the IPL 61.Pq Xr spl 9 , Xr mutex 9 , 62by disabling preemption 63.Pq Xr kpreempt_disable 9 , 64or by binding the thread to its CPU 65.Pq Xr curlwp_bind 9 . 66.Pp 67The function 68.Fn curcpu_stable 69can be used in assertions 70.Pq Xr KASSERT 9 71to verify that 72.Fn curcpu 73is stable in the current context. 74.Fn curcpu_stable 75MUST NOT be used to make dynamic decisions about whether to query 76.Fn curcpu . 77.It Dv curproc 78Yields a pointer to the 79.Vt "struct proc" 80structure representing the currently running process. 81.Pp 82The value of 83.Dv curproc 84is stable and does not change during execution except in 85machine-dependent logic to perform context switches, so it works like a 86global constant, not like a stateful procedure. 87.It Dv curlwp 88Yields a pointer to the 89.Vt "struct lwp" 90structure representing the currently running thread. 91.Pp 92The value of 93.Dv curlwp 94is stable and does not change during execution except in 95machine-dependent logic to perform context switches, so it works like a 96global constant, not like a stateful procedure. 97.El 98.Sh SOURCE REFERENCES 99The 100.Fn curcpu 101macro is defined in the machine-independent 102.Pa machine/cpu.h . 103.Pp 104The 105.Dv curproc 106macro is defined in 107.Pa sys/lwp.h . 108.Pp 109The 110.Dv curlwp 111macro has a machine-independent definition in 112.Pa sys/lwp.h , 113but it may be overridden by 114.Pa machine/cpu.h , 115and must be overridden on architectures supporting multiprocessing and 116kernel preemption. 117.Pp 118The 119.Fn curcpu_stable 120function is defined in 121.Pa kern/subr_cpu.c . 122.Sh SEE ALSO 123.Xr cpu_number 9 , 124.Xr proc_find 9 125