1 | /* |
---|
2 | * $Id$ |
---|
3 | */ |
---|
4 | |
---|
5 | #include <errno.h> |
---|
6 | #include <limits.h> |
---|
7 | #include <pthread.h> |
---|
8 | #include <string.h> |
---|
9 | |
---|
10 | #include <rtems/system.h> |
---|
11 | #include <rtems/score/thread.h> |
---|
12 | #include <rtems/score/wkspace.h> |
---|
13 | #include <rtems/posix/key.h> |
---|
14 | |
---|
15 | /*PAGE |
---|
16 | * |
---|
17 | * 17.1.1 Thread-Specific Data Key Create, P1003.1c/Draft 10, p. 163 |
---|
18 | */ |
---|
19 | |
---|
20 | int pthread_key_create( |
---|
21 | pthread_key_t *key, |
---|
22 | void (*destructor)( void * ) |
---|
23 | ) |
---|
24 | { |
---|
25 | POSIX_Keys_Control *the_key; |
---|
26 | void *table; |
---|
27 | unsigned32 the_class; |
---|
28 | unsigned32 bytes_to_allocate; |
---|
29 | |
---|
30 | |
---|
31 | _Thread_Disable_dispatch(); |
---|
32 | |
---|
33 | the_key = _POSIX_Keys_Allocate(); |
---|
34 | |
---|
35 | if ( !the_key ) { |
---|
36 | _Thread_Enable_dispatch(); |
---|
37 | return EAGAIN; |
---|
38 | } |
---|
39 | |
---|
40 | the_key->destructor = destructor; |
---|
41 | |
---|
42 | for ( the_class = OBJECTS_CLASSES_FIRST_THREAD_CLASS; |
---|
43 | the_class <= OBJECTS_CLASSES_LAST_THREAD_CLASS; |
---|
44 | the_class++ ) { |
---|
45 | |
---|
46 | bytes_to_allocate = |
---|
47 | (_Objects_Information_table[ the_class ]->maximum + 1) * sizeof( void * ); |
---|
48 | |
---|
49 | table = _Workspace_Allocate( bytes_to_allocate ); |
---|
50 | |
---|
51 | if ( !table ) { |
---|
52 | for ( --the_class; |
---|
53 | the_class >= OBJECTS_CLASSES_FIRST_THREAD_CLASS; |
---|
54 | the_class-- ) |
---|
55 | _Workspace_Free( the_key->Values[ the_class ] ); |
---|
56 | |
---|
57 | _POSIX_Keys_Free( the_key ); |
---|
58 | _Thread_Enable_dispatch(); |
---|
59 | return ENOMEM; |
---|
60 | } |
---|
61 | |
---|
62 | the_key->Values[ the_class ] = table; |
---|
63 | memset( table, '\0', bytes_to_allocate ); |
---|
64 | } |
---|
65 | |
---|
66 | the_key->is_active = TRUE; |
---|
67 | |
---|
68 | _Objects_Open( &_POSIX_Keys_Information, &the_key->Object, 0 ); |
---|
69 | |
---|
70 | *key = the_key->Object.id; |
---|
71 | |
---|
72 | _Thread_Enable_dispatch(); |
---|
73 | |
---|
74 | return 0; |
---|
75 | } |
---|