[PATCH 2/2] score: Add and use _RBTree_Insert_inline()

Sebastian Huber sebastian.huber at embedded-brains.de
Thu Mar 31 11:57:57 UTC 2016


---
 cpukit/posix/include/rtems/posix/keyimpl.h | 40 +++++++++++++-----------------
 cpukit/posix/src/keysetspecific.c          |  2 +-
 cpukit/score/include/rtems/score/rbtree.h  | 34 +++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 24 deletions(-)

diff --git a/cpukit/posix/include/rtems/posix/keyimpl.h b/cpukit/posix/include/rtems/posix/keyimpl.h
index ca70e24..df9fc5e 100644
--- a/cpukit/posix/include/rtems/posix/keyimpl.h
+++ b/cpukit/posix/include/rtems/posix/keyimpl.h
@@ -152,35 +152,29 @@ RTEMS_INLINE_ROUTINE RBTree_Node *_POSIX_Keys_Key_value_find(
   );
 }
 
-RTEMS_INLINE_ROUTINE void _POSIX_Keys_Key_value_insert(
-  pthread_key_t              key,
-  POSIX_Keys_Key_value_pair *key_value_pair,
-  Thread_Control            *the_thread
+RTEMS_INLINE_ROUTINE bool _POSIX_Keys_Key_value_insert_less(
+  const RBTree_Node *left,
+  const RBTree_Node *right
 )
 {
-  RBTree_Node **link;
-  RBTree_Node  *parent;
-
-  link = _RBTree_Root_reference( &the_thread->Keys.Key_value_pairs );
-  parent = NULL;
-
-  while ( *link != NULL ) {
-    POSIX_Keys_Key_value_pair *parent_key_value_pair;
+  const POSIX_Keys_Key_value_pair *the_left;
+  const POSIX_Keys_Key_value_pair *the_right;
 
-    parent = *link;
-    parent_key_value_pair = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( parent );
+  the_left = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( left );
+  the_right = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( right );
 
-    if ( key < parent_key_value_pair->key ) {
-      link = _RBTree_Left_reference( parent );
-    } else {
-      link = _RBTree_Right_reference( parent );
-    }
-  }
+  return the_left->key < the_right->key;
+}
 
-  _RBTree_Add_child( &key_value_pair->Lookup_node, parent, link );
-  _RBTree_Insert_color(
+RTEMS_INLINE_ROUTINE void _POSIX_Keys_Key_value_insert(
+  Thread_Control            *the_thread,
+  POSIX_Keys_Key_value_pair *key_value_pair
+)
+{
+  _RBTree_Insert_inline(
     &the_thread->Keys.Key_value_pairs,
-    &key_value_pair->Lookup_node
+    &key_value_pair->Lookup_node,
+    _POSIX_Keys_Key_value_insert_less
   );
 }
 
diff --git a/cpukit/posix/src/keysetspecific.c b/cpukit/posix/src/keysetspecific.c
index 8b0f517..4aaa7d7 100644
--- a/cpukit/posix/src/keysetspecific.c
+++ b/cpukit/posix/src/keysetspecific.c
@@ -63,7 +63,7 @@ static int _POSIX_Keys_Create_value(
       );
 
       _POSIX_Keys_Key_value_acquire( executing, &lock_context );
-      _POSIX_Keys_Key_value_insert( key, key_value_pair, executing );
+      _POSIX_Keys_Key_value_insert( executing, key_value_pair );
       _POSIX_Keys_Key_value_release( executing, &lock_context );
       eno = 0;
     } else {
diff --git a/cpukit/score/include/rtems/score/rbtree.h b/cpukit/score/include/rtems/score/rbtree.h
index 111b231..e39fe77 100644
--- a/cpukit/score/include/rtems/score/rbtree.h
+++ b/cpukit/score/include/rtems/score/rbtree.h
@@ -484,6 +484,40 @@ void _RBTree_Replace_node(
 );
 
 /**
+ * @brief Inserts the node into the red-black tree.
+ *
+ * @param the_rbtree The red-black tree control.
+ * @param the_node The node to insert.
+ * @param less Must return true if the left node is less than the right the
+ *   node, otherwise false.
+ */
+RTEMS_INLINE_ROUTINE void _RBTree_Insert_inline(
+  RBTree_Control *the_rbtree,
+  RBTree_Node    *the_node,
+  bool         ( *less )( const RBTree_Node *, const RBTree_Node *)
+)
+{
+  RBTree_Node **link;
+  RBTree_Node  *parent;
+
+  link = _RBTree_Root_reference( the_rbtree );
+  parent = NULL;
+
+  while ( *link != NULL ) {
+    parent = *link;
+
+    if ( ( *less )( the_node, parent ) ) {
+      link = _RBTree_Left_reference( parent );
+    } else {
+      link = _RBTree_Right_reference( parent );
+    }
+  }
+
+  _RBTree_Add_child( the_node, parent, link );
+  _RBTree_Insert_color( the_rbtree, the_node );
+}
+
+/**
  * @brief Finds a node in the red-black tree with the specified key.
  *
  * @param the_rbtree The red-black tree control.
-- 
1.8.4.5



More information about the devel mailing list