- Feature Name: pause_instruction
- Start Date: 2015-08-06
- RFC PR: (leave this empty)
- Rust Issue: (leave this empty)
Summary
Introduce the std::thread::pause
function that resembles the x86 PAUSE instruction.
Motivation
The x86 PAUSE instruction is semantically equivalent to nop, but:
- may save energy consumption;
- may yield resources to another hyper-thread in the same core; and
- may provide hints for processors to satisfy memory ordering constraints with a lower cost.
For this reason, Intel specifically advises to use the x86 PAUSE instruction inside a spin lock’s loop.
By introducing the std::thread::pause
function in Rust, one can implement more efficient spin locks in pure Rust.
Detailed design
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn pause() {
asm!("PAUSE");
}
Drawbacks
The Standard Library becomes larger, increasing the maintenance cost.
The maintenance cost may be bigger than it seems, since LLVM does not support such pause
function on-the-shelf.
The Standard Library has to maintain architecture-specific implementations of pause
.
Alternatives
Do not introduce the std::thread::pause
.
- No more maintenance cost.
- but no support for efficient implementation of spin locks.
- If pausing is absolutely necessary, use
std::thread::sleep_ms(0)
instead, with some performance penalty.
Unresolved questions
How to effectively maintain architecture-specific implementations for the std::thread::pause
?