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