Commit | Line | Data |
---|---|---|
f7e7a1b8 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 remove nodes from a RCU lock-free hash table. | |
14 | * 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 */ |
f7e7a1b8 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 | struct cds_lfht_node node; /* Chaining in hash table */ | |
32 | struct rcu_head rcu_head; /* For call_rcu() */ | |
33 | }; | |
34 | ||
34b3f359 | 35 | static |
f7e7a1b8 MD |
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; |
f7e7a1b8 MD |
41 | |
42 | return *key == node->value; | |
43 | } | |
44 | ||
34b3f359 MD |
45 | static |
46 | void free_node(struct rcu_head *head) | |
47 | { | |
48 | struct mynode *node = caa_container_of(head, struct mynode, rcu_head); | |
49 | ||
50 | free(node); | |
51 | } | |
52 | ||
70469b43 | 53 | int main(void) |
f7e7a1b8 MD |
54 | { |
55 | int values[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */ | |
56 | int remove_values[] = { 42, 36, 24, 123, }; | |
57 | struct cds_lfht *ht; /* Hash table */ | |
58 | unsigned int i; | |
59 | int ret = 0; | |
60 | uint32_t seed; | |
61 | struct cds_lfht_iter iter; /* For iteration on hash table */ | |
62 | struct cds_lfht_node *ht_node; | |
63 | struct mynode *node; | |
64 | ||
65 | /* | |
66 | * Each thread need using RCU read-side need to be explicitly | |
67 | * registered. | |
68 | */ | |
b9050d91 | 69 | urcu_memb_register_thread(); |
f7e7a1b8 MD |
70 | |
71 | /* Use time as seed for hash table hashing. */ | |
72 | seed = (uint32_t) time(NULL); | |
73 | ||
74 | /* | |
75 | * Allocate hash table. | |
76 | */ | |
b9050d91 | 77 | ht = cds_lfht_new_flavor(1, 1, 0, |
f7e7a1b8 | 78 | CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, |
b9050d91 | 79 | &urcu_memb_flavor, NULL); |
f7e7a1b8 MD |
80 | if (!ht) { |
81 | printf("Error allocating hash table\n"); | |
82 | ret = -1; | |
83 | goto end; | |
84 | } | |
85 | ||
86 | /* | |
87 | * Add nodes to hash table. | |
88 | */ | |
89 | for (i = 0; i < CAA_ARRAY_SIZE(values); i++) { | |
90 | unsigned long hash; | |
91 | int value; | |
92 | ||
93 | node = malloc(sizeof(*node)); | |
94 | if (!node) { | |
95 | ret = -1; | |
96 | goto end; | |
97 | } | |
98 | ||
99 | cds_lfht_node_init(&node->node); | |
100 | value = values[i]; | |
101 | node->value = value; | |
102 | hash = jhash(&value, sizeof(value), seed); | |
103 | ||
104 | /* | |
105 | * cds_lfht_add() needs to be called from RCU read-side | |
106 | * critical section. | |
107 | */ | |
b9050d91 | 108 | urcu_memb_read_lock(); |
f7e7a1b8 | 109 | cds_lfht_add(ht, hash, &node->node); |
b9050d91 | 110 | urcu_memb_read_unlock(); |
f7e7a1b8 MD |
111 | } |
112 | ||
113 | /* | |
114 | * Iterate over each hash table node. Those will appear in | |
115 | * random order, depending on the hash seed. Iteration needs to | |
116 | * be performed within RCU read-side critical section. | |
117 | */ | |
118 | printf("hash table content (random order):"); | |
b9050d91 | 119 | urcu_memb_read_lock(); |
f7e7a1b8 MD |
120 | cds_lfht_for_each_entry(ht, &iter, node, node) { |
121 | printf(" %d", node->value); | |
122 | } | |
b9050d91 | 123 | urcu_memb_read_unlock(); |
f7e7a1b8 MD |
124 | printf("\n"); |
125 | ||
126 | /* | |
127 | * Remove one node for each key, if such a node is present. | |
128 | */ | |
129 | printf("removing keys (single key, not duplicates):"); | |
130 | for (i = 0; i < CAA_ARRAY_SIZE(remove_values); i++) { | |
131 | unsigned long hash; | |
132 | int value; | |
133 | ||
134 | value = remove_values[i]; | |
135 | hash = jhash(&value, sizeof(value), seed); | |
136 | printf(" %d", value); | |
b9050d91 | 137 | urcu_memb_read_lock(); |
f7e7a1b8 MD |
138 | cds_lfht_lookup(ht, hash, match, &value, &iter); |
139 | ht_node = cds_lfht_iter_get_node(&iter); | |
140 | if (ht_node) { | |
141 | ret = cds_lfht_del(ht, ht_node); | |
142 | if (ret) { | |
143 | printf(" (concurrently deleted)"); | |
34b3f359 MD |
144 | } else { |
145 | struct mynode *del_node = | |
146 | caa_container_of(ht_node, | |
147 | struct mynode, node); | |
b9050d91 | 148 | urcu_memb_call_rcu(&del_node->rcu_head, free_node); |
f7e7a1b8 MD |
149 | } |
150 | } else { | |
151 | printf(" (not found)"); | |
152 | } | |
b9050d91 | 153 | urcu_memb_read_unlock(); |
f7e7a1b8 MD |
154 | } |
155 | printf("\n"); | |
156 | ||
157 | printf("hash table content (random order):"); | |
b9050d91 | 158 | urcu_memb_read_lock(); |
f7e7a1b8 MD |
159 | cds_lfht_for_each_entry(ht, &iter, node, node) { |
160 | printf(" %d", node->value); | |
161 | } | |
b9050d91 | 162 | urcu_memb_read_unlock(); |
f7e7a1b8 MD |
163 | printf("\n"); |
164 | ||
165 | end: | |
b9050d91 | 166 | urcu_memb_unregister_thread(); |
f7e7a1b8 MD |
167 | return ret; |
168 | } |