1 #ifndef _URCU_RCUJA_INTERNAL_H
2 #define _URCU_RCUJA_INTERNAL_H
5 * rcuja/rcuja-internal.h
7 * Userspace RCU library - RCU Judy Array Internal Header
9 * Copyright 2012 - 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
31 #include <urcu/rculfhash.h>
34 * Number of least significant pointer bits reserved to represent the
37 #define JA_TYPE_BITS 3
38 #define JA_TYPE_MAX_NR (1UL << JA_TYPE_BITS)
39 #define JA_TYPE_MASK (JA_TYPE_MAX_NR - 1)
40 #define JA_PTR_MASK (~JA_TYPE_MASK)
42 #define JA_ENTRY_PER_NODE 256UL
43 #define JA_LOG2_BITS_PER_BYTE 3U
44 #define JA_BITS_PER_BYTE (1U << JA_LOG2_BITS_PER_BYTE)
46 #define JA_POOL_1D_MASK ((JA_BITS_PER_BYTE - 1) << JA_TYPE_BITS)
47 #define JA_POOL_2D_MASK (JA_POOL_1D_MASK << JA_LOG2_BITS_PER_BYTE)
49 #define JA_MAX_DEPTH 9 /* Maximum depth, including leafs */
52 * Entry for NULL node is at index 8 of the table. It is never encoded
55 #define NODE_INDEX_NULL 8
58 * Number of removals needed on a fallback node before we try to shrink
61 #define JA_FALLBACK_REMOVAL_COUNT 8
63 /* Never declared. Opaque type used to store flagged node pointers. */
64 struct cds_ja_inode_flag
;
68 * Shadow node contains mutex and call_rcu head associated with a node.
70 struct cds_ja_shadow_node
{
71 struct cds_lfht_node ht_node
; /* hash table node */
72 struct cds_ja_inode_flag
*node_flag
; /* reverse mapping and hash table key */
74 * mutual exclusion on all nodes belonging to the same tree
75 * position (e.g. both nodes before and after recompaction
78 pthread_mutex_t
*lock
;
79 unsigned int nr_child
; /* number of children in node */
80 struct rcu_head head
; /* for deferred node and shadow node reclaim */
81 int fallback_removal_count
; /* removals left keeping fallback */
82 int level
; /* level in the tree */
83 struct cds_ja
*ja
; /* toplevel judy array */
87 struct cds_ja_inode_flag
*root
;
88 unsigned int tree_depth
;
91 * We use a hash table to associate node keys to their
92 * respective shadow node. This helps reducing lookup hot path
93 * cache footprint, especially for very small nodes.
96 unsigned long nr_fallback
; /* Number of fallback nodes used */
100 struct cds_ja_inode_flag
*ja_node_flag(struct cds_ja_inode
*node
,
103 assert(type
< (1UL << JA_TYPE_BITS
));
104 return (struct cds_ja_inode_flag
*) (((unsigned long) node
) | type
);
108 struct cds_ja_inode_flag
*ja_node_flag_pool_1d(struct cds_ja_inode
*node
,
109 unsigned long type
, unsigned long bitsel
)
111 assert(type
< (1UL << JA_TYPE_BITS
));
112 assert(bitsel
< JA_BITS_PER_BYTE
);
113 return (struct cds_ja_inode_flag
*) (((unsigned long) node
) | (bitsel
<< JA_TYPE_BITS
) | type
);
117 struct cds_ja_inode_flag
*ja_node_flag_pool_2d(struct cds_ja_inode
*node
,
118 unsigned long type
, unsigned int bitsel
[2])
120 assert(type
< (1UL << JA_TYPE_BITS
));
121 assert(bitsel
[0] < JA_BITS_PER_BYTE
);
122 assert(bitsel
[1] < JA_BITS_PER_BYTE
);
123 return (struct cds_ja_inode_flag
*) (((unsigned long) node
) | (bitsel
[0] << (JA_TYPE_BITS
+ JA_LOG2_BITS_PER_BYTE
)) | (bitsel
[1] << JA_TYPE_BITS
) | type
);
127 unsigned long ja_node_pool_1d_bitsel(struct cds_ja_inode_flag
*node
)
129 return ((unsigned long) node
& JA_POOL_1D_MASK
) >> JA_TYPE_BITS
;
133 void ja_node_pool_2d_bitsel(struct cds_ja_inode_flag
*node
, unsigned long *bits
)
135 bits
[0] = ((unsigned long) node
& JA_POOL_2D_MASK
) >> (JA_TYPE_BITS
+ JA_LOG2_BITS_PER_BYTE
);
136 bits
[1] = ((unsigned long) node
& JA_POOL_1D_MASK
) >> JA_TYPE_BITS
;
139 __attribute__((visibility("protected")))
140 unsigned long ja_node_type(struct cds_ja_inode_flag
*node
);
142 __attribute__((visibility("protected")))
143 struct cds_ja_inode
*ja_node_ptr(struct cds_ja_inode_flag
*node
);
145 __attribute__((visibility("protected")))
146 void rcuja_free_all_children(struct cds_ja_shadow_node
*shadow_node
,
147 struct cds_ja_inode_flag
*node_flag
,
148 void (*free_node_cb
)(struct rcu_head
*head
));
150 __attribute__((visibility("protected")))
151 struct cds_ja_shadow_node
*rcuja_shadow_lookup_lock(struct cds_lfht
*ht
,
152 struct cds_ja_inode_flag
*node_flag
);
154 __attribute__((visibility("protected")))
155 void rcuja_shadow_unlock(struct cds_ja_shadow_node
*shadow_node
);
157 __attribute__((visibility("protected")))
158 struct cds_ja_shadow_node
*rcuja_shadow_set(struct cds_lfht
*ht
,
159 struct cds_ja_inode_flag
*new_node_flag
,
160 struct cds_ja_shadow_node
*inherit_from
,
161 struct cds_ja
*ja
, int level
);
163 /* rcuja_shadow_clear flags */
165 RCUJA_SHADOW_CLEAR_FREE_NODE
= (1U << 0),
166 RCUJA_SHADOW_CLEAR_FREE_LOCK
= (1U << 1),
169 __attribute__((visibility("protected")))
170 int rcuja_shadow_clear(struct cds_lfht
*ht
,
171 struct cds_ja_inode_flag
*node_flag
,
172 struct cds_ja_shadow_node
*shadow_node
,
175 __attribute__((visibility("protected")))
176 void rcuja_shadow_prune(struct cds_lfht
*ht
,
178 void (*free_node_cb
)(struct rcu_head
*head
));
180 __attribute__((visibility("protected")))
181 struct cds_lfht
*rcuja_create_ht(const struct rcu_flavor_struct
*flavor
);
183 __attribute__((visibility("protected")))
184 int rcuja_delete_ht(struct cds_lfht
*ht
);
186 __attribute__((visibility("protected")))
187 void free_cds_ja_node(struct cds_ja_inode
*node
);
195 #if defined(_syscall0)
196 _syscall0(pid_t
, gettid
)
197 #elif defined(__NR_gettid)
198 static inline pid_t
gettid(void)
200 return syscall(__NR_gettid
);
203 #warning "use pid as tid"
204 static inline pid_t
gettid(void)
211 #define dbg_printf(fmt, args...) \
212 fprintf(stderr, "[debug rcuja %lu %s()@%s:%u] " fmt, \
213 (unsigned long) gettid(), __func__, \
214 __FILE__, __LINE__, ## args)
216 #define dbg_printf(fmt, args...) \
218 /* do nothing but check printf format */ \
220 fprintf(stderr, "[debug rcuja %lu %s()@%s:%u] " fmt, \
221 (unsigned long) gettid(), __func__, \
222 __FILE__, __LINE__, ## args); \
226 #endif /* _URCU_RCUJA_INTERNAL_H */