Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PAUSE instruction? #16

Closed
birkenfeld opened this issue Jan 9, 2016 · 5 comments
Closed

Use PAUSE instruction? #16

birkenfeld opened this issue Jan 9, 2016 · 5 comments

Comments

@birkenfeld
Copy link
Contributor

In obtain_lock, would it make sense (on x86) to use the PAUSE instruction inside the loop? Looking at the Intel manuals, it seems to be designed for this kind of situation.

@mvdnes
Copy link
Owner

mvdnes commented Jan 9, 2016

I am unsure what this instruction does. Can you provide a resource which has some details?

@birkenfeld
Copy link
Contributor Author

(Note that I'm not at all an expert. Just trying to learn something.)

In Linux, the cpu_relax function used in spinlocks calls rep; nop which is the same as PAUSE and says it's "good thing to insert into busy-wait loops".

This quote is from the Intel x86 developer's manual:

Improves the performance of spin-wait loops. When executing a “spin-wait loop,” processors will suffer a severe performance penalty when exiting the loop because it detects a possible memory order violation. The PAUSE instruction provides a hint to the processor that the code sequence is a spin-wait loop. The processor uses this hint to avoid the memory order violation in most situations, which greatly improves processor performance. For this reason, it is recommended that a PAUSE instruction be placed in all spin-wait loops.
An additional function of the PAUSE instruction is to reduce the power consumed by a processor while executing a spin loop. A processor can execute a spin-wait loop extremely quickly, causing the processor to consume a lot of power while it waits for the resource it is spinning on to become available. Inserting a pause instruction in a spin-wait loop greatly reduces the processor’s power consumption.

@mvdnes
Copy link
Owner

mvdnes commented Jan 10, 2016

Hmm seems like a good idea then. If you or anyone else would submit a pull request I would be happy to merge it.

birkenfeld added a commit to birkenfeld/spinlock-rs that referenced this issue Jan 10, 2016
@mvdnes mvdnes closed this as completed in ecf4b17 Jan 10, 2016
@mvdnes
Copy link
Owner

mvdnes commented Jan 10, 2016

Thanks! 👍

@mstewartgallus
Copy link

I'm thinking of putting this functionality in the std lib itself rust-lang/rust#40537

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants