Commit | Line | Data |
---|---|---|
d553beef MD |
1 | #ifndef _URCU_RCUJA_H |
2 | #define _URCU_RCUJA_H | |
3 | ||
4 | /* | |
5 | * urcu/rcuja.h | |
6 | * | |
7 | * Userspace RCU library - RCU Judy Array | |
8 | * | |
9 | * Copyright 2012 - 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/compiler.h> | |
30 | #include <urcu-call-rcu.h> | |
31 | #include <urcu-flavor.h> | |
32 | #include <stdint.h> | |
33 | #include <urcu/rcuhlist.h> | |
34 | ||
35 | #ifdef __cplusplus | |
36 | extern "C" { | |
37 | #endif | |
38 | ||
03ec1aeb MD |
39 | /* |
40 | * Duplicate nodes with the same key are chained into a singly-linked | |
41 | * list. The last item of this list has a NULL next pointer. | |
42 | */ | |
d553beef | 43 | struct cds_ja_node { |
03ec1aeb | 44 | struct cds_ja_node *next; |
d553beef MD |
45 | }; |
46 | ||
47 | struct cds_ja; | |
48 | ||
49 | /* | |
50 | * cds_ja_node_init - initialize a judy array node | |
51 | * @node: the node to initialize. | |
52 | * | |
53 | * This function is kept to be eventually used for debugging purposes | |
54 | * (detection of memory corruption). | |
55 | */ | |
56 | static inline | |
57 | void cds_ja_node_init(struct cds_ja_node *node) | |
58 | { | |
59 | } | |
60 | ||
b41da4d1 MD |
61 | /* |
62 | * cds_ja_lookup - look up by key. | |
63 | * @ja: the Judy array. | |
64 | * @key: key to look up. | |
65 | * | |
66 | * Returns the first node of a duplicate chain if a match is found, else | |
67 | * returns NULL. | |
68 | * A RCU read-side lock should be held across call to this function and | |
69 | * use of its return value. | |
70 | */ | |
03ec1aeb | 71 | struct cds_ja_node *cds_ja_lookup(struct cds_ja *ja, uint64_t key); |
b41da4d1 MD |
72 | |
73 | /* | |
74 | * cds_ja_lookup_lower_equal - look up first node with key <= @key. | |
75 | * @ja: the Judy array. | |
76 | * @key: key to look up. | |
77 | * | |
78 | * Returns the first node of a duplicate chain if a node is present in | |
79 | * the tree which has a key lower or equal to @key, else returns NULL. | |
80 | * A RCU read-side lock should be held across call to this function and | |
81 | * use of its return value. | |
82 | */ | |
03ec1aeb | 83 | struct cds_ja_node *cds_ja_lookup_lower_equal(struct cds_ja *ja, |
291b2543 | 84 | uint64_t key); |
d553beef | 85 | |
d8536f92 MD |
86 | /* |
87 | * cds_ja_add - Add @node at @key, allowing duplicates. | |
88 | * @ja: the Judy array. | |
89 | * @key: key at which @node should be added. | |
90 | * @node: node to add. | |
91 | * | |
92 | * Returns 0 on success, negative error value on error. | |
93 | * A RCU read-side lock should be held across call to this function. | |
94 | */ | |
d553beef | 95 | int cds_ja_add(struct cds_ja *ja, uint64_t key, |
d8536f92 | 96 | struct cds_ja_node *node); |
d553beef | 97 | |
d8536f92 MD |
98 | /* |
99 | * cds_ja_add_unique - Add @node at @key, without duplicates. | |
100 | * @ja: the Judy array. | |
101 | * @key: key at which @node should be added. | |
102 | * @node: node to add. | |
103 | * | |
104 | * Returns @node if successfully added, else returns the already | |
105 | * existing node (acts as a RCU lookup). | |
106 | * A RCU read-side lock should be held across call to this function and | |
107 | * use of its return value. | |
108 | */ | |
75d573aa | 109 | struct cds_ja_node *cds_ja_add_unique(struct cds_ja *ja, uint64_t key, |
d8536f92 | 110 | struct cds_ja_node *node); |
75d573aa | 111 | |
d8536f92 MD |
112 | /* |
113 | * cds_ja_del - Remove @node at @key. | |
114 | * @ja: the Judy array. | |
115 | * @key: key at which @node is expected. | |
116 | * @node: node to remove. | |
117 | * | |
118 | * Returns 0 on success, negative error value on error. | |
119 | * A RCU read-side lock should be held across call to this function. | |
120 | */ | |
d553beef MD |
121 | int cds_ja_del(struct cds_ja *ja, uint64_t key, |
122 | struct cds_ja_node *node); | |
123 | ||
124 | struct cds_ja *_cds_ja_new(unsigned int key_bits, | |
125 | const struct rcu_flavor_struct *flavor); | |
126 | ||
d8536f92 MD |
127 | /* |
128 | * cds_ja_new - Create a Judy array. | |
129 | * @key_bits: Number of bits for key. | |
130 | * | |
131 | * Returns non-NULL pointer on success, else NULL on error. @key_bits | |
132 | * needs to be multiple of 8, either: 8, 16, 24, 32, 40, 48, 56, or 64. | |
133 | */ | |
d553beef MD |
134 | static inline |
135 | struct cds_ja *cds_ja_new(unsigned int key_bits) | |
136 | { | |
137 | return _cds_ja_new(key_bits, &rcu_flavor); | |
138 | } | |
139 | ||
d8536f92 MD |
140 | /* |
141 | * cds_ja_destroy - Destroy a Judy array. | |
142 | * @ja: the Judy array. | |
143 | * @rcu_free_node_cb: callback performing memory free of leftover nodes. | |
144 | * | |
145 | * Returns 0 on success, negative error value on error. | |
dc0e9798 MD |
146 | * There should be no more concurrent add, delete, nor look-up performed |
147 | * on the Judy array while it is being destroyed (ensured by the caller). | |
148 | * There is no need for the @rcu_free_node_cb callback to wait for grace | |
149 | * periods, since there are no more concurrent users of the Judy array. | |
6a8ecb1c | 150 | * RCU read-side lock should _not_ be held when calling this function. |
d8536f92 | 151 | */ |
d553beef | 152 | int cds_ja_destroy(struct cds_ja *ja, |
dc0e9798 | 153 | void (*free_node_cb)(struct cds_ja_node *node)); |
d553beef | 154 | |
03ec1aeb MD |
155 | /* |
156 | * Iterate through duplicates returned by cds_ja_lookup*() | |
157 | * This must be done while rcu_read_lock() is held. | |
158 | * Receives a struct cds_ja_node * as parameter, which is used as start | |
159 | * of duplicate list and loop cursor. | |
160 | */ | |
161 | #define cds_ja_for_each_duplicate_rcu(pos) \ | |
162 | for (; (pos) != NULL; (pos) = rcu_dereference((pos)->next)) | |
163 | ||
d553beef MD |
164 | #ifdef __cplusplus |
165 | } | |
166 | #endif | |
167 | ||
168 | #endif /* _URCU_RCUJA_H */ |