rcuja 1d distribution: cleanup
[userspace-rcu.git] / rcuja / rcuja-internal.h
CommitLineData
61009379
MD
1#ifndef _URCU_RCUJA_INTERNAL_H
2#define _URCU_RCUJA_INTERNAL_H
3
4/*
5 * rcuja/rcuja-internal.h
6 *
7 * Userspace RCU library - RCU Judy Array Internal Header
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
7d67da99 26#define _GNU_SOURCE
511b4dcc 27#include <pthread.h>
a2a7ff59
MD
28#include <stdio.h>
29#include <inttypes.h>
7d67da99 30#include <unistd.h>
511b4dcc
MD
31#include <urcu/rculfhash.h>
32
3d8fe307
MD
33/*
34 * Number of least significant pointer bits reserved to represent the
35 * child type.
36 */
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)
41
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)
45
b1a90ce3
MD
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)
48
3d8fe307
MD
49#define JA_MAX_DEPTH 9 /* Maximum depth, including leafs */
50
51/*
52 * Entry for NULL node is at index 8 of the table. It is never encoded
53 * in flags.
54 */
55#define NODE_INDEX_NULL 8
56
57/*
58 * Number of removals needed on a fallback node before we try to shrink
59 * it.
60 */
61#define JA_FALLBACK_REMOVAL_COUNT 8
62
511b4dcc 63/* Never declared. Opaque type used to store flagged node pointers. */
b4540e8a 64struct cds_ja_inode_flag;
3d8fe307 65struct cds_ja_inode;
511b4dcc
MD
66
67/*
68 * Shadow node contains mutex and call_rcu head associated with a node.
69 */
d96bfb0d 70struct cds_ja_shadow_node {
d22c185b 71 struct cds_lfht_node ht_node; /* hash table node */
3d8fe307 72 struct cds_ja_inode_flag *node_flag; /* reverse mapping and hash table key */
e1db2db5
MD
73 /*
74 * mutual exclusion on all nodes belonging to the same tree
75 * position (e.g. both nodes before and after recompaction
76 * use the same lock).
77 */
78 pthread_mutex_t *lock;
79 unsigned int nr_child; /* number of children in node */
d22c185b 80 struct rcu_head head; /* for deferred node and shadow node reclaim */
f07b240f 81 int fallback_removal_count; /* removals left keeping fallback */
3d8fe307
MD
82 int level; /* level in the tree */
83 struct cds_ja *ja; /* toplevel judy array */
511b4dcc
MD
84};
85
d96bfb0d 86struct cds_ja {
b4540e8a
MD
87 struct cds_ja_inode_flag *root;
88 unsigned int tree_depth;
89 uint64_t key_max;
511b4dcc 90 /*
e1db2db5
MD
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.
511b4dcc
MD
94 */
95 struct cds_lfht *ht;
f07b240f 96 unsigned long nr_fallback; /* Number of fallback nodes used */
511b4dcc 97};
61009379 98
3d8fe307
MD
99static inline
100struct cds_ja_inode_flag *ja_node_flag(struct cds_ja_inode *node,
101 unsigned long type)
102{
103 assert(type < (1UL << JA_TYPE_BITS));
104 return (struct cds_ja_inode_flag *) (((unsigned long) node) | type);
105}
106
107static inline
b1a90ce3
MD
108struct cds_ja_inode_flag *ja_node_flag_pool_1d(struct cds_ja_inode *node,
109 unsigned long type, unsigned long bitsel)
3d8fe307 110{
b1a90ce3
MD
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);
3d8fe307
MD
114}
115
116static inline
b1a90ce3 117unsigned long ja_node_pool_1d_bitsel(struct cds_ja_inode_flag *node)
3d8fe307 118{
b1a90ce3
MD
119 return ((unsigned long) node & JA_POOL_1D_MASK) >> JA_TYPE_BITS;
120}
3d8fe307 121
b1a90ce3
MD
122static inline
123void ja_node_pool_2d_bitsel(struct cds_ja_inode_flag *node, unsigned long *bits)
124{
125 bits[0] = ((unsigned long) node & JA_POOL_2D_MASK) >> (JA_TYPE_BITS + JA_LOG2_BITS_PER_BYTE);
126 bits[1] = ((unsigned long) node & JA_POOL_1D_MASK) >> JA_TYPE_BITS;
3d8fe307
MD
127}
128
b1a90ce3
MD
129__attribute__((visibility("protected")))
130unsigned long ja_node_type(struct cds_ja_inode_flag *node);
131
132__attribute__((visibility("protected")))
133struct cds_ja_inode *ja_node_ptr(struct cds_ja_inode_flag *node);
134
3d8fe307
MD
135__attribute__((visibility("protected")))
136void rcuja_free_all_children(struct cds_ja_shadow_node *shadow_node,
137 struct cds_ja_inode_flag *node_flag,
138 void (*free_node_cb)(struct rcu_head *head));
139
25fde237 140__attribute__((visibility("protected")))
d96bfb0d 141struct cds_ja_shadow_node *rcuja_shadow_lookup_lock(struct cds_lfht *ht,
3d8fe307 142 struct cds_ja_inode_flag *node_flag);
be9a7474 143
25fde237 144__attribute__((visibility("protected")))
d96bfb0d 145void rcuja_shadow_unlock(struct cds_ja_shadow_node *shadow_node);
be9a7474 146
25fde237 147__attribute__((visibility("protected")))
f07b240f 148struct cds_ja_shadow_node *rcuja_shadow_set(struct cds_lfht *ht,
3d8fe307
MD
149 struct cds_ja_inode_flag *new_node_flag,
150 struct cds_ja_shadow_node *inherit_from,
151 struct cds_ja *ja);
e1db2db5
MD
152
153/* rcuja_shadow_clear flags */
154enum {
155 RCUJA_SHADOW_CLEAR_FREE_NODE = (1U << 0),
156 RCUJA_SHADOW_CLEAR_FREE_LOCK = (1U << 1),
157};
158
be9a7474 159__attribute__((visibility("protected")))
e1db2db5 160int rcuja_shadow_clear(struct cds_lfht *ht,
3d8fe307 161 struct cds_ja_inode_flag *node_flag,
a2a7ff59 162 struct cds_ja_shadow_node *shadow_node,
e1db2db5 163 unsigned int flags);
be9a7474
MD
164
165__attribute__((visibility("protected")))
166void rcuja_shadow_prune(struct cds_lfht *ht,
3d8fe307
MD
167 unsigned int flags,
168 void (*free_node_cb)(struct rcu_head *head));
be9a7474 169
25fde237 170__attribute__((visibility("protected")))
5eb692c0 171struct cds_lfht *rcuja_create_ht(const struct rcu_flavor_struct *flavor);
be9a7474 172
25fde237 173__attribute__((visibility("protected")))
be9a7474 174int rcuja_delete_ht(struct cds_lfht *ht);
25fde237 175
1a0c0717 176//#define DEBUG
a2a7ff59 177
7d67da99
MD
178#ifdef __linux__
179#include <syscall.h>
180#endif
181
182#if defined(_syscall0)
183_syscall0(pid_t, gettid)
184#elif defined(__NR_gettid)
185static inline pid_t gettid(void)
186{
187 return syscall(__NR_gettid);
188}
189#else
190#warning "use pid as tid"
191static inline pid_t gettid(void)
192{
193 return getpid();
194}
195#endif
196
a2a7ff59 197#ifdef DEBUG
7d67da99
MD
198#define dbg_printf(fmt, args...) \
199 fprintf(stderr, "[debug rcuja %lu %s()@%s:%u] " fmt, \
200 (unsigned long) gettid(), __func__, \
201 __FILE__, __LINE__, ## args)
a2a7ff59
MD
202#else
203#define dbg_printf(fmt, args...) \
204do { \
205 /* do nothing but check printf format */ \
206 if (0) \
7d67da99
MD
207 fprintf(stderr, "[debug rcuja %lu %s()@%s:%u] " fmt, \
208 (unsigned long) gettid(), __func__, \
209 __FILE__, __LINE__, ## args); \
a2a7ff59
MD
210} while (0)
211#endif
212
61009379 213#endif /* _URCU_RCUJA_INTERNAL_H */
This page took 0.031846 seconds and 4 git commands to generate.