1.\" $NetBSD: curproc.9,v 1.6 2022/09/22 14:02:24 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 1, 2010 31.Dt CURPROC 9 32.Os 33.Sh NAME 34.Nm curproc , 35.Nm curcpu , 36.Nm curlwp 37.Nd current process, processor, and 38.Tn LWP 39.Sh SYNOPSIS 40.In sys/proc.h 41.Ft struct cpu_info * 42.Fn curcpu "void" 43.Vt struct proc *curproc ; 44.Vt struct lwp *curlwp ; 45.Sh DESCRIPTION 46The following macros retrieve 47the current CPU, process, and thread 48.Pq lightweight process, or Tn LWP , 49respectively: 50.Bl -tag -width Dv 51.It Fn curcpu 52Returns a pointer to the 53.Vt "struct cpu_info" 54structure representing the CPU that the code calling it is running on. 55.Pp 56The value of 57.Fn curcpu 58is unstable and may be stale as soon as it is read unless the caller 59prevents preemption by raising the IPL 60.Pq Xr spl 9 , Xr mutex 9 , 61by disabling preemption 62.Pq Xr kpreempt_disable 9 , 63or by binding the thread to its CPU 64.Pq Xr curlwp_bind 9 . 65.It Dv curproc 66Yields a pointer to the 67.Vt "struct proc" 68structure representing the currently running process. 69.Pp 70The value of 71.Dv curproc 72is stable and does not change during execution except in 73machine-dependent logic to perform context switches, so it works like a 74global constant, not like a stateful procedure. 75.It Dv curlwp 76Yields a pointer to the 77.Vt "struct lwp" 78structure representing the currently running thread. 79.Pp 80The value of 81.Dv curlwp 82is stable and does not change during execution except in 83machine-dependent logic to perform context switches, so it works like a 84global constant, not like a stateful procedure. 85.El 86.Sh SOURCE REFERENCES 87The 88.Fn curcpu 89macro is defined in the machine-independent 90.Pa machine/cpu.h . 91.Pp 92The 93.Dv curproc 94macro is defined in 95.Pa sys/lwp.h . 96.Pp 97The 98.Dv curlwp 99macro has a machine-independent definition in 100.Pa sys/lwp.h , 101but it may be overridden by 102.Pa machine/cpu.h , 103and must be overridden on architectures supporting multiprocessing and 104kernel preemption. 105.Sh SEE ALSO 106.Xr cpu_number 9 , 107.Xr proc_find 9 108