From d308b88bb7c7be5f1e21628586f77ddd91b12fc2 Mon Sep 17 00:00:00 2001 From: Nova Date: Sun, 12 Jun 2022 01:51:12 -0400 Subject: [PATCH] refactor: use parking_lot instead of std::sync --- Cargo.toml | 1 + src/core/client.rs | 9 +-------- src/core/eventloop.rs | 31 ++++++++++--------------------- 3 files changed, 12 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7d87ca4..4935ae0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ glam = {version = "0.20.5", features = ["mint"]} lazy_static = "1.4.0" mint = "0.5.9" mio = {version = "0.8.3", features = ["net", "os-poll", "os-ext"]} +parking_lot = "0.12.1" rccell = "0.1.3" slab = "0.4.6" thiserror = "1.0.31" diff --git a/src/core/client.rs b/src/core/client.rs index c6912f2..ad7378f 100644 --- a/src/core/client.rs +++ b/src/core/client.rs @@ -1,22 +1,18 @@ -use super::eventloop::EventLoop; use super::scenegraph::Scenegraph; use crate::nodes::field; use crate::nodes::spatial; use libstardustxr::messenger::Messenger; use mio::net::UnixStream; use std::rc::Rc; -use std::sync::{Arc, Weak}; pub struct Client<'a> { - event_loop: Weak, messenger: Messenger<'a>, scenegraph: Scenegraph<'a>, } impl<'a> Client<'a> { - pub fn from_connection(connection: UnixStream, event_loop_ref: &Arc) -> Rc { + pub fn from_connection(connection: UnixStream) -> Rc { let client = Rc::new(Client { - event_loop: Arc::downgrade(event_loop_ref), messenger: Messenger::new(connection), scenegraph: Default::default(), }); @@ -29,9 +25,6 @@ impl<'a> Client<'a> { self.messenger.dispatch(&self.scenegraph) } - pub fn get_event_loop(&self) -> Arc { - self.event_loop.upgrade().unwrap() - } // pub fn get_messenger(&self) -> &Messenger<'a> { // &self.messenger // } diff --git a/src/core/eventloop.rs b/src/core/eventloop.rs index 6c80267..5d045cc 100644 --- a/src/core/eventloop.rs +++ b/src/core/eventloop.rs @@ -7,31 +7,23 @@ use mio::{Events, Interest, Poll, Token}; use slab::Slab; use std::io::Write; use std::rc::Rc; -use std::sync::{Arc, RwLock}; use std::thread::{self, JoinHandle}; pub struct EventLoop { pub socket_path: String, - join_handle: RwLock>>>, + join_handle: Option>>, stop_write: pipe::Sender, } impl EventLoop { - pub fn new(timeout: Option) -> Result> { + pub fn new(timeout: Option) -> Result { let socket_path = server::get_free_socket_path() .ok_or_else(|| std::io::Error::from(std::io::ErrorKind::Other))?; let mut socket = UnixListener::bind(socket_path.clone())?; 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() .name("event_loop".to_owned()) .spawn(move || -> Result<()> { - let event_loop_arc = event_loop_arc_captured; let mut clients: Slab>> = Slab::new(); let mut poll = Poll::new()?; let mut events = Events::with_capacity(1024); @@ -54,8 +46,7 @@ impl EventLoop { Token(client_number), Interest::READABLE, )?; - let client = - Client::from_connection(socket, &event_loop_arc); + let client = Client::from_connection(socket); *clients.get_mut(client_number).unwrap() = Some(client); } Err(e) => { @@ -83,12 +74,11 @@ impl EventLoop { } }) .ok(); - event_loop_arc.set_join_handle(join_handle); - Ok(event_loop_arc) - } - - fn set_join_handle(&self, handle: Option>>) { - *self.join_handle.write().unwrap() = handle; + Ok(EventLoop { + socket_path, + join_handle, + stop_write: sender, + }) } } @@ -98,10 +88,9 @@ impl Drop for EventLoop { let _ = self.stop_write.write(buf.as_slice()); let _ = self .join_handle - .get_mut() - .ok() - .and_then(|handle| handle.take()) + .take() .and_then(|handle| handle.join().ok()) + .as_ref() .expect("Couldn't join the event loop thread at drop"); } }