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
///
/// A common use case for `spin_loop` is implementing bounded optimistic
/// spinning in a CAS loop in synchronization primitives. To avoid problems
/// like priority inversion, it is strongly recommended that the spin loop is

@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.

The mention of priority inversion should not be lost. The main problem with spin locks that never yield to the scheduler isn't that they're less efficient, it's that they can completely explode when the thread that's in the critical section isn't running at all.

View changes since the review

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.

@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 assigned hanna-kruppe and unassigned aapoalas Jul 23, 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.

4 participants