From: Mathieu Desnoyers Date: Sun, 23 Jun 2013 18:38:10 +0000 (-0400) Subject: doc/examples: cds_lfht_destroy X-Git-Tag: v0.8.0~15 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=d14301c29dfc8761d54c4a289416d67606682675;p=userspace-rcu.git doc/examples: cds_lfht_destroy Signed-off-by: Mathieu Desnoyers --- diff --git a/.gitignore b/.gitignore index 9767ac3..bca252a 100644 --- a/.gitignore +++ b/.gitignore @@ -110,6 +110,7 @@ doc/examples/rculfhash/cds_lfht_add 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 #automake /config.h diff --git a/doc/examples/Makefile.am b/doc/examples/Makefile.am index bf19016..31bac6d 100644 --- a/doc/examples/Makefile.am +++ b/doc/examples/Makefile.am @@ -100,10 +100,12 @@ dist_doc_examples_rculfhash_DATA = \ rculfhash/Makefile.cds_lfht_add_unique \ rculfhash/Makefile.cds_lfht_add_replace \ rculfhash/Makefile.cds_lfht_del \ + rculfhash/Makefile.cds_lfht_destroy \ 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_del.c \ + rculfhash/cds_lfht_destroy.c if NO_SHARED # Don't build examples if shared libraries support was explicitly diff --git a/doc/examples/rculfhash/Makefile b/doc/examples/rculfhash/Makefile index 52da737..d7918ee 100644 --- a/doc/examples/rculfhash/Makefile +++ b/doc/examples/rculfhash/Makefile @@ -16,6 +16,7 @@ all: $(MAKE) -f Makefile.cds_lfht_add_unique $(MAKE) -f Makefile.cds_lfht_add_replace $(MAKE) -f Makefile.cds_lfht_del + $(MAKE) -f Makefile.cds_lfht_destroy .PHONY: clean clean: @@ -23,3 +24,4 @@ clean: $(MAKE) -f Makefile.cds_lfht_add_unique clean $(MAKE) -f Makefile.cds_lfht_add_replace clean $(MAKE) -f Makefile.cds_lfht_del clean + $(MAKE) -f Makefile.cds_lfht_destroy clean diff --git a/doc/examples/rculfhash/Makefile.cds_lfht_destroy b/doc/examples/rculfhash/Makefile.cds_lfht_destroy new file mode 100644 index 0000000..d797893 --- /dev/null +++ b/doc/examples/rculfhash/Makefile.cds_lfht_destroy @@ -0,0 +1,22 @@ +# Copyright (C) 2013 Mathieu Desnoyers +# +# 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_destroy + +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_destroy.c b/doc/examples/rculfhash/cds_lfht_destroy.c new file mode 100644 index 0000000..d82d81c --- /dev/null +++ b/doc/examples/rculfhash/cds_lfht_destroy.c @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2013 Mathieu Desnoyers + * + * 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 use cds_lfht_destroy() to clear memory used + * by a a RCU lock-free hash table. This hash table requires using a + * RCU scheme. + */ + +#include +#include +#include + +#include /* RCU flavor */ +#include /* RCU Lock-free hash table */ +#include /* For CAA_ARRAY_SIZE */ +#include "jhash.h" /* Example hash function */ + +/* + * Nodes populated into the hash table. + */ +struct mynode { + int value; /* Node content */ + struct cds_lfht_node node; /* Chaining in hash table */ + struct rcu_head rcu_head; /* For call_rcu() */ +}; + +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 */ + struct cds_lfht *ht; /* Hash table */ + unsigned int i; + int ret = 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; + 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); + 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(" %d", node->value); + } + rcu_read_unlock(); + printf("\n"); + + + /* + * Make sure all hash table nodes are removed before destroying. + */ + printf("removing all nodes:"); + rcu_read_lock(); + cds_lfht_for_each_entry(ht, &iter, node, node) { + ht_node = cds_lfht_iter_get_node(&iter); + ret = cds_lfht_del(ht, ht_node); + printf(" %d", node->value); + if (ret) { + printf(" (concurrently deleted)"); + } else { + call_rcu(&node->rcu_head, free_node); + } + } + rcu_read_unlock(); + printf("\n"); + + /* + * cds_lfht_destroy() must be called from a very specific + * context: it needs to be called from a registered RCU reader + * thread. However, this thread should _not_ be within a RCU + * read-side critical section. Also, it should _not_ be called + * from a call_rcu thread. + */ + ret = cds_lfht_destroy(ht, NULL); + if (ret) { + printf("Destroying hash table failed\n"); + } +end: + rcu_unregister_thread(); + return ret; +}