1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//! Nodes' membership to trees.
use core::cell::{Ref, RefCell};
use core::fmt;
use core::num::NonZeroUsize;
use alloc::rc::{Rc, Weak};
use crate::tree::{
HierarchyEditGrantError, HierarchyEditProhibitionError, LockAggregatorForNode, TreeCore,
};
/// A membership of a node to a tree.
///
/// Note that nodes may change its affiliation to another tree. In this case,
/// membership should also be changed for all `Node` objects referring to the
/// node, so this will be usually shared as `Rc<RefCell<<T>>>`.
pub(super) enum MembershipCore<T> {
/// Weak membership: non-owning reference to the tree core.
///
/// If there are no `Node<T>`s for the node, the membership will stay in
/// this state.
Weak {
/// A weak reference to the tree core.
///
/// This can dangle only when:
///
/// * the membership and the node is being initialized, or
/// * the tree is being dropped and the node is the root node.
tree_core: Weak<TreeCore<T>>,
},
/// Strong membership: shared owning reference to the tree core, and strong refcounts.
///
/// If there are any `Node<T>`s for the node, the membership will stay in
/// this state. In other words, membership in this state guarantees that the
/// tree and some nodes are alive.
Strong {
/// A strong reference to the tree core.
tree_core: Rc<TreeCore<T>>,
/// Strong reference count for the tree core from this membership.
tree_refcount: NonZeroUsize,
/// Lock aggregator.
lock_aggregator: LockAggregatorForNode,
},
}
impl<T: fmt::Debug> fmt::Debug for MembershipCore<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Weak { .. } => write!(f, "Weak {{ .. }}"),
Self::Strong {
tree_refcount,
lock_aggregator,
..
} => f
.debug_struct("Strong")
.field("tree_refcount", &tree_refcount)
.field("lock_aggregator", &lock_aggregator)
.finish_non_exhaustive(),
}
}
}
impl<T> MembershipCore<T> {
/// Creates a new (weak) `MembershipCore` for the given tree.
#[inline]
#[must_use]
pub(super) fn new_for_existing_tree(tree_core: &Rc<TreeCore<T>>) -> Self {
Self::Weak {
tree_core: Rc::downgrade(tree_core),
}
}
/// Creates a new (weak) `MembershipCore` with the dangling tree reference.
#[inline]
#[must_use]
pub(super) fn dangling() -> Self {
Self::Weak {
tree_core: Weak::new(),
}
}
}
/// A convenience wrapper for `RefCell<MembershipCore<T>>`.
#[derive(Clone, Copy)]
pub(super) struct MembershipRef<'a, T>(&'a RefCell<MembershipCore<T>>);
impl<'a, T> MembershipRef<'a, T> {
/// Creates a new membership reference.
#[inline]
#[must_use]
pub(super) fn new(core: &'a RefCell<MembershipCore<T>>) -> Self {
Self(core)
}
/// Initializes the membership with the given tree, and sets the tree refcount to 1.
///
/// # Panics
///
/// Panics if the membership has been already associated to any tree.
/// The membership should be a one that is unmodified since created by
/// `MembershipCore::dangling()`.
pub(super) fn initialize_with_tree_and_set_refcount_to_1(self, tree_core: Rc<TreeCore<T>>) {
let mut core = self
.0
.try_borrow_mut()
.expect("[consistency] membership core should never be borrowed nestedly");
match &*core {
// Not testing if `weak` is dereferenceable, but testing if the
// reference is the value created by `Weak::new()`. This condition
// will be false once the membership is fully initialized.
// This comparison with `&Weak::new()` is intentional.
MembershipCore::Weak { tree_core } if Weak::ptr_eq(tree_core, &Weak::new()) => {}
_ => panic!(
"[consistency] the membership should be unmodified since created as dangling"
),
}
debug_assert!(matches!(&*core, MembershipCore::Weak { .. }));
// Create a strong reference and set the count to 1.
let tree_refcount = NonZeroUsize::new(1).expect("[validity] 1 is nonzero");
*core = MembershipCore::Strong {
tree_core,
tree_refcount,
lock_aggregator: Default::default(),
};
}
/// Returns the temporary reference to the `Rc` of the tree core.
#[must_use]
pub(super) fn tree_core_strong_ref(self) -> Option<Ref<'a, Rc<TreeCore<T>>>> {
let core = self
.0
.try_borrow()
.expect("[consistency] membership core should never be borrowed nestedly");
Ref::filter_map(core, |core| match core {
MembershipCore::Weak { .. } => None,
MembershipCore::Strong { tree_core, .. } => Some(tree_core),
})
.ok()
}
/// Returns the shared owning reference to the tree core.
#[must_use]
pub(super) fn tree_core_opt(self) -> Option<Rc<TreeCore<T>>> {
let core = self
.0
.try_borrow()
.expect("[consistency] membership core should never be borrowed nestedly");
match &*core {
MembershipCore::Weak { tree_core } => Weak::upgrade(tree_core),
MembershipCore::Strong { tree_core, .. } => Some(tree_core.clone()),
}
}
/// Increments the reference count of the tree.
///
/// # Failures
///
/// Fails if the tree is unavailable, i.e. already released and unavailable.
pub(super) fn increment_tree_refcount(self) -> Result<(), ()> {
let mut core = self
.0
.try_borrow_mut()
.expect("[consistency] membership core should never be borrowed nestedly");
// Increment refcount.
match &mut *core {
MembershipCore::Weak { tree_core } => {
let tree_refcount = NonZeroUsize::new(1).expect("[consistency] 1 is nonzero");
let tree_core = tree_core.upgrade().ok_or(())?;
*core = MembershipCore::Strong {
tree_core,
tree_refcount,
lock_aggregator: Default::default(),
};
}
MembershipCore::Strong { tree_refcount, .. } => {
let incremented = tree_refcount
.checked_add(1)
.expect("[validity] the memory cannot have `usize::MAX` references");
*tree_refcount = incremented;
}
}
Ok(())
}
/// Increments the reference count of the tree, assuming the refcount is already nonzero.
pub(super) fn increment_nonzero_tree_refcount(self) {
let mut core = self
.0
.try_borrow_mut()
.expect("[consistency] membership core should never be borrowed nestedly");
// Increment refcount.
match &mut *core {
MembershipCore::Weak { .. } => {
unreachable!("[consistency] the membership should have a strong reference");
}
MembershipCore::Strong { tree_refcount, .. } => {
let incremented = tree_refcount
.checked_add(1)
.expect("[validity] the memory cannot have `usize::MAX` references");
*tree_refcount = incremented;
}
}
}
/// Decrements the reference count of the tree.
pub(super) fn decrement_tree_refcount(self) {
let mut core = self
.0
.try_borrow_mut()
.expect("[consistency] membership core should never be borrowed nestedly");
// Decrement refcount.
match &mut *core {
MembershipCore::Weak { .. } => {
unreachable!("[consistency] the membership should have a strong reference");
}
MembershipCore::Strong {
tree_core,
tree_refcount,
lock_aggregator,
} => {
// This subtraction never overflows.
let decremented = NonZeroUsize::new(tree_refcount.get() - 1);
match decremented {
Some(new_count) => *tree_refcount = new_count,
None => {
assert!(
!lock_aggregator.has_lock(),
"[consistency] locks can be acquired only from strong node references"
);
let weak_core = Rc::downgrade(tree_core);
*core = MembershipCore::Weak {
tree_core: weak_core,
};
}
}
}
}
}
/// Lets the membership refer to the given tree core.
///
/// # Failures
///
/// Fails if the new tree cannot be locked with the currently active tree
/// hierarchy edit lock.
pub(super) fn set_tree_core(&self, new_tree_core: &Rc<TreeCore<T>>) -> Result<(), ()> {
let mut core = self
.0
.try_borrow_mut()
.expect("[consistency] membership core should never be borrowed nestedly");
match &mut *core {
MembershipCore::Weak { tree_core } => *tree_core = Rc::downgrade(new_tree_core),
MembershipCore::Strong {
tree_core,
lock_aggregator,
..
} => {
if lock_aggregator.has_lock() {
tree_core.transfer_single_lock_to(new_tree_core)?;
}
*tree_core = new_tree_core.clone();
}
}
Ok(())
}
/// Returns true if the membership refers to the same tree core allocation.
#[must_use]
pub(super) fn ptr_eq_tree_core(&self, tree_core: &Rc<TreeCore<T>>) -> bool {
let core = self
.0
.try_borrow()
.expect("[consistency] membership core should never be borrowed nestedly");
let self_ptr = match &*core {
MembershipCore::Weak { tree_core } => tree_core.as_ptr(),
MembershipCore::Strong { tree_core, .. } => Rc::as_ptr(tree_core),
};
self_ptr == Rc::as_ptr(tree_core)
}
/// Decrements the aggregated lock count.
///
/// # Panics
///
/// Panics if:
///
/// * the aggregated lock count is zero, or
/// * the membership is not strong.
pub(super) fn decrement_edit_lock_count(&self) {
let mut core = self
.0
.try_borrow_mut()
.expect("[consistency] membership core should never be borrowed nestedly");
match &mut *core {
MembershipCore::Weak { .. } => {
unreachable!("[consistency] the node should have an incoming strong reference")
}
MembershipCore::Strong {
tree_core,
lock_aggregator,
..
} => {
lock_aggregator.decrement_count(tree_core);
}
}
}
/// Increments hierarchy edit lock count, assuming the count is nonzero.
///
/// # Panics
///
/// Panics if:
///
/// * the aggregated lock count is zero or `usize::MAX`, or
/// * the membership is not strong.
pub(super) fn increment_nonzero_edit_lock_count(&self) {
let mut core = self
.0
.try_borrow_mut()
.expect("[consistency] membership core should never be borrowed nestedly");
match &mut *core {
MembershipCore::Weak { .. } => {
unreachable!("[consistency] the node should have an incoming strong reference")
}
MembershipCore::Strong {
lock_aggregator, ..
} => lock_aggregator.increment_nonzero_count(),
}
}
/// Acquires hierarchy edit prohibition.
///
/// # Panics
///
/// Panics if the the membership is not strong.
pub(super) fn acquire_edit_prohibition(&self) -> Result<(), HierarchyEditProhibitionError> {
let mut core = self
.0
.try_borrow_mut()
.expect("[consistency] membership core should never be borrowed nestedly");
match &mut *core {
MembershipCore::Weak { .. } => {
unreachable!("[consistency] the node should have an incoming strong reference")
}
MembershipCore::Strong {
tree_core,
lock_aggregator,
..
} => lock_aggregator.acquire_edit_prohibition(tree_core),
}
}
/// Acquires hierarchy edit grant.
///
/// # Panics
///
/// Panics if the the membership is not strong.
pub(super) fn acquire_edit_grant(&self) -> Result<(), HierarchyEditGrantError> {
let mut core = self
.0
.try_borrow_mut()
.expect("[consistency] membership core should never be borrowed nestedly");
match &mut *core {
MembershipCore::Weak { .. } => {
unreachable!("[consistency] the node should have an incoming strong reference")
}
MembershipCore::Strong {
tree_core,
lock_aggregator,
..
} => lock_aggregator.acquire_edit_grant(tree_core),
}
}
}