Commit | Line | Data |
---|---|---|
a42cc659 MD |
1 | #ifndef _URCU_RCULFHASH_H |
2 | #define _URCU_RCULFHASH_H | |
ab7d5fc6 | 3 | |
f0c29ed7 MD |
4 | /* |
5 | * urcu/rculfhash.h | |
6 | * | |
7 | * Userspace RCU library - Lock-Free RCU Hash Table | |
8 | * | |
9 | * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
10 | * | |
11 | * This library is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU Lesser General Public | |
13 | * License as published by the Free Software Foundation; either | |
14 | * version 2.1 of the License, or (at your option) any later version. | |
15 | * | |
16 | * This library is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * Lesser General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU Lesser General Public | |
22 | * License along with this library; if not, write to the Free Software | |
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
cf77d1fa MD |
24 | * |
25 | * Include this file _after_ including your URCU flavor. | |
f0c29ed7 MD |
26 | */ |
27 | ||
674f7a69 | 28 | #include <stdint.h> |
abc490a1 MD |
29 | #include <urcu-call-rcu.h> |
30 | ||
01389791 MD |
31 | #ifdef __cplusplus |
32 | extern "C" { | |
33 | #endif | |
34 | ||
7f61a77f | 35 | /* |
14044b37 | 36 | * struct cds_lfht_node and struct _cds_lfht_node should be aligned on |
b198f0fd | 37 | * 4-bytes boundaries because the two lower bits are used as flags. |
7f61a77f MD |
38 | */ |
39 | ||
14044b37 | 40 | struct _cds_lfht_node { |
b198f0fd | 41 | struct cds_lfht_node *next; /* ptr | DUMMY_FLAG | REMOVED_FLAG */ |
abc490a1 | 42 | unsigned long reverse_hash; |
58ca0741 | 43 | } __attribute__((aligned(4))); |
cc4fcb10 | 44 | |
ae62aa6a MD |
45 | /* |
46 | * struct cds_lfht_node can be embedded into a structure (as a field). | |
47 | * caa_container_of() can be used to get the structure from the struct | |
48 | * cds_lfht_node after a lookup. | |
49 | */ | |
14044b37 | 50 | struct cds_lfht_node { |
cc4fcb10 | 51 | /* cache-hot for iteration */ |
14044b37 | 52 | struct _cds_lfht_node p; /* needs to be first field */ |
9b35f1e4 MD |
53 | void *key; |
54 | unsigned int key_len; | |
9b35f1e4 | 55 | /* cache-cold for iteration */ |
abc490a1 MD |
56 | struct rcu_head head; |
57 | }; | |
58 | ||
adc0de68 MD |
59 | struct cds_lfht_iter { |
60 | struct cds_lfht_node *node, *next; | |
61 | }; | |
62 | ||
63 | static inline | |
64 | struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter) | |
65 | { | |
66 | return iter->node; | |
67 | } | |
68 | ||
14044b37 | 69 | struct cds_lfht; |
ab7d5fc6 | 70 | |
5e28c532 MD |
71 | /* |
72 | * Caution ! | |
abc490a1 | 73 | * Ensure reader and writer threads are registered as urcu readers. |
5e28c532 MD |
74 | */ |
75 | ||
14044b37 MD |
76 | typedef unsigned long (*cds_lfht_hash_fct)(void *key, size_t length, |
77 | unsigned long seed); | |
78 | typedef unsigned long (*cds_lfht_compare_fct)(void *key1, size_t key1_len, | |
79 | void *key2, size_t key2_len); | |
abc490a1 | 80 | |
f0c29ed7 | 81 | /* |
14044b37 | 82 | * cds_lfht_node_init - initialize a hash table node |
f0c29ed7 | 83 | */ |
abc490a1 | 84 | static inline |
14044b37 MD |
85 | void cds_lfht_node_init(struct cds_lfht_node *node, void *key, |
86 | size_t key_len) | |
abc490a1 MD |
87 | { |
88 | node->key = key; | |
732ad076 | 89 | node->key_len = key_len; |
abc490a1 | 90 | } |
674f7a69 | 91 | |
b8af5011 MD |
92 | /* |
93 | * Hash table creation flags. | |
94 | */ | |
95 | enum { | |
96 | CDS_LFHT_AUTO_RESIZE = (1U << 0), | |
97 | }; | |
98 | ||
674f7a69 | 99 | /* |
7a9dcf9b | 100 | * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly. |
674f7a69 | 101 | */ |
7a9dcf9b | 102 | struct cds_lfht *_cds_lfht_new(cds_lfht_hash_fct hash_fct, |
14044b37 MD |
103 | cds_lfht_compare_fct compare_fct, |
104 | unsigned long hash_seed, | |
105 | unsigned long init_size, | |
b8af5011 | 106 | int flags, |
14044b37 | 107 | void (*cds_lfht_call_rcu)(struct rcu_head *head, |
1475579c | 108 | void (*func)(struct rcu_head *head)), |
01dbfa62 MD |
109 | void (*cds_lfht_synchronize_rcu)(void), |
110 | void (*cds_lfht_rcu_read_lock)(void), | |
5f511391 MD |
111 | void (*cds_lfht_rcu_read_unlock)(void), |
112 | void (*cds_lfht_rcu_thread_offline)(void), | |
b7d619b0 MD |
113 | void (*cds_lfht_rcu_thread_online)(void), |
114 | void (*cds_lfht_rcu_register_thread)(void), | |
115 | void (*cds_lfht_rcu_unregister_thread)(void), | |
116 | pthread_attr_t *attr); | |
ab7d5fc6 | 117 | |
7a9dcf9b MD |
118 | /* |
119 | * cds_lfht_new - allocate a hash table. | |
f22dd01d MD |
120 | * @hash_fct: the hashing function. |
121 | * @compare_fct: the key comparison function. | |
122 | * @hash_seed: the seed for hash function. | |
123 | * @init_size: number of nodes to allocate initially. Must be power of two. | |
124 | * @flags: hash table creation flags (can be combined with bitwise or: '|'). | |
125 | * 0: no flags. | |
126 | * CDS_LFHT_AUTO_RESIZE: automatically resize hash table. | |
b7d619b0 | 127 | * @attr: optional resize worker thread attributes. NULL for default. |
7a9dcf9b | 128 | * |
7a9dcf9b MD |
129 | * Return NULL on error. |
130 | * Note: the RCU flavor must be already included before the hash table header. | |
b7d619b0 MD |
131 | * |
132 | * The programmer is responsible for ensuring that resize operation has a | |
133 | * priority equal to hash table updater threads. It should be performed by | |
134 | * specifying the appropriate priority in the pthread "attr" argument, and, | |
135 | * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have | |
136 | * this priority level. Having lower priority for call_rcu and resize threads | |
137 | * does not pose any correctness issue, but the resize operations could be | |
138 | * starved by updates, thus leading to long hash table bucket chains. | |
7a9dcf9b MD |
139 | */ |
140 | static inline | |
141 | struct cds_lfht *cds_lfht_new(cds_lfht_hash_fct hash_fct, | |
142 | cds_lfht_compare_fct compare_fct, | |
143 | unsigned long hash_seed, | |
144 | unsigned long init_size, | |
b7d619b0 MD |
145 | int flags, |
146 | pthread_attr_t *attr) | |
7a9dcf9b MD |
147 | { |
148 | return _cds_lfht_new(hash_fct, compare_fct, hash_seed, | |
149 | init_size, flags, | |
150 | call_rcu, synchronize_rcu, rcu_read_lock, | |
151 | rcu_read_unlock, rcu_thread_offline, | |
b7d619b0 MD |
152 | rcu_thread_online, rcu_register_thread, |
153 | rcu_unregister_thread, attr); | |
7a9dcf9b MD |
154 | } |
155 | ||
f0c29ed7 | 156 | /* |
14044b37 | 157 | * cds_lfht_destroy - destroy a hash table. |
b7d619b0 MD |
158 | * @ht: the hash table to destroy. |
159 | * @attr: (output) resize worker thread attributes, as received by cds_lfht_new. | |
160 | * The caller will typically want to free this pointer if dynamically | |
161 | * allocated. | |
6878c72b MD |
162 | * |
163 | * Return 0 on success, negative error value on error. | |
f0c29ed7 | 164 | */ |
b7d619b0 | 165 | int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr); |
f0c29ed7 MD |
166 | |
167 | /* | |
14044b37 | 168 | * cds_lfht_count_nodes - count the number of nodes in the hash table. |
f0c29ed7 MD |
169 | * |
170 | * Call with rcu_read_lock held. | |
171 | */ | |
14044b37 | 172 | void cds_lfht_count_nodes(struct cds_lfht *ht, |
d933dd0e | 173 | long *approx_before, |
adc0de68 | 174 | unsigned long *count, |
973e5e1b | 175 | unsigned long *removed, |
d933dd0e | 176 | long *approx_after); |
ab7d5fc6 | 177 | |
f0c29ed7 | 178 | /* |
14044b37 | 179 | * cds_lfht_lookup - lookup a node by key. |
f0c29ed7 | 180 | * |
adc0de68 | 181 | * Output in "*iter". *iter->node set to NULL if not found. |
f0c29ed7 MD |
182 | * Call with rcu_read_lock held. |
183 | */ | |
adc0de68 MD |
184 | void cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len, |
185 | struct cds_lfht_iter *iter); | |
ab7d5fc6 | 186 | |
f0c29ed7 | 187 | /* |
3883c0e5 | 188 | * cds_lfht_next_duplicate - get the next item with same key (after a lookup). |
f0c29ed7 | 189 | * |
adc0de68 MD |
190 | * Uses an iterator initialized by a lookup. |
191 | * Sets *iter-node to the following node with same key. | |
192 | * Sets *iter->node to NULL if no following node exists with same key. | |
193 | * RCU read-side lock must be held across cds_lfht_lookup and | |
194 | * cds_lfht_next calls, and also between cds_lfht_next calls using the | |
195 | * node returned by a previous cds_lfht_next. | |
196 | * Call with rcu_read_lock held. | |
f0c29ed7 | 197 | */ |
3883c0e5 | 198 | void cds_lfht_next_duplicate(struct cds_lfht *ht, struct cds_lfht_iter *iter); |
4e9b9fbf MD |
199 | |
200 | /* | |
201 | * cds_lfht_first - get the first node in the table. | |
202 | * | |
203 | * Output in "*iter". *iter->node set to NULL if table is empty. | |
204 | * Call with rcu_read_lock held. | |
205 | */ | |
206 | void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter); | |
207 | ||
208 | /* | |
209 | * cds_lfht_next - get the next node in the table. | |
210 | * | |
211 | * Input/Output in "*iter". *iter->node set to NULL if *iter was | |
212 | * pointing to the last table node. | |
213 | * Call with rcu_read_lock held. | |
214 | */ | |
215 | void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter); | |
ab7d5fc6 | 216 | |
18117871 | 217 | /* |
14044b37 | 218 | * cds_lfht_add - add a node to the hash table. |
f0c29ed7 | 219 | * |
18117871 | 220 | * Call with rcu_read_lock held. |
48ed1c18 | 221 | * This function supports adding redundant keys into the table. |
f0c29ed7 | 222 | */ |
14044b37 | 223 | void cds_lfht_add(struct cds_lfht *ht, struct cds_lfht_node *node); |
f0c29ed7 MD |
224 | |
225 | /* | |
14044b37 | 226 | * cds_lfht_add_unique - add a node to hash table, if key is not present. |
f0c29ed7 | 227 | * |
6878c72b | 228 | * Return the node added upon success. |
860d07e8 MD |
229 | * Return the unique node already present upon failure. If |
230 | * cds_lfht_add_unique fails, the node passed as parameter should be | |
231 | * freed by the caller. | |
f0c29ed7 | 232 | * Call with rcu_read_lock held. |
48ed1c18 MD |
233 | * |
234 | * The semantic of this function is that if only this function is used | |
235 | * to add keys into the table, no duplicated keys should ever be | |
236 | * observable in the table. The same guarantee apply for combination of | |
9357c415 | 237 | * add_unique and add_replace (see below). |
18117871 | 238 | */ |
adc0de68 MD |
239 | struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht, |
240 | struct cds_lfht_node *node); | |
3eca1b8c | 241 | |
48ed1c18 | 242 | /* |
9357c415 | 243 | * cds_lfht_add_replace - replace or add a node within hash table. |
48ed1c18 MD |
244 | * |
245 | * Return the node replaced upon success. If no node matching the key | |
246 | * was present, return NULL, which also means the operation succeeded. | |
247 | * This replacement operation should never fail. | |
248 | * Call with rcu_read_lock held. | |
249 | * After successful replacement, a grace period must be waited for before | |
250 | * freeing the memory reserved for the returned node. | |
251 | * | |
252 | * The semantic of replacement vs lookups is the following: if lookups | |
9357c415 MD |
253 | * are performed between a key unique insertion and its removal, we |
254 | * guarantee that the lookups and get next will always find exactly one | |
255 | * instance of the key if it is replaced concurrently with the lookups. | |
48ed1c18 MD |
256 | * |
257 | * Providing this semantic allows us to ensure that replacement-only | |
258 | * schemes will never generate duplicated keys. It also allows us to | |
9357c415 | 259 | * guarantee that a combination of add_replace and add_unique updates |
48ed1c18 MD |
260 | * will never generate duplicated keys. |
261 | */ | |
9357c415 | 262 | struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht, |
adc0de68 | 263 | struct cds_lfht_node *node); |
48ed1c18 | 264 | |
f0c29ed7 | 265 | /* |
9357c415 | 266 | * cds_lfht_replace - replace a node pointer to by iter within hash table. |
f0c29ed7 | 267 | * |
9357c415 MD |
268 | * Return 0 if replacement is successful, negative value otherwise. |
269 | * Replacing a NULL old node or an already removed node will fail with a | |
270 | * negative value. | |
271 | * Old node can be looked up with cds_lfht_lookup and cds_lfht_next. | |
272 | * RCU read-side lock must be held between lookup and replacement. | |
273 | * Call with rcu_read_lock held. | |
274 | * After successful replacement, a grace period must be waited for before | |
275 | * freeing the memory reserved for the old node (which can be accessed | |
276 | * with cds_lfht_iter_get_node). | |
277 | * | |
278 | * The semantic of replacement vs lookups is the following: if lookups | |
279 | * are performed between a key unique insertion and its removal, we | |
280 | * guarantee that the lookups and get next will always find exactly one | |
281 | * instance of the key if it is replaced concurrently with the lookups. | |
282 | * | |
283 | * Providing this semantic allows us to ensure that replacement-only | |
284 | * schemes will never generate duplicated keys. It also allows us to | |
285 | * guarantee that a combination of add_replace and add_unique updates | |
286 | * will never generate duplicated keys. | |
287 | */ | |
288 | int cds_lfht_replace(struct cds_lfht *ht, struct cds_lfht_iter *old_iter, | |
289 | struct cds_lfht_node *new_node); | |
290 | ||
291 | /* | |
292 | * cds_lfht_del - remove node pointed to by iterator from hash table. | |
293 | * | |
294 | * Return 0 if the node is successfully removed, negative value | |
295 | * otherwise. | |
296 | * Replacing a NULL node or an already removed node will fail with a | |
297 | * negative value. | |
298 | * Node can be looked up with cds_lfht_lookup and cds_lfht_next. | |
299 | * cds_lfht_iter_get_node. | |
300 | * RCU read-side lock must be held between lookup and removal. | |
f0c29ed7 | 301 | * Call with rcu_read_lock held. |
48ed1c18 | 302 | * After successful removal, a grace period must be waited for before |
9357c415 MD |
303 | * freeing the memory reserved for old node (which can be accessed with |
304 | * cds_lfht_iter_get_node). | |
f0c29ed7 | 305 | */ |
9357c415 | 306 | int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter); |
ab7d5fc6 | 307 | |
f0c29ed7 | 308 | /* |
14044b37 | 309 | * cds_lfht_resize - Force a hash table resize |
1475579c | 310 | * @new_size: update to this hash table size. |
f0c29ed7 | 311 | */ |
1475579c | 312 | void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size); |
464a1ec9 | 313 | |
01389791 MD |
314 | #ifdef __cplusplus |
315 | } | |
316 | #endif | |
317 | ||
a42cc659 | 318 | #endif /* _URCU_RCULFHASH_H */ |