xref: /illumos-gate/usr/src/man/man3c/pthread_attr_get_np.3c (revision ecd18decdfb3e65c221849c385545953b84f1301)
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 December 2, 2023
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 pthread_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
103function returns
104.Sy 0 .
105Otherwise, an error number is returned to indicate the error.
106.Sh EXAMPLES
107The following program demonstrates how to use this function to get
108the location and stack size of a newly created thread.
109.Bd -literal
110#include <assert.h>
111#include <errno.h>
112#include <pthread.h>
113#include <stdio.h>
114#include <stdlib.h>
115#include <string.h>
116
117static pthread_t g_thr;
118
119void *
120print_stackinfo(void *arg)
121{
122	int ret;
123	pthread_attr_t attr;
124	pthread_t *thrp = arg;
125	void *stk;
126	size_t stksize;
127
128	if (pthread_attr_init(&attr) != 0) {
129		fprintf(stderr, "failed to init attr: %s\en",
130		    strerror(errno));
131		exit(1);
132	}
133
134	if (pthread_attr_get_np(*thrp, &attr) != 0) {
135		fprintf(stderr, "failed to get thread attributes: %s\en",
136		    strerror(errno));
137		exit(1);
138	}
139
140	ret = pthread_attr_getstackaddr(&attr, &stk);
141	assert(ret == 0);
142	ret = pthread_attr_getstacksize(&attr, &stksize);
143	assert(ret == 0);
144	(void) printf("stack base is at %p, it is %d bytes large\en",
145	    stk, stksize);
146	return (NULL);
147}
148
149int
150main(void)
151{
152	int ret;
153
154	if ((ret = pthread_create(&g_thr, NULL, print_stackinfo,
155	    &g_thr) != 0)) {
156		fprintf(stderr, "failed to create a thread: %s\en",
157		    strerror(errno));
158		exit(1);
159	}
160
161	pthread_join(g_thr, NULL);
162	return (0);
163}
164.Ed
165.Sh ERRORS
166The
167.Fn pthread_attr_get_np
168function will fail if:
169.Bl -tag -width Er
170.It Er EINVAL
171The pthread_attr_t object
172.Fa attr
173was not properly initialized with a call to
174.Xr pthread_attr_init 3C .
175.It Er ESRCH
176No thread could be found corresponding to the specified thread ID,
177.Fa thread .
178.El
179.Sh INTERFACE STABILITY
180.Sy Committed
181.Sh MT-LEVEL
182.Sy MT-Safe
183.Sh SEE ALSO
184.Xr pthread_attr_destroy 3C ,
185.Xr pthread_attr_getdetachstate 3C ,
186.Xr pthread_attr_getguardsize 3C ,
187.Xr pthread_attr_getinheritsched 3C ,
188.Xr pthread_attr_getschedparam 3C ,
189.Xr pthread_attr_getschedpolicy 3C ,
190.Xr pthread_attr_getscope 3C ,
191.Xr pthread_attr_getstackaddr 3C ,
192.Xr pthread_attr_getstacksize 3C ,
193.Xr pthread_attr_init 3C ,
194.Xr pthread_create 3C ,
195.Xr pthread_detach 3C ,
196.Xr pthread_getschedparam 3C ,
197.Xr pthread_setschedparam 3C ,
198.Xr attributes 7 ,
199.Xr threads 7
200