diff --git a/Cargo.lock b/Cargo.lock index 940b86cbba..d0369556fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3375,6 +3375,7 @@ dependencies = [ "metal", "naga", "objc", + "once_cell", "parking_lot", "profiling", "range-alloc", diff --git a/wgpu-hal/Cargo.toml b/wgpu-hal/Cargo.toml index e4c5192a29..fb8e068659 100644 --- a/wgpu-hal/Cargo.toml +++ b/wgpu-hal/Cargo.toml @@ -59,6 +59,7 @@ parking_lot = ">=0.11,<0.13" profiling = { version = "1", default-features = false } raw-window-handle = "0.5" thiserror = "1" +once_cell = "1.18.0" # backends common arrayvec = "0.7" diff --git a/wgpu-hal/src/gles/wgl.rs b/wgpu-hal/src/gles/wgl.rs index 5fa9ab2d57..4d17b07c34 100644 --- a/wgpu-hal/src/gles/wgl.rs +++ b/wgpu-hal/src/gles/wgl.rs @@ -3,6 +3,7 @@ use glutin_wgl_sys::wgl_extra::{ Wgl, CONTEXT_CORE_PROFILE_BIT_ARB, CONTEXT_DEBUG_BIT_ARB, CONTEXT_FLAGS_ARB, CONTEXT_PROFILE_MASK_ARB, }; +use once_cell::sync::Lazy; use parking_lot::{Mutex, MutexGuard}; use raw_window_handle::{RawDisplayHandle, RawWindowHandle}; use std::{ @@ -314,12 +315,9 @@ fn get_global_device_context() -> Result { unsafe impl Sync for SendDc {} unsafe impl Send for SendDc {} - static GLOBAL: Mutex>> = Mutex::new(None); - let mut guard = GLOBAL.lock(); - if guard.is_none() { - *guard = Some(create_global_device_context().map(SendDc)); - } - guard.clone().unwrap().map(|dc| dc.0) + static GLOBAL: Lazy> = + Lazy::new(|| create_global_device_context().map(SendDc)); + GLOBAL.clone().map(|dc| dc.0) } impl crate::Instance for Instance {