diff options
| author | bors <bors@rust-lang.org> | 2019-01-18 07:36:13 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-01-18 07:36:13 +0000 |
| commit | 38650b69cafbff61d71a275ced1e9866a08a36c0 (patch) | |
| tree | c927d8494f2da1e5de2ddca5ad2f185f11ecf5c7 /src/libcore | |
| parent | daa53a52a2667533d5fe59bfcc5b8614b79c3d31 (diff) | |
| parent | 24ca5305263e4cee6d90e740962a94aed7842805 (diff) | |
| download | rust-38650b69cafbff61d71a275ced1e9866a08a36c0.tar.gz rust-38650b69cafbff61d71a275ced1e9866a08a36c0.zip | |
Auto merge of #56996 - clarcharr:spin_loop_hint, r=KodrAus
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.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/hint.rs | 23 | ||||
| -rw-r--r-- | src/libcore/sync/atomic.rs | 12 |
2 files changed, 26 insertions, 9 deletions
diff --git a/src/libcore/hint.rs b/src/libcore/hint.rs index 9e57a4ebc99..ad5a2071a73 100644 --- a/src/libcore/hint.rs +++ b/src/libcore/hint.rs @@ -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"); + } +} diff --git a/src/libcore/sync/atomic.rs b/src/libcore/sync/atomic.rs index 99e6365d15e..8992e513e83 100644 --- a/src/libcore/sync/atomic.rs +++ b/src/libcore/sync/atomic.rs @@ -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 @@ -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. |
