|
@@ -558,142 +558,6 @@ where
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-/// A view into a tree that can be shared between multiple threads. Nodes are locked invividually.
|
|
|
|
-pub struct SharedView<'a, T, Tr: TreeView<T>> {
|
|
|
|
- tree: Arc<UnsafeCell<&'a mut Tr>>,
|
|
|
|
- node_locks: Arc<RwLock<Vec<RawMutex>>>,
|
|
|
|
- node_type: PhantomData<T>,
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-impl<'a, T, Tr: TreeView<T>> SharedView<'a, T, Tr> {
|
|
|
|
- /// Checks if a node is currently locked. Returns None if the node does not exist.
|
|
|
|
- pub fn check_lock(&self, id: NodeId) -> Option<bool> {
|
|
|
|
- let locks = self.node_locks.read();
|
|
|
|
- locks.get(id.0).map(|lock| lock.is_locked())
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-unsafe impl<'a, T, Tr: TreeView<T> + Send> Send for SharedView<'a, T, Tr> {}
|
|
|
|
-unsafe impl<'a, T, Tr: TreeView<T> + Sync> Sync for SharedView<'a, T, Tr> {}
|
|
|
|
-impl<'a, T, Tr: TreeView<T>> Clone for SharedView<'a, T, Tr> {
|
|
|
|
- fn clone(&self) -> Self {
|
|
|
|
- Self {
|
|
|
|
- tree: self.tree.clone(),
|
|
|
|
- node_locks: self.node_locks.clone(),
|
|
|
|
- node_type: PhantomData,
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-impl<'a, T, Tr: TreeView<T>> SharedView<'a, T, Tr> {
|
|
|
|
- pub fn new(tree: &'a mut Tr) -> Self {
|
|
|
|
- let tree = Arc::new(UnsafeCell::new(tree));
|
|
|
|
- let mut node_locks = Vec::new();
|
|
|
|
- for _ in 0..unsafe { (*tree.get()).size() } {
|
|
|
|
- node_locks.push(RawMutex::INIT);
|
|
|
|
- }
|
|
|
|
- Self {
|
|
|
|
- tree,
|
|
|
|
- node_locks: Arc::new(RwLock::new(node_locks)),
|
|
|
|
- node_type: PhantomData,
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- fn lock_node(&self, node: NodeId) {
|
|
|
|
- let read = self.node_locks.read();
|
|
|
|
- let lock = read.get(node.0);
|
|
|
|
- match lock {
|
|
|
|
- Some(lock) => lock.lock(),
|
|
|
|
- None => {
|
|
|
|
- drop(read);
|
|
|
|
- let mut write = self.node_locks.write();
|
|
|
|
- write.resize_with(node.0 + 1, || RawMutex::INIT);
|
|
|
|
- unsafe { write.get_unchecked(node.0).lock() }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- fn unlock_node(&self, node: NodeId) {
|
|
|
|
- let read = self.node_locks.read();
|
|
|
|
- let lock = read.get(node.0);
|
|
|
|
- match lock {
|
|
|
|
- Some(lock) => unsafe { lock.unlock() },
|
|
|
|
- None => {
|
|
|
|
- panic!("unlocking node that was not locked")
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- fn with_node<R>(&self, node_id: NodeId, f: impl FnOnce(&'a mut Tr) -> R) -> R {
|
|
|
|
- self.lock_node(node_id);
|
|
|
|
- let tree = unsafe { &mut *self.tree.get() };
|
|
|
|
- let r = f(tree);
|
|
|
|
- self.unlock_node(node_id);
|
|
|
|
- r
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-impl<'a, T, Tr: TreeView<T>> TreeView<T> for SharedView<'a, T, Tr> {
|
|
|
|
- type Iterator<'b> = Tr::Iterator<'b> where T: 'b, Self: 'b;
|
|
|
|
-
|
|
|
|
- type IteratorMut<'b>=Tr::IteratorMut<'b>
|
|
|
|
- where
|
|
|
|
- T: 'b,
|
|
|
|
- Self: 'b;
|
|
|
|
-
|
|
|
|
- fn root(&self) -> NodeId {
|
|
|
|
- unsafe { (*self.tree.get()).root() }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- fn get(&self, id: NodeId) -> Option<&T> {
|
|
|
|
- self.with_node(id, |t| t.get(id))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- fn get_mut(&mut self, id: NodeId) -> Option<&mut T> {
|
|
|
|
- self.with_node(id, |t| t.get_mut(id))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- fn children(&self, id: NodeId) -> Option<Self::Iterator<'_>> {
|
|
|
|
- self.with_node(id, |t| t.children(id))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- fn children_mut(&mut self, id: NodeId) -> Option<Self::IteratorMut<'_>> {
|
|
|
|
- self.with_node(id, |t| t.children_mut(id))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- fn children_ids(&self, id: NodeId) -> Option<&[NodeId]> {
|
|
|
|
- self.with_node(id, |t| t.children_ids(id))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- fn parent(&self, id: NodeId) -> Option<&T> {
|
|
|
|
- self.with_node(id, |t| t.get(id))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- fn parent_mut(&mut self, id: NodeId) -> Option<&mut T> {
|
|
|
|
- self.with_node(id, |t| t.parent_mut(id))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- fn parent_id(&self, id: NodeId) -> Option<NodeId> {
|
|
|
|
- self.with_node(id, |t| t.parent_id(id))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- fn height(&self, id: NodeId) -> Option<u16> {
|
|
|
|
- unsafe { (*self.tree.get()).height(id) }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- fn size(&self) -> usize {
|
|
|
|
- unsafe { (*self.tree.get()).size() }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- fn node_parent_mut(&mut self, id: NodeId) -> Option<(&mut T, Option<&mut T>)> {
|
|
|
|
- let parent_id = self.parent_id(id)?;
|
|
|
|
- self.lock_node(parent_id);
|
|
|
|
- let return_value = self.with_node(id, |t| t.node_parent_mut(id));
|
|
|
|
- self.unlock_node(parent_id);
|
|
|
|
- return_value
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
#[test]
|
|
#[test]
|
|
fn creation() {
|
|
fn creation() {
|
|
let mut tree = Tree::new(1);
|
|
let mut tree = Tree::new(1);
|