source: rtems/cpukit/posix/src/key.c @ f68401e

4.115
Last change on this file since f68401e was e6c87f7, checked in by Joel Sherrill <joel.sherrill@…>, on 03/04/14 at 21:54:12

POSIX keys now enabled in all configurations.

Formerly POSIX keys were only enabled when POSIX threads
were enabled. Because they are a truly safe alternative
to per-task variables in an SMP system, they are being
enabled in all configurations.

  • Property mode set to 100644
File size: 4.3 KB
Line 
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.com/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/**
30 * @brief This routine compares the rbtree node by comparing POSIX key first
31 * and comparing thread id second.
32 *
33 * if either of the input nodes's thread_id member is 0, then it will only
34 * compare the pthread_key_t member. That is when we pass thread_id = 0 node
35 * as a search node, the search is done only by pthread_key_t.
36 *
37 * @param[in] node1 The node to be compared
38 * @param[in] node2 The node to be compared
39 * @retval positive if first node has higher key than second
40 * @retval negative if lower
41 * @retval 0 if equal,and for all the thread id is unique, then return 0 is
42 * impossible
43 */
44
45int _POSIX_Keys_Key_value_lookup_tree_compare_function(
46  const RBTree_Node *node1,
47  const RBTree_Node *node2
48)
49{
50  POSIX_Keys_Key_value_pair *n1;
51  POSIX_Keys_Key_value_pair *n2;
52  Objects_Id thread_id1, thread_id2;
53  int diff;
54
55  n1 = _RBTree_Container_of( node1, POSIX_Keys_Key_value_pair, Key_value_lookup_node );
56  n2 = _RBTree_Container_of( node2, POSIX_Keys_Key_value_pair, Key_value_lookup_node );
57
58  diff = n1->key - n2->key;
59  if ( diff )
60    return diff;
61
62  thread_id1 = n1->thread_id;
63  thread_id2 = n2->thread_id;
64
65  /**
66   * if thread_id1 or thread_id2 equals to 0, only key1 and key2 is valued.
67   * it enables us search node only by pthread_key_t type key.
68   */
69  if ( thread_id1 && thread_id2 )
70    return thread_id1 - thread_id2;
71  return 0;
72}
73
74static uint32_t _POSIX_Keys_Get_keypool_bump_count( void )
75{
76  uint32_t max = Configuration.maximum_key_value_pairs;
77
78  return _Objects_Is_unlimited( max ) ?
79    _Objects_Maximum_per_allocation( max ) : 0;
80}
81
82static uint32_t _POSIX_Keys_Get_initial_keypool_size( void )
83{
84  uint32_t max = Configuration.maximum_key_value_pairs;
85
86  return _Objects_Maximum_per_allocation( max );
87}
88
89static bool _POSIX_Keys_Keypool_extend( Freechain_Control *keypool )
90{
91  size_t bump_count = _POSIX_Keys_Get_keypool_bump_count();
92  bool ok = bump_count > 0;
93
94  if ( ok ) {
95    size_t size = bump_count * sizeof( POSIX_Keys_Key_value_pair );
96    POSIX_Keys_Key_value_pair *nodes = _Workspace_Allocate( size );
97
98    ok = nodes != NULL;
99
100    if ( ok ) {
101      _Chain_Initialize(
102        &keypool->Freechain,
103        nodes,
104        bump_count,
105        sizeof( *nodes )
106      );
107    }
108  }
109
110  return ok;
111}
112
113static void _POSIX_Keys_Initialize_keypool( void )
114{
115  Freechain_Control *keypool = &_POSIX_Keys_Keypool;
116  size_t initial_count = _POSIX_Keys_Get_initial_keypool_size();
117  size_t size = initial_count * sizeof( POSIX_Keys_Key_value_pair );
118  POSIX_Keys_Key_value_pair *nodes = _Workspace_Allocate_or_fatal_error( size );
119
120  _Freechain_Initialize( keypool, _POSIX_Keys_Keypool_extend );
121
122  _Chain_Initialize(
123    &keypool->Freechain,
124    nodes,
125    initial_count,
126    sizeof( *nodes )
127  );
128}
129
130/**
131 * @brief This routine performs the initialization necessary for this manager.
132 */
133void _POSIX_Key_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#if defined(RTEMS_MULTIPROCESSING)
146    ,
147    false,                      /* true if this is a global object class */
148    NULL                        /* Proxy extraction support callout */
149#endif
150  );
151
152  _RBTree_Initialize_empty(
153      &_POSIX_Keys_Key_value_lookup_tree,
154      _POSIX_Keys_Key_value_lookup_tree_compare_function,
155      true
156  );
157
158  _POSIX_Keys_Initialize_keypool();
159}
Note: See TracBrowser for help on using the repository browser.