about summary refs log tree commit diff
path: root/library/std/src/sync
AgeCommit message (Collapse)AuthorLines
2024-07-07once_lock: make test not take as long in MiriRalf Jung-6/+13
2024-07-06Remove non-focused memory leak in `std` doctest for Miri.Zachary S-0/+3
2024-06-18Replace `move||` with `move ||` in `compiler/` and `library/`Vonr-15/+15
Edit from #126631 to revert changes on ui tests
2024-06-15std: suggest OnceLock over OnceJubilee Young-4/+14
2024-06-11replace version placeholderPietro Albini-12/+12
2024-06-04Rollup merge of #125696 - workingjubilee:please-dont-say-you-are-lazy, ↵Guillaume Gomez-59/+73
r=Nilstrieb Explain differences between `{Once,Lazy}{Cell,Lock}` types The question of "which once-ish cell-ish type should I use?" has been raised multiple times, and is especially important now that we have stabilized the `LazyCell` and `LazyLock` types. The answer for the `Lazy*` types is that you would be better off using them if you want to use what is by far the most common pattern: initialize it with a single nullary function that you would call at every `get_or_init` site. For everything else there's the `Once*` types. "For everything else" is a somewhat weak motivation, as it only describes by negation. While contrasting them is inevitable, I feel positive motivations are more understandable. For this, I now offer a distinct example that helps explain why `OnceLock` can be useful, despite `LazyLock` existing: you can do some cool stuff with it that `LazyLock` simply can't support due to its mere definition. The pair of `std::sync::*Lock`s are usable inside a `static`, and can serve roles in async or multithreaded (or asynchronously multithreaded) programs that `*Cell`s cannot. Because of this, they received most of my attention. Fixes #124696 Fixes #125615
2024-06-04Rollup merge of #125504 - mqudsi:once_nominal, r=cuviper许杰友 Jieyou Xu (Joe)-1/+1
Change pedantically incorrect OnceCell/OnceLock wording While the semantic intent of a OnceCell/OnceLock is that it can only be written to once (upon init), the fact of the matter is that both these types offer a `take(&mut self) -> Option<T>` mechanism that, when successful, resets the cell to its initial state, thereby [technically allowing it to be written to again](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=415c023a6ae1ef35f371a2d3bb1aa735) Despite the fact that this can only happen with a mutable reference (generally only used during the construction of the OnceCell/OnceLock), it would be incorrect to say that the type itself as a whole *categorically* prevents being initialized or written to more than once (since it is possible to imagine an identical type only without the `take()` method that actually fulfills that contract). To clarify, change "that cannot be.." to "that nominally cannot.." and add a note to OnceCell about what can be done with an `&mut Self` reference. ```@rustbot``` label +A-rustdocs
2024-06-02Add "OnceList" example to motivate OnceLockJubilee Young-0/+55
While slightly verbose, it helps explain "why bother with OnceLock?" This is a point of confusion that has been raised multiple times shortly before and after the stabilization of LazyLock.
2024-06-02Move first OnceLock example to LazyLockJubilee Young-58/+14
This example is spiritually an example of LazyLock, as it computes a variable at runtime but accepts no inputs into that process. It is also slightly simpler and thus easier to understand. Change it to an even-more concise version and move it to LazyLock. The example now editorializes slightly more. This may be unnecessary, but it can be educational for the reader.
2024-06-02Differ LazyLock vs. OnceLock in std::sync overviewJubilee Young-1/+4
2024-05-28update tracking issue for lazy_cell_consumeTrevor Spiteri-1/+1
2024-05-24Change pedantically incorrect OnceCell/OnceLock wordingMahmoud Al-Qudsi-1/+1
While the semantic intent of a OnceCell/OnceLock is that it can only be written to once (upon init), the fact of the matter is that both these types offer a `take(&mut self) -> Option<T>` mechanism that, when successful, resets the cell to its initial state, thereby technically allowing it to be written to again. Despite the fact that this can only happen with a mutable reference (generally only used during the construction of the OnceCell/OnceLock), it would be incorrect to say that the type itself as a whole categorically prevents being initialized or written to more than once (since it is possible to imagine an identical type only without the `take()` method that actually fulfills that contract). To clarify, change "that cannot be.." to "that nominally cannot.." and add a note to OnceCell about what can be done with an `&mut Self` reference.
2024-04-22remove an unused type from the reentrant lock testsRalf Jung-8/+1
2024-04-06Rollup merge of #114788 - tisonkun:get_mut_or_init, r=dtolnayMatthias Krüger-0/+81
impl get_mut_or_init and get_mut_or_try_init for OnceCell and OnceLock See also https://github.com/rust-lang/rust/issues/74465#issuecomment-1676522051 I'm trying to understand the process for such proposal. And I'll appreciate it if anyone can guide me the next step for consensus or adding tests.
2024-03-27impl get_mut_or_init and get_mut_or_try_init for OnceCell and OnceLocktison-0/+81
See also https://github.com/rust-lang/rust/issues/74465#issuecomment-1676522051 Signed-off-by: tison <wander4096@gmail.com>
2024-03-26Update `RwLock` deadlock example to not use shadowingTrevor Gross-6/+7
Tweak variable names in the deadlock example to remove any potential confusion that the behavior is somehow shadowing-related.
2024-03-22docs(sync): normalize dot in fn summariesMultisampledNight-2/+2
2024-03-20Rollup merge of #122729 - m-ou-se:relax, r=AmanieuJacob Pratt-2/+2
Relax SeqCst ordering in standard library. Every single SeqCst in the standard library is unnecessary. In all cases, Relaxed or Release+Acquire was sufficient. As I [wrote](https://marabos.nl/atomics/memory-ordering.html#common-misconceptions) in my book on atomics: > [..] when reading code, SeqCst basically tells the reader: "this operation depends on the total order of every single SeqCst operation in the program," which is an incredibly far-reaching claim. The same code would likely be easier to review and verify if it used weaker memory ordering instead, if possible. For example, Release effectively tells the reader: "this relates to an acquire operation on the same variable," which involves far fewer considerations when forming an understanding of the code. > > It is advisable to see SeqCst as a warning sign. Seeing it in the wild often means that either something complicated is going on, or simply that the author did not take the time to analyze their memory ordering related assumptions, both of which are reasons for extra scrutiny. r? ````@Amanieu```` ````@joboet````
2024-03-20SeqCst->Relaxed in condvar test.Mara Bos-2/+2
Relaxed is enough here. Synchronization is done by the mutex.
2024-03-19branch 1.78: replace-version-placeholderMark Rousskov-1/+1
2024-03-13Rollup merge of #122386 - joboet:move_pal_once, r=jhprattMatthias Krüger-5/+5
Move `Once` implementations to `sys` Part of https://github.com/rust-lang/rust/issues/117276.
2024-03-12std: move `Once` implementations to `sys`joboet-5/+5
2024-03-12Use `min_exhaustive_patterns` in core & stdNadrieril-0/+8
2024-03-01Rollup merge of #121736 - HTGAzureX1212:HTGAzureX1212/remove-mutex-unlock, ↵Matthias Krüger-20/+0
r=jhpratt Remove `Mutex::unlock` Function As of the completion of the FCP in https://github.com/rust-lang/rust/issues/81872#issuecomment-1474104525, it has come to the conclusion to be closed. This PR removes the function entirely in light of the above. Closes #81872.
2024-02-29fix typosIbraheem Ahmed-3/+3
Co-authored-by: Ralf Jung <post@ralfj.de>
2024-02-29document potential memory leak in unbounded channelIbraheem Ahmed-0/+3
2024-02-29Rollup merge of #110543 - joboet:reentrant_lock, r=m-ou-seMatthias Krüger-197/+340
Make `ReentrantLock` public Implements the ACP rust-lang/libs-team#193. ``@rustbot`` label +T-libs-api +S-waiting-on-ACP
2024-02-28remove Mutex::unlockHTGAzureX1212.-20/+0
2024-02-26fix race between block initialization and receiver disconnectionIbraheem Ahmed-2/+2
2024-02-25Auto merge of #117107 - zachs18:mapped-mutex-guard, r=Amanieubors-4/+985
Implement `MappedMutexGuard`, `MappedRwLockReadGuard`, and `MappedRwLockWriteGuard`. ACP: https://github.com/rust-lang/libs-team/issues/260 Tracking issue: https://github.com/rust-lang/rust/issues/117108 <details> <summary> (Outdated) </summary> `MutexState`/`RwLockState` structs ~~Having `sys::(Mutex|RwLock)` and `poison::Flag` as separate fields in the `Mutex`/`RwLock` would require `MappedMutexGuard`/`MappedRwLockWriteGuard` to hold an additional pointer, so I combined the two fields into a `MutexState`/`RwLockState` struct. This should not noticeably affect perf or layout, but requires an additional field projection when accessing the former `.inner` or `.poison` fields (now `.state.inner` and `.state.poison`).~~ If this is not desired, then `MappedMutexGuard`/`MappedRwLockWriteGuard` can instead hold separate pointers to the two fields. </details> The doc-comments are mostly copied from the existing `*Guard` doc-comments, with some parts from `lock_api::Mapped*Guard`'s doc-comments. Unresolved question: Are more tests needed?
2024-02-24library: use `addr_of!`Pavel Grigorenko-2/+6
2024-02-23Apply suggestions from code reviewzachs18-3/+3
Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
2024-02-23Auto merge of #119536 - Jules-Bertholet:const-barrier, r=dtolnaybors-1/+3
Make `Barrier::new()` const I guess this was just missed in #97791? `@rustbot` label T-libs-api -T-libs
2024-02-23std: make `ReentrantLock` publicjoboet-197/+340
2024-02-20Stabilize `LazyCell` and `LazyLock` (`lazy_cell`)Peter Jaszkowiak-19/+24
2024-05-24Add manual Sync impl for ReentrantLockGuardJacob Lifshay-0/+3
Fixes: #125526
2024-02-14Auto merge of #121078 - oli-obk:rollup-p11zsav, r=oli-obkbors-1/+1
Rollup of 13 pull requests Successful merges: - #116387 (Additional doc links and explanation of `Wake`.) - #118738 (Netbsd10 update) - #118890 (Clarify the lifetimes of allocations returned by the `Allocator` trait) - #120498 (Uplift `TypeVisitableExt` into `rustc_type_ir`) - #120530 (Be less confident when `dyn` suggestion is not checked for object safety) - #120915 (Fix suggestion span for `?Sized` when param type has default) - #121015 (Optimize `delayed_bug` handling.) - #121024 (implement `Default` for `AsciiChar`) - #121039 (Correctly compute adjustment casts in GVN) - #121045 (Fix two UI tests with incorrect directive / invalid revision) - #121049 (Do not point at `#[allow(_)]` as the reason for compat lint triggering) - #121071 (Use fewer delayed bugs.) - #121073 (Fix typos in `OneLock` doc) r? `@ghost` `@rustbot` modify labels: rollup
2024-02-14Auto merge of #100603 - tmandry:zst-guards, r=dtolnaybors-2/+49
Optimize away poison guards when std is built with panic=abort > **Note**: To take advantage of this PR, you will have to use `-Zbuild-std` or build your own toolchain. rustup toolchains always link to a libstd that was compiled with `panic=unwind`, since it's compatible with `panic=abort` code. When std is compiled with `panic=abort` we can remove a lot of the poison machinery from the locks. This changes the `Flag` and `Guard` types to be ZSTs. It also adds an uninhabited member to `PoisonError` so the compiler knows it can optimize away the `Result::Err` paths, and make `LockResult<T>` layout-equivalent to `T`. ### Is this a breaking change? `PoisonError::new` now panics if invoked from a libstd built with `panic="abort"` (or any non-`unwind` strategy). It is unclear to me whether to consider this a breaking change. In order to encounter this behavior, **both of the following must be true**: #### Using a libstd with `panic="abort"` This is pretty uncommon. We don't build libstd with that in rustup, except in (Tier 2-3) platforms that do not support unwinding, **most notably wasm**. Most people who do this are using cargo's `-Z build-std` feature, which is unstable. `panic="abort"` is not a supported option in Rust's build system. It is possible to configure it using `CARGO_TARGET_xxx_RUSTFLAGS`, but I believe this only works on **non-host** platforms. #### Creating `PoisonError` manually This is also unlikely. The only common use case I can think of is in tests, and you can't run tests with `panic="abort"` without the unstable `-Z panic_abort_tests` flag. It's possible that someone is implementing their own locks using std's `PoisonError` **and** defining "thread failure" to mean something other than "panic". If this is the case then we would break their code if it was used with a `panic="abort"` libstd. The locking crates I know of don't replicate std's poison API, but I haven't done much research into this yet. I've touched on a fair number of considerations here. Which ones do people consider relevant?
2024-02-14Fix typos in `OneLock` docIgor-1/+1
2024-02-08Bump version placeholdersMark Rousskov-2/+2
2024-01-14std: Doc blocking behavior of LazyLock methodsBehnam Esfahbod-6/+15
2024-01-10Stabilize mutex_unpoison featureThayne McCombs-6/+2
Closes #96469 @rustbot +T-libs-api
2024-01-02Make `Barrier::new()` constJules Bertholet-1/+3
2023-12-10Auto merge of #118692 - surechen:remove_unused_imports, r=petrochenkovbors-2/+0
remove redundant imports detects redundant imports that can be eliminated. for #117772 : In order to facilitate review and modification, split the checking code and removing redundant imports code into two PR. r? `@petrochenkov`
2023-12-10remove redundant importssurechen-2/+0
detects redundant imports that can be eliminated. for #117772 : In order to facilitate review and modification, split the checking code and removing redundant imports code into two PR.
2023-12-10Fix typo in `std::sync::Mutex` examplenaglis-1/+1
2023-12-08OnceLock: Rework example, statics aren't droppedIan Rees-14/+25
2023-12-05fmtZachary S-4/+1
2023-12-05Specify behavior if the closure passed to *Guard::*map panics.Zachary S-69/+364
2023-12-05fix MappedMutexGuard::(try_)map doc typo.Zachary S-2/+2