]> git.lttng.org Git - urcu.git/commitdiff
doc/examples: cds_lfht_lookup
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sun, 23 Jun 2013 18:56:44 +0000 (14:56 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sun, 23 Jun 2013 18:56:44 +0000 (14:56 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
.gitignore
doc/examples/Makefile.am
doc/examples/rculfhash/Makefile
doc/examples/rculfhash/Makefile.cds_lfht_lookup [new file with mode: 0644]
doc/examples/rculfhash/cds_lfht_lookup.c [new file with mode: 0644]

index bca252aa872177e971f7b275e43954b435be4e59..9d49661682e01db1f493f33cf2acbdcf21236eec 100644 (file)
@@ -111,6 +111,7 @@ doc/examples/rculfhash/cds_lfht_add_unique
 doc/examples/rculfhash/cds_lfht_add_replace
 doc/examples/rculfhash/cds_lfht_del
 doc/examples/rculfhash/cds_lfht_destroy
+doc/examples/rculfhash/cds_lfht_lookup
 
 #automake
 /config.h
index 31bac6d9544ced6ed4244dc8ce4658cb42e1bc1f..9441296b5c97c2528f95e8973a84ebe113e60f25 100644 (file)
@@ -101,11 +101,13 @@ dist_doc_examples_rculfhash_DATA = \
        rculfhash/Makefile.cds_lfht_add_replace \
        rculfhash/Makefile.cds_lfht_del \
        rculfhash/Makefile.cds_lfht_destroy \
+       rculfhash/Makefile.cds_lfht_lookup \
        rculfhash/cds_lfht_add.c \
        rculfhash/cds_lfht_add_unique.c \
        rculfhash/cds_lfht_add_replace.c \
        rculfhash/cds_lfht_del.c \
-       rculfhash/cds_lfht_destroy.c
+       rculfhash/cds_lfht_destroy.c \
+       rculfhash/cds_lfht_lookup.c
 
 if NO_SHARED
 # Don't build examples if shared libraries support was explicitly
index d7918eed306ab082431ec2da88dadb5053bb6ba3..2739499af892e983cc28af986afa078dc4052d40 100644 (file)
@@ -17,6 +17,7 @@ all:
        $(MAKE) -f Makefile.cds_lfht_add_replace
        $(MAKE) -f Makefile.cds_lfht_del
        $(MAKE) -f Makefile.cds_lfht_destroy
+       $(MAKE) -f Makefile.cds_lfht_lookup
 
 .PHONY: clean
 clean:
@@ -25,3 +26,4 @@ clean:
        $(MAKE) -f Makefile.cds_lfht_add_replace clean
        $(MAKE) -f Makefile.cds_lfht_del clean
        $(MAKE) -f Makefile.cds_lfht_destroy clean
+       $(MAKE) -f Makefile.cds_lfht_lookup clean
diff --git a/doc/examples/rculfhash/Makefile.cds_lfht_lookup b/doc/examples/rculfhash/Makefile.cds_lfht_lookup
new file mode 100644 (file)
index 0000000..79eb6c4
--- /dev/null
@@ -0,0 +1,22 @@
+# 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 makefile is purposefully kept simple to support GNU and BSD make.
+
+EXAMPLE_NAME = cds_lfht_lookup
+
+SOURCES = $(EXAMPLE_NAME).c
+DEPS = jhash.h
+OBJECTS = $(EXAMPLE_NAME).o
+BINARY = $(EXAMPLE_NAME)
+LIBS = -lurcu-cds -lurcu
+
+include ../Makefile.examples.template
diff --git a/doc/examples/rculfhash/cds_lfht_lookup.c b/doc/examples/rculfhash/cds_lfht_lookup.c
new file mode 100644 (file)
index 0000000..e76827b
--- /dev/null
@@ -0,0 +1,147 @@
+/*
+ * 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 lookup keys within a RCU lock-free hash
+ * table. This hash table requires using a RCU scheme.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#include <urcu.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 */
+
+/*
+ * Nodes populated into the hash table.
+ */
+struct mynode {
+       int value;                      /* Node content */
+       int seqnum;                     /* Our node sequence number */
+       struct cds_lfht_node node;      /* Chaining in hash table */
+};
+
+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;
+
+       return *key == node->value;
+}
+
+int main(int argc, char **argv)
+{
+       int values[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */
+       int lookup_values[] = { 42, 200, 36, };
+       struct cds_lfht *ht;    /* Hash table */
+       unsigned int i;
+       int ret = 0, seqnum = 0;
+       uint32_t seed;
+       struct cds_lfht_iter iter;      /* For iteration on hash table */
+       struct cds_lfht_node *ht_node;
+       struct mynode *node;
+
+       /*
+        * Each thread need using RCU read-side need to be explicitly
+        * registered.
+        */
+       rcu_register_thread();
+
+       /* Use time as seed for hash table hashing. */
+       seed = (uint32_t) time(NULL);
+
+       /*
+        * Allocate hash table.
+        */
+       ht = cds_lfht_new(1, 1, 0,
+               CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING,
+               NULL);
+       if (!ht) {
+               printf("Error allocating hash table\n");
+               ret = -1;
+               goto end;
+       }
+
+       /*
+        * Add nodes to hash table.
+        */
+       for (i = 0; i < CAA_ARRAY_SIZE(values); i++) {
+               unsigned long hash;
+               int value;
+
+               node = malloc(sizeof(*node));
+               if (!node) {
+                       ret = -1;
+                       goto end;
+               }
+
+               cds_lfht_node_init(&node->node);
+               value = values[i];
+               node->value = value;
+               node->seqnum = seqnum++;
+               hash = jhash(&value, sizeof(value), seed);
+
+               /*
+                * cds_lfht_add() needs to be called from RCU read-side
+                * critical section.
+                */
+               rcu_read_lock();
+               cds_lfht_add(ht, hash, &node->node);
+               printf("Add (key: %d, seqnum: %d)\n",
+                       node->value, node->seqnum);
+               rcu_read_unlock();
+       }
+
+       /*
+        * Iterate over each hash table node. Those will appear in
+        * random order, depending on the hash seed. Iteration needs to
+        * be performed within RCU read-side critical section.
+        */
+       printf("hash table content (random order):");
+       rcu_read_lock();
+       cds_lfht_for_each_entry(ht, &iter, node, node) {
+               printf(" (key: %d, seqnum: %d)",
+                       node->value, node->seqnum);
+       }
+       rcu_read_unlock();
+       printf("\n");
+
+       /*
+        * Lookup queries. Note that which node (seqnum) within
+        * duplicates will be found by lookup is random.
+        */
+       printf("Lookups:\n");
+       for (i = 0; i < CAA_ARRAY_SIZE(lookup_values); i++) {
+               int value = lookup_values[i];
+               unsigned long hash = jhash(&value, sizeof(value), seed);
+
+               rcu_read_lock();
+               cds_lfht_lookup(ht, hash, match, &value, &iter);
+               ht_node = cds_lfht_iter_get_node(&iter);
+               if (!ht_node) {
+                       printf("Key %d not found\n", value);
+               } else {
+                       node = caa_container_of(ht_node, struct mynode, node);
+                       printf("(key %d, seqnum %d) found\n",
+                               node->value, node->seqnum);
+               }
+               rcu_read_unlock();
+       }
+
+end:
+       rcu_unregister_thread();
+       return ret;
+}
This page took 0.032341 seconds and 4 git commands to generate.