rculfhash: spawn only number of threads required for resize
[urcu.git] / urcu / rculfhash.h
... / ...
CommitLineData
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
32extern "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
40struct _cds_lfht_node {
41 struct cds_lfht_node *next; /* ptr | DUMMY_FLAG | REMOVED_FLAG */
42 unsigned long reverse_hash;
43};
44
45struct 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
54struct cds_lfht;
55
56/*
57 * Caution !
58 * Ensure reader and writer threads are registered as urcu readers.
59 */
60
61typedef unsigned long (*cds_lfht_hash_fct)(void *key, size_t length,
62 unsigned long seed);
63typedef unsigned long (*cds_lfht_compare_fct)(void *key1, size_t key1_len,
64 void *key2, size_t key2_len);
65
66/*
67 * cds_lfht_node_init - initialize a hash table node
68 */
69static inline
70void cds_lfht_node_init(struct cds_lfht_node *node, void *key,
71 size_t key_len)
72{
73 node->key = key;
74 node->key_len = key_len;
75}
76
77/*
78 * Hash table creation flags.
79 */
80enum {
81 CDS_LFHT_AUTO_RESIZE = (1U << 0),
82};
83
84/*
85 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
86 */
87struct cds_lfht *_cds_lfht_new(cds_lfht_hash_fct hash_fct,
88 cds_lfht_compare_fct compare_fct,
89 unsigned long hash_seed,
90 unsigned long init_size,
91 int flags,
92 void (*cds_lfht_call_rcu)(struct rcu_head *head,
93 void (*func)(struct rcu_head *head)),
94 void (*cds_lfht_synchronize_rcu)(void),
95 void (*cds_lfht_rcu_read_lock)(void),
96 void (*cds_lfht_rcu_read_unlock)(void),
97 void (*cds_lfht_rcu_thread_offline)(void),
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);
102
103/*
104 * cds_lfht_new - allocate a hash table.
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.
112 * @attr: optional resize worker thread attributes. NULL for default.
113 *
114 * Return NULL on error.
115 * Note: the RCU flavor must be already included before the hash table header.
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.
124 */
125static inline
126struct 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,
130 int flags,
131 pthread_attr_t *attr)
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,
137 rcu_thread_online, rcu_register_thread,
138 rcu_unregister_thread, attr);
139}
140
141/*
142 * cds_lfht_destroy - destroy a hash table.
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.
147 *
148 * Return 0 on success, negative error value on error.
149 */
150int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr);
151
152/*
153 * cds_lfht_count_nodes - count the number of nodes in the hash table.
154 *
155 * Call with rcu_read_lock held.
156 */
157void cds_lfht_count_nodes(struct cds_lfht *ht,
158 unsigned long *count,
159 unsigned long *removed);
160
161/*
162 * cds_lfht_lookup - lookup a node by key.
163 *
164 * Return NULL if not found.
165 * Call with rcu_read_lock held.
166 */
167struct cds_lfht_node *cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len);
168
169/*
170 * cds_lfht_next - get the next item with same key (after a lookup).
171 *
172 * Return NULL if no following node exists with same key.
173 * RCU read-side lock must be held across cds_lfht_lookup and cds_lfht_next calls, and also
174 * between cds_lfht_next calls using the node returned by a previous cds_lfht_next.
175 * Call with rcu_read_lock held.
176 */
177struct cds_lfht_node *cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_node *node);
178
179/*
180 * cds_lfht_add - add a node to the hash table.
181 *
182 * Call with rcu_read_lock held.
183 */
184void cds_lfht_add(struct cds_lfht *ht, struct cds_lfht_node *node);
185
186/*
187 * cds_lfht_add_unique - add a node to hash table, if key is not present.
188 *
189 * Return the node added upon success.
190 * Return the unique node already present upon failure. If cds_lfht_add_unique fails,
191 * the node passed as parameter should be freed by the caller.
192 * Call with rcu_read_lock held.
193 */
194struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht, struct cds_lfht_node *node);
195
196/*
197 * cds_lfht_remove - remove node from hash table.
198 *
199 * Node can be looked up with cds_lfht_lookup. RCU read-side lock must be held between
200 * lookup and removal.
201 * Call with rcu_read_lock held.
202 */
203int cds_lfht_remove(struct cds_lfht *ht, struct cds_lfht_node *node);
204
205/*
206 * cds_lfht_resize - Force a hash table resize
207 * @new_size: update to this hash table size.
208 */
209void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size);
210
211#ifdef __cplusplus
212}
213#endif
214
215#endif /* _URCU_RCULFHASH_H */
This page took 0.022553 seconds and 4 git commands to generate.