-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Conversation
This comment has been minimized.
This comment has been minimized.
068039b
to
2d34036
Compare
There was a problem hiding this 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.
library/std/src/sync/poison/mutex.rs
Outdated
/// 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. |
There was a problem hiding this comment.
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
/// 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. |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
2d34036
to
ffb26ec
Compare
@joboet Thanks. lmk what you think :) |
ffb26ec
to
162ae1b
Compare
@rustbot label -S-waiting-on-author +S-waiting-on-review |
There was a problem hiding this 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
@@ -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. |
There was a problem hiding this comment.
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...
library/std/src/sync/poison/mutex.rs
Outdated
#[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`. |
There was a problem hiding this comment.
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:
/// [`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).
@rustbot author |
162ae1b
to
3d9c134
Compare
No worries. I was expecting a fairly stringent review for this. 😆 I believe I addressed your comments above so let me know what you think. :) @rustbot reviewer |
library/std/src/sync/poison/mutex.rs
Outdated
/// Also note that it is not necessary for `T` to be `Sync` as it is not possible | ||
/// to acquire a `&T` from a [`Mutex`]. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// A [`MutexGuard`] is not `Send` to maximize platform portablity. | |
/// A [`MutexGuard`] is not `Send` to maximize platform portablity. | |
/// |
3d9c134
to
3d84a49
Compare
@rustbot reviewer |
Thank you for the PR! |
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`
…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
…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
…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
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``
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 forMutexGuard
to the best of my knowledge.Let me know if I got anything wrong 😄
fixes #122856
cc: @IoaNNUwU
r? @joboet