xref: /netbsd-src/lib/libpthread/pthread_attr_getstack.3 (revision 76c7fc5f6b13ed0b1508e6b313e88e59977ed78e)
1.\"	$NetBSD: pthread_attr_getstack.3,v 1.8 2017/10/23 01:03:23 wiz Exp $
2.\"
3.\" Copyright (c) 2010 Jukka Ruohonen <jruohonen@iki.fi>
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\"
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26.\" POSSIBILITY OF SUCH DAMAGE.
27.\"
28.Dd July 9, 2010
29.Dt PTHREAD_ATTR_GETSTACK 3
30.Os
31.Sh NAME
32.Nm pthread_attr_getstack ,
33.Nm pthread_attr_setstack ,
34.Nm pthread_attr_getstacksize ,
35.Nm pthread_attr_setstacksize ,
36.Nm pthread_attr_getstackaddr ,
37.Nm pthread_attr_setstackaddr
38.Nd get and set thread stack attributes
39.Sh LIBRARY
40.Lb libpthread
41.Sh SYNOPSIS
42.In pthread.h
43.Ft int
44.Fn pthread_attr_getstack \
45"const pthread_attr_t * restrict attr" \
46"void ** restrict stackaddr" "size_t * restrict stacksize"
47.Ft int
48.Fn pthread_attr_setstack \
49"pthread_attr_t * restrict attr" "void *stackaddr" "size_t stacksize"
50.Ft int
51.Fn pthread_attr_getstacksize \
52"const pthread_attr_t * restrict attr" "size_t * restrict stacksize"
53.Ft int
54.Fn pthread_attr_setstacksize \
55"pthread_attr_t *attr" "size_t stacksize"
56.Ft int
57.Fn pthread_attr_getstackaddr \
58"const pthread_attr_t * restrict attr" "void ** restrict stackaddr"
59.Ft int
60.Fn pthread_attr_setstackaddr \
61"pthread_attr_t *attr" "void *stackaddr"
62.Sh DESCRIPTION
63The
64.Fn pthread_attr_getstack
65and
66.Fn pthread_attr_setstack
67functions get and set, respectively, the thread stack attributes
68.Fa stackaddr
69and
70.Fa stacksize
71in the
72.Fa attr
73object.
74The remaining four functions behave similarly,
75but instead of getting or setting both
76.Fa stackaddr
77and
78.Fa stacksize ,
79these get and set the values individually.
80.Pp
81The
82.Fa stacksize
83parameter is defined to be the minimum stack size (in bytes)
84allocated for the thread's stack during the creation of the thread.
85The
86.Fa stackaddr
87attribute specifies the location of storage to be used for the thread's stack.
88All pages within the stack described by
89.Fa stackaddr
90and
91.Fa stacksize
92should be both readable and writable by the thread.
93.Pp
94The behavior is undefined in all functions if the
95.Fa attr
96parameter does not refer to an attribute object initialized by using
97.Xr pthread_attr_init 3
98prior to the call.
99In addition, undefined behavior may follow if the
100.Fn pthread_attr_getstack
101function is called before the
102.Fa stackaddr
103attribute has been set.
104.Ss Rationale
105The rationale behind these functions is to address cases where an application
106may be used in an environment where the stack of a thread must be placed to
107some particular region of memory.
108For the majority of applications, this is seldom necessary,
109and the use of these functions should be generally avoided.
110At least few potential caveats can be mentioned.
111.Bl -bullet -offset 2n
112.It
113There is a certain degree of ambiguity in the POSIX
114standard with respect to thread stack.
115.It
116The exact behavior of the functions may vary
117both across machines and operating systems.
118In particular, the address specified by
119.Fa stackaddr
120should be suitably aligned.
121The system page size, as specified by
122.Xr sysconf 3 ,
123and the use of
124.Xr posix_memalign 3
125may guarantee some degree of portability.
126Also
127.Xr mmap 2
128provides means for alignment.
129.It
130If the application modifies the stack address, it claims also
131the responsibility of allocating the stack area and guarding it against
132possible stack overflow.
133No default guard area will be allocated (see
134.Xr pthread_attr_getguardsize 3 ) .
135It may be necessary to manually use
136.Xr mprotect 2
137in order to define a guard area at the end of the allocated stack.
138.It
139Moreover, if
140.Fa attr
141is used to create multiple threads, the stack address must be changed
142by the application between successive calls to
143.Xr pthread_create 3 .
144.El
145.Sh RETURN VALUES
146If successful, these functions return 0.
147Otherwise, an error number is returned to indicate the error.
148.Sh ERRORS
149No errors are defined for the three functions that obtain the stack values.
150The three functions that set the stack values may fail if:
151.Bl -tag -width Er
152.It Bq Er ENOMEM
153There was insufficient memory to complete the operation.
154.El
155.Pp
156The
157.Fn pthread_attr_setstacksize
158function may additionally fail if:
159.Bl -tag -width Er
160.It Bq Er EINVAL
161The specified
162.Fa stacksize
163is less than
164.Dv PTHREAD_STACK_MIN
165or exceeds some system-imposed limit.
166.El
167.Sh SEE ALSO
168.Xr pthread_attr 3 ,
169.Xr pthread_attr_setguardsize 3
170.Sh STANDARDS
171All described functions conform to
172.St -p1003.1-2001 .
173Note that
174.Fn pthread_attr_getstackaddr
175and
176.Fn pthread_attr_setstackaddr
177were however removed from the specification in the
178.St -p1003.1-2008
179revision.
180