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