Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaces static mutable references in perf_libs #4417

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions perf/src/perf_libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use {
fs,
os::raw::{c_int, c_uint},
path::{Path, PathBuf},
sync::Once,
sync::{Once, OnceLock},
},
};

Expand Down Expand Up @@ -81,20 +81,16 @@ pub struct Api<'a> {
Symbol<'a, unsafe extern "C" fn(packed_ge: *const u8) -> c_int>,
}

static mut API: Option<Container<Api>> = None;
static API: OnceLock<Container<Api>> = OnceLock::new();

fn init(name: &OsStr) {
static INIT_HOOK: Once = Once::new();

info!("Loading {:?}", name);
unsafe {
INIT_HOOK.call_once(|| {
API = Some(Container::load(name).unwrap_or_else(|err| {
error!("Unable to load {:?}: {}", name, err);
std::process::exit(1);
}));
API.get_or_init(|| {
unsafe { Container::load(name) }.unwrap_or_else(|err| {
error!("Unable to load {:?}: {}", name, err);
std::process::exit(1);
})
}
});
}

pub fn locate_perf_libs() -> Option<PathBuf> {
Expand Down Expand Up @@ -181,8 +177,8 @@ pub fn api() -> Option<&'static Container<Api<'static>>> {
if std::env::var("TEST_PERF_LIBS_CUDA").is_ok() {
init_cuda();
}
})
});
}

unsafe { API.as_ref() }
API.get()
}
Loading