1 #ifndef _URCU_RCULFHASH_H
2 #define _URCU_RCULFHASH_H
7 * Userspace RCU library - Lock-Free RCU Hash Table
9 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
27 #include <urcu-call-rcu.h>
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.
38 struct _cds_lfht_node
{
39 struct cds_lfht_node
*next
; /* ptr | DUMMY_FLAG | REMOVED_FLAG */
40 unsigned long reverse_hash
;
43 struct cds_lfht_node
{
44 /* cache-hot for iteration */
45 struct _cds_lfht_node p
; /* needs to be first field */
48 /* cache-cold for iteration */
56 * Ensure reader and writer threads are registered as urcu readers.
59 typedef unsigned long (*cds_lfht_hash_fct
)(void *key
, size_t length
,
61 typedef unsigned long (*cds_lfht_compare_fct
)(void *key1
, size_t key1_len
,
62 void *key2
, size_t key2_len
);
65 * cds_lfht_node_init - initialize a hash table node
68 void cds_lfht_node_init(struct cds_lfht_node
*node
, void *key
,
72 node
->key_len
= key_len
;
76 * cds_lfht_new - allocate a hash table.
78 * init_size must be power of two.
80 struct cds_lfht
*cds_lfht_new(cds_lfht_hash_fct hash_fct
,
81 cds_lfht_compare_fct compare_fct
,
82 unsigned long hash_seed
,
83 unsigned long init_size
,
84 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
85 void (*func
)(struct rcu_head
*head
)));
88 * cds_lfht_destroy - destroy a hash table.
90 int cds_lfht_destroy(struct cds_lfht
*ht
);
93 * cds_lfht_count_nodes - count the number of nodes in the hash table.
95 * Call with rcu_read_lock held.
97 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
99 unsigned long *removed
);
102 * cds_lfht_lookup - lookup a node by key.
104 * Returns NULL if not found.
105 * Call with rcu_read_lock held.
107 struct cds_lfht_node
*cds_lfht_lookup(struct cds_lfht
*ht
, void *key
, size_t key_len
);
110 * cds_lfht_next - get the next item with same key (after a lookup).
112 * Returns NULL if no following node exists with same key.
113 * RCU read-side lock must be held across cds_lfht_lookup and cds_lfht_next calls, and also
114 * between cds_lfht_next calls using the node returned by a previous cds_lfht_next.
115 * Call with rcu_read_lock held.
117 struct cds_lfht_node
*cds_lfht_next(struct cds_lfht
*ht
, struct cds_lfht_node
*node
);
120 * cds_lfht_add - add a node to the hash table.
122 * Call with rcu_read_lock held.
124 void cds_lfht_add(struct cds_lfht
*ht
, struct cds_lfht_node
*node
);
127 * cds_lfht_add_unique - add a node to hash table, if key is not present.
129 * Returns the node added upon success.
130 * Returns the unique node already present upon failure. If cds_lfht_add_unique fails,
131 * the node passed as parameter should be freed by the caller.
132 * Call with rcu_read_lock held.
134 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
, struct cds_lfht_node
*node
);
137 * cds_lfht_remove - remove node from hash table.
139 * Node can be looked up with cds_lfht_lookup. RCU read-side lock must be held between
140 * lookup and removal.
141 * Call with rcu_read_lock held.
143 int cds_lfht_remove(struct cds_lfht
*ht
, struct cds_lfht_node
*node
);
146 * cds_lfht_resize - Force a hash table resize
147 * @growth: growth order (current size is multiplied by 2^growth)
149 * Currently, only expand operation is supported (growth >= 0).
151 void cds_lfht_resize(struct cds_lfht
*ht
, int growth
);
157 #endif /* _URCU_RCULFHASH_H */