-
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
update mutex docs for send & sync #123225
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Mark-Simulacrum (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
library/std/src/sync/mutex.rs
Outdated
/// - `Sync` is implemented for `Mutex<T>` if and only if `T` is both `Send` and | ||
/// `Sync`. This ensures that `Mutex<T>` can be safely shared between threads |
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.
That's not true. Since passing around a &Mutex<T>
is basically the same as passing a &mut T
, Send
suffices.
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.
oh yeahh, thanks for that. will correct now
library/std/src/sync/mutex.rs
Outdated
/// safe to share instances of `MutexGuard` between threads when the protected | ||
/// data is also thread-safe. The following explains the safety guarantees: | ||
/// | ||
/// - `MutexGuard` is not `Send` because it represents exclusive access to the |
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 is a platform limitation not intrinsically implied by the concept of mutual exclusion.
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.
got it, fixed
library/std/src/sync/mutex.rs
Outdated
#[stable(feature = "rust1", since = "1.0.0")] | ||
unsafe impl<T: ?Sized + Send> Send for Mutex<T> {} | ||
|
||
/// SAFETY |
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.
Why is this describing MutexGuard
but applied to an impl
for 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.
oh that must be an oversight, will see to that
This comment was marked as outdated.
This comment was marked as outdated.
This comment has been minimized.
This comment has been minimized.
Hi, I'm the author of original issue. It's a good idea to link the issue you're solving in your PR: Read more on How to link PR to the issue |
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 for your work, I think this could be improved.
library/std/src/sync/mutex.rs
Outdated
/// | ||
/// The `Send` and `Sync` implementations for `Mutex` ensure that it is safe to | ||
/// share instances of `Mutex` between threads when the protected data is also | ||
/// thread-safe. The following explains the safety guarantees: |
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 part explains what Sync
& Send
do, which is already explained in Sync
& Send
doc.
I think SAFETY comments should be as short as possible, so this part should be removed.
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.
okay done
library/std/src/sync/mutex.rs
Outdated
/// - `Send` is implemented for `Mutex<T>` if and only if `T` is also `Send`. | ||
/// This guarantees that `Mutex<T>` can be safely transferred between threads, | ||
/// and `T` can be sent across thread boundaries. This is crucial for allowing | ||
/// safe access to the protected data from multiple threads. |
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 think this statement is obvious, because Send
doc states:
Types that can be transferred across thread boundaries.
I'd much prefer to see exactly why is it crucial for T
to be Send
. So I think this should be changed to something along the lines of:
Mutex
is container which wrapsT
, so it's necessary forT
to beSend
to safely sendMutex
to another thread.
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.
alright
library/std/src/sync/mutex.rs
Outdated
/// This ensures that `Mutex<T>` can be safely shared between threads | ||
/// without requiring further synchronization, assuming `T` can be sent across | ||
/// thread boundaries. It guarantees that multiple threads can safely access the | ||
/// protected data concurrently without data races. |
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 describes Sync
, so it should be probably moved to impl Sync for Mutex
.
Also, I'm not a big fan of is basically the same as
terminology in the docs. I would much prefer to see something like:
Mutex<T>
provides mutable access toT
to one thread at the time, yet stillT
has to beSend
because it's not OK forNon-Send
structures to be accessed this way.
One example of this is
Rc
(non-atomic reference counted smart pointer), which is notSend
. We can have multiple copies ofRc
pointing to same heap allocation with non-atomic reference count.Mutex<Rc<_>>
would only protect one copy ofRc
from shared access, making it vulnerable to data-races.
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.
okay
/// It's important to note that `Mutex` can be `Sync` even if its inner type `T` | ||
/// is not `Sync` itself. This is because `Mutex` provides a safe interface for | ||
/// accessing `T` through locking mechanisms, ensuring that only one thread can | ||
/// access `T` at a time. |
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 note is great, but it should also be moved to impl Sync for 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.
okay
This comment was marked as outdated.
This comment was marked as outdated.
This comment has been minimized.
This comment has been minimized.
Hi, as you can see, your PR is blocked by rustbot. It has 2 issues:
Please check again and apply bot's suggestions. When all checks are green, add |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
how do i add the label, please |
We have a bot for labels (it also handles auto-assignments). You can use the shorthand |
This comment was marked as outdated.
This comment was marked as outdated.
I do need help with that please, because everything looks good from here |
You may follow the instructions offered by rustbot. Also, it will be better to squash these commits into one. |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as outdated.
This comment was marked as outdated.
should i fork again and create a new pr to fix these merge commits issues. im really having trouble on it |
@Psalmuel01 you don't need to, but that's totally fine thing to do if it's easier for you |
tbvh, i'm totally lost now and dont know what to do next. this has been pending for so long |
You could try:
or
to just update Then
To rebase onto Basically, I never use |
Thanks @jieyouxu, will try this |
This comment was marked as resolved.
This comment was marked as resolved.
The job Click to see the possible cause of the failure (guessed by this bot)
|
@Psalmuel01 |
@Psalmuel01 |
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`
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``
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``
closes #122856
This is an update to the mutex docs for send and sync to fix the issue raised