927e38d99c24dba7ecf8ef667f549d0b3c631ca2
1 // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: MIT
6 * This example shows how to add nodes (allowing duplicate keys) into a
7 * RCU lock-free hash table.
8 * This hash table requires using a RCU scheme.
15 #include <urcu/urcu-memb.h> /* RCU flavor */
16 #include <urcu/rculfhash.h> /* RCU Lock-free hash table */
17 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
18 #include "jhash.h" /* Example hash function */
21 * Nodes populated into the hash table.
24 int value
; /* Node content */
25 struct cds_lfht_node node
; /* Chaining in hash table */
30 int values
[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */
31 struct cds_lfht
*ht
; /* Hash table */
35 struct cds_lfht_iter iter
; /* For iteration on hash table */
39 * Each thread need using RCU read-side need to be explicitly
42 urcu_memb_register_thread();
44 /* Use time as seed for hash table hashing. */
45 seed
= (uint32_t) time(NULL
);
48 * Allocate hash table.
50 ht
= cds_lfht_new_flavor(1, 1, 0,
51 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
,
52 &urcu_memb_flavor
, NULL
);
54 printf("Error allocating hash table\n");
60 * Add nodes to hash table.
62 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
65 node
= malloc(sizeof(*node
));
71 cds_lfht_node_init(&node
->node
);
72 node
->value
= values
[i
];
73 hash
= jhash(&values
[i
], sizeof(values
[i
]), seed
);
76 * cds_lfht_add() needs to be called from RCU read-side
79 urcu_memb_read_lock();
80 cds_lfht_add(ht
, hash
, &node
->node
);
81 urcu_memb_read_unlock();
85 * Iterate over each hash table node. Those will appear in
86 * random order, depending on the hash seed. Iteration needs to
87 * be performed within RCU read-side critical section.
89 printf("hash table content (random order):");
90 urcu_memb_read_lock();
91 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
92 printf(" %d", node
->value
);
94 urcu_memb_read_unlock();
97 urcu_memb_unregister_thread();
This page took 0.032117 seconds and 5 git commands to generate.