rculfhash: Move "struct rcu_head head" out of "struct cds_lfht_node"
[urcu.git] / urcu / rculfhash.h
1 #ifndef _URCU_RCULFHASH_H
2 #define _URCU_RCULFHASH_H
3
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
24 *
25 * Include this file _after_ including your URCU flavor.
26 */
27
28 #include <stdint.h>
29 #include <urcu-call-rcu.h>
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /*
36 * struct cds_lfht_node and struct _cds_lfht_node should be aligned on
37 * 4-bytes boundaries because the two lower bits are used as flags.
38 */
39
40 /*
41 * _cds_lfht_node: Contains the internal pointers and reverse-hash
42 * value required for lookup and traversal of the hash table.
43 */
44 struct _cds_lfht_node {
45 struct cds_lfht_node *next; /* ptr | DUMMY_FLAG | REMOVED_FLAG */
46 unsigned long reverse_hash;
47 } __attribute__((aligned(4)));
48
49 /*
50 * cds_lfht_node: Contains the full key and length required to check for
51 * an actual match, and also contains an rcu_head structure that is used
52 * by RCU to track a node through a given RCU grace period. There is an
53 * instance of _cds_lfht_node enclosed as a field within each
54 * _cds_lfht_node structure.
55 *
56 * struct cds_lfht_node can be embedded into a structure (as a field).
57 * caa_container_of() can be used to get the structure from the struct
58 * cds_lfht_node after a lookup.
59 */
60 struct cds_lfht_node {
61 /* cache-hot for iteration */
62 struct _cds_lfht_node p; /* needs to be first field */
63 void *key;
64 unsigned int key_len;
65 };
66
67 /* cds_lfht_iter: Used to track state while traversing a hash chain. */
68 struct cds_lfht_iter {
69 struct cds_lfht_node *node, *next;
70 };
71
72 static inline
73 struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter)
74 {
75 return iter->node;
76 }
77
78 struct cds_lfht;
79
80 /*
81 * Caution !
82 * Ensure reader and writer threads are registered as urcu readers.
83 */
84
85 typedef unsigned long (*cds_lfht_hash_fct)(void *key, size_t length,
86 unsigned long seed);
87 typedef unsigned long (*cds_lfht_compare_fct)(void *key1, size_t key1_len,
88 void *key2, size_t key2_len);
89
90 /*
91 * cds_lfht_node_init - initialize a hash table node
92 */
93 static inline
94 void cds_lfht_node_init(struct cds_lfht_node *node, void *key,
95 size_t key_len)
96 {
97 node->key = key;
98 node->key_len = key_len;
99 }
100
101 /*
102 * Hash table creation flags.
103 */
104 enum {
105 CDS_LFHT_AUTO_RESIZE = (1U << 0),
106 CDS_LFHT_ACCOUNTING = (1U << 1),
107 };
108
109 /*
110 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
111 */
112 struct cds_lfht *_cds_lfht_new(cds_lfht_hash_fct hash_fct,
113 cds_lfht_compare_fct compare_fct,
114 unsigned long hash_seed,
115 unsigned long init_size,
116 unsigned long min_alloc_size,
117 int flags,
118 void (*cds_lfht_call_rcu)(struct rcu_head *head,
119 void (*func)(struct rcu_head *head)),
120 void (*cds_lfht_synchronize_rcu)(void),
121 void (*cds_lfht_rcu_read_lock)(void),
122 void (*cds_lfht_rcu_read_unlock)(void),
123 void (*cds_lfht_rcu_thread_offline)(void),
124 void (*cds_lfht_rcu_thread_online)(void),
125 void (*cds_lfht_rcu_register_thread)(void),
126 void (*cds_lfht_rcu_unregister_thread)(void),
127 pthread_attr_t *attr);
128
129 /*
130 * cds_lfht_new - allocate a hash table.
131 * @hash_fct: the hashing function.
132 * @compare_fct: the key comparison function.
133 * @hash_seed: the seed for hash function.
134 * @init_size: number of nodes to allocate initially. Must be power of two.
135 * @min_alloc_size: the smallest allocation size to use. Must be power of two.
136 * @flags: hash table creation flags (can be combined with bitwise or: '|').
137 * 0: no flags.
138 * CDS_LFHT_AUTO_RESIZE: automatically resize hash table.
139 * @attr: optional resize worker thread attributes. NULL for default.
140 *
141 * Return NULL on error.
142 * Note: the RCU flavor must be already included before the hash table header.
143 *
144 * The programmer is responsible for ensuring that resize operation has a
145 * priority equal to hash table updater threads. It should be performed by
146 * specifying the appropriate priority in the pthread "attr" argument, and,
147 * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have
148 * this priority level. Having lower priority for call_rcu and resize threads
149 * does not pose any correctness issue, but the resize operations could be
150 * starved by updates, thus leading to long hash table bucket chains.
151 * Threads calling this API need to be registered RCU read-side threads.
152 */
153 static inline
154 struct cds_lfht *cds_lfht_new(cds_lfht_hash_fct hash_fct,
155 cds_lfht_compare_fct compare_fct,
156 unsigned long hash_seed,
157 unsigned long init_size,
158 unsigned long min_alloc_size,
159 int flags,
160 pthread_attr_t *attr)
161 {
162 return _cds_lfht_new(hash_fct, compare_fct, hash_seed,
163 init_size, min_alloc_size, flags,
164 call_rcu, synchronize_rcu, rcu_read_lock,
165 rcu_read_unlock, rcu_thread_offline,
166 rcu_thread_online, rcu_register_thread,
167 rcu_unregister_thread, attr);
168 }
169
170 /*
171 * cds_lfht_destroy - destroy a hash table.
172 * @ht: the hash table to destroy.
173 * @attr: (output) resize worker thread attributes, as received by cds_lfht_new.
174 * The caller will typically want to free this pointer if dynamically
175 * allocated. The attr point can be NULL if the caller does not
176 * need to be informed of the value passed to cds_lfht_new().
177 *
178 * Return 0 on success, negative error value on error.
179 * Threads calling this API need to be registered RCU read-side threads.
180 */
181 int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr);
182
183 /*
184 * cds_lfht_count_nodes - count the number of nodes in the hash table.
185 * @ht: the hash table.
186 * @split_count_before: Sample the node count split-counter before traversal.
187 * @count: Traverse the hash table, count the number of nodes observed.
188 * @removed: Number of logically removed nodes observed during traversal.
189 * @split_count_after: Sample the node count split-counter after traversal.
190 * Call with rcu_read_lock held.
191 * Threads calling this API need to be registered RCU read-side threads.
192 */
193 void cds_lfht_count_nodes(struct cds_lfht *ht,
194 long *split_count_before,
195 unsigned long *count,
196 unsigned long *removed,
197 long *split_count_after);
198
199 /*
200 * cds_lfht_lookup - lookup a node by key.
201 *
202 * Output in "*iter". *iter->node set to NULL if not found.
203 * Call with rcu_read_lock held.
204 * Threads calling this API need to be registered RCU read-side threads.
205 */
206 void cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len,
207 struct cds_lfht_iter *iter);
208
209 /*
210 * cds_lfht_next_duplicate - get the next item with same key (after a lookup).
211 *
212 * Uses an iterator initialized by a lookup.
213 * Sets *iter-node to the following node with same key.
214 * Sets *iter->node to NULL if no following node exists with same key.
215 * RCU read-side lock must be held across cds_lfht_lookup and
216 * cds_lfht_next calls, and also between cds_lfht_next calls using the
217 * node returned by a previous cds_lfht_next.
218 * Call with rcu_read_lock held.
219 * Threads calling this API need to be registered RCU read-side threads.
220 */
221 void cds_lfht_next_duplicate(struct cds_lfht *ht, struct cds_lfht_iter *iter);
222
223 /*
224 * cds_lfht_first - get the first node in the table.
225 *
226 * Output in "*iter". *iter->node set to NULL if table is empty.
227 * Call with rcu_read_lock held.
228 * Threads calling this API need to be registered RCU read-side threads.
229 */
230 void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter);
231
232 /*
233 * cds_lfht_next - get the next node in the table.
234 *
235 * Input/Output in "*iter". *iter->node set to NULL if *iter was
236 * pointing to the last table node.
237 * Call with rcu_read_lock held.
238 * Threads calling this API need to be registered RCU read-side threads.
239 */
240 void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter);
241
242 /*
243 * cds_lfht_add - add a node to the hash table.
244 *
245 * This function supports adding redundant keys into the table.
246 * Call with rcu_read_lock held.
247 * Threads calling this API need to be registered RCU read-side threads.
248 */
249 void cds_lfht_add(struct cds_lfht *ht, struct cds_lfht_node *node);
250
251 /*
252 * cds_lfht_add_unique - add a node to hash table, if key is not present.
253 *
254 * Return the node added upon success.
255 * Return the unique node already present upon failure. If
256 * cds_lfht_add_unique fails, the node passed as parameter should be
257 * freed by the caller.
258 * Call with rcu_read_lock held.
259 * Threads calling this API need to be registered RCU read-side threads.
260 *
261 * The semantic of this function is that if only this function is used
262 * to add keys into the table, no duplicated keys should ever be
263 * observable in the table. The same guarantee apply for combination of
264 * add_unique and add_replace (see below).
265 */
266 struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
267 struct cds_lfht_node *node);
268
269 /*
270 * cds_lfht_add_replace - replace or add a node within hash table.
271 *
272 * Return the node replaced upon success. If no node matching the key
273 * was present, return NULL, which also means the operation succeeded.
274 * This replacement operation should never fail.
275 * Call with rcu_read_lock held.
276 * Threads calling this API need to be registered RCU read-side threads.
277 * After successful replacement, a grace period must be waited for before
278 * freeing the memory reserved for the returned node.
279 *
280 * The semantic of replacement vs lookups is the following: if lookups
281 * are performed between a key unique insertion and its removal, we
282 * guarantee that the lookups and get next will always find exactly one
283 * instance of the key if it is replaced concurrently with the lookups.
284 *
285 * Providing this semantic allows us to ensure that replacement-only
286 * schemes will never generate duplicated keys. It also allows us to
287 * guarantee that a combination of add_replace and add_unique updates
288 * will never generate duplicated keys.
289 */
290 struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
291 struct cds_lfht_node *node);
292
293 /*
294 * cds_lfht_replace - replace a node pointer to by iter within hash table.
295 *
296 * Return 0 if replacement is successful, negative value otherwise.
297 * Replacing a NULL old node or an already removed node will fail with a
298 * negative value.
299 * Old node can be looked up with cds_lfht_lookup and cds_lfht_next.
300 * RCU read-side lock must be held between lookup and replacement.
301 * Call with rcu_read_lock held.
302 * Threads calling this API need to be registered RCU read-side threads.
303 * After successful replacement, a grace period must be waited for before
304 * freeing the memory reserved for the old node (which can be accessed
305 * with cds_lfht_iter_get_node).
306 *
307 * The semantic of replacement vs lookups is the following: if lookups
308 * are performed between a key unique insertion and its removal, we
309 * guarantee that the lookups and get next will always find exactly one
310 * instance of the key if it is replaced concurrently with the lookups.
311 *
312 * Providing this semantic allows us to ensure that replacement-only
313 * schemes will never generate duplicated keys. It also allows us to
314 * guarantee that a combination of add_replace and add_unique updates
315 * will never generate duplicated keys.
316 */
317 int cds_lfht_replace(struct cds_lfht *ht, struct cds_lfht_iter *old_iter,
318 struct cds_lfht_node *new_node);
319
320 /*
321 * cds_lfht_del - remove node pointed to by iterator from hash table.
322 *
323 * Return 0 if the node is successfully removed, negative value
324 * otherwise.
325 * Replacing a NULL node or an already removed node will fail with a
326 * negative value.
327 * Node can be looked up with cds_lfht_lookup and cds_lfht_next.
328 * cds_lfht_iter_get_node.
329 * RCU read-side lock must be held between lookup and removal.
330 * Call with rcu_read_lock held.
331 * Threads calling this API need to be registered RCU read-side threads.
332 * After successful removal, a grace period must be waited for before
333 * freeing the memory reserved for old node (which can be accessed with
334 * cds_lfht_iter_get_node).
335 */
336 int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter);
337
338 /*
339 * cds_lfht_resize - Force a hash table resize
340 * @new_size: update to this hash table size.
341 *
342 * Threads calling this API need to be registered RCU read-side threads.
343 */
344 void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size);
345
346 #ifdef __cplusplus
347 }
348 #endif
349
350 #endif /* _URCU_RCULFHASH_H */
This page took 0.036455 seconds and 5 git commands to generate.