ecab5708c6cff3620ca2b906d1b7da8c6701b319
1 // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: MIT
6 * This example shows how to add nodes into a RCU lock-free hash table
7 * with cds_lfht_add_replace(), which replaces existing nodes with the
9 * We use a "seqnum" field to show which node is staying in the hash
10 * table. This hash table requires using a RCU scheme.
17 #include <urcu/urcu-memb.h> /* RCU flavor */
18 #include <urcu/rculfhash.h> /* RCU Lock-free hash table */
19 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
20 #include "jhash.h" /* Example hash function */
23 * Nodes populated into the hash table.
26 int value
; /* Node content */
27 int seqnum
; /* Our node sequence number */
28 struct cds_lfht_node node
; /* Chaining in hash table */
29 struct rcu_head rcu_head
; /* For call_rcu() */
33 int match(struct cds_lfht_node
*ht_node
, const void *_key
)
36 caa_container_of(ht_node
, struct mynode
, node
);
37 const int *key
= _key
;
39 return *key
== node
->value
;
43 void free_node(struct rcu_head
*head
)
45 struct mynode
*node
= caa_container_of(head
, struct mynode
, rcu_head
);
52 int values
[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */
53 struct cds_lfht
*ht
; /* Hash table */
55 int ret
= 0, seqnum
= 0;
57 struct cds_lfht_iter iter
; /* For iteration on hash table */
58 struct cds_lfht_node
*ht_node
;
62 * Each thread need using RCU read-side need to be explicitly
65 urcu_memb_register_thread();
67 /* Use time as seed for hash table hashing. */
68 seed
= (uint32_t) time(NULL
);
71 * Allocate hash table.
73 ht
= cds_lfht_new_flavor(1, 1, 0,
74 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
,
75 &urcu_memb_flavor
, NULL
);
77 printf("Error allocating hash table\n");
83 * Add nodes to hash table.
85 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
89 node
= malloc(sizeof(*node
));
95 cds_lfht_node_init(&node
->node
);
98 node
->seqnum
= seqnum
++;
99 hash
= jhash(&value
, sizeof(value
), seed
);
102 * cds_lfht_add() needs to be called from RCU read-side
105 urcu_memb_read_lock();
106 ht_node
= cds_lfht_add_replace(ht
, hash
, match
, &value
,
109 struct mynode
*ret_node
=
110 caa_container_of(ht_node
, struct mynode
, node
);
112 printf("Replaced node (key: %d, seqnum: %d) by (key: %d, seqnum: %d)\n",
113 ret_node
->value
, ret_node
->seqnum
,
114 node
->value
, node
->seqnum
);
115 urcu_memb_call_rcu(&ret_node
->rcu_head
, free_node
);
117 printf("Add (key: %d, seqnum: %d)\n",
118 node
->value
, node
->seqnum
);
120 urcu_memb_read_unlock();
124 * Iterate over each hash table node. Those will appear in
125 * random order, depending on the hash seed. Iteration needs to
126 * be performed within RCU read-side critical section.
128 printf("hash table content (random order):");
129 urcu_memb_read_lock();
130 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
131 printf(" (key: %d, seqnum: %d)",
132 node
->value
, node
->seqnum
);
134 urcu_memb_read_unlock();
138 urcu_memb_unregister_thread();
This page took 0.033353 seconds and 5 git commands to generate.