Skip to content

Commit

Permalink
netbsd: Implement std::sync::OnceLock-like semantics for weak lookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Jun 19, 2024
1 parent baae5fc commit bc25789
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/netbsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,19 @@ fn init() -> *mut c_void {
const POLYFILL: GetRandomFn = polyfill_using_kern_arand;
ptr = POLYFILL as *mut c_void;
}
GETRANDOM.store(ptr, Ordering::Release);
ptr
// TODO(MSRV 1.64): Consider using `Ordering::Release` instead of
// `Ordering::AcqRel`; see https://github.com/matklad/once_cell/issues/220
// and https://github.com/rust-lang/rust/pull/98383.
match GETRANDOM.compare_exchange(ptr::null_mut(), ptr, Ordering::AcqRel, Ordering::Acquire) {
// We won the race; `GETRANDOM` now has the value `ptr`.
Ok(_) => ptr,
// We lost the race; another thread stored a different value. We always
// use the first value stored so that we only ever use one
// implementation. This usually won't matter but in theory it could
// matter if a sandbox or antivirus or something is causing dlsym to
// act in a non-idempotent way.
Err(previously_stored) => previously_stored,
}
}

pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
Expand Down

0 comments on commit bc25789

Please sign in to comment.