Skip to content

RWLock: Add deadlock example #82624

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

Merged
merged 1 commit into from
Jun 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion library/std/src/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,19 @@ use crate::sys_common::rwlock as sys;
/// system's implementation, and this type does not guarantee that any
/// particular policy will be used. In particular, a writer which is waiting to
/// acquire the lock in `write` might or might not block concurrent calls to
/// `read`.
/// `read`, e.g.:
///
/// <details><summary>Potential deadlock example</summary>
///
/// ```text
Copy link
Member

Choose a reason for hiding this comment

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

I'm not convinced we spend a bunch of vertical space on a fairly obscure edge case, but I'd defer to @rust-lang/docs.

Copy link
Member

Choose a reason for hiding this comment

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

I think it's up to you and libs; we don't have any real precedent here in either direction.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps we can get the best of both worlds: is there a way to collapse a subset of the docs by default? It would be useful for detailed examples, e.g. like we do in GitHub:

Time-threads diagram
// Thread 1             |  // Thread 2
let _rg = lock.read();  |
                        |  // will block
                        |  let _wg = lock.write();
// may deadlock         |
let _rg = lock.read();  |

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 with the <details> tag, which, given that markdown is a superset of HTML, should be usable. I don't think we currently use it at all, though.

Copy link
Contributor Author

@ojeda ojeda Mar 5, 2021

Choose a reason for hiding this comment

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

Tested it, and it works nicely, although it needs a small tweak: see #82805.

From what I can tell, at least we use <details> in the unstable/yellow orange boxes.

/// // Thread 1 | // Thread 2
/// let _rg = lock.read(); |
/// | // will block
/// | let _wg = lock.write();
/// // may deadlock |
/// let _rg = lock.read(); |
/// ```
/// </details>
///
/// The type parameter `T` represents the data that this lock protects. It is
/// required that `T` satisfies [`Send`] to be shared across threads and
Expand Down