summaryrefslogtreecommitdiff
path: root/static/netbsd/man3/pthread_attr_getstack.3
blob: ba003d726b811ebaf635ac84879dc699ce510e8e (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
.\"	$NetBSD: pthread_attr_getstack.3,v 1.9 2023/12/07 16:55:01 riastradh Exp $
.\"
.\" Copyright (c) 2010 Jukka Ruohonen <jruohonen@iki.fi>
.\" All rights reserved.
.\"
.\" 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 9, 2010
.Dt PTHREAD_ATTR_GETSTACK 3
.Os
.Sh NAME
.Nm pthread_attr_getstack ,
.Nm pthread_attr_setstack ,
.Nm pthread_attr_getstacksize ,
.Nm pthread_attr_setstacksize ,
.Nm pthread_attr_getstackaddr ,
.Nm pthread_attr_setstackaddr
.Nd get and set thread stack attributes
.Sh LIBRARY
.Lb libpthread
.Sh SYNOPSIS
.In pthread.h
.Ft int
.Fn pthread_attr_getstack \
"const pthread_attr_t * restrict attr" \
"void ** restrict stackaddr" "size_t * restrict stacksize"
.Ft int
.Fn pthread_attr_setstack \
"pthread_attr_t * restrict attr" "void *stackaddr" "size_t stacksize"
.Ft int
.Fn pthread_attr_getstacksize \
"const pthread_attr_t * restrict attr" "size_t * restrict stacksize"
.Ft int
.Fn pthread_attr_setstacksize \
"pthread_attr_t *attr" "size_t stacksize"
.Ft int
.Fn pthread_attr_getstackaddr \
"const pthread_attr_t * restrict attr" "void ** restrict stackaddr"
.Ft int
.Fn pthread_attr_setstackaddr \
"pthread_attr_t *attr" "void *stackaddr"
.Sh DESCRIPTION
The
.Fn pthread_attr_getstack
and
.Fn pthread_attr_setstack
functions get and set, respectively, the thread stack attributes
.Fa stackaddr
and
.Fa stacksize
in the
.Fa attr
object.
The remaining four functions behave similarly,
but instead of getting or setting both
.Fa stackaddr
and
.Fa stacksize ,
these get and set the values individually.
.Pp
The
.Fa stacksize
parameter is defined to be the minimum stack size (in bytes)
allocated for the thread's stack during the creation of the thread.
The
.Fa stackaddr
attribute specifies the location of storage to be used for the thread's stack.
All pages within the stack described by
.Fa stackaddr
and
.Fa stacksize
should be both readable and writable by the thread.
.Pp
The behavior is undefined in all functions if the
.Fa attr
parameter does not refer to an attribute object initialized by using
.Xr pthread_attr_init 3
prior to the call.
In addition, undefined behavior may follow if the
.Fn pthread_attr_getstack
function is called before the
.Fa stackaddr
attribute has been set.
.Ss Rationale
The rationale behind these functions is to address cases where an application
may be used in an environment where the stack of a thread must be placed to
some particular region of memory.
For the majority of applications, this is seldom necessary,
and the use of these functions should be generally avoided.
At least few potential caveats can be mentioned.
.Bl -bullet -offset 2n
.It
There is a certain degree of ambiguity in the POSIX
standard with respect to thread stack.
.It
The exact behavior of the functions may vary
both across machines and operating systems.
In particular, the address specified by
.Fa stackaddr
should be suitably aligned.
The system page size, as specified by
.Xr sysconf 3 ,
and the use of
.Xr posix_memalign 3
may guarantee some degree of portability.
Also
.Xr mmap 2
provides means for alignment.
.It
If the application modifies the stack address, it claims also
the responsibility of allocating the stack area and guarding it against
possible stack overflow.
No default guard area will be allocated (see
.Xr pthread_attr_getguardsize 3 ) .
It may be necessary to manually use
.Xr mprotect 2
in order to define a guard area at the end of the allocated stack.
.It
Moreover, if
.Fa attr
is used to create multiple threads, the stack address must be changed
by the application between successive calls to
.Xr pthread_create 3 .
.El
.Sh RETURN VALUES
If successful, these functions return 0.
Otherwise, an error number is returned to indicate the error.
.Sh ERRORS
No errors are defined for the three functions that obtain the stack values.
The three functions that set the stack values may fail if:
.Bl -tag -width Er
.It Bq Er ENOMEM
There was insufficient memory to complete the operation.
.El
.Pp
The
.Fn pthread_attr_setstacksize
function may additionally fail if:
.Bl -tag -width Er
.It Bq Er EINVAL
The specified
.Fa stacksize
is less than
.Dv PTHREAD_STACK_MIN
or exceeds some system-imposed limit.
.El
.Sh SEE ALSO
.Xr pthread_attr 3 ,
.Xr pthread_attr_setguardsize 3
.Sh STANDARDS
All described functions conform to
.St -p1003.1-2001 .
Note that
.Fn pthread_attr_getstackaddr
and
.Fn pthread_attr_setstackaddr
were however removed from the specification in the
.St -p1003.1-2008
revision.
.Sh BUGS
Older versions of
.Nx ,
prior to 10.0, 9.4, and 8.3, incorrectly adjust the stack address by
the guard size in threads configured with
.Fn pthread_attr_setstack ,
instead of ignoring the guard size in that case as
.Tn POSIX
prescribes
.Po
see
.Lk https://gnats.NetBSD.org/57721 "PR lib/57721"
.Pc .
.Pp
Even if you didn't set a nonzero guard size with
.Xr pthread_attr_setguardsize 3 ,
the system will choose a nonzero default guard size.
.Pp
To work around this in applications that run on older and newer
versions of
.Nx ,
as well as on other operating systems, you can safely set the guard
size to zero:
.Dl "pthread_attr_setguardsize(&attr, 0);"