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 |
48ed1c18 | 37 | * 8-bytes boundaries because the two lower bits are used as flags. |
7f61a77f MD |
38 | */ |
39 | ||
14044b37 | 40 | struct _cds_lfht_node { |
48ed1c18 | 41 | struct cds_lfht_node *next; /* ptr | DUMMY_FLAG | GC_FLAG | REMOVED_FLAG */ |
abc490a1 | 42 | unsigned long reverse_hash; |
48ed1c18 | 43 | } __attribute__((aligned(8))); |
cc4fcb10 | 44 | |
14044b37 | 45 | struct cds_lfht_node { |
cc4fcb10 | 46 | /* cache-hot for iteration */ |
14044b37 | 47 | struct _cds_lfht_node p; /* needs to be first field */ |
9b35f1e4 MD |
48 | void *key; |
49 | unsigned int key_len; | |
9b35f1e4 | 50 | /* cache-cold for iteration */ |
abc490a1 MD |
51 | struct rcu_head head; |
52 | }; | |
53 | ||
14044b37 | 54 | struct cds_lfht; |
ab7d5fc6 | 55 | |
5e28c532 MD |
56 | /* |
57 | * Caution ! | |
abc490a1 | 58 | * Ensure reader and writer threads are registered as urcu readers. |
5e28c532 MD |
59 | */ |
60 | ||
14044b37 MD |
61 | typedef unsigned long (*cds_lfht_hash_fct)(void *key, size_t length, |
62 | unsigned long seed); | |
63 | typedef unsigned long (*cds_lfht_compare_fct)(void *key1, size_t key1_len, | |
64 | void *key2, size_t key2_len); | |
abc490a1 | 65 | |
f0c29ed7 | 66 | /* |
14044b37 | 67 | * cds_lfht_node_init - initialize a hash table node |
f0c29ed7 | 68 | */ |
abc490a1 | 69 | static inline |
14044b37 MD |
70 | void cds_lfht_node_init(struct cds_lfht_node *node, void *key, |
71 | size_t key_len) | |
abc490a1 MD |
72 | { |
73 | node->key = key; | |
732ad076 | 74 | node->key_len = key_len; |
abc490a1 | 75 | } |
674f7a69 | 76 | |
b8af5011 MD |
77 | /* |
78 | * Hash table creation flags. | |
79 | */ | |
80 | enum { | |
81 | CDS_LFHT_AUTO_RESIZE = (1U << 0), | |
82 | }; | |
83 | ||
674f7a69 | 84 | /* |
7a9dcf9b | 85 | * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly. |
674f7a69 | 86 | */ |
7a9dcf9b | 87 | struct cds_lfht *_cds_lfht_new(cds_lfht_hash_fct hash_fct, |
14044b37 MD |
88 | cds_lfht_compare_fct compare_fct, |
89 | unsigned long hash_seed, | |
90 | unsigned long init_size, | |
b8af5011 | 91 | int flags, |
14044b37 | 92 | void (*cds_lfht_call_rcu)(struct rcu_head *head, |
1475579c | 93 | void (*func)(struct rcu_head *head)), |
01dbfa62 MD |
94 | void (*cds_lfht_synchronize_rcu)(void), |
95 | void (*cds_lfht_rcu_read_lock)(void), | |
5f511391 MD |
96 | void (*cds_lfht_rcu_read_unlock)(void), |
97 | void (*cds_lfht_rcu_thread_offline)(void), | |
b7d619b0 MD |
98 | void (*cds_lfht_rcu_thread_online)(void), |
99 | void (*cds_lfht_rcu_register_thread)(void), | |
100 | void (*cds_lfht_rcu_unregister_thread)(void), | |
101 | pthread_attr_t *attr); | |
ab7d5fc6 | 102 | |
7a9dcf9b MD |
103 | /* |
104 | * cds_lfht_new - allocate a hash table. | |
f22dd01d MD |
105 | * @hash_fct: the hashing function. |
106 | * @compare_fct: the key comparison function. | |
107 | * @hash_seed: the seed for hash function. | |
108 | * @init_size: number of nodes to allocate initially. Must be power of two. | |
109 | * @flags: hash table creation flags (can be combined with bitwise or: '|'). | |
110 | * 0: no flags. | |
111 | * CDS_LFHT_AUTO_RESIZE: automatically resize hash table. | |
b7d619b0 | 112 | * @attr: optional resize worker thread attributes. NULL for default. |
7a9dcf9b | 113 | * |
7a9dcf9b MD |
114 | * Return NULL on error. |
115 | * Note: the RCU flavor must be already included before the hash table header. | |
b7d619b0 MD |
116 | * |
117 | * The programmer is responsible for ensuring that resize operation has a | |
118 | * priority equal to hash table updater threads. It should be performed by | |
119 | * specifying the appropriate priority in the pthread "attr" argument, and, | |
120 | * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have | |
121 | * this priority level. Having lower priority for call_rcu and resize threads | |
122 | * does not pose any correctness issue, but the resize operations could be | |
123 | * starved by updates, thus leading to long hash table bucket chains. | |
7a9dcf9b MD |
124 | */ |
125 | static inline | |
126 | struct cds_lfht *cds_lfht_new(cds_lfht_hash_fct hash_fct, | |
127 | cds_lfht_compare_fct compare_fct, | |
128 | unsigned long hash_seed, | |
129 | unsigned long init_size, | |
b7d619b0 MD |
130 | int flags, |
131 | pthread_attr_t *attr) | |
7a9dcf9b MD |
132 | { |
133 | return _cds_lfht_new(hash_fct, compare_fct, hash_seed, | |
134 | init_size, flags, | |
135 | call_rcu, synchronize_rcu, rcu_read_lock, | |
136 | rcu_read_unlock, rcu_thread_offline, | |
b7d619b0 MD |
137 | rcu_thread_online, rcu_register_thread, |
138 | rcu_unregister_thread, attr); | |
7a9dcf9b MD |
139 | } |
140 | ||
f0c29ed7 | 141 | /* |
14044b37 | 142 | * cds_lfht_destroy - destroy a hash table. |
b7d619b0 MD |
143 | * @ht: the hash table to destroy. |
144 | * @attr: (output) resize worker thread attributes, as received by cds_lfht_new. | |
145 | * The caller will typically want to free this pointer if dynamically | |
146 | * allocated. | |
6878c72b MD |
147 | * |
148 | * Return 0 on success, negative error value on error. | |
f0c29ed7 | 149 | */ |
b7d619b0 | 150 | int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr); |
f0c29ed7 MD |
151 | |
152 | /* | |
14044b37 | 153 | * cds_lfht_count_nodes - count the number of nodes in the hash table. |
f0c29ed7 MD |
154 | * |
155 | * Call with rcu_read_lock held. | |
156 | */ | |
14044b37 MD |
157 | void cds_lfht_count_nodes(struct cds_lfht *ht, |
158 | unsigned long *count, | |
159 | unsigned long *removed); | |
ab7d5fc6 | 160 | |
f0c29ed7 | 161 | /* |
14044b37 | 162 | * cds_lfht_lookup - lookup a node by key. |
f0c29ed7 | 163 | * |
6878c72b | 164 | * Return NULL if not found. |
f0c29ed7 MD |
165 | * Call with rcu_read_lock held. |
166 | */ | |
14044b37 | 167 | struct cds_lfht_node *cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len); |
ab7d5fc6 | 168 | |
f0c29ed7 | 169 | /* |
14044b37 | 170 | * cds_lfht_next - get the next item with same key (after a lookup). |
f0c29ed7 | 171 | * |
6878c72b | 172 | * Return NULL if no following node exists with same key. |
14044b37 | 173 | * RCU read-side lock must be held across cds_lfht_lookup and cds_lfht_next calls, and also |
860d07e8 MD |
174 | * between cds_lfht_next calls using the node returned by a previous |
175 | * cds_lfht_next. Call with rcu_read_lock held. | |
f0c29ed7 | 176 | */ |
14044b37 | 177 | struct cds_lfht_node *cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_node *node); |
ab7d5fc6 | 178 | |
18117871 | 179 | /* |
14044b37 | 180 | * cds_lfht_add - add a node to the hash table. |
f0c29ed7 | 181 | * |
18117871 | 182 | * Call with rcu_read_lock held. |
48ed1c18 | 183 | * This function supports adding redundant keys into the table. |
f0c29ed7 | 184 | */ |
14044b37 | 185 | void cds_lfht_add(struct cds_lfht *ht, struct cds_lfht_node *node); |
f0c29ed7 MD |
186 | |
187 | /* | |
14044b37 | 188 | * cds_lfht_add_unique - add a node to hash table, if key is not present. |
f0c29ed7 | 189 | * |
6878c72b | 190 | * Return the node added upon success. |
860d07e8 MD |
191 | * Return the unique node already present upon failure. If |
192 | * cds_lfht_add_unique fails, the node passed as parameter should be | |
193 | * freed by the caller. | |
f0c29ed7 | 194 | * Call with rcu_read_lock held. |
48ed1c18 MD |
195 | * |
196 | * The semantic of this function is that if only this function is used | |
197 | * to add keys into the table, no duplicated keys should ever be | |
198 | * observable in the table. The same guarantee apply for combination of | |
199 | * add_unique and replace (see below). | |
18117871 | 200 | */ |
14044b37 | 201 | struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht, struct cds_lfht_node *node); |
3eca1b8c | 202 | |
48ed1c18 MD |
203 | /* |
204 | * cds_lfht_replace - replace a node within hash table. | |
205 | * | |
206 | * Return the node replaced upon success. If no node matching the key | |
207 | * was present, return NULL, which also means the operation succeeded. | |
208 | * This replacement operation should never fail. | |
209 | * Call with rcu_read_lock held. | |
210 | * After successful replacement, a grace period must be waited for before | |
211 | * freeing the memory reserved for the returned node. | |
212 | * | |
213 | * The semantic of replacement vs lookups is the following: if lookups | |
214 | * are performed between a key insertion and its removal, we guarantee | |
215 | * that the lookups will always find the key if it is replaced | |
216 | * concurrently with the lookups. Providing this guarantee require us to | |
217 | * pin the node to remove in place (disallowing any insertion after this | |
218 | * node temporarily) before we can proceed to its exchange with the new | |
219 | * node atomically. This renders the "replace" operation not strictly | |
220 | * lock-free, because a thread crashing in the middle of the replace | |
221 | * operation could stop progress for other updaters. | |
222 | * | |
223 | * Providing this semantic allows us to ensure that replacement-only | |
224 | * schemes will never generate duplicated keys. It also allows us to | |
225 | * guarantee that a combination of replacement and add_unique updates | |
226 | * will never generate duplicated keys. | |
227 | */ | |
228 | struct cds_lfht_node *cds_lfht_replace(struct cds_lfht *ht, struct cds_lfht_node *node); | |
229 | ||
f0c29ed7 | 230 | /* |
860d07e8 | 231 | * cds_lfht_del - remove node from hash table. |
f0c29ed7 | 232 | * |
48ed1c18 | 233 | * Return 0 if the node is successfully removed. |
860d07e8 MD |
234 | * Node can be looked up with cds_lfht_lookup. RCU read-side lock must |
235 | * be held between lookup and removal. | |
f0c29ed7 | 236 | * Call with rcu_read_lock held. |
48ed1c18 MD |
237 | * After successful removal, a grace period must be waited for before |
238 | * freeing the memory reserved for node. | |
f0c29ed7 | 239 | */ |
860d07e8 | 240 | int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_node *node); |
ab7d5fc6 | 241 | |
f0c29ed7 | 242 | /* |
14044b37 | 243 | * cds_lfht_resize - Force a hash table resize |
1475579c | 244 | * @new_size: update to this hash table size. |
f0c29ed7 | 245 | */ |
1475579c | 246 | void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size); |
464a1ec9 | 247 | |
01389791 MD |
248 | #ifdef __cplusplus |
249 | } | |
250 | #endif | |
251 | ||
a42cc659 | 252 | #endif /* _URCU_RCULFHASH_H */ |