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 | |
24 | */ | |
25 | ||
674f7a69 | 26 | #include <stdint.h> |
abc490a1 MD |
27 | #include <urcu-call-rcu.h> |
28 | ||
01389791 MD |
29 | #ifdef __cplusplus |
30 | extern "C" { | |
31 | #endif | |
32 | ||
7f61a77f | 33 | /* |
14044b37 | 34 | * struct cds_lfht_node and struct _cds_lfht_node should be aligned on |
7f61a77f MD |
35 | * 4-bytes boundaries because the two lower bits are used as flags. |
36 | */ | |
37 | ||
14044b37 MD |
38 | struct _cds_lfht_node { |
39 | struct cds_lfht_node *next; /* ptr | DUMMY_FLAG | REMOVED_FLAG */ | |
abc490a1 | 40 | unsigned long reverse_hash; |
cc4fcb10 MD |
41 | }; |
42 | ||
14044b37 | 43 | struct cds_lfht_node { |
cc4fcb10 | 44 | /* cache-hot for iteration */ |
14044b37 | 45 | struct _cds_lfht_node p; /* needs to be first field */ |
9b35f1e4 MD |
46 | void *key; |
47 | unsigned int key_len; | |
9b35f1e4 | 48 | /* cache-cold for iteration */ |
abc490a1 MD |
49 | struct rcu_head head; |
50 | }; | |
51 | ||
14044b37 | 52 | struct cds_lfht; |
ab7d5fc6 | 53 | |
5e28c532 MD |
54 | /* |
55 | * Caution ! | |
abc490a1 | 56 | * Ensure reader and writer threads are registered as urcu readers. |
5e28c532 MD |
57 | */ |
58 | ||
14044b37 MD |
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); | |
abc490a1 | 63 | |
f0c29ed7 | 64 | /* |
14044b37 | 65 | * cds_lfht_node_init - initialize a hash table node |
f0c29ed7 | 66 | */ |
abc490a1 | 67 | static inline |
14044b37 MD |
68 | void cds_lfht_node_init(struct cds_lfht_node *node, void *key, |
69 | size_t key_len) | |
abc490a1 MD |
70 | { |
71 | node->key = key; | |
732ad076 | 72 | node->key_len = key_len; |
abc490a1 | 73 | } |
674f7a69 | 74 | |
b8af5011 MD |
75 | /* |
76 | * Hash table creation flags. | |
77 | */ | |
78 | enum { | |
79 | CDS_LFHT_AUTO_RESIZE = (1U << 0), | |
80 | }; | |
81 | ||
674f7a69 | 82 | /* |
14044b37 | 83 | * cds_lfht_new - allocate a hash table. |
f0c29ed7 | 84 | * |
674f7a69 | 85 | * init_size must be power of two. |
6878c72b | 86 | * Return NULL on error. |
674f7a69 | 87 | */ |
14044b37 MD |
88 | struct cds_lfht *cds_lfht_new(cds_lfht_hash_fct hash_fct, |
89 | cds_lfht_compare_fct compare_fct, | |
90 | unsigned long hash_seed, | |
91 | unsigned long init_size, | |
b8af5011 | 92 | int flags, |
14044b37 | 93 | void (*cds_lfht_call_rcu)(struct rcu_head *head, |
1475579c MD |
94 | void (*func)(struct rcu_head *head)), |
95 | void (*cds_lfht_synchronize_rcu)(void)); | |
ab7d5fc6 | 96 | |
f0c29ed7 | 97 | /* |
14044b37 | 98 | * cds_lfht_destroy - destroy a hash table. |
6878c72b MD |
99 | * |
100 | * Return 0 on success, negative error value on error. | |
f0c29ed7 | 101 | */ |
14044b37 | 102 | int cds_lfht_destroy(struct cds_lfht *ht); |
f0c29ed7 MD |
103 | |
104 | /* | |
14044b37 | 105 | * cds_lfht_count_nodes - count the number of nodes in the hash table. |
f0c29ed7 MD |
106 | * |
107 | * Call with rcu_read_lock held. | |
108 | */ | |
14044b37 MD |
109 | void cds_lfht_count_nodes(struct cds_lfht *ht, |
110 | unsigned long *count, | |
111 | unsigned long *removed); | |
ab7d5fc6 | 112 | |
f0c29ed7 | 113 | /* |
14044b37 | 114 | * cds_lfht_lookup - lookup a node by key. |
f0c29ed7 | 115 | * |
6878c72b | 116 | * Return NULL if not found. |
f0c29ed7 MD |
117 | * Call with rcu_read_lock held. |
118 | */ | |
14044b37 | 119 | struct cds_lfht_node *cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len); |
ab7d5fc6 | 120 | |
f0c29ed7 | 121 | /* |
14044b37 | 122 | * cds_lfht_next - get the next item with same key (after a lookup). |
f0c29ed7 | 123 | * |
6878c72b | 124 | * Return NULL if no following node exists with same key. |
14044b37 MD |
125 | * RCU read-side lock must be held across cds_lfht_lookup and cds_lfht_next calls, and also |
126 | * between cds_lfht_next calls using the node returned by a previous cds_lfht_next. | |
f0c29ed7 MD |
127 | * Call with rcu_read_lock held. |
128 | */ | |
14044b37 | 129 | struct cds_lfht_node *cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_node *node); |
ab7d5fc6 | 130 | |
18117871 | 131 | /* |
14044b37 | 132 | * cds_lfht_add - add a node to the hash table. |
f0c29ed7 | 133 | * |
18117871 | 134 | * Call with rcu_read_lock held. |
f0c29ed7 | 135 | */ |
14044b37 | 136 | void cds_lfht_add(struct cds_lfht *ht, struct cds_lfht_node *node); |
f0c29ed7 MD |
137 | |
138 | /* | |
14044b37 | 139 | * cds_lfht_add_unique - add a node to hash table, if key is not present. |
f0c29ed7 | 140 | * |
6878c72b MD |
141 | * Return the node added upon success. |
142 | * Return the unique node already present upon failure. If cds_lfht_add_unique fails, | |
18117871 | 143 | * the node passed as parameter should be freed by the caller. |
f0c29ed7 | 144 | * Call with rcu_read_lock held. |
18117871 | 145 | */ |
14044b37 | 146 | struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht, struct cds_lfht_node *node); |
3eca1b8c | 147 | |
f0c29ed7 | 148 | /* |
14044b37 | 149 | * cds_lfht_remove - remove node from hash table. |
f0c29ed7 | 150 | * |
14044b37 | 151 | * Node can be looked up with cds_lfht_lookup. RCU read-side lock must be held between |
f0c29ed7 MD |
152 | * lookup and removal. |
153 | * Call with rcu_read_lock held. | |
154 | */ | |
14044b37 | 155 | int cds_lfht_remove(struct cds_lfht *ht, struct cds_lfht_node *node); |
ab7d5fc6 | 156 | |
f0c29ed7 | 157 | /* |
14044b37 | 158 | * cds_lfht_resize - Force a hash table resize |
1475579c | 159 | * @new_size: update to this hash table size. |
f0c29ed7 | 160 | */ |
1475579c | 161 | void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size); |
464a1ec9 | 162 | |
01389791 MD |
163 | #ifdef __cplusplus |
164 | } | |
165 | #endif | |
166 | ||
a42cc659 | 167 | #endif /* _URCU_RCULFHASH_H */ |