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