From 83c1d08930207a2aeca748f600fa8b4f04531dd1 Mon Sep 17 00:00:00 2001 From: Jonathan Schwender <55576758+jschwe@users.noreply.github.com> Date: Thu, 5 Sep 2024 09:32:55 +0200 Subject: [PATCH] Replace lazy_statics with std::sync::LazyLocks (#314) * Remove needless lazy_static Mutex::new() is available in const contexts since Rust 1.63, so no lazy_static is needed. * Replace lazy_statics with std::sync::LazyLock --- Cargo.toml | 1 - src/context.rs | 6 ++--- src/lib.rs | 2 -- src/platform/generic/egl/device.rs | 38 ++++++++++++----------------- src/platform/generic/egl/ffi.rs | 33 ++++++++++++------------- src/platform/unix/x11/connection.rs | 18 ++++++-------- src/platform/windows/wgl/context.rs | 7 +++--- 7 files changed, 45 insertions(+), 60 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index aa1e6e27..4477d7ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,6 @@ sm-raw-window-handle-06 = ["dep:rwh_06"] bitflags = "2.6" euclid = "0.22" fnv = { version = "1.0", optional = true } -lazy_static = "1" libc = "0.2" log = "0.4" sparkle = { version = "0.1", optional = true } diff --git a/src/context.rs b/src/context.rs index 5bdf0f06..55e7e8b2 100644 --- a/src/context.rs +++ b/src/context.rs @@ -19,10 +19,8 @@ use std::sync::Mutex; #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub struct ContextID(pub u64); -lazy_static! { - #[doc(hidden)] - pub static ref CREATE_CONTEXT_MUTEX: Mutex = Mutex::new(ContextID(0)); -} +#[doc(hidden)] +pub static CREATE_CONTEXT_MUTEX: Mutex = Mutex::new(ContextID(0)); bitflags! { /// Various flags that control attributes of the context and/or surfaces created from that diff --git a/src/lib.rs b/src/lib.rs index 8826577c..93a6d682 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,8 +12,6 @@ #[macro_use] extern crate bitflags; -#[macro_use] -extern crate lazy_static; #[allow(unused_imports)] #[macro_use] extern crate log; diff --git a/src/platform/generic/egl/device.rs b/src/platform/generic/egl/device.rs index cc0c8897..3fde4958 100644 --- a/src/platform/generic/egl/device.rs +++ b/src/platform/generic/egl/device.rs @@ -4,12 +4,12 @@ use crate::egl::Egl; +#[cfg(not(target_os = "windows"))] +use libc::{dlopen, dlsym, RTLD_LAZY}; use std::ffi::{CStr, CString}; use std::mem; use std::os::raw::c_void; - -#[cfg(not(target_os = "windows"))] -use libc::{dlopen, dlsym, RTLD_LAZY}; +use std::sync::LazyLock; #[cfg(target_os = "windows")] use winapi::shared::minwindef::HMODULE; #[cfg(target_os = "windows")] @@ -20,29 +20,23 @@ thread_local! { } #[cfg(target_os = "windows")] -lazy_static! { - static ref EGL_LIBRARY: EGLLibraryWrapper = { - unsafe { - let module = libloaderapi::LoadLibraryA(c"libEGL.dll".as_ptr()); - EGLLibraryWrapper(module) - } - }; -} +static EGL_LIBRARY: LazyLock = LazyLock::new(|| unsafe { + let module = libloaderapi::LoadLibraryA(c"libEGL.dll".as_ptr()); + EGLLibraryWrapper(module) +}); #[cfg(not(any(target_os = "windows", target_os = "macos")))] -lazy_static! { - static ref EGL_LIBRARY: EGLLibraryWrapper = { - for soname in [c"libEGL.so.1".as_ptr(), c"libEGL.so".as_ptr()] { - unsafe { - let handle = dlopen(soname as *const _, RTLD_LAZY); - if !handle.is_null() { - return EGLLibraryWrapper(handle); - } +static EGL_LIBRARY: LazyLock = LazyLock::new(|| { + for soname in [c"libEGL.so.1".as_ptr(), c"libEGL.so".as_ptr()] { + unsafe { + let handle = dlopen(soname as *const _, RTLD_LAZY); + if !handle.is_null() { + return EGLLibraryWrapper(handle); } } - panic!("Unable to load the libEGL shared object"); - }; -} + } + panic!("Unable to load the libEGL shared object"); +}); #[cfg(target_os = "windows")] struct EGLLibraryWrapper(HMODULE); diff --git a/src/platform/generic/egl/ffi.rs b/src/platform/generic/egl/ffi.rs index 8ed4d110..01bd2057 100644 --- a/src/platform/generic/egl/ffi.rs +++ b/src/platform/generic/egl/ffi.rs @@ -8,6 +8,7 @@ use crate::egl::types::{EGLAttrib, EGLBoolean, EGLContext, EGLDeviceEXT, EGLDisp use crate::egl::types::{EGLenum, EGLint}; use std::os::raw::c_void; +use std::sync::LazyLock; pub enum EGLClientBufferOpaque {} pub type EGLClientBuffer = *mut EGLClientBufferOpaque; @@ -76,22 +77,20 @@ pub(crate) struct EGLExtensionFunctions { >, } -lazy_static! { - pub(crate) static ref EGL_EXTENSION_FUNCTIONS: EGLExtensionFunctions = { - use crate::platform::generic::egl::device::lookup_egl_extension as get; - use std::mem::transmute as cast; - unsafe { - EGLExtensionFunctions { - CreateImageKHR: cast(get(c"eglCreateImageKHR")), - DestroyImageKHR: cast(get(c"eglDestroyImageKHR")), - ImageTargetTexture2DOES: cast(get(c"glEGLImageTargetTexture2DOES")), +pub(crate) static EGL_EXTENSION_FUNCTIONS: LazyLock = LazyLock::new(|| { + use crate::platform::generic::egl::device::lookup_egl_extension as get; + use std::mem::transmute as cast; + unsafe { + EGLExtensionFunctions { + CreateImageKHR: cast(get(c"eglCreateImageKHR")), + DestroyImageKHR: cast(get(c"eglDestroyImageKHR")), + ImageTargetTexture2DOES: cast(get(c"glEGLImageTargetTexture2DOES")), - CreateDeviceANGLE: cast(get(c"eglCreateDeviceANGLE")), - GetNativeClientBufferANDROID: cast(get(c"eglGetNativeClientBufferANDROID")), - QueryDeviceAttribEXT: cast(get(c"eglQueryDeviceAttribEXT")), - QueryDisplayAttribEXT: cast(get(c"eglQueryDisplayAttribEXT")), - QuerySurfacePointerANGLE: cast(get(c"eglQuerySurfacePointerANGLE")), - } + CreateDeviceANGLE: cast(get(c"eglCreateDeviceANGLE")), + GetNativeClientBufferANDROID: cast(get(c"eglGetNativeClientBufferANDROID")), + QueryDeviceAttribEXT: cast(get(c"eglQueryDeviceAttribEXT")), + QueryDisplayAttribEXT: cast(get(c"eglQueryDisplayAttribEXT")), + QuerySurfacePointerANGLE: cast(get(c"eglQuerySurfacePointerANGLE")), } - }; -} + } +}); diff --git a/src/platform/unix/x11/connection.rs b/src/platform/unix/x11/connection.rs index cdeb743e..3eae82df 100644 --- a/src/platform/unix/x11/connection.rs +++ b/src/platform/unix/x11/connection.rs @@ -17,16 +17,10 @@ use euclid::default::Size2D; use std::marker::PhantomData; use std::os::raw::c_void; use std::ptr; -use std::sync::Arc; +use std::sync::{Arc, Once}; use x11::xlib::{Display, XCloseDisplay, XInitThreads, XLockDisplay, XOpenDisplay, XUnlockDisplay}; -lazy_static! { - static ref X_THREADS_INIT: () = { - unsafe { - XInitThreads(); - } - }; -} +static X_THREADS_INIT: Once = Once::new(); /// A connection to the X11 display server. #[derive(Clone)] @@ -72,7 +66,9 @@ impl Connection { #[inline] pub fn new() -> Result { unsafe { - *X_THREADS_INIT; + X_THREADS_INIT.call_once(|| { + XInitThreads(); + }); let x11_display = XOpenDisplay(ptr::null()); if x11_display.is_null() { @@ -93,7 +89,9 @@ impl Connection { /// Wraps an existing X11 `Display` in a `Connection`. /// - /// Important: Before calling this function, X11 must have be initialized in a thread-safe + /// # Safety + /// + /// Before calling this function, X11 must have be initialized in a thread-safe /// manner by using `XInitThreads()`. Otherwise, it will not be safe to use `surfman` from /// multiple threads. /// diff --git a/src/platform/windows/wgl/context.rs b/src/platform/windows/wgl/context.rs index dc8c31b6..6a021b3c 100644 --- a/src/platform/windows/wgl/context.rs +++ b/src/platform/windows/wgl/context.rs @@ -17,6 +17,7 @@ use std::ffi::{CStr, CString}; use std::mem; use std::os::raw::{c_char, c_int, c_void}; use std::ptr; +use std::sync::LazyLock; use std::thread; use winapi::shared::minwindef::{BOOL, FALSE, FLOAT, HMODULE, LPARAM, LPVOID, LRESULT, UINT}; use winapi::shared::minwindef::{WORD, WPARAM}; @@ -153,10 +154,8 @@ thread_local! { }; } -lazy_static! { - pub(crate) static ref WGL_EXTENSION_FUNCTIONS: WGLExtensionFunctions = - thread::spawn(extension_loader_thread).join().unwrap(); -} +pub(crate) static WGL_EXTENSION_FUNCTIONS: LazyLock = + LazyLock::new(|| thread::spawn(extension_loader_thread).join().unwrap()); impl Device { /// Creates a context descriptor with the given attributes.