xref: /illumos-gate/usr/src/man/man3c/pthread_attr_get_np.3c (revision 6e6545bfaed3bab9ce836ee82d1abd8f2edba89a)
1.\"
2.\" This file and its contents are supplied under the terms of the
3.\" Common Development and Distribution License ("CDDL"), version 1.0.
4.\" You may only use this file in accordance with the terms of version
5.\" 1.0 of the CDDL.
6.\"
7.\" A full copy of the text of the CDDL should have accompanied this
8.\" source.  A copy of the CDDL is also available via the Internet at
9.\" http://www.illumos.org/license/CDDL.
10.\"
11.\"
12.\" Copyright 2016 Joyent, Inc.
13.\"
14.Dd Feb 07, 2015
15.Dt PTHREAD_ATTR_GET_NP 3C
16.Os
17.Sh NAME
18.Nm pthread_attr_get_np
19.Nd get pthread attributes of a running thread
20.Sh SYNOPSIS
21.In pthread.h
22.Ft int
23.Fo pthread_attr_get_np
24.Fa "pthread_t thread"
25.Fa "pthread_attr_t *attr"
26.Fc
27.Sh DESCRIPTION
28The
29.Fn pthread_attr_get_np
30function provides a way to get the attributes of the thread
31.Fa thread
32after it has been created.
33This function is most commonly used to obtain the actual location and size of a
34thread's stack.
35.Pp
36The attributes pointer,
37.Fa attr ,
38will be filled in with the current attributes for the thread.
39The attributes should be allocated by a call to
40.Xr pthread_attr_init 3C
41prior to calling the
42.Fn pthrad_attr_get_np
43function.
44When
45.Fa attr
46is done being used, it should be destroyed through a call to
47.Xr pthread_attr_destroy 3C .
48.Pp
49The attributes of the thread
50.Fa thread
51will be the same as those passed in at the time
52.Xr pthread_create 3C
53was called (or the default set if none were specified), except that the
54following values will be updated:
55.Bl -tag -width Sy
56.It Sy Thread Stack Size
57If no explicit stack size was specified, then
58.Fa attr
59will contain the actual size of the stack.
60.Pp
61If the size of the stack was specified, then it may have been changed to
62ensure that the required alignment of the platform is satisfied.
63.It Sy The Stack Address
64If no stack address was specified, then
65.Fa attr
66will contain the actual address of the stack that the system allocated
67for the thread.
68.It Sy Thread Detach State
69The detach state, whether or not the thread may be joined by a call to
70.Xr pthread_join 3C ,
71may have changed since the process was created due to a call to
72.Xr pthread_detach 3C .
73.Fa attr
74will reflect the current setting of
75.Fa thread .
76.It Sy Thread Scheduling Parameter
77The scheduling parameter attribute will be updated with the current
78scheduling parameter of
79.Fa thread .
80This is the same information as available through
81.Xr pthread_getschedparam 3C
82and it is the preferred interface for obtaining that information.
83.It Sy Thread Scheduling Policy
84The scheduling policy attribute of
85.Fa attr
86will be updated with the current scheduling policy being applied to the
87thread.
88This may have changed, for example, due to a call to
89.Xr pthread_setschedparam 3C .
90As with the thread's scheduling parameter, the preferred interface for
91obtaining this information is by using
92.Xr pthread_getschedparam 3C .
93.It Sy Thread Guard Size
94The value of the guard size attribute for the thread will be updated to
95reflect the actual size of the guard installed for
96.Fa thread .
97For more information on the guard size of a thread and its purpose, see
98.Xr pthread_attr_getguardsize 3C .
99.El
100.Sh RETURN VALUES
101Upon successful completion, the
102.Fn pthread_attr_get_np
103and
104.Fn pthread_getattr_np
105functions return
106.Sy 0 .
107Otherwise, an error number is returned to indicate the error.
108.Sh EXAMPLES
109The following program demonstrates how to use these functions to get
110the location and stack size of a newly created thread.
111.Bd -literal
112#include <assert.h>
113#include <errno.h>
114#include <pthread.h>
115#include <stdio.h>
116#include <stdlib.h>
117#include <string.h>
118
119static pthread_t g_thr;
120
121void *
122print_stackinfo(void *arg)
123{
124	int ret;
125	pthread_attr_t attr;
126	pthread_t *thrp = arg;
127	void *stk;
128	size_t stksize;
129
130	if (pthread_attr_init(&attr) != 0) {
131		fprintf(stderr, "failed to init attr: %s\\n",
132		    strerror(errno));
133		exit(1);
134	}
135
136	if (pthread_attr_get_np(*thrp, &attr) != 0) {
137		fprintf(stderr, "failed to get thread attributes: %s\\n",
138		    strerror(errno));
139		exit(1);
140	}
141
142	ret = pthread_attr_getstackaddr(&attr, &stk);
143	assert(ret == 0);
144	ret = pthread_attr_getstacksize(&attr, &stksize);
145	assert(ret == 0);
146	(void) printf("stack base is at %p, it is %d bytes large\\n",
147	    stk, stksize);
148	return (NULL);
149}
150
151int
152main(void)
153{
154	int ret;
155
156	if ((ret = pthread_create(&g_thr, NULL, print_stackinfo,
157	    &g_thr) != 0)) {
158		fprintf(stderr, "failed to create a thread: %s\\n",
159		    strerror(errno));
160		exit(1);
161	}
162
163	pthread_join(g_thr, NULL);
164	return (0);
165}
166.Ed
167.Sh ERRORS
168The
169.Fn pthread_attr_get_np
170function will fail if:
171.Bl -tag -width Er
172.It Er EINVAL
173The pthread_attr_t object
174.Fa attr
175was not properly initialized with a call to
176.Xr pthread_attr_init 3C .
177.It Er ESRCH
178No thread could be found corresponding to the specified thread ID,
179.Fa thread .
180.El
181.Sh INTERFACE STABILITY
182.Sy Committed
183.Sh MT-LEVEL
184.Sy MT-Safe
185.Sh SEE ALSO
186.Xr pthread_attr_destroy 3C ,
187.Xr pthread_attr_getdetachstate 3C ,
188.Xr pthread_attr_getguardsize 3C ,
189.Xr pthread_attr_getinheritsched 3C ,
190.Xr pthread_attr_getschedparam 3C ,
191.Xr pthread_attr_getschedpolicy 3C ,
192.Xr pthread_attr_getscope 3C ,
193.Xr pthread_attr_getstackaddr 3C ,
194.Xr pthread_attr_getstacksize 3C ,
195.Xr pthread_attr_init 3C ,
196.Xr pthread_create 3C ,
197.Xr pthread_detach 3C ,
198.Xr pthread_getschedparam 3C ,
199.Xr pthread_setschedparam 3C ,
200.Xr attributes 5 ,
201.Xr threads 5
202