2 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
13 * This example shows how to remove nodes from a RCU lock-free hash table.
14 * This hash table requires using a RCU scheme.
21 #include <urcu/urcu-memb.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 */
27 * Nodes populated into the hash table.
30 int value
; /* Node content */
31 struct cds_lfht_node node
; /* Chaining in hash table */
32 struct rcu_head rcu_head
; /* For call_rcu() */
36 int match(struct cds_lfht_node
*ht_node
, const void *_key
)
39 caa_container_of(ht_node
, struct mynode
, node
);
40 const int *key
= _key
;
42 return *key
== node
->value
;
46 void free_node(struct rcu_head
*head
)
48 struct mynode
*node
= caa_container_of(head
, struct mynode
, rcu_head
);
53 int main(int argc
, char **argv
)
55 int values
[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */
56 int remove_values
[] = { 42, 36, 24, 123, };
57 struct cds_lfht
*ht
; /* Hash table */
61 struct cds_lfht_iter iter
; /* For iteration on hash table */
62 struct cds_lfht_node
*ht_node
;
66 * Each thread need using RCU read-side need to be explicitly
69 urcu_memb_register_thread();
71 /* Use time as seed for hash table hashing. */
72 seed
= (uint32_t) time(NULL
);
75 * Allocate hash table.
77 ht
= cds_lfht_new_flavor(1, 1, 0,
78 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
,
79 &urcu_memb_flavor
, NULL
);
81 printf("Error allocating hash table\n");
87 * Add nodes to hash table.
89 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
93 node
= malloc(sizeof(*node
));
99 cds_lfht_node_init(&node
->node
);
102 hash
= jhash(&value
, sizeof(value
), seed
);
105 * cds_lfht_add() needs to be called from RCU read-side
108 urcu_memb_read_lock();
109 cds_lfht_add(ht
, hash
, &node
->node
);
110 urcu_memb_read_unlock();
114 * Iterate over each hash table node. Those will appear in
115 * random order, depending on the hash seed. Iteration needs to
116 * be performed within RCU read-side critical section.
118 printf("hash table content (random order):");
119 urcu_memb_read_lock();
120 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
121 printf(" %d", node
->value
);
123 urcu_memb_read_unlock();
127 * Remove one node for each key, if such a node is present.
129 printf("removing keys (single key, not duplicates):");
130 for (i
= 0; i
< CAA_ARRAY_SIZE(remove_values
); i
++) {
134 value
= remove_values
[i
];
135 hash
= jhash(&value
, sizeof(value
), seed
);
136 printf(" %d", value
);
137 urcu_memb_read_lock();
138 cds_lfht_lookup(ht
, hash
, match
, &value
, &iter
);
139 ht_node
= cds_lfht_iter_get_node(&iter
);
141 ret
= cds_lfht_del(ht
, ht_node
);
143 printf(" (concurrently deleted)");
145 struct mynode
*del_node
=
146 caa_container_of(ht_node
,
147 struct mynode
, node
);
148 urcu_memb_call_rcu(&del_node
->rcu_head
, free_node
);
151 printf(" (not found)");
153 urcu_memb_read_unlock();
157 printf("hash table content (random order):");
158 urcu_memb_read_lock();
159 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
160 printf(" %d", node
->value
);
162 urcu_memb_read_unlock();
166 urcu_memb_unregister_thread();
This page took 0.035133 seconds and 5 git commands to generate.