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 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.org/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #if HAVE_CONFIG_H |
---|
19 | #include "config.h" |
---|
20 | #endif |
---|
21 | |
---|
22 | #include <rtems/config.h> |
---|
23 | |
---|
24 | #include <rtems/posix/keyimpl.h> |
---|
25 | #include <rtems/score/chainimpl.h> |
---|
26 | #include <rtems/score/objectimpl.h> |
---|
27 | #include <rtems/score/wkspace.h> |
---|
28 | |
---|
29 | RBTREE_DEFINE_EMPTY( _POSIX_Keys_Key_value_lookup_tree ); |
---|
30 | |
---|
31 | /** |
---|
32 | * @brief This routine compares the rbtree node by comparing POSIX key first |
---|
33 | * and comparing thread id second. |
---|
34 | * |
---|
35 | * if either of the input nodes's thread_id member is 0, then it will only |
---|
36 | * compare the pthread_key_t member. That is when we pass thread_id = 0 node |
---|
37 | * as a search node, the search is done only by pthread_key_t. |
---|
38 | * |
---|
39 | * @param[in] node1 The node to be compared |
---|
40 | * @param[in] node2 The node to be compared |
---|
41 | * @retval positive if first node has higher key than second |
---|
42 | * @retval negative if lower |
---|
43 | * @retval 0 if equal,and for all the thread id is unique, then return 0 is |
---|
44 | * impossible |
---|
45 | */ |
---|
46 | |
---|
47 | RBTree_Compare_result _POSIX_Keys_Key_value_compare( |
---|
48 | const RBTree_Node *node1, |
---|
49 | const RBTree_Node *node2 |
---|
50 | ) |
---|
51 | { |
---|
52 | POSIX_Keys_Key_value_pair *n1; |
---|
53 | POSIX_Keys_Key_value_pair *n2; |
---|
54 | Objects_Id thread_id1, thread_id2; |
---|
55 | RBTree_Compare_result diff; |
---|
56 | |
---|
57 | n1 = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( node1 ); |
---|
58 | n2 = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( node2 ); |
---|
59 | |
---|
60 | diff = n1->key - n2->key; |
---|
61 | if ( diff ) |
---|
62 | return diff; |
---|
63 | |
---|
64 | thread_id1 = n1->thread_id; |
---|
65 | thread_id2 = n2->thread_id; |
---|
66 | |
---|
67 | /** |
---|
68 | * if thread_id1 or thread_id2 equals to 0, only key1 and key2 is valued. |
---|
69 | * it enables us search node only by pthread_key_t type key. |
---|
70 | */ |
---|
71 | if ( thread_id1 && thread_id2 ) |
---|
72 | return thread_id1 - thread_id2; |
---|
73 | return 0; |
---|
74 | } |
---|
75 | |
---|
76 | static uint32_t _POSIX_Keys_Get_keypool_bump_count( void ) |
---|
77 | { |
---|
78 | uint32_t max = Configuration.maximum_key_value_pairs; |
---|
79 | |
---|
80 | return _Objects_Is_unlimited( max ) ? |
---|
81 | _Objects_Maximum_per_allocation( max ) : 0; |
---|
82 | } |
---|
83 | |
---|
84 | static uint32_t _POSIX_Keys_Get_initial_keypool_size( void ) |
---|
85 | { |
---|
86 | uint32_t max = Configuration.maximum_key_value_pairs; |
---|
87 | |
---|
88 | return _Objects_Maximum_per_allocation( max ); |
---|
89 | } |
---|
90 | |
---|
91 | static bool _POSIX_Keys_Keypool_extend( Freechain_Control *keypool ) |
---|
92 | { |
---|
93 | size_t bump_count = _POSIX_Keys_Get_keypool_bump_count(); |
---|
94 | bool ok = bump_count > 0; |
---|
95 | |
---|
96 | if ( ok ) { |
---|
97 | size_t size = bump_count * sizeof( POSIX_Keys_Key_value_pair ); |
---|
98 | POSIX_Keys_Key_value_pair *nodes = _Workspace_Allocate( size ); |
---|
99 | |
---|
100 | ok = nodes != NULL; |
---|
101 | |
---|
102 | if ( ok ) { |
---|
103 | _Chain_Initialize( |
---|
104 | &keypool->Freechain, |
---|
105 | nodes, |
---|
106 | bump_count, |
---|
107 | sizeof( *nodes ) |
---|
108 | ); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | return ok; |
---|
113 | } |
---|
114 | |
---|
115 | static void _POSIX_Keys_Initialize_keypool( void ) |
---|
116 | { |
---|
117 | Freechain_Control *keypool = &_POSIX_Keys_Keypool; |
---|
118 | size_t initial_count = _POSIX_Keys_Get_initial_keypool_size(); |
---|
119 | |
---|
120 | _Freechain_Initialize( keypool, _POSIX_Keys_Keypool_extend ); |
---|
121 | |
---|
122 | if ( initial_count > 0 ) { |
---|
123 | size_t size = initial_count * sizeof( POSIX_Keys_Key_value_pair ); |
---|
124 | POSIX_Keys_Key_value_pair *nodes = |
---|
125 | _Workspace_Allocate_or_fatal_error( size ); |
---|
126 | |
---|
127 | _Chain_Initialize( |
---|
128 | &keypool->Freechain, |
---|
129 | nodes, |
---|
130 | initial_count, |
---|
131 | sizeof( *nodes ) |
---|
132 | ); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * @brief This routine performs the initialization necessary for this manager. |
---|
138 | */ |
---|
139 | void _POSIX_Key_Manager_initialization(void) |
---|
140 | { |
---|
141 | _Objects_Initialize_information( |
---|
142 | &_POSIX_Keys_Information, /* object information table */ |
---|
143 | OBJECTS_POSIX_API, /* object API */ |
---|
144 | OBJECTS_POSIX_KEYS, /* object class */ |
---|
145 | Configuration.maximum_keys, |
---|
146 | /* maximum objects of this class */ |
---|
147 | sizeof( POSIX_Keys_Control ), |
---|
148 | /* size of this object's control block */ |
---|
149 | true, /* true if names for this object are strings */ |
---|
150 | _POSIX_PATH_MAX /* maximum length of each object's name */ |
---|
151 | #if defined(RTEMS_MULTIPROCESSING) |
---|
152 | , |
---|
153 | false, /* true if this is a global object class */ |
---|
154 | NULL /* Proxy extraction support callout */ |
---|
155 | #endif |
---|
156 | ); |
---|
157 | |
---|
158 | _POSIX_Keys_Initialize_keypool(); |
---|
159 | } |
---|