summaryrefslogtreecommitdiff
path: root/static/netbsd/man9/curproc.9
blob: 282af8d1fbb96fc5654329ab68dd9ec4763792c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
.\"     $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