Skip to content

Commit

Permalink
Replace lazy_statics with std::sync::LazyLocks (#314)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jschwe authored Sep 5, 2024
1 parent f03bad7 commit 83c1d08
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 60 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
6 changes: 2 additions & 4 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContextID> = Mutex::new(ContextID(0));
}
#[doc(hidden)]
pub static CREATE_CONTEXT_MUTEX: Mutex<ContextID> = Mutex::new(ContextID(0));

bitflags! {
/// Various flags that control attributes of the context and/or surfaces created from that
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate lazy_static;
#[allow(unused_imports)]
#[macro_use]
extern crate log;
Expand Down
38 changes: 16 additions & 22 deletions src/platform/generic/egl/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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<EGLLibraryWrapper> = 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<EGLLibraryWrapper> = 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);
Expand Down
33 changes: 16 additions & 17 deletions src/platform/generic/egl/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<EGLExtensionFunctions> = 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")),
}
};
}
}
});
18 changes: 8 additions & 10 deletions src/platform/unix/x11/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -72,7 +66,9 @@ impl Connection {
#[inline]
pub fn new() -> Result<Connection, Error> {
unsafe {
*X_THREADS_INIT;
X_THREADS_INIT.call_once(|| {
XInitThreads();
});

let x11_display = XOpenDisplay(ptr::null());
if x11_display.is_null() {
Expand All @@ -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.
///
Expand Down
7 changes: 3 additions & 4 deletions src/platform/windows/wgl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<WGLExtensionFunctions> =
LazyLock::new(|| thread::spawn(extension_loader_thread).join().unwrap());

impl Device {
/// Creates a context descriptor with the given attributes.
Expand Down

0 comments on commit 83c1d08

Please sign in to comment.