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