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