uatomic/x86: Remove redundant memory barriers
[urcu.git] / doc / examples / rculfhash / cds_lfht_add_replace.c
index 62de40c033f97feca95c25b456c4bb1caa16714d..ecab5708c6cff3620ca2b906d1b7da8c6701b319 100644 (file)
@@ -1,15 +1,8 @@
+// SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+//
+// SPDX-License-Identifier: MIT
+
 /*
- * Copyright (C) 2013  Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
- * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
- *
- * Permission is hereby granted to use or copy this program for any
- * purpose,  provided the above notices are retained on all copies.
- * Permission to modify the code and to distribute modified code is
- * granted, provided the above notices are retained, and a notice that
- * the code was modified is included with the above copyright notice.
- *
  * This example shows how to add nodes into a RCU lock-free hash table
  * with cds_lfht_add_replace(), which replaces existing nodes with the
  * same key if found.
@@ -21,7 +14,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 */
@@ -41,7 +34,7 @@ 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;
 }
@@ -54,7 +47,7 @@ void free_node(struct rcu_head *head)
        free(node);
 }
 
-int main(int argc, char **argv)
+int main(void)
 {
        int values[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */
        struct cds_lfht *ht;    /* Hash table */
@@ -69,7 +62,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);
@@ -77,9 +70,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;
@@ -109,7 +102,7 @@ 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();
                ht_node = cds_lfht_add_replace(ht, hash, match, &value,
                        &node->node);
                if (ht_node) {
@@ -119,12 +112,12 @@ int main(int argc, char **argv)
                        printf("Replaced node (key: %d, seqnum: %d) by (key: %d, seqnum: %d)\n",
                                ret_node->value, ret_node->seqnum,
                                node->value, node->seqnum);
-                       call_rcu(&ret_node->rcu_head, free_node);
+                       urcu_memb_call_rcu(&ret_node->rcu_head, free_node);
                } else {
                        printf("Add (key: %d, seqnum: %d)\n",
                                node->value, node->seqnum);
                }
-               rcu_read_unlock();
+               urcu_memb_read_unlock();
        }
 
        /*
@@ -133,15 +126,15 @@ 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(" (key: %d, seqnum: %d)",
                        node->value, node->seqnum);
        }
-       rcu_read_unlock();
+       urcu_memb_read_unlock();
        printf("\n");
 
 end:
-       rcu_unregister_thread();
+       urcu_memb_unregister_thread();
        return ret;
 }
This page took 0.024628 seconds and 4 git commands to generate.