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