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 MD |
33 | /* |
34 | * struct rcu_ht_node and struct _rcu_ht_node should be aligned on | |
35 | * 4-bytes boundaries because the two lower bits are used as flags. | |
36 | */ | |
37 | ||
cc4fcb10 | 38 | struct _rcu_ht_node { |
d95bd160 | 39 | struct rcu_ht_node *next; /* ptr | DUMMY_FLAG | REMOVED_FLAG */ |
abc490a1 | 40 | unsigned long reverse_hash; |
cc4fcb10 MD |
41 | }; |
42 | ||
43 | struct rcu_ht_node { | |
44 | /* cache-hot for iteration */ | |
45 | struct _rcu_ht_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 | ||
52 | struct rcu_ht; | |
ab7d5fc6 | 53 | |
5e28c532 MD |
54 | /* |
55 | * Caution ! | |
abc490a1 | 56 | * Ensure reader and writer threads are registered as urcu readers. |
5e28c532 MD |
57 | */ |
58 | ||
732ad076 MD |
59 | typedef unsigned long (*ht_hash_fct)(void *key, size_t length, |
60 | unsigned long seed); | |
61 | typedef unsigned long (*ht_compare_fct)(void *key1, size_t key1_len, | |
62 | void *key2, size_t key2_len); | |
abc490a1 | 63 | |
f0c29ed7 MD |
64 | /* |
65 | * ht_node_init - initialize a hash table node | |
66 | */ | |
abc490a1 | 67 | static inline |
732ad076 | 68 | void ht_node_init(struct rcu_ht_node *node, void *key, |
cb233752 | 69 | size_t key_len) |
abc490a1 MD |
70 | { |
71 | node->key = key; | |
732ad076 | 72 | node->key_len = key_len; |
abc490a1 | 73 | } |
674f7a69 MD |
74 | |
75 | /* | |
f0c29ed7 MD |
76 | * ht_new - allocate a hash table. |
77 | * | |
674f7a69 MD |
78 | * init_size must be power of two. |
79 | */ | |
abc490a1 | 80 | struct rcu_ht *ht_new(ht_hash_fct hash_fct, |
732ad076 MD |
81 | ht_compare_fct compare_fct, |
82 | unsigned long hash_seed, | |
abc490a1 MD |
83 | unsigned long init_size, |
84 | void (*ht_call_rcu)(struct rcu_head *head, | |
85 | void (*func)(struct rcu_head *head))); | |
ab7d5fc6 | 86 | |
f0c29ed7 MD |
87 | /* |
88 | * ht_destroy - destroy a hash table. | |
89 | */ | |
5e28c532 | 90 | int ht_destroy(struct rcu_ht *ht); |
f0c29ed7 MD |
91 | |
92 | /* | |
93 | * ht_count_nodes - count the number of nodes in the hash table. | |
94 | * | |
95 | * Call with rcu_read_lock held. | |
96 | */ | |
273399de MD |
97 | void ht_count_nodes(struct rcu_ht *ht, |
98 | unsigned long *count, | |
99 | unsigned long *removed); | |
ab7d5fc6 | 100 | |
f0c29ed7 MD |
101 | /* |
102 | * ht_lookup - lookup a node by key. | |
103 | * | |
104 | * Returns NULL if not found. | |
105 | * Call with rcu_read_lock held. | |
106 | */ | |
732ad076 | 107 | struct rcu_ht_node *ht_lookup(struct rcu_ht *ht, void *key, size_t key_len); |
ab7d5fc6 | 108 | |
f0c29ed7 MD |
109 | /* |
110 | * ht_next - get the next item with same key (after a lookup). | |
111 | * | |
112 | * Returns NULL if no following node exists with same key. | |
113 | * RCU read-side lock must be held across ht_lookup and ht_next calls, and also | |
114 | * between ht_next calls using the node returned by a previous ht_next. | |
115 | * Call with rcu_read_lock held. | |
116 | */ | |
117 | struct rcu_ht_node *ht_next(struct rcu_ht *ht, struct rcu_ht_node *node); | |
ab7d5fc6 | 118 | |
18117871 | 119 | /* |
f0c29ed7 MD |
120 | * ht_add - add a node to the hash table. |
121 | * | |
18117871 | 122 | * Call with rcu_read_lock held. |
f0c29ed7 MD |
123 | */ |
124 | void ht_add(struct rcu_ht *ht, struct rcu_ht_node *node); | |
125 | ||
126 | /* | |
127 | * ht_add_unique - add a node to hash table, if key is not present. | |
128 | * | |
18117871 MD |
129 | * Returns the node added upon success. |
130 | * Returns the unique node already present upon failure. If ht_add_unique fails, | |
131 | * the node passed as parameter should be freed by the caller. | |
f0c29ed7 | 132 | * Call with rcu_read_lock held. |
18117871 MD |
133 | */ |
134 | struct rcu_ht_node *ht_add_unique(struct rcu_ht *ht, struct rcu_ht_node *node); | |
3eca1b8c | 135 | |
f0c29ed7 MD |
136 | /* |
137 | * ht_remove - remove node from hash table. | |
138 | * | |
139 | * Node can be looked up with ht_lookup. RCU read-side lock must be held between | |
140 | * lookup and removal. | |
141 | * Call with rcu_read_lock held. | |
142 | */ | |
abc490a1 | 143 | int ht_remove(struct rcu_ht *ht, struct rcu_ht_node *node); |
ab7d5fc6 | 144 | |
f0c29ed7 MD |
145 | /* |
146 | * ht_resize - Force a hash table resize | |
147 | * @growth: growth order (current size is multiplied by 2^growth) | |
148 | * | |
149 | * Currently, only expand operation is supported (growth >= 0). | |
150 | */ | |
464a1ec9 MD |
151 | void ht_resize(struct rcu_ht *ht, int growth); |
152 | ||
01389791 MD |
153 | #ifdef __cplusplus |
154 | } | |
155 | #endif | |
156 | ||
a42cc659 | 157 | #endif /* _URCU_RCULFHASH_H */ |