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