Make all nodes thread safe

This commit is contained in:
Nova
2022-06-14 19:34:27 -04:00
parent 651fa5f012
commit 3aa691475c
6 changed files with 182 additions and 207 deletions

View File

@@ -6,8 +6,8 @@ use mio::net::UnixStream;
use std::rc::Rc;
pub struct Client {
messenger: Messenger,
scenegraph: Scenegraph,
pub messenger: Messenger,
pub scenegraph: Scenegraph,
}
impl Client {
@@ -24,11 +24,4 @@ impl Client {
pub fn dispatch(&self) -> Result<(), std::io::Error> {
self.messenger.dispatch(&self.scenegraph)
}
pub fn get_messenger(&self) -> &Messenger {
&self.messenger
}
pub fn get_scenegraph(&self) -> &Scenegraph {
&self.scenegraph
}
}

View File

@@ -3,9 +3,9 @@ use crate::nodes::core::Node;
use anyhow::Result;
use libstardustxr::scenegraph;
use libstardustxr::scenegraph::ScenegraphError;
use rccell::RcCell;
use std::cell::RefCell;
use std::rc::{Rc, Weak};
use std::sync::Arc;
use core::hash::BuildHasherDefault;
use dashmap::DashMap;
@@ -14,7 +14,7 @@ use rustc_hash::FxHasher;
#[derive(Default)]
pub struct Scenegraph {
client: RefCell<Weak<Client>>,
nodes: DashMap<String, RcCell<Node>, BuildHasherDefault<FxHasher>>,
nodes: DashMap<String, Arc<Node>, BuildHasherDefault<FxHasher>>,
}
impl Scenegraph {
@@ -26,20 +26,20 @@ impl Scenegraph {
*self.client.borrow_mut() = Rc::downgrade(client);
}
pub fn add_node(&self, node: Node) -> RcCell<Node> {
pub fn add_node(&self, node: Node) -> Arc<Node> {
let mut node = node;
node.client = Rc::downgrade(&self.get_client());
let path = node.get_path().to_string();
let node_rc = RcCell::new(node);
self.nodes.insert(path, node_rc.clone());
node_rc
let node_arc = Arc::new(node);
self.nodes.insert(path, node_arc.clone());
node_arc
}
pub fn get_node(&self, path: &str) -> Option<RcCell<Node>> {
pub fn get_node(&self, path: &str) -> Option<Arc<Node>> {
Some(self.nodes.get(path)?.clone())
}
pub fn remove_node(&self, path: &str) -> Option<RcCell<Node>> {
pub fn remove_node(&self, path: &str) -> Option<Arc<Node>> {
let (_, node) = self.nodes.remove(path)?;
Some(node)
}
@@ -49,7 +49,6 @@ impl scenegraph::Scenegraph for Scenegraph {
fn send_signal(&self, path: &str, method: &str, data: &[u8]) -> Result<(), ScenegraphError> {
self.get_node(path)
.ok_or(ScenegraphError::NodeNotFound)?
.borrow()
.send_local_signal(self.get_client(), method, data)
}
fn execute_method(
@@ -60,7 +59,6 @@ impl scenegraph::Scenegraph for Scenegraph {
) -> Result<Vec<u8>, ScenegraphError> {
self.get_node(path)
.ok_or(ScenegraphError::NodeNotFound)?
.borrow()
.execute_local_method(self.get_client(), method, data)
}
}