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

docs: Documented Send and Sync requirements for Mutex + MutexGuard #135684

Merged
merged 1 commit into from
Feb 1, 2025

Conversation

ranger-ross
Copy link
Contributor

This an attempt to continue where #123225 left off.

I did some light clean up from the work done in that PR.
I also documented the !Send + Sync implementations for MutexGuard to the best of my knowledge.
Let me know if I got anything wrong 😄

fixes #122856

cc: @IoaNNUwU

r? @joboet

@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 Jan 18, 2025
@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@joboet joboet left a comment

Choose a reason for hiding this comment

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

Thank you for picking this up, it would be great to have some documentation here.

Comment on lines 185 to 187
/// to safely send `Mutex` to another thread. This ensures that the protected
/// data can be accessed safely from multiple threads without causing data races
/// or other unsafe behavior.
Copy link
Member

Choose a reason for hiding this comment

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

The part about multiple threads is relevant for Sync. If you rely on Send, you have exclusive access anyways.

library/std/src/sync/poison/mutex.rs Outdated Show resolved Hide resolved
Comment on lines 239 to 241
/// a new thread. Because ownership always stays on the original thread `Sync`
/// is safe to implement for [`MutexGuard`].
/// See the `!Send` implementation on [`MutexGuard`] for more details.
Copy link
Member

Choose a reason for hiding this comment

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

I don't think ownership is very relevant here. The important part is that you can get a &T from &MutexGuard<T>, so as long as T is Sync (and therefore &T: Send), MutexGuard<T> can also be Sync.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I think this is a much better way of framing it.

@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 Jan 21, 2025
@ranger-ross
Copy link
Contributor Author

@joboet Thanks.
I updated this PR based on your feedback.

lmk what you think :)

@ranger-ross
Copy link
Contributor Author

@rustbot label -S-waiting-on-author +S-waiting-on-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 Jan 22, 2025
Copy link
Member

@joboet joboet left a comment

Choose a reason for hiding this comment

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

Sorry for the language-lawyering. I'm studying a humanities subject, so I can be a bit picky about this stuff... 😄

library/std/src/sync/poison/mutex.rs Outdated Show resolved Hide resolved
@@ -181,10 +181,28 @@ pub struct Mutex<T: ?Sized> {
data: UnsafeCell<T>,
}

// these are the only places where `T: Send` matters; all other
// functionality works fine on a single thread.
/// A [`Mutex`] is safe to send to other threads by design.
Copy link
Member

Choose a reason for hiding this comment

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

Mmmh, this line seems redundant and conflicts with the rest...

#[stable(feature = "rust1", since = "1.0.0")]
unsafe impl<T: ?Sized + Send> Send for Mutex<T> {}

/// [`Mutex`] can be `Sync` if its inner type `T` is `Send`.
Copy link
Member

Choose a reason for hiding this comment

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

I much prefer your phrasing above: "T must be Send" makes the precondition more explicit:

Suggested change
/// [`Mutex`] can be `Sync` if its inner type `T` is `Send`.
/// `T` must be `Send` for [`Mutex`] to be `Sync`.

I think that would also help with understanding the next sentence – my mind currently interprets the "This" as refering to the "Mutex can be Sync" part, not the T: Send part (which is the relevant point).

library/std/src/sync/poison/mutex.rs Show resolved Hide resolved
@joboet
Copy link
Member

joboet commented Jan 24, 2025

@rustbot author

@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 Jan 24, 2025
@ranger-ross
Copy link
Contributor Author

ranger-ross commented Jan 26, 2025

Sorry for the language-lawyering. I'm studying a humanities subject, so I can be a bit picky about this stuff... 😄

No worries. I was expecting a fairly stringent review for this. 😆
Thanks for the feedback.

I believe I addressed your comments above so let me know what you think. :)

@rustbot reviewer

@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 Jan 26, 2025
Comment on lines 203 to 204
/// Also note that it is not necessary for `T` to be `Sync` as it is not possible
/// to acquire a `&T` from a [`Mutex`].
Copy link
Member

Choose a reason for hiding this comment

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

It is possible to get a &T by dereferencing a mutex guard, so this is misleading. The argument here is that the &T is only made available to one thread at a time (if T is not Sync). This is essentially equivalent to sending a &mut T between threads and creating a &T from that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, my bad. I updated this doc based on your comment above. (it was basically a copy/paste since I think your explanation is quiet good)

Copy link
Member

@joboet joboet left a comment

Choose a reason for hiding this comment

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

This reads very nicely. I still have one concern and one nit, but otherwise, this seems ready to merge.

@@ -211,8 +230,16 @@ pub struct MutexGuard<'a, T: ?Sized + 'a> {
poison: poison::Guard,
}

/// A [`MutexGuard`] is not `Send` to maximize platform portablity.
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
/// A [`MutexGuard`] is not `Send` to maximize platform portablity.
/// A [`MutexGuard`] is not `Send` to maximize platform portablity.
///

@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 Jan 29, 2025
@ranger-ross
Copy link
Contributor Author

@rustbot reviewer

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Feb 1, 2025
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 1, 2025
@joboet
Copy link
Member

joboet commented Feb 1, 2025

Thank you for the PR!
@bors r+ rollup

@bors
Copy link
Contributor

bors commented Feb 1, 2025

📌 Commit 3d84a49 has been approved by joboet

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 Feb 1, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 1, 2025
docs: Documented Send and Sync requirements for Mutex + MutexGuard

This an attempt to continue where rust-lang#123225 left off.

I did some light clean up from the work done in that PR.
I also documented the `!Send` + `Sync` implementations for `MutexGuard` to the best of my knowledge.
Let me know if I got anything wrong 😄

fixes rust-lang#122856

cc: `@IoaNNUwU`

r? `@joboet`
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 1, 2025
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#130514 (Implement MIR lowering for unsafe binders)
 - rust-lang#135684 (docs: Documented Send and Sync requirements for Mutex + MutexGuard)
 - rust-lang#135760 (Add `unchecked_disjoint_bitor` per ACP373)
 - rust-lang#136154 (Use +secure-plt for powerpc-unknown-linux-gnu{,spe})
 - rust-lang#136309 (set rustc dylib on manually constructed rustc command)
 - rust-lang#136339 (CompileTest: Add Directives to Ignore `arm-unknown-*` Targets)
 - rust-lang#136368 (Make comma separated lists of anything easier to make for errors)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 1, 2025
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#130514 (Implement MIR lowering for unsafe binders)
 - rust-lang#135684 (docs: Documented Send and Sync requirements for Mutex + MutexGuard)
 - rust-lang#136307 (Implement all mix/max functions in a (hopefully) more optimization amendable way)
 - rust-lang#136360 (Stabilize `once_wait`)
 - rust-lang#136364 (document that ptr cmp is unsigned)
 - rust-lang#136374 (Add link attribute for Enzyme's LLVMRust FFI)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 1, 2025
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#130514 (Implement MIR lowering for unsafe binders)
 - rust-lang#135684 (docs: Documented Send and Sync requirements for Mutex + MutexGuard)
 - rust-lang#136307 (Implement all mix/max functions in a (hopefully) more optimization amendable way)
 - rust-lang#136360 (Stabilize `once_wait`)
 - rust-lang#136364 (document that ptr cmp is unsigned)
 - rust-lang#136374 (Add link attribute for Enzyme's LLVMRust FFI)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 9dfdef6 into rust-lang:master Feb 1, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Feb 1, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 1, 2025
Rollup merge of rust-lang#135684 - ranger-ross:mutex-docs, r=joboet

docs: Documented Send and Sync requirements for Mutex + MutexGuard

This an attempt to continue where rust-lang#123225 left off.

I did some light clean up from the work done in that PR.
I also documented the `!Send` + `Sync` implementations for `MutexGuard` to the best of my knowledge.
Let me know if I got anything wrong 😄

fixes rust-lang#122856

cc: ``@IoaNNUwU``

r? ``@joboet``
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.

Add documentation to Send & Sync impls
5 participants