Skip to content

Commit

Permalink
Replaces static mutable references in perf_libs (#4417)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored Jan 14, 2025
1 parent 946c777 commit e7301b2
Showing 1 changed file with 9 additions and 13 deletions.
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()
}

0 comments on commit e7301b2

Please sign in to comment.