1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2007. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.com/license/LICENSE. |
---|
8 | * |
---|
9 | * $Id$ |
---|
10 | */ |
---|
11 | |
---|
12 | #if HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <errno.h> |
---|
17 | #include <limits.h> |
---|
18 | #include <pthread.h> |
---|
19 | #include <string.h> |
---|
20 | |
---|
21 | #include <rtems/system.h> |
---|
22 | #include <rtems/score/thread.h> |
---|
23 | #include <rtems/score/wkspace.h> |
---|
24 | #include <rtems/posix/key.h> |
---|
25 | |
---|
26 | /*PAGE |
---|
27 | * |
---|
28 | * 17.1.1 Thread-Specific Data Key Create, P1003.1c/Draft 10, p. 163 |
---|
29 | */ |
---|
30 | |
---|
31 | int pthread_key_create( |
---|
32 | pthread_key_t *key, |
---|
33 | void (*destructor)( void * ) |
---|
34 | ) |
---|
35 | { |
---|
36 | POSIX_Keys_Control *the_key; |
---|
37 | void *table; |
---|
38 | uint32_t the_api; |
---|
39 | uint32_t bytes_to_allocate; |
---|
40 | |
---|
41 | |
---|
42 | _Thread_Disable_dispatch(); |
---|
43 | |
---|
44 | the_key = _POSIX_Keys_Allocate(); |
---|
45 | |
---|
46 | if ( !the_key ) { |
---|
47 | _Thread_Enable_dispatch(); |
---|
48 | return EAGAIN; |
---|
49 | } |
---|
50 | |
---|
51 | the_key->destructor = destructor; |
---|
52 | |
---|
53 | /* |
---|
54 | * This is a bit more complex than one might initially expect because |
---|
55 | * APIs are optional. Thus there may be no ITRON tasks to have keys |
---|
56 | * for. [NOTE: Currently RTEMS Classic API tasks are always enabled.] |
---|
57 | */ |
---|
58 | |
---|
59 | for ( the_api = 1; |
---|
60 | the_api <= OBJECTS_APIS_LAST; |
---|
61 | the_api++ ) { |
---|
62 | |
---|
63 | if ( _Objects_Information_table[ the_api ] ) { |
---|
64 | #if defined(RTEMS_DEBUG) |
---|
65 | /* |
---|
66 | * Currently all managers are installed if the API is installed. |
---|
67 | * This would be a horrible implementation error. |
---|
68 | */ |
---|
69 | if (_Objects_Information_table[ the_api ][ 1 ] == NULL ) |
---|
70 | _Internal_error_Occurred( |
---|
71 | INTERNAL_ERROR_CORE, |
---|
72 | true, |
---|
73 | INTERNAL_ERROR_IMPLEMENTATION_KEY_CREATE_INCONSISTENCY |
---|
74 | ); |
---|
75 | #endif |
---|
76 | bytes_to_allocate = sizeof( void * ) * |
---|
77 | (_Objects_Information_table[ the_api ][ 1 ]->maximum + 1); |
---|
78 | table = _Workspace_Allocate( bytes_to_allocate ); |
---|
79 | if ( !table ) { |
---|
80 | for ( --the_api; |
---|
81 | the_api >= 1; |
---|
82 | the_api-- ) |
---|
83 | _Workspace_Free( the_key->Values[ the_api ] ); |
---|
84 | |
---|
85 | _POSIX_Keys_Free( the_key ); |
---|
86 | _Thread_Enable_dispatch(); |
---|
87 | return ENOMEM; |
---|
88 | } |
---|
89 | |
---|
90 | the_key->Values[ the_api ] = table; |
---|
91 | memset( table, '\0', bytes_to_allocate ); |
---|
92 | } else { |
---|
93 | the_key->Values[ the_api ] = NULL; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | } |
---|
98 | |
---|
99 | _Objects_Open_u32( &_POSIX_Keys_Information, &the_key->Object, 0 ); |
---|
100 | |
---|
101 | *key = the_key->Object.id; |
---|
102 | |
---|
103 | _Thread_Enable_dispatch(); |
---|
104 | |
---|
105 | return 0; |
---|
106 | } |
---|