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

update mutex docs for send & sync #123225

Closed
wants to merge 6 commits into from
Closed

Conversation

Psalmuel01
Copy link

@Psalmuel01 Psalmuel01 commented Mar 30, 2024

closes #122856

This is an update to the mutex docs for send and sync to fix the issue raised

@rustbot
Copy link
Collaborator

rustbot commented Mar 30, 2024

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 (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@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 Mar 30, 2024
Comment on lines 198 to 199
/// - `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
Copy link
Member

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.

Copy link
Author

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

/// 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
Copy link
Member

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.

Copy link
Author

Choose a reason for hiding this comment

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

got it, fixed

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

/// SAFETY
Copy link
Member

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?

Copy link
Author

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

@joboet joboet added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Mar 30, 2024
@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 Mar 30, 2024
@rustbot

This comment was marked as outdated.

@rustbot rustbot added the has-merge-commits PR has merge commits, merge with caution. label Mar 31, 2024
@rust-log-analyzer

This comment has been minimized.

@IoaNNUwU
Copy link

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

Copy link

@IoaNNUwU IoaNNUwU left a 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.

///
/// 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:

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.

Copy link
Author

Choose a reason for hiding this comment

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

okay done

/// - `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.

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 wraps T, so it's necessary for T to be Send to safely send Mutex to another thread.

Copy link
Author

Choose a reason for hiding this comment

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

alright

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

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 to T to one thread at the time, yet still T has to be Send because it's not OK for Non-Send structures to be accessed this way.

One example of this is Rc (non-atomic reference counted smart pointer), which is not Send. We can have multiple copies of Rc pointing to same heap allocation with non-atomic reference count. Mutex<Rc<_>> would only protect one copy of Rc from shared access, making it vulnerable to data-races.

Copy link
Author

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.

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.

Copy link
Author

Choose a reason for hiding this comment

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

okay

@rustbot

This comment was marked as outdated.

@rust-log-analyzer

This comment has been minimized.

@IoaNNUwU
Copy link

IoaNNUwU commented Apr 7, 2024

Hi, as you can see, your PR is blocked by rustbot.

It has 2 issues:

  • your PR has merge commit
  • wrong HTML tags in added documentation.

Please check again and apply bot's suggestions. When all checks are green, add ready-for-review label to get someone to review your PR.

@rustbot

This comment was marked as outdated.

@rustbot

This comment was marked as outdated.

@Psalmuel01
Copy link
Author

Hi, as you can see, your PR is blocked by rustbot.

It has 2 issues:

  • your PR has merge commit
  • wrong HTML tags in added documentation.

Please check again and apply bot's suggestions. When all checks are green, add ready-for-review label to get someone to review your PR.

how do i add the label, please

@joboet
Copy link
Member

joboet commented Apr 15, 2024

We have a bot for labels (it also handles auto-assignments). You can use the shorthand @rustbot ready to switch to S-waiting-on-review. Though could I ask you not to set the label yet? Your PR still has merge commits which you should get rid of, otherwise we unfortunately cannot accept it. Feel free to ask if you need help with that.

@rustbot

This comment was marked as outdated.

@Psalmuel01
Copy link
Author

We have a bot for labels (it also handles auto-assignments). You can use the shorthand @rustbot ready to switch to S-waiting-on-review. Though could I ask you not to set the label yet? Your PR still has merge commits which you should get rid of, otherwise we unfortunately cannot accept it. Feel free to ask if you need help with that.

I do need help with that please, because everything looks good from here

@slanterns
Copy link
Contributor

You may follow the instructions offered by rustbot. Also, it will be better to squash these commits into one.

@rustbot

This comment was marked as outdated.

@rustbot

This comment was marked as resolved.

@rustbot

This comment was marked as outdated.

@Psalmuel01
Copy link
Author

should i fork again and create a new pr to fix these merge commits issues. im really having trouble on it

@Dylan-DPC
Copy link
Member

@Psalmuel01 you don't need to, but that's totally fine thing to do if it's easier for you

@Psalmuel01
Copy link
Author

tbvh, i'm totally lost now and dont know what to do next. this has been pending for so long

@jieyouxu
Copy link
Member

jieyouxu commented Aug 12, 2024

tbvh, i'm totally lost now and dont know what to do next. this has been pending for so long

You could try:

$ git fetch --all --prune

or

$ git fetch origin master

to just update origin/master. You want to update your origin/master branch, otherwise simply rebasing onto your local master will still have the merge commits.

Then

$ get rebase origin/master --interactive --autosquash

To rebase onto origin/master, then drop the merge commits and squash your own commits into one.

Basically, I never use git merge or git pull without rebase.

@Psalmuel01
Copy link
Author

Thanks @jieyouxu, will try this

@rustbot

This comment was marked as resolved.

@rustbot rustbot removed the has-merge-commits PR has merge commits, merge with caution. label Aug 15, 2024
@rust-log-analyzer
Copy link
Collaborator

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#16 2.882 Building wheels for collected packages: reuse
#16 2.883   Building wheel for reuse (pyproject.toml): started
#16 3.128   Building wheel for reuse (pyproject.toml): finished with status 'done'
#16 3.129   Created wheel for reuse: filename=reuse-4.0.3-cp310-cp310-manylinux_2_35_x86_64.whl size=132715 sha256=dfa09868353292d98f811d3efdb0d54d07389e808efc71d68e3b93c514bf8bec
#16 3.129   Stored in directory: /tmp/pip-ephem-wheel-cache-m2lhg_y4/wheels/3d/8d/0a/e0fc6aba4494b28a967ab5eaf951c121d9c677958714e34532
#16 3.132 Installing collected packages: boolean-py, binaryornot, tomlkit, reuse, python-debian, markupsafe, license-expression, jinja2, chardet, attrs
#16 3.533 Successfully installed attrs-23.2.0 binaryornot-0.4.4 boolean-py-4.0 chardet-5.2.0 jinja2-3.1.4 license-expression-30.3.0 markupsafe-2.1.5 python-debian-0.1.49 reuse-4.0.3 tomlkit-0.13.0
#16 3.534 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#16 DONE 3.6s
---


# RECOMMENDATIONS

* Fix missing copyright/licensing information: For one or more files, the tool
  cannot find copyright and/or licensing information. You typically do this by
  adding 'SPDX-FileCopyrightText' and 'SPDX-License-Identifier' tags to each
  file. The tutorial explains additional ways to do this:
  <https://reuse.software/tutorial/>
  local time: Thu Aug 15 16:51:26 UTC 2024
  network time: Thu, 15 Aug 2024 16:51:26 GMT
##[error]Process completed with exit code 1.
Post job cleanup.

@alex-semenyuk
Copy link
Member

@Psalmuel01
From wg-triage. Do you have any issues/question to proceed with this PR?

@alex-semenyuk
Copy link
Member

@Psalmuel01
From wg-triage. Closed this PR due to inactivity. Feel free to reopen or raised new one. Thanks for your efforts.

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`
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``
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
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools 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.

Add documentation to Send & Sync impls
10 participants