about summary refs log tree commit diff
path: root/library/std/src/sync
AgeCommit message (Collapse)AuthorLines
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-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
2020-09-18Finish moving to intra doc links for std::syncAlexis Bourget-51/+34
2020-09-12Mark Once::new as #[inline].Mara Bos-0/+1
Without this, it was not inlined in SyncOnceCell::into_inner(), causing unecessary checks and dead code.
2020-09-06Auto merge of #76128 - poliorcetics:doc-use-arc-clone, r=KodrAusbors-17/+17
Use Arc::clone and Rc::clone in documentation This PR replaces uses of `x.clone()` by `Rc::clone(&x)` (or `Arc::clone(&x)`) to better match the documentation for those types. @rustbot modify labels: T-doc
2020-08-31std: move "mod tests/benches" to separate filesLzu Tao-2384/+2375
Also doing fmt inplace as requested.
2020-08-30Move to Arc::clone(&x) over x.clone() in library/stdAlexis Bourget-17/+17
2020-08-23Prefer https link for wikipedia URLsLzu Tao-1/+1
2020-08-22Use intra-doc-links in `std::sync::*`LeSeulArtichaut-136/+53
2020-08-02fix typosliuzhenyu-1/+1
2020-07-27mv std libs to library/mark-0/+8854