about summary refs log tree commit diff
path: root/library/std/src/sync
AgeCommit message (Collapse)AuthorLines
2022-10-25more dupe typos againRageking8-1/+1
2022-10-11Rollup merge of #102277 - mgeisler:rwlock, r=m-ou-seYuki Okushi-31/+32
Consistently write `RwLock` Before the documentation sometimes referred to an "rwlock" and sometimes to "`RwLock`".
2022-10-08Auto merge of #99505 - joboet:futex_once, r=thomccbors-289/+23
std: use futex in `Once` Now that we have efficient locks, let's optimize the rest of `sync` as well. This PR adds a futex-based implementation for `Once`, which drastically simplifies the implementation compared to the generic version, which is provided as fallback for platforms without futex (Windows only supports them on newer versions, so it uses the fallback for now). Instead of storing a linked list of waiters, the new implementation adds another state (`QUEUED`), which is set when there are waiting threads. These now use `futex_wait` on that state and are woken by the running thread when it finishes and notices the `QUEUED` state, thereby avoiding unnecessary calls to `futex_wake_all`.
2022-10-07std: use futex in `Once`joboet-289/+23
2022-09-27Address feedbackmejrs-0/+1
2022-09-25Consistently write `RwLock`Martin Geisler-31/+32
Before the documentation sometimes referred to an "rwlock" and sometimes to "`RwLock`".
2022-09-04Auto merge of #100576 - joboet:movable_const_remutex, r=Mark-Simulacrumbors-55/+0
Make `ReentrantMutex` movable and `const` As `MovableMutex` is now `const`, it can be used to simplify the implementation and interface of the internal reentrant mutex type. Consequently, the standard error stream does not need to be wrapped in `OnceLock` and `OnceLock::get_or_init_pin()` can be removed.
2022-09-03std: make `ReentrantMutex` movable and `const`; simplify `Stdout` initializationjoboet-55/+0
2022-09-02Rollup merge of #97739 - a2aaron:let_underscore, r=estebankGuillaume Gomez-0/+3
Uplift the `let_underscore` lints from clippy into rustc. This PR resolves #97241. This PR adds three lints from clippy--`let_underscore_drop`, `let_underscore_lock`, and `let_underscore_must_use`, which are meant to capture likely-incorrect uses of `let _ = ...` bindings (in particular, doing this on a type with a non-trivial `Drop` causes the `Drop` to occur immediately, instead of at the end of the scope. For a type like `MutexGuard`, this effectively releases the lock immediately, which is almost certainly the wrong behavior) In porting the lints from clippy I had to copy over a bunch of utility functions from `clippy_util` that these lints also relied upon. Is that the right approach? Note that I've set the `must_use` and `drop` lints to Allow by default and set `lock` to Deny by default (this matches the same settings that clippy has). In talking with `@estebank` he informed me to do a Crater run (I am not sure what type of Crater run to request here--I think it's just "check only"?) On the linked issue, there's some discussion about using `must_use` and `Drop` together as a heuristic for when to warn--I did not implement this yet. r? `@estebank`
2022-08-18make many std tests work in MiriRalf Jung-18/+24
2022-06-30Rollup merge of #97629 - guswynn:exclusive_struct, r=m-ou-seMatthias Krüger-0/+2
[core] add `Exclusive` to sync (discussed here: https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Adding.20.60SyncWrapper.60.20to.20std) `Exclusive` is a wrapper that exclusively allows mutable access to the inner value if you have exclusive access to the wrapper. It acts like a compile time mutex, and hold an unconditional `Sync` implementation. ## Justification for inclusion into std - This wrapper unblocks actual problems: - The example that I hit was a vector of `futures::future::BoxFuture`'s causing a central struct in a script to be non-`Sync`. To work around it, you either write really difficult code, or wrap the futures in a needless mutex. - Easy to maintain: this struct is as simple as a wrapper can get, and its `Sync` implementation has very clear reasoning - Fills a gap: `&/&mut` are to `RwLock` as `Exclusive` is to `Mutex` ## Public Api ```rust // core::sync #[derive(Default)] struct Exclusive<T: ?Sized> { ... } impl<T: ?Sized> Sync for Exclusive {} impl<T> Exclusive<T> { pub const fn new(t: T) -> Self; pub const fn into_inner(self) -> T; } impl<T: ?Sized> Exclusive<T> { pub const fn get_mut(&mut self) -> &mut T; pub const fn get_pin_mut(Pin<&mut self>) -> Pin<&mut T>; pub const fn from_mut(&mut T) -> &mut Exclusive<T>; pub const fn from_pin_mut(Pin<&mut T>) -> Pin<&mut Exclusive<T>>; } impl<T: Future> Future for Exclusive { ... } impl<T> From<T> for Exclusive<T> { ... } impl<T: ?Sized> Debug for Exclusive { ... } ``` ## Naming This is a big bikeshed, but I felt that `Exclusive` captured its general purpose quite well. ## Stability and location As this is so simple, it can be in `core`. I feel that it can be stabilized quite soon after it is merged, if the libs teams feels its reasonable to add. Also, I don't really know how unstable feature work in std/core's codebases, so I might need help fixing them ## Tips for review The docs probably are the thing that needs to be reviewed! I tried my best, but I'm sure people have more experience than me writing docs for `Core` ### Implementation: The API is mostly pulled from https://docs.rs/sync_wrapper/latest/sync_wrapper/struct.SyncWrapper.html (which is apache 2.0 licenesed), and the implementation is trivial: - its an unsafe justification for pinning - its an unsafe justification for the `Sync` impl (mostly reasoned about by ````@danielhenrymantilla```` here: https://github.com/Actyx/sync_wrapper/pull/2) - and forwarding impls, starting with derivable ones and `Future`
2022-06-25Auto merge of #96820 - r-raymond:master, r=cuviperbors-5/+37
Make RwLockReadGuard covariant Hi, first time contributor here, if anything is not as expected, please let me know. `RwLockReadGoard`'s type constructor is invariant. Since it behaves like a smart pointer to an immutable reference, there is no reason that it should not be covariant. Take e.g. ``` fn test_read_guard_covariance() { fn do_stuff<'a>(_: RwLockReadGuard<'_, &'a i32>, _: &'a i32) {} let j: i32 = 5; let lock = RwLock::new(&j); { let i = 6; do_stuff(lock.read().unwrap(), &i); } drop(lock); } ``` where the compiler complains that &i doesn't live long enough. If `RwLockReadGuard` is covariant, then the above code is accepted because the lifetime can be shorter than `'a`. In order for `RwLockReadGuard` to be covariant, it can't contain a full reference to the `RwLock`, which can never be covariant (because it exposes a mutable reference to the underlying data structure). By reducing the data structure to the required pieces of `RwLock`, the rest falls in place. If there is a better way to do a test that tests successful compilation, please let me know. Fixes #80392
2022-06-23add tracking issue for exclusiveGus Wynn-1/+1
2022-06-19Auto merge of #97791 - m-ou-se:const-locks, r=m-ou-sebors-3/+10
Make {Mutex, Condvar, RwLock}::new() const. This makes it possible to have `static M: Mutex<_> = Mutex::new(..);` 🎉 Our implementations [on Linux](https://github.com/rust-lang/rust/pull/95035), [on Windows](https://github.com/rust-lang/rust/pull/77380), and various BSDs and some tier 3 platforms have already been using a non-allocating const-constructible implementation. As of https://github.com/rust-lang/rust/pull/97647, the remaining platforms (most notably macOS) now have a const-constructible implementation as well. This means we can finally make these functions publicly const. Tracking issue: https://github.com/rust-lang/rust/issues/93740
2022-06-19Add comment explaining why we use NonNullRobin Raymond-0/+4
2022-06-19Add safety commentsRobin Raymond-6/+9
2022-06-19Documentation typoRobin Raymond-1/+1
2022-06-19*const to NonNull plus documentationRobin Raymond-3/+12
2022-06-19Address commentsRobin Raymond-3/+3
2022-06-19More formattingRobin Raymond-1/+1
2022-06-19FormattingRobin Raymond-2/+5
2022-06-19Make RwLockReadGuard covariantRobin Raymond-5/+18
2022-06-19Rollup merge of #98165 - WaffleLapkin:once_things_renamings, r=m-ou-seMatthias Krüger-0/+970
once cell renamings This PR does the renamings proposed in https://github.com/rust-lang/rust/issues/74465#issuecomment-1153703128 - Move/rename `lazy::{OnceCell, Lazy}` to `cell::{OnceCell, LazyCell}` - Move/rename `lazy::{SyncOnceCell, SyncLazy}` to `sync::{OnceLock, LazyLock}` (I used `Lazy...` instead of `...Lazy` as it seems to be more consistent, easier to pronounce, etc) ```@rustbot``` label +T-libs-api -T-libs
2022-06-16Move/rename `lazy::Sync{OnceCell,Lazy}` to `sync::{Once,Lazy}Lock`Maybe Waffle-0/+970
2022-06-09Avoid `thread::panicking()` in non-poisoning methods of `Mutex` and `RwLock`Josh Stone-8/+15
`Mutex::lock()` and `RwLock::write()` are poison-guarded against panics, in that they set the poison flag if a panic occurs while they're locked. But if we're already in a panic (`thread::panicking()`), they leave the poison flag alone. That check is a bit of a waste for methods that never set the poison flag though, namely `get_mut()`, `into_inner()`, and `RwLock::read()`. These use-cases are now split to avoid that unnecessary call.
2022-06-07[core] add Exclusive to syncGus Wynn-0/+2
2022-06-07Update library/std/src/sync/mutex.rsDylan DPC-1/+1
Co-authored-by: Weiyi Wang <wwylele@gmail.com>
2022-06-07Remove confusing sentence from `Mutex` docsNilstrieb-5/+4
The docs were saying something about "statically initializing" the mutex, and it's not clear what this means. Remove that part to avoid confusion.
2022-06-06Make {Mutex, Condvar, RwLock}::new() const.Mara Bos-0/+3
2022-06-06Make all {Mutex, Condvar, RwLock}::new #[inline].Mara Bos-3/+7
2022-06-05Add diagnostic items to MutexGuard and RwLock GuardsAaron Kofsky-0/+3
I forgot to add the diagnostic to the actual types in `std` earlier.
2022-06-01use 128 cache align for m1 macyifei-1/+2
2022-05-29Use Box::new() instead of box syntax in std testsest31-17/+17
2022-05-20Auto merge of #96422 - tmccombs:mutex-unpoison, r=m-ou-sebors-0/+83
Add functions to un-poison Mutex and RwLock See discussion at https://internals.rust-lang.org/t/unpoisoning-a-mutex/16521/3
2022-05-20Remove references to guards in documentation for clear_poisonThayne McCombs-10/+10
2022-05-19Change clear_poison to take the lock instead of a guardThayne McCombs-8/+18
2022-05-09Auto merge of #95960 - jhpratt:remove-rustc_deprecated, r=compiler-errorsbors-3/+3
Remove `#[rustc_deprecated]` This removes `#[rustc_deprecated]` and introduces diagnostics to help users to the right direction (that being `#[deprecated]`). All uses of `#[rustc_deprecated]` have been converted. CI is expected to fail initially; this requires #95958, which includes converting `stdarch`. I plan on following up in a short while (maybe a bootstrap cycle?) removing the diagnostics, as they're only intended to be short-term.
2022-05-06Mark locks in std lib with clippy::has_significant_dropPreston From-0/+3
2022-05-05Remove condvar::two_mutexes test.Mara Bos-21/+0
We don't guarantee this panics. On most platforms it doesn't anymore.
2022-04-27Add tracking issue number for mutex_unpoisonThayne McCombs-2/+2
2022-04-26Add functions to un-poison Mutex and RwLockThayne McCombs-0/+73
See discussion at https://internals.rust-lang.org/t/unpoisoning-a-mutex/16521/3
2022-04-14Remove use of `#[rustc_deprecated]`Jacob Pratt-3/+3
2022-04-08Remove ptr-int transmute in std::sync::mpscBen Kimock-49/+50
Since https://github.com/rust-lang/rust/pull/95340 landed, Miri with -Zmiri-check-number-validity produces an error on the test suites of some crates which implement concurrency tools, because it seems like such crates tend to use std::sync::mpsc in their tests. This fixes the problem by storing pointer bytes in a pointer.
2022-04-06Rename RWLock to RwLock in std::sys.Mara Bos-2/+2
2022-04-05Auto merge of #95035 - m-ou-se:futex-locks-on-linux, r=Amanieubors-1/+1
Replace Linux Mutex and Condvar with futex based ones. Tracking issue: https://github.com/rust-lang/rust/issues/93740
2022-03-29Make the stdlib largely conform to strict provenance.Aria Beingessner-21/+28
Some things like the unwinders and system APIs are not fully conformant, this only covers a lot of low-hanging fruit.
2022-03-23Update tests.Mara Bos-1/+1
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-17/+17
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2022-03-03Add #[track_caller] to track callers when initializing poisoned Oncereez12g-0/+2
2022-01-28update cfg(bootstrap)sPietro Albini-15/+6