From 0d9cc1b9ad274029c9589c5d0c6c2ac0cffe5ecc Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 10 Oct 2026 14:47:41 +0200 Subject: perf(engine): use thread pool for asset importing --- engine/src/work_queue.rs | 77 ------------------------------------------------ 1 file changed, 77 deletions(-) delete mode 100644 engine/src/work_queue.rs (limited to 'engine/src/work_queue.rs') diff --git a/engine/src/work_queue.rs b/engine/src/work_queue.rs deleted file mode 100644 index 765aec3..0000000 --- a/engine/src/work_queue.rs +++ /dev/null @@ -1,77 +0,0 @@ -use std::borrow::Cow; -use std::marker::PhantomData; -use std::panic::catch_unwind; -use std::sync::mpsc::{channel as mpsc_channel, Sender as MpscSender}; -use std::sync::{Arc, OnceLock}; -use std::thread::{Builder as ThreadBuilder, JoinHandle as ThreadJoinHandle}; - -pub struct Work -{ - pub func: fn(UserData), - pub user_data: UserData, -} - -#[derive(Debug)] -pub struct WorkQueue -{ - work_sender: MpscSender>, - thread_panic: Arc>>, - _thread: ThreadJoinHandle<()>, - _pd: PhantomData, -} - -impl WorkQueue -{ - pub fn new(name: &str) -> Self - { - let (work_sender, work_receiver) = mpsc_channel::>(); - - let thread_panic = Arc::new(OnceLock::new()); - - let thread_panic_b = thread_panic.clone(); - - Self { - work_sender, - thread_panic, - _thread: ThreadBuilder::new() - .name(name.to_string()) - .spawn(move || { - if let Err(panic_err) = catch_unwind(|| { - while let Ok(work) = work_receiver.recv() { - (work.func)(work.user_data); - } - }) { - let panic_message: Cow<'static, str> = - if let Some(panic_message) = - panic_err.downcast_ref::<&'static str>() - { - (*panic_message).into() - } else if let Some(panic_message) = - panic_err.downcast_ref::() - { - panic_message.clone().into() - } else { - "(unknown panic payload type)".into() - }; - - let _ = thread_panic_b - .set(panic_message.into_owned().into_boxed_str()); - } - }) - .expect("Failed to create work queue thread"), - _pd: PhantomData, - } - } - - pub fn get_thread_panic(&self) -> Option<&str> - { - self.thread_panic.get().map(|thread_panic| &**thread_panic) - } - - pub fn add_work(&self, work: Work) - { - if self.work_sender.send(work).is_err() { - tracing::error!("Cannot add work to work queue. Work queue thread is dead"); - } - } -} -- cgit v1.2.3-18-g5258