Skip to content

Don't use spin_no_std feature #50

@newpavlov

Description

@newpavlov

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions