1 | /* pthread.c |
---|
2 | * |
---|
3 | */ |
---|
4 | |
---|
5 | #include <errno.h> |
---|
6 | #include <pthread.h> |
---|
7 | #include <limits.h> |
---|
8 | |
---|
9 | #include <rtems/score/stack.h> |
---|
10 | #include <rtems/score/thread.h> |
---|
11 | #include <rtems/posix/pthread.h> |
---|
12 | |
---|
13 | /*PAGE |
---|
14 | * |
---|
15 | * The default pthreads attributes structure. |
---|
16 | */ |
---|
17 | |
---|
18 | const pthread_attr_t _POSIX_Threads_Default_attributes = { |
---|
19 | TRUE, /* is_initialized */ |
---|
20 | 0, /* stackaddr */ |
---|
21 | STACK_MINIMUM_SIZE, /* stacksize */ |
---|
22 | PTHREAD_SCOPE_PROCESS, /* contentionscope */ |
---|
23 | PTHREAD_INHERIT_SCHED, /* inheritsched */ |
---|
24 | SCHED_FIFO, /* schedpolicy */ |
---|
25 | { /* schedparam */ |
---|
26 | 128, /* sched_priority */ |
---|
27 | 0, /* ss_low_priority */ |
---|
28 | { 0L, 0 }, /* ss_replenish_period */ |
---|
29 | { 0L, 0 } /* ss_initial_budget */ |
---|
30 | }, |
---|
31 | PTHREAD_CREATE_DETACHED, /* detachstate */ |
---|
32 | 1 /* cputime_clock_allowed */ |
---|
33 | }; |
---|
34 | |
---|
35 | /*PAGE |
---|
36 | * |
---|
37 | * _POSIX_Threads_Manager_initialization |
---|
38 | * |
---|
39 | * This routine initializes all threads manager related data structures. |
---|
40 | * |
---|
41 | * Input parameters: |
---|
42 | * maximum_pthreads - maximum configured pthreads |
---|
43 | * |
---|
44 | * Output parameters: NONE |
---|
45 | */ |
---|
46 | |
---|
47 | void _POSIX_Threads_Manager_initialization( |
---|
48 | unsigned32 maximum_pthreads |
---|
49 | ) |
---|
50 | { |
---|
51 | _Objects_Initialize_information( |
---|
52 | &_POSIX_Threads_Information, |
---|
53 | OBJECTS_POSIX_THREADS, |
---|
54 | TRUE, |
---|
55 | maximum_pthreads, |
---|
56 | sizeof( POSIX_Threads_Control ), |
---|
57 | TRUE, |
---|
58 | _POSIX_PATH_MAX, |
---|
59 | TRUE |
---|
60 | ); |
---|
61 | } |
---|
62 | |
---|
63 | #ifdef NOT_IMPLEMENTED_YET |
---|
64 | |
---|
65 | /*PAGE |
---|
66 | * |
---|
67 | * 3.1.3 Register Fork Handlers, P1003.1c/Draft 10, P1003.1c/Draft 10, p. 27 |
---|
68 | */ |
---|
69 | |
---|
70 | int pthread_atfork( |
---|
71 | void (*prepare)(void), |
---|
72 | void (*parent)(void), |
---|
73 | void (*child)(void) |
---|
74 | ) |
---|
75 | { |
---|
76 | return POSIX_NOT_IMPLEMENTED(); |
---|
77 | } |
---|
78 | |
---|
79 | #endif |
---|
80 | |
---|
81 | /*PAGE |
---|
82 | * |
---|
83 | * 13.5.1 Thread Creation Scheduling Attributes, P1003.1c/Draft 10, p. 120 |
---|
84 | */ |
---|
85 | |
---|
86 | int pthread_attr_setscope( |
---|
87 | pthread_attr_t *attr, |
---|
88 | int contentionscope |
---|
89 | ) |
---|
90 | { |
---|
91 | if ( !attr || !attr->is_initialized ) |
---|
92 | return EINVAL; |
---|
93 | |
---|
94 | attr->contentionscope = contentionscope; |
---|
95 | return 0; |
---|
96 | } |
---|
97 | |
---|
98 | /*PAGE |
---|
99 | * |
---|
100 | * 13.5.1 Thread Creation Scheduling Attributes, P1003.1c/Draft 10, p. 120 |
---|
101 | */ |
---|
102 | |
---|
103 | int pthread_attr_getscope( |
---|
104 | const pthread_attr_t *attr, |
---|
105 | int *contentionscope |
---|
106 | ) |
---|
107 | { |
---|
108 | if ( !attr || !attr->is_initialized ) |
---|
109 | return EINVAL; |
---|
110 | |
---|
111 | *contentionscope = attr->contentionscope; |
---|
112 | return 0; |
---|
113 | } |
---|
114 | |
---|
115 | /*PAGE |
---|
116 | * |
---|
117 | * 13.5.1 Thread Creation Scheduling Attributes, P1003.1c/Draft 10, p. 120 |
---|
118 | */ |
---|
119 | |
---|
120 | int pthread_attr_setinheritsched( |
---|
121 | pthread_attr_t *attr, |
---|
122 | int inheritsched |
---|
123 | ) |
---|
124 | { |
---|
125 | if ( !attr || !attr->is_initialized ) |
---|
126 | return EINVAL; |
---|
127 | |
---|
128 | attr->inheritsched = inheritsched; |
---|
129 | return 0; |
---|
130 | } |
---|
131 | |
---|
132 | /*PAGE |
---|
133 | * |
---|
134 | * 13.5.1 Thread Creation Scheduling Attributes, P1003.1c/Draft 10, p. 120 |
---|
135 | */ |
---|
136 | |
---|
137 | int pthread_attr_getinheritsched( |
---|
138 | const pthread_attr_t *attr, |
---|
139 | int *inheritsched |
---|
140 | ) |
---|
141 | { |
---|
142 | if ( !attr || !attr->is_initialized ) |
---|
143 | return EINVAL; |
---|
144 | |
---|
145 | *inheritsched = attr->inheritsched; |
---|
146 | return 0; |
---|
147 | } |
---|
148 | |
---|
149 | /*PAGE |
---|
150 | * |
---|
151 | * 13.5.1 Thread Creation Scheduling Attributes, P1003.1c/Draft 10, p. 120 |
---|
152 | */ |
---|
153 | |
---|
154 | int pthread_attr_setschedpolicy( |
---|
155 | pthread_attr_t *attr, |
---|
156 | int policy |
---|
157 | ) |
---|
158 | { |
---|
159 | if ( !attr || !attr->is_initialized ) |
---|
160 | return EINVAL; |
---|
161 | |
---|
162 | attr->schedpolicy = policy; |
---|
163 | return 0; |
---|
164 | } |
---|
165 | |
---|
166 | /*PAGE |
---|
167 | * |
---|
168 | * 13.5.1 Thread Creation Scheduling Attributes, P1003.1c/Draft 10, p. 120 |
---|
169 | */ |
---|
170 | |
---|
171 | int pthread_attr_getschedpolicy( |
---|
172 | const pthread_attr_t *attr, |
---|
173 | int *policy |
---|
174 | ) |
---|
175 | { |
---|
176 | if ( !attr || !attr->is_initialized ) |
---|
177 | return EINVAL; |
---|
178 | |
---|
179 | *policy = attr->schedpolicy; |
---|
180 | return 0; |
---|
181 | } |
---|
182 | |
---|
183 | /*PAGE |
---|
184 | * |
---|
185 | * 13.5.1 Thread Creation Scheduling Attributes, P1003.1c/Draft 10, p. 120 |
---|
186 | */ |
---|
187 | |
---|
188 | int pthread_attr_setschedparam( |
---|
189 | pthread_attr_t *attr, |
---|
190 | const struct sched_param param |
---|
191 | ) |
---|
192 | { |
---|
193 | if ( !attr || !attr->is_initialized ) |
---|
194 | return EINVAL; |
---|
195 | |
---|
196 | attr->schedparam = param; |
---|
197 | return 0; |
---|
198 | } |
---|
199 | |
---|
200 | /*PAGE |
---|
201 | * |
---|
202 | * 13.5.1 Thread Creation Scheduling Attributes, P1003.1c/Draft 10, p. 120 |
---|
203 | */ |
---|
204 | |
---|
205 | int pthread_attr_getschedparam( |
---|
206 | const pthread_attr_t *attr, |
---|
207 | struct sched_param *param |
---|
208 | ) |
---|
209 | { |
---|
210 | if ( !attr || !attr->is_initialized ) |
---|
211 | return EINVAL; |
---|
212 | |
---|
213 | *param = attr->schedparam; |
---|
214 | return 0; |
---|
215 | } |
---|
216 | |
---|
217 | /*PAGE |
---|
218 | * |
---|
219 | * 13.5.2 Dynamic Thread Scheduling Parameters Access, |
---|
220 | * P1003.1c/Draft 10, p. 124 |
---|
221 | */ |
---|
222 | |
---|
223 | int pthread_getschedparam( |
---|
224 | pthread_t thread, |
---|
225 | int *policy, |
---|
226 | struct sched_param *param |
---|
227 | ) |
---|
228 | { |
---|
229 | pthread_attr_t *attr; /* XXX: really need to get this from the thread */ |
---|
230 | |
---|
231 | if ( !policy || !param ) |
---|
232 | return EINVAL; |
---|
233 | |
---|
234 | *policy = attr->schedpolicy; |
---|
235 | *param = attr->schedparam; |
---|
236 | return 0; |
---|
237 | } |
---|
238 | |
---|
239 | /*PAGE |
---|
240 | * |
---|
241 | * 13.5.2 Dynamic Thread Scheduling Parameters Access, |
---|
242 | * P1003.1c/Draft 10, p. 124 |
---|
243 | */ |
---|
244 | |
---|
245 | int pthread_setschedparam( |
---|
246 | pthread_t thread, |
---|
247 | int policy, |
---|
248 | struct sched_param *param |
---|
249 | ) |
---|
250 | { |
---|
251 | /* XXX need to reschedule after doing this to the thread */ |
---|
252 | pthread_attr_t *attr; /* XXX: really need to get this from the thread */ |
---|
253 | |
---|
254 | if ( !param ) |
---|
255 | return EINVAL; |
---|
256 | |
---|
257 | attr->schedpolicy = policy; |
---|
258 | attr->schedparam = *param; |
---|
259 | return 0; |
---|
260 | } |
---|
261 | |
---|
262 | /*PAGE |
---|
263 | * |
---|
264 | * 16.1.1 Thread Creation Attributes, P1003.1c/Draft 10, p, 140 |
---|
265 | */ |
---|
266 | |
---|
267 | int pthread_attr_init( |
---|
268 | pthread_attr_t *attr |
---|
269 | ) |
---|
270 | { |
---|
271 | if ( !attr ) |
---|
272 | return EINVAL; |
---|
273 | |
---|
274 | *attr = _POSIX_Threads_Default_attributes; |
---|
275 | return 0; |
---|
276 | } |
---|
277 | |
---|
278 | /*PAGE |
---|
279 | * |
---|
280 | * 16.1.1 Thread Creation Attributes, P1003.1c/Draft 10, p, 140 |
---|
281 | */ |
---|
282 | |
---|
283 | int pthread_attr_destroy( |
---|
284 | pthread_attr_t *attr |
---|
285 | ) |
---|
286 | { |
---|
287 | if ( !attr || !attr->is_initialized ) |
---|
288 | return EINVAL; |
---|
289 | |
---|
290 | attr->is_initialized = FALSE; |
---|
291 | return 0; |
---|
292 | } |
---|
293 | |
---|
294 | /*PAGE |
---|
295 | * |
---|
296 | * 16.1.1 Thread Creation Attributes, P1003.1c/Draft 10, p, 140 |
---|
297 | */ |
---|
298 | |
---|
299 | int pthread_attr_getstacksize( |
---|
300 | const pthread_attr_t *attr, |
---|
301 | size_t *stacksize |
---|
302 | ) |
---|
303 | { |
---|
304 | if ( !attr || !attr->is_initialized ) |
---|
305 | return EINVAL; |
---|
306 | |
---|
307 | *stacksize = attr->stacksize; |
---|
308 | return 0; |
---|
309 | } |
---|
310 | |
---|
311 | /*PAGE |
---|
312 | * |
---|
313 | * 16.1.1 Thread Creation Attributes, P1003.1c/Draft 10, p, 140 |
---|
314 | */ |
---|
315 | |
---|
316 | int pthread_attr_setstacksize( |
---|
317 | pthread_attr_t *attr, |
---|
318 | size_t stacksize |
---|
319 | ) |
---|
320 | { |
---|
321 | if ( !attr || !attr->is_initialized ) |
---|
322 | return EINVAL; |
---|
323 | |
---|
324 | attr->stacksize = stacksize; |
---|
325 | return 0; |
---|
326 | } |
---|
327 | |
---|
328 | /*PAGE |
---|
329 | * |
---|
330 | * 16.1.1 Thread Creation Attributes, P1003.1c/Draft 10, p, 140 |
---|
331 | */ |
---|
332 | |
---|
333 | int pthread_attr_getstackaddr( |
---|
334 | const pthread_attr_t *attr, |
---|
335 | void **stackaddr |
---|
336 | ) |
---|
337 | { |
---|
338 | if ( !attr || !attr->is_initialized ) |
---|
339 | return EINVAL; |
---|
340 | |
---|
341 | *stackaddr = attr->stackaddr; |
---|
342 | return 0; |
---|
343 | } |
---|
344 | |
---|
345 | /*PAGE |
---|
346 | * |
---|
347 | * 16.1.1 Thread Creation Attributes, P1003.1c/Draft 10, p, 140 |
---|
348 | */ |
---|
349 | |
---|
350 | int pthread_attr_setstackaddr( |
---|
351 | pthread_attr_t *attr, |
---|
352 | void *stackaddr |
---|
353 | ) |
---|
354 | { |
---|
355 | if ( !attr || !attr->is_initialized ) |
---|
356 | return EINVAL; |
---|
357 | |
---|
358 | attr->stackaddr = stackaddr; |
---|
359 | return 0; |
---|
360 | } |
---|
361 | |
---|
362 | /*PAGE |
---|
363 | * |
---|
364 | * 16.1.1 Thread Creation Attributes, P1003.1c/Draft 10, p, 140 |
---|
365 | */ |
---|
366 | |
---|
367 | int pthread_attr_getdetachstate( |
---|
368 | const pthread_attr_t *attr, |
---|
369 | int *detachstate |
---|
370 | ) |
---|
371 | { |
---|
372 | if ( !attr || !attr->is_initialized ) |
---|
373 | return EINVAL; |
---|
374 | |
---|
375 | *detachstate = attr->detachstate; |
---|
376 | return 0; |
---|
377 | } |
---|
378 | |
---|
379 | /*PAGE |
---|
380 | * |
---|
381 | * 16.1.1 Thread Creation Attributes, P1003.1c/Draft 10, p, 140 |
---|
382 | */ |
---|
383 | |
---|
384 | int pthread_attr_setdetachstate( |
---|
385 | pthread_attr_t *attr, |
---|
386 | int detachstate |
---|
387 | ) |
---|
388 | { |
---|
389 | if ( !attr || !attr->is_initialized ) |
---|
390 | return EINVAL; |
---|
391 | |
---|
392 | attr->detachstate = detachstate; |
---|
393 | return 0; |
---|
394 | } |
---|
395 | |
---|
396 | #ifdef NOT_IMPLEMENTED_YET |
---|
397 | |
---|
398 | /*PAGE |
---|
399 | * |
---|
400 | * 16.1.2 Thread Creation, P1003.1c/Draft 10, p. 144 |
---|
401 | */ |
---|
402 | |
---|
403 | int pthread_create( |
---|
404 | pthread_t *thread, |
---|
405 | const pthread_attr_t *attr, |
---|
406 | void (*start_routine)( void * ), |
---|
407 | void *arg |
---|
408 | ) |
---|
409 | { |
---|
410 | return POSIX_NOT_IMPLEMENTED(); |
---|
411 | } |
---|
412 | |
---|
413 | /*PAGE |
---|
414 | * |
---|
415 | * 16.1.3 Wait for Thread Termination, P1003.1c/Draft 10, p. 147 |
---|
416 | */ |
---|
417 | |
---|
418 | int pthread_join( |
---|
419 | pthread_t thread, |
---|
420 | void **value_ptr |
---|
421 | ) |
---|
422 | { |
---|
423 | return POSIX_NOT_IMPLEMENTED(); |
---|
424 | } |
---|
425 | |
---|
426 | /*PAGE |
---|
427 | * |
---|
428 | * 16.1.4 Detaching a Thread, P1003.1c/Draft 10, p. 149 |
---|
429 | */ |
---|
430 | |
---|
431 | int pthread_detach( |
---|
432 | pthread_t thread |
---|
433 | ) |
---|
434 | { |
---|
435 | return POSIX_NOT_IMPLEMENTED(); |
---|
436 | } |
---|
437 | |
---|
438 | #endif |
---|
439 | |
---|
440 | /*PAGE |
---|
441 | * |
---|
442 | * 16.1.6 Get Calling Thread's ID, p1003.1c/Draft 10, p. XXX |
---|
443 | */ |
---|
444 | |
---|
445 | pthread_t pthread_self( void ) |
---|
446 | { |
---|
447 | return _Thread_Executing->Object.id; |
---|
448 | } |
---|
449 | |
---|
450 | /*PAGE |
---|
451 | * |
---|
452 | * 16.1.7 Compare Thread IDs, p1003.1c/Draft 10, p. 153 |
---|
453 | */ |
---|
454 | |
---|
455 | int pthread_equal( |
---|
456 | pthread_t t1, |
---|
457 | pthread_t t2 |
---|
458 | ) |
---|
459 | { |
---|
460 | #ifdef RTEMS_DEBUG |
---|
461 | /* XXX may want to do a "get" to make sure both are valid. */ |
---|
462 | /* XXX behavior is undefined if not valid pthread_t's */ |
---|
463 | #endif |
---|
464 | return _Objects_Are_ids_equal( t1, t1 ); |
---|
465 | } |
---|
466 | |
---|
467 | /*PAGE |
---|
468 | * |
---|
469 | * 16.1.8 Dynamic Package Initialization |
---|
470 | */ |
---|
471 | |
---|
472 | int pthread_once( |
---|
473 | pthread_once_t *once_control, |
---|
474 | void (*init_routine)(void) |
---|
475 | ) |
---|
476 | { |
---|
477 | /* XXX: Should we implement this routine this way or make it a full */ |
---|
478 | /* XXX: fledged object? */ |
---|
479 | |
---|
480 | if ( !once_control || !init_routine ) |
---|
481 | return EINVAL; |
---|
482 | |
---|
483 | _Thread_Disable_dispatch(); |
---|
484 | |
---|
485 | if ( !once_control->is_initialized ) { |
---|
486 | |
---|
487 | once_control->is_initialized = TRUE; |
---|
488 | once_control->init_executed = TRUE; |
---|
489 | (*init_routine)(); |
---|
490 | |
---|
491 | } if ( !once_control->init_executed ) { |
---|
492 | |
---|
493 | once_control->init_executed = TRUE; |
---|
494 | (*init_routine)(); |
---|
495 | |
---|
496 | } |
---|
497 | |
---|
498 | _Thread_Enable_dispatch(); |
---|
499 | |
---|
500 | return 0; |
---|
501 | } |
---|
502 | |
---|
503 | #ifdef NOT_IMPLEMENTED_YET |
---|
504 | |
---|
505 | /*PAGE |
---|
506 | * |
---|
507 | * 20.1.6 Accessing a Thread CPU-time Clock, P1003.4b/D8, p. 58 |
---|
508 | */ |
---|
509 | |
---|
510 | int pthread_getcpuclockid( |
---|
511 | pthread_t pid, |
---|
512 | clockid_t *clock_id |
---|
513 | ) |
---|
514 | { |
---|
515 | return POSIX_NOT_IMPLEMENTED(); |
---|
516 | } |
---|
517 | |
---|
518 | #endif |
---|
519 | |
---|
520 | /*PAGE |
---|
521 | * |
---|
522 | * 20.1.7 CPU-time Clock Thread Creation Attribute, P1003.4b/D8, p. 59 |
---|
523 | */ |
---|
524 | |
---|
525 | int pthread_attr_setcputime( |
---|
526 | pthread_attr_t *attr, |
---|
527 | int clock_allowed |
---|
528 | ) |
---|
529 | { |
---|
530 | if ( !attr || !attr->is_initialized ) |
---|
531 | return EINVAL; |
---|
532 | |
---|
533 | attr->cputime_clock_allowed = clock_allowed; |
---|
534 | return 0; |
---|
535 | } |
---|
536 | |
---|
537 | /*PAGE |
---|
538 | * |
---|
539 | * 20.1.7 CPU-time Clock Thread Creation Attribute, P1003.4b/D8, p. 59 |
---|
540 | */ |
---|
541 | |
---|
542 | int pthread_attr_getcputime( |
---|
543 | pthread_attr_t *attr, |
---|
544 | int *clock_allowed |
---|
545 | ) |
---|
546 | { |
---|
547 | if ( !attr || !attr->is_initialized ) |
---|
548 | return EINVAL; |
---|
549 | |
---|
550 | *clock_allowed = attr->cputime_clock_allowed; |
---|
551 | return 0; |
---|
552 | } |
---|