d5a726484a335de620b531a05df029e26643a18d
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.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() */
35 int match(struct cds_lfht_node
*ht_node
, const void *_key
)
38 caa_container_of(ht_node
, struct mynode
, node
);
39 const unsigned int *key
= _key
;
41 return *key
== node
->value
;
44 int main(int argc
, char **argv
)
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 */
52 struct cds_lfht_iter iter
; /* For iteration on hash table */
53 struct cds_lfht_node
*ht_node
;
57 * Each thread need using RCU read-side need to be explicitly
60 rcu_register_thread();
62 /* Use time as seed for hash table hashing. */
63 seed
= (uint32_t) time(NULL
);
66 * Allocate hash table.
68 ht
= cds_lfht_new(1, 1, 0,
69 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
,
72 printf("Error allocating hash table\n");
78 * Add nodes to hash table.
80 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
84 node
= malloc(sizeof(*node
));
90 cds_lfht_node_init(&node
->node
);
93 hash
= jhash(&value
, sizeof(value
), seed
);
96 * cds_lfht_add() needs to be called from RCU read-side
100 cds_lfht_add(ht
, hash
, &node
->node
);
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.
109 printf("hash table content (random order):");
111 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
112 printf(" %d", node
->value
);
118 * Remove one node for each key, if such a node is present.
120 printf("removing keys (single key, not duplicates):");
121 for (i
= 0; i
< CAA_ARRAY_SIZE(remove_values
); i
++) {
125 value
= remove_values
[i
];
126 hash
= jhash(&value
, sizeof(value
), seed
);
127 printf(" %d", value
);
129 cds_lfht_lookup(ht
, hash
, match
, &value
, &iter
);
130 ht_node
= cds_lfht_iter_get_node(&iter
);
132 ret
= cds_lfht_del(ht
, ht_node
);
134 printf(" (concurrently deleted)");
137 printf(" (not found)");
143 printf("hash table content (random order):");
145 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
146 printf(" %d", node
->value
);
152 rcu_unregister_thread();
This page took 0.031878 seconds and 3 git commands to generate.