Skip to content

Commit

Permalink
fix(input): Remove SendWrapper from GILRS_CONTEXT (unless on wasm) (#477
Browse files Browse the repository at this point in the history
)

I don't think we want SendWrapper here - this will panic if accessed
from a different thread than it was initialized with, was panicking on
start updating bones in jumpy. Bevy systems may be scheduled to other
threads.

Arc + Mutex should make us sync + send here anyway. Lmk if any concerns
with this @RockasMockas
  • Loading branch information
MaxCWhitehead authored Oct 7, 2024
1 parent 8722551 commit 4a72715
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion framework_crates/bones_framework/src/input/gilrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@
use crate::prelude::*;
use gilrs::{ev::filter::axis_dpad_to_button, EventType, Filter, Gilrs as GilrsContext};
use once_cell::sync::Lazy;
use send_wrapper::SendWrapper;
use std::sync::{Arc, Mutex};

#[cfg(target_arch = "wasm32")]
use send_wrapper::SendWrapper;

/// Lazy-initialized GilrsContext
#[cfg(not(target_arch = "wasm32"))]
static GILRS_CONTEXT: Lazy<Arc<Mutex<GilrsContext>>> = Lazy::new(|| {
Arc::new(Mutex::new(
GilrsContext::new().expect("Failed to initialize GilrsContext"),
))
});

// Use SendWrapper when on wasm - GilrsContext is not Sync/Send on wasm,
// this is ok because bevy is single threaded on wasm, it will only be
// accessed from one thread.
/// Lazy-initialized GilrsContext
#[cfg(target_arch = "wasm32")]
static GILRS_CONTEXT: Lazy<Arc<Mutex<SendWrapper<GilrsContext>>>> = Lazy::new(|| {
Arc::new(Mutex::new(SendWrapper::new(
GilrsContext::new().expect("Failed to initialize GilrsContext"),
Expand Down

0 comments on commit 4a72715

Please sign in to comment.