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