Skip to content

Commit

Permalink
cpufeatures: use #[cold] for initialization code (#1096)
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov authored Aug 12, 2024
1 parent 030ae1d commit 807f77f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
21 changes: 14 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cpufeatures/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cpufeatures"
version = "0.2.12"
version = "0.2.13"
description = """
Lightweight runtime CPU feature detection for aarch64, loongarch64, and x86/x86_64 targets,
with no_std support and support for mobile targets including Android and iOS
Expand Down
21 changes: 12 additions & 9 deletions cpufeatures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,24 @@ macro_rules! new {
}
}

/// Initialize underlying storage if needed and get
/// stored value and initialization token.
/// Get stored value and initialization token,
/// initializing underlying storage if needed.
#[inline]
pub fn init_get() -> (InitToken, bool) {
let res = $crate::__unless_target_features! {
$($tf),+ => {
#[cold]
fn init_inner() -> bool {
let res = $crate::__detect_target_features!($($tf),+);
STORAGE.store(res as u8, Relaxed);
res
}

// Relaxed ordering is fine, as we only have a single atomic variable.
let val = STORAGE.load(Relaxed);

if val == UNINIT {
let res = $crate::__detect_target_features!($($tf),+);
STORAGE.store(res as u8, Relaxed);
res
init_inner()
} else {
val == 1
}
Expand All @@ -199,15 +204,13 @@ macro_rules! new {
(InitToken(()), res)
}

/// Initialize underlying storage if needed and get
/// initialization token.
/// Initialize underlying storage if needed and get initialization token.
#[inline]
pub fn init() -> InitToken {
init_get().0
}

/// Initialize underlying storage if needed and get
/// stored value.
/// Initialize underlying storage if needed and get stored value.
#[inline]
pub fn get() -> bool {
init_get().1
Expand Down

0 comments on commit 807f77f

Please sign in to comment.