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

Don't use spin_no_std feature #50

Closed
newpavlov opened this issue Jun 30, 2019 · 0 comments · Fixed by #51
Closed

Don't use spin_no_std feature #50

newpavlov opened this issue Jun 30, 2019 · 0 comments · Fixed by #51

Comments

@newpavlov
Copy link
Member

newpavlov commented Jun 30, 2019

See this issue: rust-lang-nursery/lazy-static.rs#150

I think we can relatively easily replace code here with something like this:

fn is_rdrand_supported() -> bool {
    use core::arch::x86_64::__cpuid;
    use std::sync::atomic::{AtomicUsize, Ordering};
    
    static RDRAND: AtomicUsize = AtomicUsize::new(0);
    const RDRAND_FLAG: u32 = 1 << 30;
    const STATE_FLAG: usize = 1 << 1;
    const RESULT_FLAG: usize = 1 << 0;

    let flags = RDRAND.load(Ordering::Relaxed);
    if (flags & STATE_FLAG) == 0 {
        let leaf1_ecx = unsafe { __cpuid(1).ecx };
        if leaf1_ecx & RDRAND_FLAG != 0 {
            // rdrand is supported
            RDRAND.store(RESULT_FLAG | STATE_FLAG, Ordering::Release);
            true
        } else {
            // rdrand is not supported
            RDRAND.store(STATE_FLAG, Ordering::Release);
            false
        }
    } else {
        (flags & RESULT_FLAG) != 0
    }
}

In the worst-case scenario we may call CPUID several times instead of just one, but I don't think it's a big deal. Also it will remove spin crate from our dependency tree, which will help a bit with std inclusion.

cc @josephlr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant