rculfhash: simplify lfht_new API with inline wrapper
[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
26 #include <stdint.h>
27 #include <urcu-call-rcu.h>
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 /*
34 * struct cds_lfht_node and struct _cds_lfht_node should be aligned on
35 * 4-bytes boundaries because the two lower bits are used as flags.
36 */
37
38 struct _cds_lfht_node {
39 struct cds_lfht_node *next; /* ptr | DUMMY_FLAG | REMOVED_FLAG */
40 unsigned long reverse_hash;
41 };
42
43 struct cds_lfht_node {
44 /* cache-hot for iteration */
45 struct _cds_lfht_node p; /* needs to be first field */
46 void *key;
47 unsigned int key_len;
48 /* cache-cold for iteration */
49 struct rcu_head head;
50 };
51
52 struct cds_lfht;
53
54 /*
55 * Caution !
56 * Ensure reader and writer threads are registered as urcu readers.
57 */
58
59 typedef unsigned long (*cds_lfht_hash_fct)(void *key, size_t length,
60 unsigned long seed);
61 typedef unsigned long (*cds_lfht_compare_fct)(void *key1, size_t key1_len,
62 void *key2, size_t key2_len);
63
64 /*
65 * cds_lfht_node_init - initialize a hash table node
66 */
67 static inline
68 void cds_lfht_node_init(struct cds_lfht_node *node, void *key,
69 size_t key_len)
70 {
71 node->key = key;
72 node->key_len = key_len;
73 }
74
75 /*
76 * Hash table creation flags.
77 */
78 enum {
79 CDS_LFHT_AUTO_RESIZE = (1U << 0),
80 };
81
82 /*
83 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
84 */
85 struct cds_lfht *_cds_lfht_new(cds_lfht_hash_fct hash_fct,
86 cds_lfht_compare_fct compare_fct,
87 unsigned long hash_seed,
88 unsigned long init_size,
89 int flags,
90 void (*cds_lfht_call_rcu)(struct rcu_head *head,
91 void (*func)(struct rcu_head *head)),
92 void (*cds_lfht_synchronize_rcu)(void),
93 void (*cds_lfht_rcu_read_lock)(void),
94 void (*cds_lfht_rcu_read_unlock)(void),
95 void (*cds_lfht_rcu_thread_offline)(void),
96 void (*cds_lfht_rcu_thread_online)(void));
97
98 /*
99 * cds_lfht_new - allocate a hash table.
100 *
101 * init_size must be power of two.
102 * Return NULL on error.
103 * Note: the RCU flavor must be already included before the hash table header.
104 */
105 static inline
106 struct cds_lfht *cds_lfht_new(cds_lfht_hash_fct hash_fct,
107 cds_lfht_compare_fct compare_fct,
108 unsigned long hash_seed,
109 unsigned long init_size,
110 int flags)
111 {
112 return _cds_lfht_new(hash_fct, compare_fct, hash_seed,
113 init_size, flags,
114 call_rcu, synchronize_rcu, rcu_read_lock,
115 rcu_read_unlock, rcu_thread_offline,
116 rcu_thread_online);
117 }
118
119 /*
120 * cds_lfht_destroy - destroy a hash table.
121 *
122 * Return 0 on success, negative error value on error.
123 */
124 int cds_lfht_destroy(struct cds_lfht *ht);
125
126 /*
127 * cds_lfht_count_nodes - count the number of nodes in the hash table.
128 *
129 * Call with rcu_read_lock held.
130 */
131 void cds_lfht_count_nodes(struct cds_lfht *ht,
132 unsigned long *count,
133 unsigned long *removed);
134
135 /*
136 * cds_lfht_lookup - lookup a node by key.
137 *
138 * Return NULL if not found.
139 * Call with rcu_read_lock held.
140 */
141 struct cds_lfht_node *cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len);
142
143 /*
144 * cds_lfht_next - get the next item with same key (after a lookup).
145 *
146 * Return NULL if no following node exists with same key.
147 * RCU read-side lock must be held across cds_lfht_lookup and cds_lfht_next calls, and also
148 * between cds_lfht_next calls using the node returned by a previous cds_lfht_next.
149 * Call with rcu_read_lock held.
150 */
151 struct cds_lfht_node *cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_node *node);
152
153 /*
154 * cds_lfht_add - add a node to the hash table.
155 *
156 * Call with rcu_read_lock held.
157 */
158 void cds_lfht_add(struct cds_lfht *ht, struct cds_lfht_node *node);
159
160 /*
161 * cds_lfht_add_unique - add a node to hash table, if key is not present.
162 *
163 * Return the node added upon success.
164 * Return the unique node already present upon failure. If cds_lfht_add_unique fails,
165 * the node passed as parameter should be freed by the caller.
166 * Call with rcu_read_lock held.
167 */
168 struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht, struct cds_lfht_node *node);
169
170 /*
171 * cds_lfht_remove - remove node from hash table.
172 *
173 * Node can be looked up with cds_lfht_lookup. RCU read-side lock must be held between
174 * lookup and removal.
175 * Call with rcu_read_lock held.
176 */
177 int cds_lfht_remove(struct cds_lfht *ht, struct cds_lfht_node *node);
178
179 /*
180 * cds_lfht_resize - Force a hash table resize
181 * @new_size: update to this hash table size.
182 */
183 void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size);
184
185 #ifdef __cplusplus
186 }
187 #endif
188
189 #endif /* _URCU_RCULFHASH_H */
This page took 0.047805 seconds and 5 git commands to generate.