xref: /haiku/src/system/libroot/posix/pthread/pthread_attr.c (revision a5a3b2d9a3d95cbae71eaf371708c73a1780ac0d)
1 /*
2  * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2008, Axel Dörfler, axeld@pinc-software.de.
4  * Copyright 2006, Jérôme Duval. All rights reserved.
5  * Distributed under the terms of the MIT License.
6  */
7 
8 
9 #include <pthread.h>
10 #include "pthread_private.h"
11 
12 #include <limits.h>
13 #include <stdlib.h>
14 
15 #include <Debug.h>
16 
17 #include <thread_defs.h>
18 
19 
20 int
21 pthread_attr_init(pthread_attr_t *_attr)
22 {
23 	pthread_attr *attr;
24 
25 	if (_attr == NULL)
26 		return B_BAD_VALUE;
27 
28 	attr = (pthread_attr *)malloc(sizeof(pthread_attr));
29 	if (attr == NULL)
30 		return B_NO_MEMORY;
31 
32 	attr->detach_state = PTHREAD_CREATE_JOINABLE;
33 	attr->sched_priority = B_NORMAL_PRIORITY;
34 	attr->stack_size = USER_STACK_SIZE;
35 	attr->guard_size = USER_STACK_GUARD_SIZE;
36 
37 	*_attr = attr;
38 	return B_OK;
39 }
40 
41 
42 int
43 pthread_attr_destroy(pthread_attr_t *_attr)
44 {
45 	pthread_attr *attr;
46 
47 	if (_attr == NULL || (attr = *_attr) == NULL)
48 		return B_BAD_VALUE;
49 
50 	*_attr = NULL;
51 	free(attr);
52 
53 	return B_OK;
54 }
55 
56 
57 int
58 pthread_attr_getdetachstate(const pthread_attr_t *_attr, int *state)
59 {
60 	pthread_attr *attr;
61 
62 	if (_attr == NULL || (attr = *_attr) == NULL || state == NULL)
63 		return B_BAD_VALUE;
64 
65 	*state = attr->detach_state;
66 
67 	return B_OK;
68 }
69 
70 
71 int
72 pthread_attr_setdetachstate(pthread_attr_t *_attr, int state)
73 {
74 	pthread_attr *attr;
75 
76 	if (_attr == NULL || (attr = *_attr) == NULL)
77 		return B_BAD_VALUE;
78 
79 	if (state != PTHREAD_CREATE_JOINABLE && state != PTHREAD_CREATE_DETACHED)
80 		return B_BAD_VALUE;
81 
82 	attr->detach_state = state;
83 
84 	return B_OK;
85 }
86 
87 
88 int
89 pthread_attr_getstacksize(const pthread_attr_t *_attr, size_t *stacksize)
90 {
91 	pthread_attr *attr;
92 
93 	if (_attr == NULL || (attr = *_attr) == NULL || stacksize == NULL)
94 		return B_BAD_VALUE;
95 
96 	*stacksize = attr->stack_size;
97 
98 	return 0;
99 }
100 
101 
102 int
103 pthread_attr_setstacksize(pthread_attr_t *_attr, size_t stacksize)
104 {
105 	pthread_attr *attr;
106 
107 	if (_attr == NULL || (attr = *_attr) == NULL)
108 		return B_BAD_VALUE;
109 
110 	STATIC_ASSERT(PTHREAD_STACK_MIN >= MIN_USER_STACK_SIZE
111 		&& PTHREAD_STACK_MIN <= MAX_USER_STACK_SIZE);
112 	if (stacksize < PTHREAD_STACK_MIN || stacksize > MAX_USER_STACK_SIZE)
113 		return B_BAD_VALUE;
114 
115 	attr->stack_size = stacksize;
116 
117 	return 0;
118 }
119 
120 
121 int
122 pthread_attr_getscope(const pthread_attr_t *attr, int *contentionScope)
123 {
124 	if (attr == NULL || contentionScope == NULL)
125 		return EINVAL;
126 
127 	*contentionScope = PTHREAD_SCOPE_SYSTEM;
128 	return 0;
129 }
130 
131 
132 int
133 pthread_attr_setscope(pthread_attr_t *attr, int contentionScope)
134 {
135 	if (attr == NULL)
136 		return EINVAL;
137 
138 	if (contentionScope != PTHREAD_SCOPE_SYSTEM)
139 		return ENOTSUP;
140 
141 	return 0;
142 }
143 
144 
145 int
146 pthread_attr_setschedparam(pthread_attr_t *attr,
147 	const struct sched_param *param)
148 {
149 	if (attr == NULL || param == NULL)
150 		return EINVAL;
151 
152 	(*attr)->sched_priority = param->sched_priority;
153 
154 	return 0;
155 }
156 
157 
158 int
159 pthread_attr_getschedparam(const pthread_attr_t *attr,
160 	struct sched_param *param)
161 {
162 	if (attr == NULL || param == NULL)
163 		return EINVAL;
164 
165 	param->sched_priority = (*attr)->sched_priority;
166 
167 	return 0;
168 }
169 
170 
171 int
172 pthread_attr_getguardsize(const pthread_attr_t *_attr, size_t *guardsize)
173 {
174 	pthread_attr *attr;
175 
176 	if (_attr == NULL || (attr = *_attr) == NULL || guardsize == NULL)
177 		return B_BAD_VALUE;
178 
179 	*guardsize = attr->guard_size;
180 
181 	return 0;
182 }
183 
184 
185 int
186 pthread_attr_setguardsize(pthread_attr_t *_attr, size_t guardsize)
187 {
188 	pthread_attr *attr;
189 
190 	if (_attr == NULL || (attr = *_attr) == NULL)
191 		return B_BAD_VALUE;
192 
193 	attr->guard_size = guardsize;
194 
195 	return 0;
196 }
197