Skip to content

Conversation

@matthieu-m
Copy link
Contributor

Attempt to "fix" two flaws of the current documentation:

  1. The over-emphasis of fence - fence synchronization, relegating atomic - fence and fence - atomic synchronization to second fiddle.
  2. The lack of explanation as to how to properly perform atomic - fence and fence - atomic synchronization.

It does so by first making it clear that there are 3 different ways to use an atomic fence, then presenting a full example for each usecase, noting the particular position of the fence with regard to the atomic operation, and rounding up with generic notes.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 19, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 19, 2025

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
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

@rust-log-analyzer

This comment has been minimized.

/// An atomic operation 'X' with (at least) [`Release`] ordering semantics on some atomic object
/// 'm' on thread 1 is paired on thread 2 with an atomic read 'Y' with any order on 'm' followed by
/// a fence 'B' with (at least) [`Acquire`] ordering semantics. This provides a happens-before
/// dependence between X and B.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can copy the cppreference on this?

https://en.cppreference.com/w/cpp/atomic/atomic_thread_fence.html

In particular, this language seems to miss "Y reads the value written by X (or the value would be written by release sequence headed by X if X were a release operation)" -- it's sort of implied by the example, but stating it explicitly feels better. It's also maybe obvious (otherwise it's not clear what would establish a relation between a particular release operation and particular acquire fence), but having a precise rule set seems better than prose? I find the bullets in the reference relatively easier to read than the language here.

Copy link
Member

Choose a reason for hiding this comment

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

(I'm not sure what license that language is under, so copying may not be an option).

Copy link
Contributor Author

@matthieu-m matthieu-m Nov 3, 2025

Choose a reason for hiding this comment

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

Interesting. I tend to find the language on cppreference too "formal" for my taste.

I would say for me it's the difference between the "Guide-level" and "Reference-level" portions of Rust RFCs: the language here is guide-level, whereas the explanation on cppreference is reference-level, a formal specification of sort.

I do think having a "formal" specification is a good thing, but I find the esoteric language makes it hard to approach, and raises more questions than it answers -- what's a "release sequence headed by"? -- and thus I much prefer the Rust documentation (on fence, or Ordering) as it's so much more approachable, due to its use of familiar language.


With the ongoing work on the Rust specification, may I suggest leaving a more familiar language in the documentation, and later on linking to the specification for the more formal definition?

This way we can have our cake and eat it too.

Comment on lines 4293 to 4296
/// An atomic operation 'X' with (at least) [`Release`] ordering semantics on some atomic object
/// 'm' on thread 1 is paired on thread 2 with an atomic read 'Y' with any order on 'm' followed by
/// a fence 'B' with (at least) [`Acquire`] ordering semantics. This provides a happens-before
/// dependence between X and B.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// An atomic operation 'X' with (at least) [`Release`] ordering semantics on some atomic object
/// 'm' on thread 1 is paired on thread 2 with an atomic read 'Y' with any order on 'm' followed by
/// a fence 'B' with (at least) [`Acquire`] ordering semantics. This provides a happens-before
/// dependence between X and B.
/// * An atomic operation 'X' with (at least) [`Release`] ordering semantics on some atomic object
/// 'm' on thread 1 is paired with
/// * an atomic read 'Y' on thread 2 with any order on 'm'
//// * followed by a fence 'B' with (at least) [`Acquire`] ordering semantics.
/// This provides a happens-before dependence between X and B.

Can we reformat something like this for the statements? I think splitting it up help make the readability better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I love the idea of introducing structure to better highlight the various steps.

I wasn't quite sure how to introduce structure, so I've structured each of the 3 "chapters" in a slightly different way.

I personally prefer the 3rd way (fence-fence) which clearly isolates the operation on either thread, while I feel that the 1st way (atomic-fence) which puts the "on thread1" and "is paired on thread 2" in the steps is the less clear.

I'm looking forward to your opinion, and suggestions.

Copy link
Member

@Mark-Simulacrum Mark-Simulacrum Nov 15, 2025

Choose a reason for hiding this comment

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

I think 3rd way seems good to me. Let's update to use that style and then I think it'll be good to merge.

@Mark-Simulacrum Mark-Simulacrum 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 Nov 8, 2025
@matthieu-m
Copy link
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 Nov 9, 2025
@Mark-Simulacrum Mark-Simulacrum 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 Nov 15, 2025
@matthieu-m
Copy link
Contributor Author

@rustbot review

I've also aligned the wording more closely between the 3 paragraphs. There were egregious uses of load/store for the Relaxed operations which could confuse the reader. Relaxed operations now use the read/write wording instead, making it clear that both load/store and RMW operations work just as well.

(This is closer to the wording used on cppreference, which use read/written with regard to the values, not loaded/stored)

@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 Nov 16, 2025
@Mark-Simulacrum
Copy link
Member

r=me with commits squashed

@matthieu-m matthieu-m force-pushed the task/lib-core-sync-atomic-fence-doc-improvement branch from 4825440 to 15b3414 Compare November 17, 2025 17:07
@rustbot

This comment has been minimized.

Attempt to "fix" two flaws of the current documentation:

1. The over-emphasis of fence - fence synchronization, relegating
   atomic - fence and fence - atomic synchronization to second fiddle.
2. The lack of explanation as to how to properly perform atomic - fence
   and fence - atomic synchronization.

It does so by first making it clear that there are 3 different ways to
use an atomic fence, then presenting a full example for each usecase,
noting the particular position of the fence with regard to the atomic
operation, and rounding up with generic notes.
@matthieu-m matthieu-m force-pushed the task/lib-core-sync-atomic-fence-doc-improvement branch from 15b3414 to 5431c6f Compare November 17, 2025 17:12
@rustbot
Copy link
Collaborator

rustbot commented Nov 17, 2025

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.

@matthieu-m
Copy link
Contributor Author

@rustbot r=Mark-Simulacrum

@Mark-Simulacrum
Copy link
Member

@bors r=Mark-Simulacrum

@bors
Copy link
Collaborator

bors commented Nov 18, 2025

📌 Commit 5431c6f has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 18, 2025
bors added a commit that referenced this pull request Nov 18, 2025
Rollup of 5 pull requests

Successful merges:

 - #147887 (Improve the documentation of atomic::fence)
 - #148281 (repr(transparent) check: do not compute check_unsuited more than once)
 - #148484 (Fix suggestion for the `cfg!` macro)
 - #149057 (`rust-analyzer` subtree update)
 - #149061 (debug-assert FixedSizeEncoding invariant)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Nov 18, 2025
Rollup of 5 pull requests

Successful merges:

 - #147887 (Improve the documentation of atomic::fence)
 - #148281 (repr(transparent) check: do not compute check_unsuited more than once)
 - #148484 (Fix suggestion for the `cfg!` macro)
 - #149057 (`rust-analyzer` subtree update)
 - #149061 (debug-assert FixedSizeEncoding invariant)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit e6d08c6 into rust-lang:main Nov 19, 2025
11 checks passed
@rustbot rustbot added this to the 1.93.0 milestone Nov 19, 2025
rust-timer added a commit that referenced this pull request Nov 19, 2025
Rollup merge of #147887 - matthieu-m:task/lib-core-sync-atomic-fence-doc-improvement, r=Mark-Simulacrum

Improve the documentation of atomic::fence

Attempt to "fix" two flaws of the current documentation:

1. The over-emphasis of fence - fence synchronization, relegating atomic - fence and fence - atomic synchronization to second fiddle.
2. The lack of explanation as to how to properly perform atomic - fence and fence - atomic synchronization.

It does so by first making it clear that there are 3 different ways to use an atomic fence, then presenting a full example for each usecase, noting the particular position of the fence with regard to the atomic operation, and rounding up with generic notes.
lnicola pushed a commit to lnicola/rust-analyzer that referenced this pull request Nov 19, 2025
Rollup of 5 pull requests

Successful merges:

 - rust-lang/rust#147887 (Improve the documentation of atomic::fence)
 - rust-lang/rust#148281 (repr(transparent) check: do not compute check_unsuited more than once)
 - rust-lang/rust#148484 (Fix suggestion for the `cfg!` macro)
 - rust-lang/rust#149057 (`rust-analyzer` subtree update)
 - rust-lang/rust#149061 (debug-assert FixedSizeEncoding invariant)

r? `@ghost`
`@rustbot` modify labels: rollup
@matthieu-m matthieu-m deleted the task/lib-core-sync-atomic-fence-doc-improvement branch November 19, 2025 17:18
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Nov 30, 2025
…ic-fence-doc-improvement, r=Mark-Simulacrum

Improve the documentation of atomic::fence

Attempt to "fix" two flaws of the current documentation:

1. The over-emphasis of fence - fence synchronization, relegating atomic - fence and fence - atomic synchronization to second fiddle.
2. The lack of explanation as to how to properly perform atomic - fence and fence - atomic synchronization.

It does so by first making it clear that there are 3 different ways to use an atomic fence, then presenting a full example for each usecase, noting the particular position of the fence with regard to the atomic operation, and rounding up with generic notes.
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Nov 30, 2025
…iaskrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#147887 (Improve the documentation of atomic::fence)
 - rust-lang#148281 (repr(transparent) check: do not compute check_unsuited more than once)
 - rust-lang#148484 (Fix suggestion for the `cfg!` macro)
 - rust-lang#149057 (`rust-analyzer` subtree update)
 - rust-lang#149061 (debug-assert FixedSizeEncoding invariant)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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