uatomic/x86: Remove redundant memory barriers
[urcu.git] / doc / examples / rculfhash / cds_lfht_for_each_entry_duplicate.c
CommitLineData
1c87adb3
MJ
1// SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2//
3// SPDX-License-Identifier: MIT
4
b5e35d2d 5/*
b5e35d2d
MD
6 * This example shows how to iterate on duplicate keys within a RCU
7 * lock-free hash table. This hash table requires using a RCU scheme.
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <time.h>
13
b9050d91 14#include <urcu/urcu-memb.h> /* RCU flavor */
b5e35d2d
MD
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 */
18
19/*
20 * Nodes populated into the hash table.
21 */
22struct mynode {
23 int value; /* Node content */
24 int seqnum; /* Our node sequence number */
25 struct cds_lfht_node node; /* Chaining in hash table */
26};
27
28static
29int match(struct cds_lfht_node *ht_node, const void *_key)
30{
31 struct mynode *node =
32 caa_container_of(ht_node, struct mynode, node);
83e334d0 33 const int *key = _key;
b5e35d2d
MD
34
35 return *key == node->value;
36}
37
70469b43 38int main(void)
b5e35d2d
MD
39{
40 int values[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */
41 int lookup_values[] = { 42, 200, 36, };
42 struct cds_lfht *ht; /* Hash table */
43 unsigned int i;
44 int ret = 0, seqnum = 0;
45 uint32_t seed;
46 struct cds_lfht_iter iter; /* For iteration on hash table */
47 struct mynode *node;
48
49 /*
50 * Each thread need using RCU read-side need to be explicitly
51 * registered.
52 */
b9050d91 53 urcu_memb_register_thread();
b5e35d2d
MD
54
55 /* Use time as seed for hash table hashing. */
56 seed = (uint32_t) time(NULL);
57
58 /*
59 * Allocate hash table.
60 */
b9050d91 61 ht = cds_lfht_new_flavor(1, 1, 0,
b5e35d2d 62 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING,
b9050d91 63 &urcu_memb_flavor, NULL);
b5e35d2d
MD
64 if (!ht) {
65 printf("Error allocating hash table\n");
66 ret = -1;
67 goto end;
68 }
69
70 /*
71 * Add nodes to hash table.
72 */
73 for (i = 0; i < CAA_ARRAY_SIZE(values); i++) {
74 unsigned long hash;
75 int value;
76
77 node = malloc(sizeof(*node));
78 if (!node) {
79 ret = -1;
80 goto end;
81 }
82
83 cds_lfht_node_init(&node->node);
84 value = values[i];
85 node->value = value;
86 node->seqnum = seqnum++;
87 hash = jhash(&value, sizeof(value), seed);
88
89 /*
90 * cds_lfht_add() needs to be called from RCU read-side
91 * critical section.
92 */
b9050d91 93 urcu_memb_read_lock();
b5e35d2d
MD
94 cds_lfht_add(ht, hash, &node->node);
95 printf("Add (key: %d, seqnum: %d)\n",
96 node->value, node->seqnum);
b9050d91 97 urcu_memb_read_unlock();
b5e35d2d
MD
98 }
99
100 /*
101 * Iterate over each hash table node. Those will appear in
102 * random order, depending on the hash seed. Iteration needs to
103 * be performed within RCU read-side critical section.
104 */
105 printf("hash table content (random order):");
b9050d91 106 urcu_memb_read_lock();
b5e35d2d
MD
107 cds_lfht_for_each_entry(ht, &iter, node, node) {
108 printf(" (key: %d, seqnum: %d)",
109 node->value, node->seqnum);
110 }
b9050d91 111 urcu_memb_read_unlock();
b5e35d2d
MD
112 printf("\n");
113
114 /*
115 * Lookup queries. Note that which node (seqnum) within
116 * duplicates will be found by lookup is random.
117 */
118 printf("Lookups, with iteration on duplicates:\n");
119 for (i = 0; i < CAA_ARRAY_SIZE(lookup_values); i++) {
120 int value = lookup_values[i];
121 unsigned long hash = jhash(&value, sizeof(value), seed);
122
123 printf("lookup key: %d\n", value);
b9050d91 124 urcu_memb_read_lock();
b5e35d2d
MD
125 cds_lfht_for_each_entry_duplicate(ht, hash, match,
126 &value, &iter, node, node) {
127 printf(" (key %d, seqnum %d) found\n",
128 node->value, node->seqnum);
129 }
b9050d91 130 urcu_memb_read_unlock();
b5e35d2d
MD
131 }
132
133end:
b9050d91 134 urcu_memb_unregister_thread();
b5e35d2d
MD
135 return ret;
136}
This page took 0.042157 seconds and 5 git commands to generate.