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

Add documentation to Send & Sync impls #122856

Closed
IoaNNUwU opened this issue Mar 22, 2024 · 6 comments · Fixed by #135684
Closed

Add documentation to Send & Sync impls #122856

IoaNNUwU opened this issue Mar 22, 2024 · 6 comments · Fixed by #135684
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools E-help-wanted Call for participation: Help is requested to fix this issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@IoaNNUwU
Copy link

Location

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

Summary

I think it is a good idea to document Send & Sync impls in types such as Mutex, RwLock etc. especially considering they are unsafe traits.

Also it's not obvious why some types have particular restrictions, for example:

  • why Mutex is Sync only if the T: Send
  • why MutexGuard is !Send

It can be done as // SAFETY comments or proper docs.

@IoaNNUwU IoaNNUwU added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Mar 22, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 22, 2024
@workingjubilee workingjubilee added T-libs Relevant to the library team, which will review and decide on the PR/issue. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 22, 2024
@workingjubilee
Copy link
Member

for Mutex in particular, elsewhere we document:

/// A mutual exclusion primitive useful for protecting shared data
///
/// This mutex will block threads waiting for the lock to become available. The
/// mutex can be created via a [`new`] constructor. Each mutex has a type parameter
/// which represents the data that it is protecting. The data can only be accessed
/// through the RAII guards returned from [`lock`] and [`try_lock`], which
/// guarantees that the data is only ever accessed when the mutex is locked.
///

this makes the Mutex safety docs for these unsafe impls slightly tautological: "why is this Sync? because we guarantee threads are blocked until they get the lock, thus taking care of data races. why do we block and make people wait? to make this soundly Sync. why must it be Send to be Sync? because if it's not Send, it's not capable of being accessed safely from multiple threads at all."

so it's mostly organizing data already there into something nicely written. bikeshed prone, but """easy""" (if you are a decent writer).

@workingjubilee workingjubilee added E-help-wanted Call for participation: Help is requested to fix this issue. and removed E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. labels Mar 22, 2024
@workingjubilee
Copy link
Member

I suppose that is possibly not easy. 🤔 subjective.

@IoaNNUwU
Copy link
Author

Sync doc states:

Types for which it is safe to share references between threads.

I would assume that Mutex is Sync regardless of T boundries, because we cannot get unprotected access to inner structure and can only access it using lock() method by single thread at the time. So it should be safe to share for example &'static Mutex<T> between threads regardless of T being Send.

Also I do understand the problem. We cannot have, for example Arc<Mutex<Rc<T>>> (Rc is not Send) because Rc<T> is not-thread-safe reference-like structure that wraps pointer to heap allocated RcInner<T> and enables non-atomic reference counting, so other copies of Rc are unprotected.

I think it is a good idea to add this example in some place:

  • Sync doc
  • Send doc
  • Mutex doc
  • impl Sync for Mutex

Also Sync & Send docs can be improved adding notion of Reference-like structures and why we can think of implementing Send on them is like implementing Sync on the inner structure. I think with this knowledge trait bounds on Send & Sync impls for Mutex are obvious and docs on these impls could link to Send & Sync docs.

@Psalmuel01
Copy link

i would like to pick this up, and improve the docs

@workingjubilee
Copy link
Member

@Psalmuel01 Go for it!

@Psalmuel01
Copy link

you didnt assign to me yet. ive worked on it, do i just go ahead and create a pr

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue 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 bors closed this as completed in 9dfdef6 Feb 1, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue 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 E-help-wanted Call for participation: Help is requested to fix this issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
4 participants