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 985a186
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/netbsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,16 @@ fn init() -> *mut c_void {
const POLYFILL: GetRandomFn = polyfill_using_kern_arand;
ptr = POLYFILL as *mut c_void;
}
GETRANDOM.store(ptr, Ordering::Release);
ptr
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 985a186

Please sign in to comment.