From de4586dc510243ca89c34518e94e4712312c4b5b Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Sat, 3 Aug 2024 11:24:02 +0200 Subject: [PATCH] examples/winit_multithread: Make thread persistent again --- examples/winit_multithread.rs | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/examples/winit_multithread.rs b/examples/winit_multithread.rs index 6323592..c0d208d 100644 --- a/examples/winit_multithread.rs +++ b/examples/winit_multithread.rs @@ -19,16 +19,15 @@ mod ex { fn render_thread( window: Arc, - surface: Arc>, - do_render: mpsc::Receiver<()>, + do_render: mpsc::Receiver>>, done: mpsc::Sender<()>, ) { loop { println!("waiting for render..."); - if do_render.recv().is_err() { - println!("surface state destroyed"); + let Ok(surface) = do_render.recv() else { + println!("main thread destroyed"); break; - } + }; // Perform the rendering. let mut surface = surface.lock().unwrap(); @@ -73,15 +72,6 @@ mod ex { let context = softbuffer::Context::new(window.clone()).unwrap(); - (window, context) - }, - |_elwt, (window, context)| { - let surface = { - println!("making surface..."); - let surface = softbuffer::Surface::new(context, window.clone()).unwrap(); - Arc::new(Mutex::new(surface)) - }; - // Spawn a thread to handle rendering for this specific surface. The channels will // be closed and the thread will be stopped whenever this surface (the returned // context below) is dropped, so that it can all be recreated again (on Android) @@ -91,15 +81,20 @@ mod ex { println!("starting thread..."); std::thread::spawn({ let window = window.clone(); - let surface = surface.clone(); - move || render_thread(window, surface, do_render, render_done) + move || render_thread(window, do_render, render_done) }); - (surface, start_render, finish_render) + (window, context, start_render, finish_render) + }, + |_elwt, (window, context, _start_render, _finish_render)| { + println!("making surface..."); + Arc::new(Mutex::new( + softbuffer::Surface::new(context, window.clone()).unwrap(), + )) }, ) .with_event_handler(|state, surface, event, elwt| { - let (window, _context) = state; + let (window, _context, start_render, finish_render) = state; elwt.set_control_flow(ControlFlow::Wait); match event { @@ -107,12 +102,12 @@ mod ex { window_id, event: WindowEvent::RedrawRequested, } if window_id == window.id() => { - let Some((_surface, start_render, finish_render)) = surface else { + let Some(surface) = surface else { eprintln!("RedrawRequested fired before Resumed or after Suspended"); return; }; // Start the render and then finish it. - start_render.send(()).unwrap(); + start_render.send(surface.clone()).unwrap(); finish_render.recv().unwrap(); } Event::WindowEvent {