4eb3486e5833da4d11333b5977d49ccb312a5421
1 // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: MIT
6 * This example shows how to remove nodes from a RCU lock-free hash table.
7 * This hash table requires using a RCU scheme.
14 #include <urcu/urcu-memb.h> /* RCU flavor */
15 #include <urcu/rculfhash.h> /* RCU Lock-free hash table */
16 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
17 #include "jhash.h" /* Example hash function */
20 * Nodes populated into the hash table.
23 int value
; /* Node content */
24 struct cds_lfht_node node
; /* Chaining in hash table */
25 struct rcu_head rcu_head
; /* For call_rcu() */
29 int match(struct cds_lfht_node
*ht_node
, const void *_key
)
32 caa_container_of(ht_node
, struct mynode
, node
);
33 const int *key
= _key
;
35 return *key
== node
->value
;
39 void free_node(struct rcu_head
*head
)
41 struct mynode
*node
= caa_container_of(head
, struct mynode
, rcu_head
);
48 int values
[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */
49 int remove_values
[] = { 42, 36, 24, 123, };
50 struct cds_lfht
*ht
; /* Hash table */
54 struct cds_lfht_iter iter
; /* For iteration on hash table */
55 struct cds_lfht_node
*ht_node
;
59 * Each thread need using RCU read-side need to be explicitly
62 urcu_memb_register_thread();
64 /* Use time as seed for hash table hashing. */
65 seed
= (uint32_t) time(NULL
);
68 * Allocate hash table.
70 ht
= cds_lfht_new_flavor(1, 1, 0,
71 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
,
72 &urcu_memb_flavor
, NULL
);
74 printf("Error allocating hash table\n");
80 * Add nodes to hash table.
82 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
86 node
= malloc(sizeof(*node
));
92 cds_lfht_node_init(&node
->node
);
95 hash
= jhash(&value
, sizeof(value
), seed
);
98 * cds_lfht_add() needs to be called from RCU read-side
101 urcu_memb_read_lock();
102 cds_lfht_add(ht
, hash
, &node
->node
);
103 urcu_memb_read_unlock();
107 * Iterate over each hash table node. Those will appear in
108 * random order, depending on the hash seed. Iteration needs to
109 * be performed within RCU read-side critical section.
111 printf("hash table content (random order):");
112 urcu_memb_read_lock();
113 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
114 printf(" %d", node
->value
);
116 urcu_memb_read_unlock();
120 * Remove one node for each key, if such a node is present.
122 printf("removing keys (single key, not duplicates):");
123 for (i
= 0; i
< CAA_ARRAY_SIZE(remove_values
); i
++) {
127 value
= remove_values
[i
];
128 hash
= jhash(&value
, sizeof(value
), seed
);
129 printf(" %d", value
);
130 urcu_memb_read_lock();
131 cds_lfht_lookup(ht
, hash
, match
, &value
, &iter
);
132 ht_node
= cds_lfht_iter_get_node(&iter
);
134 ret
= cds_lfht_del(ht
, ht_node
);
136 printf(" (concurrently deleted)");
138 struct mynode
*del_node
=
139 caa_container_of(ht_node
,
140 struct mynode
, node
);
141 urcu_memb_call_rcu(&del_node
->rcu_head
, free_node
);
144 printf(" (not found)");
146 urcu_memb_read_unlock();
150 printf("hash table content (random order):");
151 urcu_memb_read_lock();
152 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
153 printf(" %d", node
->value
);
155 urcu_memb_read_unlock();
159 urcu_memb_unregister_thread();
This page took 0.0319 seconds and 4 git commands to generate.