about summary refs log tree commit diff
path: root/library/std/src/sync
AgeCommit message (Collapse)AuthorLines
2021-10-10Add #[must_use] to core and std constructorsJohn Kugelman-0/+3
2021-10-03Practice diagnostic message conventionHirochika Matsumoto-3/+3
2021-10-02Make diangostic item names consistentCameron Steffen-1/+1
2021-09-28ref/refmutGus Wynn-2/+2
2021-09-27lock typesGus Wynn-0/+18
2021-09-22Update library/std/src/sync/mpsc/shared.rsMara Bos-1/+1
2021-09-05rust fmtlovasoa-1/+5
2021-09-03Add a better error message for #39364Ophir LOJKINE-1/+1
There is a known bug in the implementation of mpsc channels in rust. This adds a clearer error message when the bug occurs, so that developers don't lose too much time looking for the origin of the bug. See https://github.com/rust-lang/rust/issues/39364
2021-07-29Fix may not to appropriate might not or must notAli Malik-8/+8
2021-07-02Rollup merge of #86783 - mark-i-m:mutex-drop-unsized, r=XanewokYuki Okushi-20/+20
Move Mutex::unlock to T: ?Sized r? ``@mbrubeck`` cc https://github.com/rust-lang/rust/issues/81872
2021-07-01Move Mutex::unlock to T: ?SizedMark Mansi-20/+20
2021-06-28Auto merge of #82624 - ojeda:rwlock-example-deadlock, r=JohnTitorbors-1/+13
RWLock: Add deadlock example Suggested in https://github.com/rust-lang/rust/pull/82596 but it was a bit too late. `@matklad` `@azdavis` `@sfackler`
2021-06-23Use HTTPS links where possibleSmitty-3/+3
2021-06-15Rollup merge of #80269 - pickfire:patch-4, r=joshtriplettYuki Okushi-7/+46
Explain non-dropped sender recv in docs Original senders that are still hanging around could cause Receiver::recv to not block since this is a potential footgun for beginners, clarify more on this in the docs for readers to be aware about it. Maybe it would be better to show an example of the pattern where `drop(tx)` is used when it is being cloned multiple times? Although I have seen it in quite a few articles but I am surprised that this part is not very clear with the current words without careful reading. > If the corresponding Sender has disconnected, or it disconnects while this call is blocking, this call will wake up and return Err to indicate that no more messages can ever be received on this channel. However, since channels are buffered, messages sent before the disconnect will still be properly received. Some words there may seemed similar if I carefully read and relate it but if I am new, I probably does not know "drop" makes it "disconnected". So I mention the words "drop" and "alive" to make it more relatable to lifetime.
2021-06-12Explain non-dropped sender recv in docsIvan Tham-7/+46
Original senders that are still hanging around could cause Receiver::recv to not block since this is a potential footgun for beginners, clarify more on this in the docs for readers to be aware about it. Fix minor tidbits in sender recv doc Co-authored-by: Dylan DPC <dylan.dpc@gmail.com> Add example for unbounded receive loops in doc Show the drop(tx) pattern, based on tokio docs https://tokio-rs.github.io/tokio/doc/tokio/sync/index.html Fix example code for drop sender recv Fix wording in sender docs Co-authored-by: Josh Triplett <josh@joshtriplett.org>
2021-06-01Multiple improvements to RwLocksBenoît du Garreau-30/+4
- Split `sys_common::RWLock` between `StaticRWLock` and `MovableRWLock` - Unbox `RwLock` on some platforms (Windows, Wasm and unsupported) - Simplify `RwLock::into_inner`
2021-05-24minor rewording after reviewTaylor Yu-6/+6
Use "the `WouldBlock` error" instead of "the error `WouldBlock`", etc.
2021-05-20doc: clarify Mutex::try_lock, etc. errorsTaylor Yu-9/+28
Clarify error returns from Mutex::try_lock, RwLock::try_read, RwLock::try_write to make it more obvious that both poisoning and the lock being already locked are possible errors.
2021-04-28Simplify `Mutex::into_inner`Benoît du Garreau-19/+2
2021-04-22Move `sys_common::poison` to `sync::poison`Christiaan Dirkx-6/+265
2021-04-21Replace all `fmt.pad` with `debug_struct`Christiaan Dirkx-4/+4
2021-04-01Fix minor typo in once.rsPredrag Gruevski-1/+1
2021-03-28Rollup merge of #83561 - m-ou-se:lock-debug, r=jackh726Yuki Okushi-8/+16
Improve Debug implementations of Mutex and RwLock. This improves the Debug implementations of Mutex and RwLock. They now show the poison flag and use debug_non_exhaustive. (See #67364.)
2021-03-28Rollup merge of #83559 - m-ou-se:rwlock-guard-debug-fix, r=jackh726Yuki Okushi-2/+2
Fix Debug implementation for RwLock{Read,Write}Guard. This would attempt to print the Debug representation of the lock that the guard has locked, which will try to lock again, fail, and just print `"<locked>"` unhelpfully. After this change, this just prints the contents of the mutex, like the other smart pointers (and MutexGuard) do. MutexGuard had this problem too: https://github.com/rust-lang/rust/issues/57702
2021-03-27Improve Debug implementations of Mutex and RwLock.Mara Bos-8/+16
They now show the poison flag and use debug_non_exhaustive.
2021-03-27Fix Debug implementation for RwLock{Read,Write}Guard.Mara Bos-2/+2
This would attempt to print the Debug representation of the lock that the guard has locked, which will try to lock again, fail, and just print "<locked>" unhelpfully. After this change, this just prints the contents of the mutex, like the other smart pointers (and MutexGuard) do.
2021-03-27Use DebugStruct::finish_non_exhaustive() in std.Mara Bos-3/+3
2021-03-05RWLock: Add deadlock exampleMiguel Ojeda-1/+13
Suggested in https://github.com/rust-lang/rust/pull/82596 but it was a bit too late. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2021-03-01Rollup merge of #82578 - camsteffen:diag-items, r=oli-obkJoshua Nelson-0/+1
Add some diagnostic items for Clippy
2021-03-01Add diagnostic itemsCameron Steffen-0/+1
2021-02-27Update library/std/src/sync/rwlock.rsAleksey Kladov-1/+1
Co-authored-by: Steven Fackler <sfackler@gmail.com>
2021-02-27clarify RW lock's priority gotchaAleksey Kladov-1/+3
In particular, the following program works on Linux, but deadlocks on mac: use std::{ sync::{Arc, RwLock}, thread, time::Duration, }; fn main() { let lock = Arc::new(RwLock::new(())); let r1 = thread::spawn({ let lock = Arc::clone(&lock); move || { let _rg = lock.read(); eprintln!("r1/1"); sleep(1000); let _rg = lock.read(); eprintln!("r1/2"); sleep(5000); } }); sleep(100); let w = thread::spawn({ let lock = Arc::clone(&lock); move || { let _wg = lock.write(); eprintln!("w"); } }); sleep(100); let r2 = thread::spawn({ let lock = Arc::clone(&lock); move || { let _rg = lock.read(); eprintln!("r2"); sleep(2000); } }); r1.join().unwrap(); r2.join().unwrap(); w.join().unwrap(); } fn sleep(ms: u64) { std::thread::sleep(Duration::from_millis(ms)) }
2021-02-18add Mutex::unlockmark-0/+20
2021-02-04Stabilize poison API of Once, rename poisoned()Martin Habovstiak-15/+9
This stabilizes: * `OnceState` * `OnceState::is_poisoned()` (previously named `poisoned()`) * `Once::call_once_force()` `poisoned()` was renamed because the new name is more clear as a few people agreed and nobody objected. Closes #33577
2020-12-22Fix documentation typoLinus Färnstrand-1/+1
2020-12-22Migrate standard library away from compare_and_swapLinus Färnstrand-14/+42
2020-11-07Convert a bunch of intra-doc linksCamelid-3/+0
2020-10-07(docs): make mutex error comment consistent with codebaseSteve Manuel-1/+1
2020-10-02Disable condvar::two_mutexes test on non-unix platforms.Mara Bos-1/+1
Condvars are no longer guaranteed to panic in this case on all platforms. At least the unix implementation still does.
2020-10-02Move boxing and mutex checking logic of condvar into sys_common.Mara Bos-40/+4
2020-09-27Split sys_common::Mutex in StaticMutex and MovableMutex.Mara Bos-28/+6
The (unsafe) Mutex from sys_common had a rather complicated interface. You were supposed to call init() manually, unless you could guarantee it was neither moved nor used reentrantly. Calling `destroy()` was also optional, although it was unclear if 1) resources might be leaked or not, and 2) if destroy() should only be called when `init()` was called. This allowed for a number of interesting (confusing?) different ways to use this Mutex, all captured in a single type. In practice, this type was only ever used in two ways: 1. As a static variable. In this case, neither init() nor destroy() are called. The variable is never moved, and it is never used reentrantly. It is only ever locked using the LockGuard, never with raw_lock. 2. As a Boxed variable. In this case, both init() and destroy() are called, it will be moved and possibly used reentrantly. No other combinations are used anywhere in `std`. This change simplifies things by splitting this Mutex type into two types matching the two use cases: StaticMutex and MovableMutex. The interface of both new types is now both safer and simpler. The first one does not call nor expose init/destroy, and the second one calls those automatically in its new() and Drop functions. Also, the locking functions of MovableMutex are no longer unsafe.
2020-09-25Rollup merge of #76932 - fusion-engineering-forks:condvar-promise, r=sfacklerJonas Schievink-11/+5
Relax promises about condition variable. For quite a while now, there have been plans to at some point use parking_lot or some other more efficient implementation of mutexes and condition variables. Right now, Mutex and CondVar both Box the 'real' mutex/condvar inside, to give it a stable address. This was done because implementations like pthread and Windows critical sections may not be moved. More efficient implementations based on futexes, WaitOnAddress, Windows SRW locks, parking_lot, etc. may be moved (while not borrowed), so wouldn't need boxing. However, not boxing them (which would be great goal to achieve), breaks a promise std currently makes about CondVar. CondVar promises to panic when used with different mutexes, to ensure consistent behaviour on all platforms. To this check, a mutex is considered 'the same' if the address of the 'real mutex' in the Box is the same. This address doesn't change when moving a `std::mutex::Mutex` object, effectively giving it an identity that survives moves of the Mutex object. If we ever switch to a non-boxed version, they no longer carry such an identity, and this check can no longer be made. Four options: 1. Always box mutexes. 2. Add a `MutexId` similar to `ThreadId`. Making mutexes bigger, and making it hard to ever have a `const fn new` for them. 3. Making the requirement of CondVar stricter: panic if the Mutex object itself moved. 4. Making the promise of CondVar weaker: don't promise to panic. 1, 2, and 3 seem like bad options. This PR updates the documentation for 4.
2020-09-25Rollup merge of #76978 - duckymirror:mpsc-from-doc, r=jyn514Jonas Schievink-0/+15
Documented From impls in std/sync/mpsc/mod.rs This is for #51430. r? @steveklabnik
2020-09-21Applied review commentsErik Hofmayer-0/+6
2020-09-21Rollup merge of #76936 - danielhenrymantilla:unsafecell_get_mut, r=RalfJungRalf Jung-6/+2
Add non-`unsafe` `.get_mut()` for `Unsafecell` - Tracking issue: https://github.com/rust-lang/rust/issues/76943 As discussed in: https://internals.rust-lang.org/t/add-non-unsafe-get-mut-for-unsafecell/12407 - ### [Rendered documentation](https://modest-dubinsky-1f9f47.netlify.app/core/cell/struct.unsafecell) This PR tries to move the sound `&mut UnsafeCell<T> -> &mut T` projection that all the "downstream" constructions were already relying on, up to the root abstraction, where it rightfully belongs, and officially blessing it. - this **helps reduce the amount of `unsafe` snippets out there** (_c.f._, the second commit of this PR: https://github.com/rust-lang/rust/pull/76936/commits/09503fd1b30c83ca605546fa3f899721e41e68c6) The fact that this getter is now expose for `UnsafeCell<T>` itself, will also help convey the idea that **`UnsafeCell` is not magical _w.r.t._ `&mut` accesses**, contrary to what some people incorrectly think. - Even the standard library itself at some point had such a confusion, _c.f._ this comment where there is a mention of multi-threaded (and thus _shared_) access despite dealing with exclusive references over unique ownership: https://github.com/rust-lang/rust/blob/59fb88d061544a035f3043b47594b34789204cee/library/core/src/cell.rs#L498-L499 r? @RalfJung
2020-09-20Fix nitsAlexis Bourget-4/+3
2020-09-20Replace unneeded `unsafe` calls to `.get()` with calls to `.get_mut()`Daniel Henry-Mantilla-6/+2
2020-09-20Documented From impls in std/sync/mpsc/mod.rsErik Hofmayer-0/+9
2020-09-19Relax promises about condition variable.Mara Bos-11/+5
This allows for futex or thread parking based implementations in the future.
2020-09-18Fix broken linkAlexis Bourget-3/+3