Cleanup: enable signed/unsigned compare compiler warning
[urcu.git] / doc / examples / rculfhash / cds_lfht_del.c
index d5a726484a335de620b531a05df029e26643a18d..f030ee17c6063491130333e5bcbf07989a49c137 100644 (file)
@@ -18,7 +18,7 @@
 #include <stdlib.h>
 #include <time.h>
 
-#include <urcu.h>              /* RCU flavor */
+#include <urcu/urcu-memb.h>    /* RCU flavor */
 #include <urcu/rculfhash.h>    /* RCU Lock-free hash table */
 #include <urcu/compiler.h>     /* For CAA_ARRAY_SIZE */
 #include "jhash.h"             /* Example hash function */
@@ -32,15 +32,24 @@ struct mynode {
        struct rcu_head rcu_head;       /* For call_rcu() */
 };
 
+static
 int match(struct cds_lfht_node *ht_node, const void *_key)
 {
        struct mynode *node =
                caa_container_of(ht_node, struct mynode, node);
-       const unsigned int *key = _key;
+       const int *key = _key;
 
        return *key == node->value;
 }
 
+static
+void free_node(struct rcu_head *head)
+{
+       struct mynode *node = caa_container_of(head, struct mynode, rcu_head);
+
+       free(node);
+}
+
 int main(int argc, char **argv)
 {
        int values[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */
@@ -57,7 +66,7 @@ int main(int argc, char **argv)
         * Each thread need using RCU read-side need to be explicitly
         * registered.
         */
-       rcu_register_thread();
+       urcu_memb_register_thread();
 
        /* Use time as seed for hash table hashing. */
        seed = (uint32_t) time(NULL);
@@ -65,9 +74,9 @@ int main(int argc, char **argv)
        /*
         * Allocate hash table.
         */
-       ht = cds_lfht_new(1, 1, 0,
+       ht = cds_lfht_new_flavor(1, 1, 0,
                CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING,
-               NULL);
+               &urcu_memb_flavor, NULL);
        if (!ht) {
                printf("Error allocating hash table\n");
                ret = -1;
@@ -96,9 +105,9 @@ int main(int argc, char **argv)
                 * cds_lfht_add() needs to be called from RCU read-side
                 * critical section.
                 */
-               rcu_read_lock();
+               urcu_memb_read_lock();
                cds_lfht_add(ht, hash, &node->node);
-               rcu_read_unlock();
+               urcu_memb_read_unlock();
        }
 
        /*
@@ -107,11 +116,11 @@ int main(int argc, char **argv)
         * be performed within RCU read-side critical section.
         */
        printf("hash table content (random order):");
-       rcu_read_lock();
+       urcu_memb_read_lock();
        cds_lfht_for_each_entry(ht, &iter, node, node) {
                printf(" %d", node->value);
        }
-       rcu_read_unlock();
+       urcu_memb_read_unlock();
        printf("\n");
 
        /*
@@ -125,30 +134,35 @@ int main(int argc, char **argv)
                value = remove_values[i];
                hash = jhash(&value, sizeof(value), seed);
                printf(" %d", value);
-               rcu_read_lock();
+               urcu_memb_read_lock();
                cds_lfht_lookup(ht, hash, match, &value, &iter);
                ht_node = cds_lfht_iter_get_node(&iter);
                if (ht_node) {
                        ret = cds_lfht_del(ht, ht_node);
                        if (ret) {
                                printf(" (concurrently deleted)");
+                       } else {
+                               struct mynode *del_node =
+                                       caa_container_of(ht_node,
+                                               struct mynode, node);
+                               urcu_memb_call_rcu(&del_node->rcu_head, free_node);
                        }
                } else {
                        printf(" (not found)");
                }
-               rcu_read_unlock();
+               urcu_memb_read_unlock();
        }
        printf("\n");
 
        printf("hash table content (random order):");
-       rcu_read_lock();
+       urcu_memb_read_lock();
        cds_lfht_for_each_entry(ht, &iter, node, node) {
                printf(" %d", node->value);
        }
-       rcu_read_unlock();
+       urcu_memb_read_unlock();
        printf("\n");
 
 end:
-       rcu_unregister_thread();
+       urcu_memb_unregister_thread();
        return ret;
 }
This page took 0.024376 seconds and 4 git commands to generate.