refactor: use parking_lot instead of std::sync

This commit is contained in:
Nova
2022-06-12 01:51:12 -04:00
parent 01971b5048
commit d308b88bb7
3 changed files with 12 additions and 29 deletions

View File

@@ -14,6 +14,7 @@ glam = {version = "0.20.5", features = ["mint"]}
lazy_static = "1.4.0" lazy_static = "1.4.0"
mint = "0.5.9" mint = "0.5.9"
mio = {version = "0.8.3", features = ["net", "os-poll", "os-ext"]} mio = {version = "0.8.3", features = ["net", "os-poll", "os-ext"]}
parking_lot = "0.12.1"
rccell = "0.1.3" rccell = "0.1.3"
slab = "0.4.6" slab = "0.4.6"
thiserror = "1.0.31" thiserror = "1.0.31"

View File

@@ -1,22 +1,18 @@
use super::eventloop::EventLoop;
use super::scenegraph::Scenegraph; use super::scenegraph::Scenegraph;
use crate::nodes::field; use crate::nodes::field;
use crate::nodes::spatial; use crate::nodes::spatial;
use libstardustxr::messenger::Messenger; use libstardustxr::messenger::Messenger;
use mio::net::UnixStream; use mio::net::UnixStream;
use std::rc::Rc; use std::rc::Rc;
use std::sync::{Arc, Weak};
pub struct Client<'a> { pub struct Client<'a> {
event_loop: Weak<EventLoop>,
messenger: Messenger<'a>, messenger: Messenger<'a>,
scenegraph: Scenegraph<'a>, scenegraph: Scenegraph<'a>,
} }
impl<'a> Client<'a> { impl<'a> Client<'a> {
pub fn from_connection(connection: UnixStream, event_loop_ref: &Arc<EventLoop>) -> Rc<Self> { pub fn from_connection(connection: UnixStream) -> Rc<Self> {
let client = Rc::new(Client { let client = Rc::new(Client {
event_loop: Arc::downgrade(event_loop_ref),
messenger: Messenger::new(connection), messenger: Messenger::new(connection),
scenegraph: Default::default(), scenegraph: Default::default(),
}); });
@@ -29,9 +25,6 @@ impl<'a> Client<'a> {
self.messenger.dispatch(&self.scenegraph) self.messenger.dispatch(&self.scenegraph)
} }
pub fn get_event_loop(&self) -> Arc<EventLoop> {
self.event_loop.upgrade().unwrap()
}
// pub fn get_messenger(&self) -> &Messenger<'a> { // pub fn get_messenger(&self) -> &Messenger<'a> {
// &self.messenger // &self.messenger
// } // }

View File

@@ -7,31 +7,23 @@ use mio::{Events, Interest, Poll, Token};
use slab::Slab; use slab::Slab;
use std::io::Write; use std::io::Write;
use std::rc::Rc; use std::rc::Rc;
use std::sync::{Arc, RwLock};
use std::thread::{self, JoinHandle}; use std::thread::{self, JoinHandle};
pub struct EventLoop { pub struct EventLoop {
pub socket_path: String, pub socket_path: String,
join_handle: RwLock<Option<JoinHandle<Result<()>>>>, join_handle: Option<JoinHandle<Result<()>>>,
stop_write: pipe::Sender, stop_write: pipe::Sender,
} }
impl EventLoop { impl EventLoop {
pub fn new(timeout: Option<core::time::Duration>) -> Result<Arc<Self>> { pub fn new(timeout: Option<core::time::Duration>) -> Result<Self> {
let socket_path = server::get_free_socket_path() let socket_path = server::get_free_socket_path()
.ok_or_else(|| std::io::Error::from(std::io::ErrorKind::Other))?; .ok_or_else(|| std::io::Error::from(std::io::ErrorKind::Other))?;
let mut socket = UnixListener::bind(socket_path.clone())?; let mut socket = UnixListener::bind(socket_path.clone())?;
let (sender, mut receiver) = pipe::new()?; let (sender, mut receiver) = pipe::new()?;
let event_loop_arc = Arc::new(EventLoop {
socket_path,
join_handle: RwLock::new(None),
stop_write: sender,
});
let event_loop_arc_captured = event_loop_arc.clone();
let join_handle = thread::Builder::new() let join_handle = thread::Builder::new()
.name("event_loop".to_owned()) .name("event_loop".to_owned())
.spawn(move || -> Result<()> { .spawn(move || -> Result<()> {
let event_loop_arc = event_loop_arc_captured;
let mut clients: Slab<Option<Rc<Client>>> = Slab::new(); let mut clients: Slab<Option<Rc<Client>>> = Slab::new();
let mut poll = Poll::new()?; let mut poll = Poll::new()?;
let mut events = Events::with_capacity(1024); let mut events = Events::with_capacity(1024);
@@ -54,8 +46,7 @@ impl EventLoop {
Token(client_number), Token(client_number),
Interest::READABLE, Interest::READABLE,
)?; )?;
let client = let client = Client::from_connection(socket);
Client::from_connection(socket, &event_loop_arc);
*clients.get_mut(client_number).unwrap() = Some(client); *clients.get_mut(client_number).unwrap() = Some(client);
} }
Err(e) => { Err(e) => {
@@ -83,12 +74,11 @@ impl EventLoop {
} }
}) })
.ok(); .ok();
event_loop_arc.set_join_handle(join_handle); Ok(EventLoop {
Ok(event_loop_arc) socket_path,
} join_handle,
stop_write: sender,
fn set_join_handle(&self, handle: Option<JoinHandle<Result<()>>>) { })
*self.join_handle.write().unwrap() = handle;
} }
} }
@@ -98,10 +88,9 @@ impl Drop for EventLoop {
let _ = self.stop_write.write(buf.as_slice()); let _ = self.stop_write.write(buf.as_slice());
let _ = self let _ = self
.join_handle .join_handle
.get_mut() .take()
.ok()
.and_then(|handle| handle.take())
.and_then(|handle| handle.join().ok()) .and_then(|handle| handle.join().ok())
.as_ref()
.expect("Couldn't join the event loop thread at drop"); .expect("Couldn't join the event loop thread at drop");
} }
} }