1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief POSIX Keys Manager Initialization |
---|
5 | * @ingroup POSIX_KEY Key |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * Copyright (c) 2012 Zhongwei Yao. |
---|
10 | * COPYRIGHT (c) 1989-2014. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * Copyright (c) 2016 embedded brains GmbH. |
---|
13 | * |
---|
14 | * The license and distribution terms for this file may be |
---|
15 | * found in the file LICENSE in this distribution or at |
---|
16 | * http://www.rtems.org/license/LICENSE. |
---|
17 | */ |
---|
18 | |
---|
19 | #if HAVE_CONFIG_H |
---|
20 | #include "config.h" |
---|
21 | #endif |
---|
22 | |
---|
23 | #include <rtems/config.h> |
---|
24 | #include <rtems/sysinit.h> |
---|
25 | |
---|
26 | #include <rtems/posix/keyimpl.h> |
---|
27 | #include <rtems/score/userextimpl.h> |
---|
28 | #include <rtems/score/wkspace.h> |
---|
29 | |
---|
30 | Objects_Information _POSIX_Keys_Information; |
---|
31 | |
---|
32 | Freechain_Control _POSIX_Keys_Keypool; |
---|
33 | |
---|
34 | static uint32_t _POSIX_Keys_Get_keypool_bump_count( void ) |
---|
35 | { |
---|
36 | uint32_t max = Configuration.maximum_key_value_pairs; |
---|
37 | |
---|
38 | return _Objects_Is_unlimited( max ) ? |
---|
39 | _Objects_Maximum_per_allocation( max ) : 0; |
---|
40 | } |
---|
41 | |
---|
42 | static uint32_t _POSIX_Keys_Get_initial_keypool_size( void ) |
---|
43 | { |
---|
44 | uint32_t max = Configuration.maximum_key_value_pairs; |
---|
45 | |
---|
46 | return _Objects_Maximum_per_allocation( max ); |
---|
47 | } |
---|
48 | |
---|
49 | static void _POSIX_Keys_Initialize_keypool( void ) |
---|
50 | { |
---|
51 | _Freechain_Initialize( |
---|
52 | &_POSIX_Keys_Keypool, |
---|
53 | _Workspace_Allocate_or_fatal_error, |
---|
54 | _POSIX_Keys_Get_initial_keypool_size(), |
---|
55 | sizeof( POSIX_Keys_Key_value_pair ) |
---|
56 | ); |
---|
57 | } |
---|
58 | |
---|
59 | POSIX_Keys_Key_value_pair * _POSIX_Keys_Key_value_allocate( void ) |
---|
60 | { |
---|
61 | return (POSIX_Keys_Key_value_pair *) _Freechain_Get( |
---|
62 | &_POSIX_Keys_Keypool, |
---|
63 | _Workspace_Allocate, |
---|
64 | _POSIX_Keys_Get_keypool_bump_count(), |
---|
65 | sizeof( POSIX_Keys_Key_value_pair ) |
---|
66 | ); |
---|
67 | } |
---|
68 | |
---|
69 | static void _POSIX_Keys_Run_destructors( Thread_Control *the_thread ) |
---|
70 | { |
---|
71 | while ( true ) { |
---|
72 | ISR_lock_Context lock_context; |
---|
73 | RBTree_Node *node; |
---|
74 | |
---|
75 | _Objects_Allocator_lock(); |
---|
76 | _POSIX_Keys_Key_value_acquire( the_thread, &lock_context ); |
---|
77 | |
---|
78 | node = _RBTree_Root( &the_thread->Keys.Key_value_pairs ); |
---|
79 | if ( node != NULL ) { |
---|
80 | POSIX_Keys_Key_value_pair *key_value_pair; |
---|
81 | pthread_key_t key; |
---|
82 | void *value; |
---|
83 | POSIX_Keys_Control *the_key; |
---|
84 | void ( *destructor )( void * ); |
---|
85 | |
---|
86 | key_value_pair = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( node ); |
---|
87 | key = key_value_pair->key; |
---|
88 | value = key_value_pair->value; |
---|
89 | _RBTree_Extract( |
---|
90 | &the_thread->Keys.Key_value_pairs, |
---|
91 | &key_value_pair->Lookup_node |
---|
92 | ); |
---|
93 | |
---|
94 | _POSIX_Keys_Key_value_release( the_thread, &lock_context ); |
---|
95 | _POSIX_Keys_Key_value_free( key_value_pair ); |
---|
96 | |
---|
97 | the_key = _POSIX_Keys_Get( key ); |
---|
98 | _Assert( the_key != NULL ); |
---|
99 | destructor = the_key->destructor; |
---|
100 | |
---|
101 | _Objects_Allocator_unlock(); |
---|
102 | |
---|
103 | if ( destructor != NULL && value != NULL ) { |
---|
104 | ( *destructor )( value ); |
---|
105 | } |
---|
106 | } else { |
---|
107 | _POSIX_Keys_Key_value_release( the_thread, &lock_context ); |
---|
108 | _Objects_Allocator_unlock(); |
---|
109 | break; |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | static void _POSIX_Keys_Restart_run_destructors( |
---|
115 | Thread_Control *executing, |
---|
116 | Thread_Control *the_thread |
---|
117 | ) |
---|
118 | { |
---|
119 | (void) executing; |
---|
120 | _POSIX_Keys_Run_destructors( the_thread ); |
---|
121 | } |
---|
122 | |
---|
123 | static User_extensions_Control _POSIX_Keys_Extensions = { |
---|
124 | .Callouts = { |
---|
125 | .thread_restart = _POSIX_Keys_Restart_run_destructors, |
---|
126 | .thread_terminate = _POSIX_Keys_Run_destructors |
---|
127 | } |
---|
128 | }; |
---|
129 | |
---|
130 | /** |
---|
131 | * @brief This routine performs the initialization necessary for this manager. |
---|
132 | */ |
---|
133 | static void _POSIX_Keys_Manager_initialization(void) |
---|
134 | { |
---|
135 | _Objects_Initialize_information( |
---|
136 | &_POSIX_Keys_Information, /* object information table */ |
---|
137 | OBJECTS_POSIX_API, /* object API */ |
---|
138 | OBJECTS_POSIX_KEYS, /* object class */ |
---|
139 | Configuration.maximum_keys, |
---|
140 | /* maximum objects of this class */ |
---|
141 | sizeof( POSIX_Keys_Control ), |
---|
142 | /* size of this object's control block */ |
---|
143 | true, /* true if names for this object are strings */ |
---|
144 | _POSIX_PATH_MAX, /* maximum length of each object's name */ |
---|
145 | NULL /* Proxy extraction support callout */ |
---|
146 | ); |
---|
147 | |
---|
148 | _POSIX_Keys_Initialize_keypool(); |
---|
149 | |
---|
150 | _User_extensions_Add_API_set( &_POSIX_Keys_Extensions ); |
---|
151 | } |
---|
152 | |
---|
153 | RTEMS_SYSINIT_ITEM( |
---|
154 | _POSIX_Keys_Manager_initialization, |
---|
155 | RTEMS_SYSINIT_POSIX_KEYS, |
---|
156 | RTEMS_SYSINIT_ORDER_MIDDLE |
---|
157 | ); |
---|