-
Notifications
You must be signed in to change notification settings - Fork 19
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 sync::ReentrantLock
#193
Comments
We briefly discussed this in the libs-api meeting. We're happy to see this added as unstable. Feel free to open a tracking issue (with the unresolved questions) and add its number to your implementation PR. Thanks! |
I think it's better keeping the name |
What different behavior would |
You mean |
|
about the only sane pub struct ReentrantRwLock<T> { .. }
unsafe impl<T: Send> Send for ReentrantRwLock<T> {}
unsafe impl<T: Send + Sync> Sync for ReentrantRwLock<T> {}
impl<T> ReentrantRwLock<T> {
pub fn read(&self) -> ReadLock<T> { .. }
pub fn write(&self) -> WriteLock<T> { .. }
..
}
pub struct ReadLock<'a, T: 'a> { .. }
pub struct WriteLock<'a, T: 'a> { .. }
impl<T> Deref for ReadLock<'_, T> {
type Target = T; // allows access via `&T` since we have a read lock
fn deref(&self) -> &Self::Target { .. }
}
impl<T> Deref for WriteLock<'_, T> {
type Target = Cell<T>; // allows access via `&Cell<T>` (shared mutable access) since we have a write lock
fn deref(&self) -> &Self::Target { .. }
} |
The thing about RwLock besides getting a Anyway I'm not proposing std to include ReentrantRwLock, I'm just saying the name ReentrantMutex sounds better than ReentrantLock. The naming motivation also mentioned OnceLock and LazyLock, but these two have totally different API from Mutex so I think that argument is irrelevant. Not to mention that, for "newer primitives", we have Exclusive rather than ExclusiveLock. |
if both guards return
|
Make `ReentrantLock` public Implements the ACP rust-lang/libs-team#193. `@rustbot` label +T-libs-api +S-waiting-on-ACP
Make `ReentrantLock` public Implements the ACP rust-lang/libs-team#193. ``@rustbot`` label +T-libs-api +S-waiting-on-ACP
Rollup merge of rust-lang#110543 - joboet:reentrant_lock, r=m-ou-se Make `ReentrantLock` public Implements the ACP rust-lang/libs-team#193. ``@rustbot`` label +T-libs-api +S-waiting-on-ACP
This has me confused for a bit, since deadlocks are still possible with reentrant locks -- e.g. by lock order mismatches. Would be good to spell out a bit more where reentrant mutexes come up. |
As this is a closed ACP, I won't bother to change that here. The tracking issue and documentation are (a bit) clearer about the intended use-case, do they resolve your concern? |
Proposal
Problem statement
Provide a lightweight, correct reentrant mutex in
std
.Motivation, use-cases
A number of crates need a reentrant mutex for implementing deadlock-free synchronization. Because such a lock is not currently available, crate authors turn to
parking_lot
1 (bringing in quite a lot of extra weight for such a simple purpose), manually wrappthread_mutex
2 (aaaaah!) or implement it themselves 3, sometimes in very hacky fashion4.Solution sketches
std
already has the privateReentrantMutex
, which it uses to implementStdout
locking. I propose to make this type public with the following API (copying some elements fromMutex
):Unresolved questions
Poisoning
I would argue that
ReentrantLock
should not implement lock poisoning. Since it only provides shared access, breaking the invariants of the inner type is something that the lock has no control over, as it requires interior mutability. It would make sense to require thatT: RefUnwindSafe
, but I think that is too inconvenient for now. IfRefUnwindSafe
were a lint trait, it should definitely be used.Fallible locking
Is there a need for fallible locking, even if it cannot fail because of reentrancy? How would this look like, considering that
TryLockResult
also has a poisoning case, which would never be used here?Naming
New synchronization primitives like
OnceLock
have used theLock
suffix. IMHO the nameReentrantLock
rhymes well with the other types and is more consistent, butReentrantMutex
better highlights the similarity toMutex
.Links and related work
parking_lot
'sReentrantMutex
What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.
Footnotes
https://github.com/imgui-rs/imgui-rs/blob/ba02f2287fb5781bb921b7dc4a7ec95cd88b74d0/imgui/src/context.rs#L1 ↩
https://github.com/douchuan/jvm/blob/5678df31a13ec56faa0daddb561d34ae2def8f1d/crates/vm/src/runtime/thread/mutex.rs#L10 ↩
https://github.com/servo/servo/blob/be6e25a1b223325f47db85050c47982c641c8f0f/components/remutex/lib.rs#L156 ↩
https://github.com/rust-lang/backtrace-rs/blob/a3406f93f3e6d91e05525c671fa694a0aea22c82/src/lib.rs#L154 ↩
The text was updated successfully, but these errors were encountered: