refactor: use parking_lot instead of std::sync
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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<EventLoop>,
|
||||
messenger: Messenger<'a>,
|
||||
scenegraph: Scenegraph<'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 {
|
||||
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<EventLoop> {
|
||||
self.event_loop.upgrade().unwrap()
|
||||
}
|
||||
// pub fn get_messenger(&self) -> &Messenger<'a> {
|
||||
// &self.messenger
|
||||
// }
|
||||
|
||||
@@ -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<Option<JoinHandle<Result<()>>>>,
|
||||
join_handle: Option<JoinHandle<Result<()>>>,
|
||||
stop_write: pipe::Sender,
|
||||
}
|
||||
|
||||
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()
|
||||
.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<Option<Rc<Client>>> = 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<JoinHandle<Result<()>>>) {
|
||||
*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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user