Skip to content

Commit 24ca530

Browse files
author
Clar Fon
committed
Move spin_loop_hint to core::hint module
1 parent e2f221c commit 24ca530

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

Diff for: src/libcore/hint.rs

+23
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,26 @@ use intrinsics;
4949
pub unsafe fn unreachable_unchecked() -> ! {
5050
intrinsics::unreachable()
5151
}
52+
53+
/// Save power or switch hyperthreads in a busy-wait spin-loop.
54+
///
55+
/// This function is deliberately more primitive than
56+
/// [`std::thread::yield_now`](../../std/thread/fn.yield_now.html) and
57+
/// does not directly yield to the system's scheduler.
58+
/// In some cases it might be useful to use a combination of both functions.
59+
/// Careful benchmarking is advised.
60+
///
61+
/// On some platforms this function may not do anything at all.
62+
#[inline]
63+
#[unstable(feature = "renamed_spin_loop", issue = "55002")]
64+
pub fn spin_loop() {
65+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
66+
unsafe {
67+
asm!("pause" ::: "memory" : "volatile");
68+
}
69+
70+
#[cfg(target_arch = "aarch64")]
71+
unsafe {
72+
asm!("yield" ::: "memory" : "volatile");
73+
}
74+
}

Diff for: src/libcore/sync/atomic.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ use intrinsics;
8484
use cell::UnsafeCell;
8585
use fmt;
8686

87+
use hint::spin_loop;
88+
8789
/// Save power or switch hyperthreads in a busy-wait spin-loop.
8890
///
8991
/// This function is deliberately more primitive than
@@ -96,15 +98,7 @@ use fmt;
9698
#[inline]
9799
#[stable(feature = "spin_loop_hint", since = "1.24.0")]
98100
pub fn spin_loop_hint() {
99-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
100-
unsafe {
101-
asm!("pause" ::: "memory" : "volatile");
102-
}
103-
104-
#[cfg(target_arch = "aarch64")]
105-
unsafe {
106-
asm!("yield" ::: "memory" : "volatile");
107-
}
101+
spin_loop()
108102
}
109103

110104
/// A boolean type which can be safely shared between threads.

Diff for: src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@
286286
#![feature(staged_api)]
287287
#![feature(stmt_expr_attributes)]
288288
#![feature(str_internals)]
289+
#![feature(renamed_spin_loop)]
289290
#![feature(rustc_private)]
290291
#![feature(thread_local)]
291292
#![feature(toowned_clone_into)]

0 commit comments

Comments
 (0)