Rollup merge of #46174 - stjepang:stabilize-spinloophint, r=sfackler
Stabilize spin_loop_hint Stabilize `spin_loop_hint` in release `1.23.0`. I've also renamed feature `hint_core_should_pause` to `spin_loop_hint`. cc #41196
This commit is contained in:
commit
aa99bd96fd
@ -1,41 +0,0 @@
|
||||
# `hint_core_should_pause`
|
||||
|
||||
The tracking issue for this feature is: [#41196]
|
||||
|
||||
[#41196]: https://github.com/rust-lang/rust/issues/41196
|
||||
|
||||
------------------------
|
||||
|
||||
Many programs have spin loops like the following:
|
||||
|
||||
```rust,no_run
|
||||
use std::sync::atomic::{AtomicBool,Ordering};
|
||||
|
||||
fn spin_loop(value: &AtomicBool) {
|
||||
loop {
|
||||
if value.load(Ordering::Acquire) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
These programs can be improved in performance like so:
|
||||
|
||||
```rust,no_run
|
||||
#![feature(hint_core_should_pause)]
|
||||
use std::sync::atomic;
|
||||
use std::sync::atomic::{AtomicBool,Ordering};
|
||||
|
||||
fn spin_loop(value: &AtomicBool) {
|
||||
loop {
|
||||
if value.load(Ordering::Acquire) {
|
||||
break;
|
||||
}
|
||||
atomic::hint_core_should_pause();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Further improvements could combine `hint_core_should_pause` with
|
||||
exponential backoff or `std::thread::yield_now`.
|
@ -103,9 +103,8 @@ use fmt;
|
||||
///
|
||||
/// On some platforms this function may not do anything at all.
|
||||
#[inline]
|
||||
#[unstable(feature = "hint_core_should_pause", issue = "41196")]
|
||||
pub fn hint_core_should_pause()
|
||||
{
|
||||
#[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");
|
||||
|
Loading…
x
Reference in New Issue
Block a user