Skip to content

Commit

Permalink
examples/winit_multithread: Make thread persistent again
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Aug 3, 2024
1 parent e33ca8c commit c1162bf
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions examples/winit_multithread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ mod ex {

fn render_thread(
window: Arc<Window>,
surface: Arc<Mutex<Surface>>,
do_render: mpsc::Receiver<()>,
do_render: mpsc::Receiver<Arc<Mutex<Surface>>>,
done: mpsc::Sender<()>,
) {
loop {
println!("waiting for render...");
if do_render.recv().is_err() {
println!("Main thread destroyed");
let Ok(surface) = do_render.recv() else {
println!("main thread destroyed");
// The surface state returned from init_surface below is destroyed.
break;
}
};

// Perform the rendering.
let mut surface = surface.lock().unwrap();
Expand Down Expand Up @@ -74,15 +73,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)
Expand All @@ -92,28 +82,33 @@ 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)| {

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-apple-darwin, macos-latest)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-apple-darwin, macos-latest)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-apple-darwin, macos-latest)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-apple-darwin, macos-latest)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-apple-darwin, macos-latest)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-apple-darwin, macos-latest)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-dl...

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-dl...

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, x11,x11-dlopen)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, x11,x11-dlopen)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, kms)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, kms)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, x11,x11-dlopen)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, x11,x11-dlopen)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-linux-gnu, ubuntu-latest)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-linux-gnu, ubuntu-latest)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-pc-windows-msvc, windows-latest)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-pc-windows-msvc, windows-latest)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-d...

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-d...

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, i686-pc-windows-msvc, windows-latest)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, i686-pc-windows-msvc, windows-latest)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, kms)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, kms)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, i686-pc-windows-msvc, windows-latest)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, i686-pc-windows-msvc, windows-latest)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, kms)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, kms)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-dl...

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-dl...

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, x11,x11-dlopen)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, x11,x11-dlopen)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, i686-pc-windows-msvc, windows-latest)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, i686-pc-windows-msvc, windows-latest)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-pc-windows-msvc, windows-latest)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-pc-windows-msvc, windows-latest)

unused variable: `finish_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-pc-windows-msvc, windows-latest)

unused variable: `start_render`

Check failure on line 90 in examples/winit_multithread.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-pc-windows-msvc, windows-latest)

unused variable: `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 {
Event::WindowEvent {
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 {
Expand Down

0 comments on commit c1162bf

Please sign in to comment.