doc/examples: cds_lfht_add/cds_lfht_del
[urcu.git] / doc / examples / rculfhash / cds_lfht_del.c
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 remove nodes from a RCU lock-free hash table.
14 * This hash table requires using a RCU scheme.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <time.h>
20
21 #include <urcu.h> /* RCU flavor */
22 #include <urcu/rculfhash.h> /* RCU Lock-free hash table */
23 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
24 #include "jhash.h" /* Example hash function */
25
26 /*
27 * Nodes populated into the hash table.
28 */
29 struct mynode {
30 int value; /* Node content */
31 struct cds_lfht_node node; /* Chaining in hash table */
32 struct rcu_head rcu_head; /* For call_rcu() */
33 };
34
35 int match(struct cds_lfht_node *ht_node, const void *_key)
36 {
37 struct mynode *node =
38 caa_container_of(ht_node, struct mynode, node);
39 const unsigned int *key = _key;
40
41 return *key == node->value;
42 }
43
44 int main(int argc, char **argv)
45 {
46 int values[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */
47 int remove_values[] = { 42, 36, 24, 123, };
48 struct cds_lfht *ht; /* Hash table */
49 unsigned int i;
50 int ret = 0;
51 uint32_t seed;
52 struct cds_lfht_iter iter; /* For iteration on hash table */
53 struct cds_lfht_node *ht_node;
54 struct mynode *node;
55
56 /*
57 * Each thread need using RCU read-side need to be explicitly
58 * registered.
59 */
60 rcu_register_thread();
61
62 /* Use time as seed for hash table hashing. */
63 seed = (uint32_t) time(NULL);
64
65 /*
66 * Allocate hash table.
67 */
68 ht = cds_lfht_new(1, 1, 0,
69 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING,
70 NULL);
71 if (!ht) {
72 printf("Error allocating hash table\n");
73 ret = -1;
74 goto end;
75 }
76
77 /*
78 * Add nodes to hash table.
79 */
80 for (i = 0; i < CAA_ARRAY_SIZE(values); i++) {
81 unsigned long hash;
82 int value;
83
84 node = malloc(sizeof(*node));
85 if (!node) {
86 ret = -1;
87 goto end;
88 }
89
90 cds_lfht_node_init(&node->node);
91 value = values[i];
92 node->value = value;
93 hash = jhash(&value, sizeof(value), seed);
94
95 /*
96 * cds_lfht_add() needs to be called from RCU read-side
97 * critical section.
98 */
99 rcu_read_lock();
100 cds_lfht_add(ht, hash, &node->node);
101 rcu_read_unlock();
102 }
103
104 /*
105 * Iterate over each hash table node. Those will appear in
106 * random order, depending on the hash seed. Iteration needs to
107 * be performed within RCU read-side critical section.
108 */
109 printf("hash table content (random order):");
110 rcu_read_lock();
111 cds_lfht_for_each_entry(ht, &iter, node, node) {
112 printf(" %d", node->value);
113 }
114 rcu_read_unlock();
115 printf("\n");
116
117 /*
118 * Remove one node for each key, if such a node is present.
119 */
120 printf("removing keys (single key, not duplicates):");
121 for (i = 0; i < CAA_ARRAY_SIZE(remove_values); i++) {
122 unsigned long hash;
123 int value;
124
125 value = remove_values[i];
126 hash = jhash(&value, sizeof(value), seed);
127 printf(" %d", value);
128 rcu_read_lock();
129 cds_lfht_lookup(ht, hash, match, &value, &iter);
130 ht_node = cds_lfht_iter_get_node(&iter);
131 if (ht_node) {
132 ret = cds_lfht_del(ht, ht_node);
133 if (ret) {
134 printf(" (concurrently deleted)");
135 }
136 } else {
137 printf(" (not found)");
138 }
139 rcu_read_unlock();
140 }
141 printf("\n");
142
143 printf("hash table content (random order):");
144 rcu_read_lock();
145 cds_lfht_for_each_entry(ht, &iter, node, node) {
146 printf(" %d", node->value);
147 }
148 rcu_read_unlock();
149 printf("\n");
150
151 end:
152 rcu_unregister_thread();
153 return ret;
154 }
This page took 0.04429 seconds and 4 git commands to generate.