summaryrefslogtreecommitdiffstats
path: root/cpukit/posix/src/key.c
blob: 99654c2da4a8439169e51c90ebd09a8848bbb7db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
 * @file
 *
 * @brief POSIX Keys Manager Initialization
 * @ingroup POSIX_KEY Key
 */

/*
 * Copyright (c) 2012 Zhongwei Yao.
 * COPYRIGHT (c) 1989-2008.
 * On-Line Applications Research Corporation (OAR).
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.com/license/LICENSE.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems/posix/keyimpl.h>
#include <rtems/posix/config.h>
#include <rtems/score/chainimpl.h>
#include <rtems/score/wkspace.h>

/**
 * @brief This routine compares the rbtree node by comparing POSIX key first
 * and comparing thread id second.
 *
 * if either of the input nodes's thread_id member is 0, then it will only
 * compare the pthread_key_t member. That is when we pass thread_id = 0 node
 * as a search node, the search is done only by pthread_key_t.
 *
 * @param[in] node1 The node to be compared
 * @param[in] node2 The node to be compared
 * @retval positive if first node has higher key than second
 * @retval negative if lower
 * @retval 0 if equal,and for all the thread id is unique, then return 0 is
 * impossible
 */

int _POSIX_Keys_Key_value_lookup_tree_compare_function(
  const RBTree_Node *node1,
  const RBTree_Node *node2
)
{
  POSIX_Keys_Key_value_pair *n1;
  POSIX_Keys_Key_value_pair *n2;
  Objects_Id thread_id1, thread_id2;
  int diff;

  n1 = _RBTree_Container_of( node1, POSIX_Keys_Key_value_pair, Key_value_lookup_node );
  n2 = _RBTree_Container_of( node2, POSIX_Keys_Key_value_pair, Key_value_lookup_node );

  diff = n1->key - n2->key;
  if ( diff )
    return diff;

  thread_id1 = n1->thread_id;
  thread_id2 = n2->thread_id;

  /**
   * if thread_id1 or thread_id2 equals to 0, only key1 and key2 is valued.
   * it enables us search node only by pthread_key_t type key.
   */
  if ( thread_id1 && thread_id2 )
    return thread_id1 - thread_id2;
  return 0;
}

/**
 * @brief This routine does user side freechain initialization
 */
static void _POSIX_Keys_Freechain_init(Freechain_Control *freechain)
{
  POSIX_Keys_Freechain *psx_freechain_p = (POSIX_Keys_Freechain *)freechain;
  psx_freechain_p->bump_count =
    Configuration_POSIX_API.maximum_key_value_pairs & 0x7FFFFFFF;
  size_t size = psx_freechain_p->bump_count * sizeof(POSIX_Keys_Key_value_pair);
  POSIX_Keys_Key_value_pair *nodes = _Workspace_Allocate(size);

  _Chain_Initialize(
                    &freechain->Freechain,
                    nodes,
                    psx_freechain_p->bump_count,
                    sizeof(POSIX_Keys_Key_value_pair)
                    );
}

/**
 * @brief This routine does keypool initialize, keypool contains all
 * POSIX_Keys_Key_value_pair
 */

static void _POSIX_Keys_Keypool_init(void)
{
  _Freechain_Initialize((Freechain_Control *)&_POSIX_Keys_Keypool,
                       &_POSIX_Keys_Freechain_extend);

  _POSIX_Keys_Freechain_init((Freechain_Control *)&_POSIX_Keys_Keypool);
}

/**
 * @brief This routine is user defined freechain extension handle
 */
bool _POSIX_Keys_Freechain_extend(Freechain_Control *freechain)
{
  POSIX_Keys_Freechain *psx_freechain_p = (POSIX_Keys_Freechain *)freechain;
  size_t node_size = sizeof(POSIX_Keys_Key_value_pair);
  size_t size = psx_freechain_p->bump_count * node_size;
  int i;
  POSIX_Keys_Key_value_pair *nodes = _Workspace_Allocate(size);

  if (!nodes)
    return false;

  for ( i = 0; i < psx_freechain_p->bump_count; i++ ) {
      _Freechain_Put(freechain,
                     nodes + i);
  }
  return true;
}

/**
 * @brief This routine performs the initialization necessary for this manager.
 */
void _POSIX_Key_Manager_initialization(void)
{
  _Objects_Initialize_information(
    &_POSIX_Keys_Information,   /* object information table */
    OBJECTS_POSIX_API,          /* object API */
    OBJECTS_POSIX_KEYS,         /* object class */
    Configuration_POSIX_API.maximum_keys,
                                /* maximum objects of this class */
    sizeof( POSIX_Keys_Control ),
                                /* size of this object's control block */
    true,                       /* true if names for this object are strings */
    _POSIX_PATH_MAX             /* maximum length of each object's name */
#if defined(RTEMS_MULTIPROCESSING)
    ,
    false,                      /* true if this is a global object class */
    NULL                        /* Proxy extraction support callout */
#endif
  );

  _RBTree_Initialize_empty(
      &_POSIX_Keys_Key_value_lookup_tree,
      _POSIX_Keys_Key_value_lookup_tree_compare_function,
      true
  );

  _POSIX_Keys_Keypool_init();
}