Skip to content

Commit 5e0572e

Browse files
committed
fixup! Optimize compat_fn!, add compat_fn_lazy!, add unicows support
1 parent c07b724 commit 5e0572e

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

library/std/src/sys/windows/compat.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ macro_rules! compat_fn_with_fallback {
147147
use super::*;
148148
use crate::mem;
149149
use crate::ffi::CStr;
150-
use crate::sync::atomic::{AtomicBool, AtomicPtr, Ordering};
150+
use crate::sync::atomic::{AtomicPtr, Ordering};
151151
use crate::sys::compat::Module;
152152

153153
type F = unsafe extern "system" fn($($argtype),*) -> $rettype;
@@ -158,7 +158,6 @@ macro_rules! compat_fn_with_fallback {
158158
/// If it succeeds, `PTR` is set to the address of that symbol.
159159
/// If it fails, then `PTR` is set to `fallback`.
160160
static PTR: AtomicPtr<c_void> = AtomicPtr::new(load as *mut _);
161-
static AVAILABLE: AtomicBool = AtomicBool::new(false);
162161

163162
unsafe extern "system" fn load($($argname: $argtype),*) -> $rettype {
164163
let func = load_from_module(Module::new($module));
@@ -173,11 +172,9 @@ macro_rules! compat_fn_with_fallback {
173172
.or_else(|| module.and_then(|m| m.proc_address(SYMBOL_NAME)))
174173
{
175174
PTR.store(f.as_ptr(), Ordering::Relaxed);
176-
AVAILABLE.store(true, Ordering::Relaxed);
177175
mem::transmute(f)
178176
} else {
179177
PTR.store(fallback as *mut _, Ordering::Relaxed);
180-
AVAILABLE.store(false, Ordering::Relaxed);
181178
fallback
182179
}
183180
}
@@ -189,17 +186,21 @@ macro_rules! compat_fn_with_fallback {
189186
}
190187

191188
pub unsafe fn available() -> bool {
192-
// Quickly return if already available
193-
if AVAILABLE.load(Ordering::Relaxed) {
189+
let func = PTR.load(Ordering::Relaxed);
190+
191+
// Check the function pointer to see if it is not equal to `load`
192+
if func == fallback as *mut _ {
193+
return false;
194+
} else if func != load as *mut _ {
194195
return true;
195196
}
196197

197-
// Try to avoid fetching the load status every time
198-
if PTR.load(Ordering::Relaxed) == load as *mut _ {
199-
let _ = load_from_module(Module::new($module));
200-
}
198+
// Otherwise, the function pointer should be resolved
199+
let _ = load_from_module(Module::new($module));
201200

202-
AVAILABLE.load(Ordering::Relaxed)
201+
// After resolution, the function pointer will only point to `fallback` if
202+
// the target function was not found
203+
PTR.load(Ordering::Relaxed) == fallback as *mut _
203204
}
204205

205206
#[inline(always)]

0 commit comments

Comments
 (0)