1 | /* |
---|
2 | * $Id$ |
---|
3 | */ |
---|
4 | |
---|
5 | #if HAVE_CONFIG_H |
---|
6 | #include "config.h" |
---|
7 | #endif |
---|
8 | |
---|
9 | #include <errno.h> |
---|
10 | #include <limits.h> |
---|
11 | #include <pthread.h> |
---|
12 | #include <string.h> |
---|
13 | |
---|
14 | #include <rtems/system.h> |
---|
15 | #include <rtems/score/thread.h> |
---|
16 | #include <rtems/score/wkspace.h> |
---|
17 | #include <rtems/posix/key.h> |
---|
18 | |
---|
19 | /*PAGE |
---|
20 | * |
---|
21 | * 17.1.1 Thread-Specific Data Key Create, P1003.1c/Draft 10, p. 163 |
---|
22 | */ |
---|
23 | |
---|
24 | int pthread_key_create( |
---|
25 | pthread_key_t *key, |
---|
26 | void (*destructor)( void * ) |
---|
27 | ) |
---|
28 | { |
---|
29 | POSIX_Keys_Control *the_key; |
---|
30 | void *table; |
---|
31 | unsigned32 the_class; |
---|
32 | unsigned32 bytes_to_allocate; |
---|
33 | |
---|
34 | |
---|
35 | _Thread_Disable_dispatch(); |
---|
36 | |
---|
37 | the_key = _POSIX_Keys_Allocate(); |
---|
38 | |
---|
39 | if ( !the_key ) { |
---|
40 | _Thread_Enable_dispatch(); |
---|
41 | return EAGAIN; |
---|
42 | } |
---|
43 | |
---|
44 | the_key->destructor = destructor; |
---|
45 | |
---|
46 | /* |
---|
47 | * This is a bit more complex than one might initially expect because |
---|
48 | * APIs are optional. Thus there may be no ITRON tasks to have keys |
---|
49 | * for. [NOTE: Currently RTEMS Classic API tasks are not always enabled.] |
---|
50 | */ |
---|
51 | |
---|
52 | for ( the_class = OBJECTS_CLASSES_FIRST_THREAD_CLASS; |
---|
53 | the_class <= OBJECTS_CLASSES_LAST_THREAD_CLASS; |
---|
54 | the_class++ ) { |
---|
55 | |
---|
56 | if ( _Objects_Information_table[ the_class ] ) { |
---|
57 | bytes_to_allocate = sizeof( void * ) * |
---|
58 | (_Objects_Information_table[ the_class ]->maximum + 1); |
---|
59 | table = _Workspace_Allocate( bytes_to_allocate ); |
---|
60 | if ( !table ) { |
---|
61 | for ( --the_class; |
---|
62 | the_class >= OBJECTS_CLASSES_FIRST_THREAD_CLASS; |
---|
63 | the_class-- ) |
---|
64 | _Workspace_Free( the_key->Values[ the_class ] ); |
---|
65 | |
---|
66 | _POSIX_Keys_Free( the_key ); |
---|
67 | _Thread_Enable_dispatch(); |
---|
68 | return ENOMEM; |
---|
69 | } |
---|
70 | |
---|
71 | the_key->Values[ the_class ] = table; |
---|
72 | memset( table, '\0', bytes_to_allocate ); |
---|
73 | } else { |
---|
74 | the_key->Values[ the_class ] = NULL; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | } |
---|
79 | |
---|
80 | the_key->is_active = TRUE; |
---|
81 | |
---|
82 | _Objects_Open( &_POSIX_Keys_Information, &the_key->Object, 0 ); |
---|
83 | |
---|
84 | *key = the_key->Object.id; |
---|
85 | |
---|
86 | _Thread_Enable_dispatch(); |
---|
87 | |
---|
88 | return 0; |
---|
89 | } |
---|