cleanup: explicitly mark unused parameters (-Wunused-parameter)
[urcu.git] / doc / examples / rculfhash / cds_lfht_destroy.c
CommitLineData
d14301c2
MD
1/*
2 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
7 * Permission is hereby granted to use or copy this program for any
8 * purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is
10 * granted, provided the above notices are retained, and a notice that
11 * the code was modified is included with the above copyright notice.
12 *
13 * This example shows how to use cds_lfht_destroy() to clear memory used
14 * by a a RCU lock-free hash table. This hash table requires using a
15 * RCU scheme.
16 */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <time.h>
21
b9050d91 22#include <urcu/urcu-memb.h> /* RCU flavor */
d14301c2
MD
23#include <urcu/rculfhash.h> /* RCU Lock-free hash table */
24#include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
25#include "jhash.h" /* Example hash function */
26
27/*
28 * Nodes populated into the hash table.
29 */
30struct mynode {
31 int value; /* Node content */
32 struct cds_lfht_node node; /* Chaining in hash table */
33 struct rcu_head rcu_head; /* For call_rcu() */
34};
35
36static
37void free_node(struct rcu_head *head)
38{
39 struct mynode *node = caa_container_of(head, struct mynode, rcu_head);
40
41 free(node);
42}
43
70469b43 44int main(void)
d14301c2
MD
45{
46 int values[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */
47 struct cds_lfht *ht; /* Hash table */
48 unsigned int i;
49 int ret = 0;
50 uint32_t seed;
51 struct cds_lfht_iter iter; /* For iteration on hash table */
52 struct cds_lfht_node *ht_node;
53 struct mynode *node;
54
55 /*
56 * Each thread need using RCU read-side need to be explicitly
57 * registered.
58 */
b9050d91 59 urcu_memb_register_thread();
d14301c2
MD
60
61 /* Use time as seed for hash table hashing. */
62 seed = (uint32_t) time(NULL);
63
64 /*
65 * Allocate hash table.
66 */
b9050d91 67 ht = cds_lfht_new_flavor(1, 1, 0,
d14301c2 68 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING,
b9050d91 69 &urcu_memb_flavor, NULL);
d14301c2
MD
70 if (!ht) {
71 printf("Error allocating hash table\n");
72 ret = -1;
73 goto end;
74 }
75
76 /*
77 * Add nodes to hash table.
78 */
79 for (i = 0; i < CAA_ARRAY_SIZE(values); i++) {
80 unsigned long hash;
81 int value;
82
83 node = malloc(sizeof(*node));
84 if (!node) {
85 ret = -1;
86 goto end;
87 }
88
89 cds_lfht_node_init(&node->node);
90 value = values[i];
91 node->value = value;
92 hash = jhash(&value, sizeof(value), seed);
93
94 /*
95 * cds_lfht_add() needs to be called from RCU read-side
96 * critical section.
97 */
b9050d91 98 urcu_memb_read_lock();
d14301c2 99 cds_lfht_add(ht, hash, &node->node);
b9050d91 100 urcu_memb_read_unlock();
d14301c2
MD
101 }
102
103 /*
104 * Iterate over each hash table node. Those will appear in
105 * random order, depending on the hash seed. Iteration needs to
106 * be performed within RCU read-side critical section.
107 */
108 printf("hash table content (random order):");
b9050d91 109 urcu_memb_read_lock();
d14301c2
MD
110 cds_lfht_for_each_entry(ht, &iter, node, node) {
111 printf(" %d", node->value);
112 }
b9050d91 113 urcu_memb_read_unlock();
d14301c2
MD
114 printf("\n");
115
116
117 /*
118 * Make sure all hash table nodes are removed before destroying.
119 */
120 printf("removing all nodes:");
b9050d91 121 urcu_memb_read_lock();
d14301c2
MD
122 cds_lfht_for_each_entry(ht, &iter, node, node) {
123 ht_node = cds_lfht_iter_get_node(&iter);
124 ret = cds_lfht_del(ht, ht_node);
125 printf(" %d", node->value);
126 if (ret) {
127 printf(" (concurrently deleted)");
128 } else {
b9050d91 129 urcu_memb_call_rcu(&node->rcu_head, free_node);
d14301c2
MD
130 }
131 }
b9050d91 132 urcu_memb_read_unlock();
d14301c2
MD
133 printf("\n");
134
135 /*
136 * cds_lfht_destroy() must be called from a very specific
137 * context: it needs to be called from a registered RCU reader
138 * thread. However, this thread should _not_ be within a RCU
139 * read-side critical section. Also, it should _not_ be called
140 * from a call_rcu thread.
141 */
142 ret = cds_lfht_destroy(ht, NULL);
143 if (ret) {
144 printf("Destroying hash table failed\n");
145 }
146end:
b9050d91 147 urcu_memb_unregister_thread();
d14301c2
MD
148 return ret;
149}
This page took 0.03751 seconds and 4 git commands to generate.