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