Skip to content

Commit

Permalink
Auto merge of #56996 - clarcharr:spin_loop_hint, r=KodrAus
Browse files Browse the repository at this point in the history
Move spin_loop_hint to core::hint module

As mentioned in #55002. The new name is kept unstable to decide whether the function should have `_hint` in its name.
  • Loading branch information
bors committed Jan 18, 2019
2 parents daa53a5 + 24ca530 commit 38650b6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
23 changes: 23 additions & 0 deletions src/libcore/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,26 @@ use intrinsics;
pub unsafe fn unreachable_unchecked() -> ! {
intrinsics::unreachable()
}

/// Save power or switch hyperthreads in a busy-wait spin-loop.
///
/// This function is deliberately more primitive than
/// [`std::thread::yield_now`](../../std/thread/fn.yield_now.html) and
/// does not directly yield to the system's scheduler.
/// In some cases it might be useful to use a combination of both functions.
/// Careful benchmarking is advised.
///
/// On some platforms this function may not do anything at all.
#[inline]
#[unstable(feature = "renamed_spin_loop", issue = "55002")]
pub fn spin_loop() {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
unsafe {
asm!("pause" ::: "memory" : "volatile");
}

#[cfg(target_arch = "aarch64")]
unsafe {
asm!("yield" ::: "memory" : "volatile");
}
}
12 changes: 3 additions & 9 deletions src/libcore/sync/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ use intrinsics;
use cell::UnsafeCell;
use fmt;

use hint::spin_loop;

/// Save power or switch hyperthreads in a busy-wait spin-loop.
///
/// This function is deliberately more primitive than
Expand All @@ -96,15 +98,7 @@ use fmt;
#[inline]
#[stable(feature = "spin_loop_hint", since = "1.24.0")]
pub fn spin_loop_hint() {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
unsafe {
asm!("pause" ::: "memory" : "volatile");
}

#[cfg(target_arch = "aarch64")]
unsafe {
asm!("yield" ::: "memory" : "volatile");
}
spin_loop()
}

/// A boolean type which can be safely shared between threads.
Expand Down
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@
#![feature(staged_api)]
#![feature(stmt_expr_attributes)]
#![feature(str_internals)]
#![feature(renamed_spin_loop)]
#![feature(rustc_private)]
#![feature(thread_local)]
#![feature(toowned_clone_into)]
Expand Down

0 comments on commit 38650b6

Please sign in to comment.