test multiflavor single compile unit
[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
22#include <urcu.h> /* RCU flavor */
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
44int main(int argc, char **argv)
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 */
59 rcu_register_thread();
60
61 /* Use time as seed for hash table hashing. */
62 seed = (uint32_t) time(NULL);
63
64 /*
65 * Allocate hash table.
66 */
67 ht = cds_lfht_new(1, 1, 0,
68 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING,
69 NULL);
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 */
98 rcu_read_lock();
99 cds_lfht_add(ht, hash, &node->node);
100 rcu_read_unlock();
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):");
109 rcu_read_lock();
110 cds_lfht_for_each_entry(ht, &iter, node, node) {
111 printf(" %d", node->value);
112 }
113 rcu_read_unlock();
114 printf("\n");
115
116
117 /*
118 * Make sure all hash table nodes are removed before destroying.
119 */
120 printf("removing all nodes:");
121 rcu_read_lock();
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 {
129 call_rcu(&node->rcu_head, free_node);
130 }
131 }
132 rcu_read_unlock();
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:
147 rcu_unregister_thread();
148 return ret;
149}
This page took 0.034986 seconds and 4 git commands to generate.