Skip to content

Add more clarification to spin_loop docs - #159679

Open
clarfonthey wants to merge 1 commit into
rust-lang:mainfrom
clarfonthey:spin-loop
Open

Add more clarification to spin_loop docs#159679
clarfonthey wants to merge 1 commit into
rust-lang:mainfrom
clarfonthey:spin-loop

Conversation

@clarfonthey

@clarfonthey clarfonthey commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #159121.

Attempting to update the spin_loop docs to make it a little more clear what the use case for this hint is. Also uses the term "hardware thread" since "hyper-thread" is an Intel-specific term.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 21, 2026
@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jul 21, 2026
@rustbot

rustbot commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

r? @aapoalas

rustbot has assigned @aapoalas.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from 6 candidates

Comment thread library/core/src/hint.rs
Comment thread library/core/src/hint.rs
/// [hardware threads]: https://en.wikipedia.org/wiki/Simultaneous_multithreading
///
/// Because the operating system is not notified at all, this hint should be used only
/// when the expected wait time is short, i.e. a few instructions. Additionally, since

@hanna-kruppe hanna-kruppe Jul 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think talking about "expected wait time" here is a red herring.

  • If you're waiting on another thread, then there is generally no way to get a useful bound on how long it might take until that other thread releases the lock because that's up to the OS scheduler. Especially if you're spinning and thus potentially preventing the other thread from being scheduled. Critical section length in instructions or clock cycles has nothing to do with it.
  • If you're doing a non-blocking CAS or LL/SC loop (like Atomic*::update is intended for), then the duration of the operation that the loop tried to does matter, but only because longer update sequences marginally increase the likelihood and cost of contention. In any case, such loops shouldn't use spin_loop in the first place (as the rest of this paragraph explains).

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conversely: some mutex implementations (including std's futex-based one) happily do the optimistic N-iterations-of spinning even though they have no idea how long the critical sections protected by any particular mutex are. That's fine because it's a still a win when it works out, not too costly compared to the syscall when it doesn't work out, and the fallback to the syscall is still needed in any case.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the "only when expected wait time is short" point remained; I still think it's a red herring that distracts from the other (correct) points being made. In addition, many people have already internalized the folklore that "spin locks are great for short critical sections" and I'm worried about implicitly reinforcing it by mentioning something that sounds related.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 21, 2026
@rustbot

rustbot commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@aapoalas

Copy link
Copy Markdown
Contributor

r? @hanna-kruppe

@rustbot

rustbot commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@clarfonthey

Copy link
Copy Markdown
Contributor Author

So, finally getting back to this after sitting on it for a while. I agree with everything you said, but since documentation for atomics is often very technical and poorly explained, I wanted to try and come up with something a bit more digestible without compromising accuracy.

Hopefully the new version covers the feedback you gave a bit better.

@clarfonthey

Copy link
Copy Markdown
Contributor Author

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 29, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@hanna-kruppe hanna-kruppe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I like this direction and agree it helps make it more digestible. There's still two points (one new, one old) where I think this is inaccurate and could encourage a wrong mental model.

@rustbot author

View changes since this review

Comment thread library/core/src/hint.rs
Comment on lines +233 to +234
/// Be careful when using this hint, since improper usage can lead to [priority inversion]
/// and halt progress entirely. For example, take the below implementation of [`update`]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see the argument for trying to tie this point to"improper usage of spin_loop" as that's the method being documented here. But I think it's a bit misleading to attribute the problem to using spin_loop rather than to busy-waiting at all, especially when immediately followed by an example that illustrates a different reason to not use spin_loop. It would be unfortunate if someone read this and concluded that they should remove spin_loop from their spin-lock-esque loop.

An easy fix would be to move the mention of priority inversion to the paragraph below that talks about use in mutexes. But if you're up for it, I could see splitting this up into two separate examples:

  1. Show a very simple busy-looping spin lock that uses spin_loop (just the inner loop trying to acquire the lock, doesn't need to be runnable), and mention the problems with that1. This would go together with the discussion of how proper lock implementations use spin_loop and why they still yield to the OS after some attempts.
  2. Explain that this is only for busy-wait loops that wait on another thread, using the Atomic::update example and existing discussion.

Footnotes

  1. Priority inversion for one, but if you're up for it, it would also be great to mention the terrible performance even for same-priority threads if the thread that has acquired the lock is not currently running.

Comment thread library/core/src/hint.rs
/// and halt progress entirely. For example, take the below implementation of [`update`]:
///
/// [priority inversion]: https://en.wikipedia.org/wiki/Priority_inversion
/// [`update`]: crate::sync::atomic::Atomic::update

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Atomic and the generic methods on it are still unstable, so linking there is a bit weird for users of stable. Maybe just link AtomicU32::update specifically?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, was thinking about that and not sure. I can go with the u32 version

Comment thread library/core/src/hint.rs
/// [hardware threads]: https://en.wikipedia.org/wiki/Simultaneous_multithreading
///
/// Because the operating system is not notified at all, this hint should be used only
/// when the expected wait time is short, i.e. a few instructions. Additionally, since

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the "only when expected wait time is short" point remained; I still think it's a red herring that distracts from the other (correct) points being made. In addition, many people have already internalized the folklore that "spin locks are great for short critical sections" and I'm worried about implicitly reinforcing it by mentioning something that sounds related.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants