Commit Graph

367 Commits

Author SHA1 Message Date
Matthias Krüger 605a4fc7ab Rollup merge of #109142 - the8472:mutex-block-docs, r=cuviper
Add block-based mutex unlocking example

This modifies the existing example in the Mutex docs to show both `drop()` and block based early unlocking.

Alternative to #81872, which is getting closed.
2023-03-24 01:22:05 +01:00
Matthias Krüger 93a82a44a1 Rollup merge of #108164 - joboet:discard_messages_mpmc_array, r=Amanieu
Drop all messages in bounded channel when destroying the last receiver

Fixes #107466 by splitting the `disconnect` function for receivers/transmitters and dropping all messages in `disconnect_receivers` like the unbounded channel does. Since all receivers must be dropped before the channel is, the messages will already be discarded at that point, so the `Drop` implementation for the channel can be removed.

``@rustbot`` label +T-libs +A-concurrency
2023-03-21 19:00:11 +01:00
the8472 a3e41b5759 Apply suggestions from code review
Co-authored-by: Josh Stone <cuviper@gmail.com>
2023-03-20 12:15:17 +01:00
The 8472 db5dfd2373 Add block-based mutex unlocking example 2023-03-20 12:15:17 +01:00
gimbles e5a5b90afc unequal → not equal 2023-03-15 23:55:48 +05:30
joboet 34aa87292c std: leak remaining messages in bounded channel if message destructor panics 2023-03-14 16:42:34 +01:00
joboet 4e9e465bd4 std: disconnect senders before discarding messages 2023-02-26 11:57:27 +01:00
bors 3701bdc633 Auto merge of #107329 - joboet:optimize_lazylock, r=m-ou-se
Optimize `LazyLock` size

The initialization function was unnecessarily stored separately from the data to be initialized. Since both cannot exist at the same time, a `union` can be used, with the `Once` acting as discriminant. This unfortunately requires some extra methods on `Once` so that `Drop` can be implemented correctly and efficiently.

`@rustbot` label +T-libs +A-atomic
2023-02-18 09:29:21 +00:00
joboet 642a324746 std: add regression test for #107466
Tests that messages are immediately dropped once the last receiver is destroyed.
2023-02-17 15:58:15 +01:00
joboet 746331edf3 std: drop all messages in bounded channel when destroying the last receiver 2023-02-17 15:47:58 +01:00
joboet 6520488e37 std: add safety comment in LazyLock::get 2023-01-27 10:11:42 +01:00
joboet 7165e610a2 std: fix Debug implementation on LazyLock 2023-01-27 10:09:38 +01:00
joboet c1cced8d04 std: optimize LazyLock size 2023-01-26 16:16:03 +01:00
Ibraheem Ahmed ab20f8d5ba remove optimistic spinning from mpsc::SyncSender 2023-01-14 00:36:51 -05:00
Matthias Krüger 720137b5da Rollup merge of #106701 - ibraheemdev:sync-sender-spin, r=Amanieu
Fix `mpsc::SyncSender` spinning behavior

Resolves https://github.com/rust-lang/rust/issues/106668.
2023-01-13 19:16:44 +01:00
Ibraheem Ahmed 8917e99362 rework and document backoff behavior of sync::mpsc 2023-01-11 21:29:14 -05:00
Ibraheem Ahmed f8276c94ac add SyncSender::send_timeout test 2023-01-10 21:54:53 -05:00
Ibraheem Ahmed 2538c0c170 fix SyncSender spinning behavior 2023-01-10 21:39:02 -05:00
Ralf Jung 997101824b std test: better type name, clarifying comment 2023-01-09 16:08:26 +01:00
Thom Chiovoloni a4bf36e87b Update rand in the stdlib tests, and remove the getrandom feature from it 2023-01-04 14:52:41 -08:00
bors ce85c98575 Auto merge of #105651 - tgross35:once-cell-inline, r=m-ou-se
Add #[inline] markers to once_cell methods

Added inline markers to all simple methods under the `once_cell` feature. Relates to #74465 and  #105587

This should not block #105587
2022-12-30 19:22:33 +00:00
Matthias Krüger d91432832e Rollup merge of #104402 - joboet:sync_remutex, r=m-ou-se
Move `ReentrantMutex` to `std::sync`

If I understand #84187 correctly, `sys_common` should not contain platform-independent code, even if it is private.
2022-12-28 22:22:17 +01:00
fee1-dead 0818ba43a4 Rollup merge of #104708 - jonasspinner:fix-backoff-doc-to-match-implementation, r=compiler-errors
Fix backoff doc to match implementation

The commit 8dddb22943 in the crossbeam-channel PR (#93563) changed the backoff strategy to be quadratic instead of exponential. This updates the doc to prevent confusion.
2022-12-28 15:51:40 +08:00
Michael Goulet 4b668a1fee Rollup merge of #103718 - matklad:infer-lazy, r=dtolnay
More inference-friendly API for lazy

The signature for new was

```
fn new<F>(f: F) -> Lazy<T, F>
```

Notably, with `F` unconstrained, `T` can be literally anything, and just `let _ = Lazy::new(|| 92)` would not typecheck.

This historiacally was a necessity -- `new` is a `const` function, it couldn't have any bounds. Today though, we can move `new` under the `F: FnOnce() -> T` bound, which gives the compiler enough data to infer the type of T from closure.
2022-12-27 12:33:33 -08:00
Trevor Gross b9558a15dc Add #[inline] marker to OnceCell/LazyCell/OnceLock/LazyLock 2022-12-13 02:18:15 -05:00
Matthias Krüger 7d8e329194 Rollup merge of #105243 - RalfJung:no-op-let, r=Mark-Simulacrum
remove no-op 'let _ = '

Also see the discussion at https://github.com/rust-lang/rust/pull/93563#discussion_r1034057555.

I don't know why these `Drop` implementations exist to begin with, given that their body does literally nothing, but did not want to change that. (It might affect dropck.)

Cc `````@ibraheemdev````` `````@Amanieu`````
2022-12-06 13:27:42 +01:00
Rageking8 58110572fb fix dupe word typos 2022-12-05 16:42:36 +08:00
Ralf Jung c823dfa8b2 remove no-op 'let _ = ' 2022-12-04 10:32:00 +01:00
Jonas Spinner 70bba3b62a rustdoc: Fix backoff doc to match implementation 2022-11-22 11:06:57 +01:00
Dylan DPC 5caac92dc0 Rollup merge of #104528 - WaffleLapkin:lazy_lock_docfix, r=matklad
Properly link `{Once,Lazy}{Cell,Lock}` in docs

See https://github.com/rust-lang/rust/issues/74465#issuecomment-1317947443
2022-11-19 11:54:44 +05:30
Maybe Waffle 57e726108a Properly link {Once,Lazy}{Cell,Lock} in docs 2022-11-17 11:05:56 +00:00
joboet c66494474c std: move ReentrantMutex to sync 2022-11-14 14:25:44 +01:00
Ralf Jung 5fd561dea2 avoid memory leak in mpsc test 2022-11-14 13:38:53 +01:00
bors afd7977c85 Auto merge of #93563 - ibraheemdev:crossbeam-channel, r=Amanieu
Merge crossbeam-channel into `std::sync::mpsc`

This PR imports the [`crossbeam-channel`](https://github.com/crossbeam-rs/crossbeam/tree/master/crossbeam-channel#crossbeam-channel) crate into the standard library as a private module, `sync::mpmc`. `sync::mpsc` is now implemented as a thin wrapper around `sync::mpmc`. The primary purpose of this PR is to resolve https://github.com/rust-lang/rust/issues/39364. The public API intentionally remains the same.

The reason https://github.com/rust-lang/rust/issues/39364 has not been fixed in over 5 years is that the current channel is *incredibly* complex. It was written many years ago and has sat mostly untouched since. `crossbeam-channel` has become the most popular alternative on crates.io, amassing over 30 million downloads. While crossbeam's channel is also complex, like all fast concurrent data structures, it avoids some of the major issues with the current implementation around dynamic flavor upgrades. The new implementation decides on the datastructure to be used when the channel is created, and the channel retains that structure until it is dropped.

Replacing `sync::mpsc` with a simpler, less performant implementation has been discussed as an alternative. However, Rust touts itself as enabling *fearless concurrency*, and having the standard library feature a subpar implementation of a core concurrency primitive doesn't feel right. The argument is that slower is better than broken, but this PR shows that we can do better.

As mentioned before, the primary purpose of this PR is to fix https://github.com/rust-lang/rust/issues/39364, and so the public API intentionally remains the same. *After* that problem is fixed, the fact that `sync::mpmc` now exists makes it easier to fix the primary limitation of `mpsc`, the fact that it only supports a single consumer. spmc and mpmc are two other common concurrency patterns, and this change enables a path to deprecating `mpsc` and exposing a general `sync::channel` module that supports multiple consumers. It also implements other useful methods such as `send_timeout`. That said, exposing MPMC and other new functionality is mostly out of scope for this PR, and it would be helpful if discussion stays on topic :)

For what it's worth, the new implementation has also been shown to be more performant in [some basic benchmarks](https://github.com/crossbeam-rs/crossbeam/tree/master/crossbeam-channel/benchmarks#results).

cc `@taiki-e`

r? rust-lang/libs
2022-11-13 12:08:42 +00:00
Ibraheem Ahmed a22426916d avoid calling thread::current in channel destructor 2022-11-12 23:13:58 -05:00
bors b0c6527912 Auto merge of #103150 - joboet:remove_lock_wrappers, r=m-ou-se
Remove lock wrappers in `sys_common`

This moves the lazy allocation to `sys` (SGX and UNIX). While this leads to a bit more verbosity, it will simplify future improvements by making room in `sys_common` for platform-independent implementations.

This also removes the condvar check on SGX as it is not necessary for soundness and will be removed anyway once mutex has been made movable.

For simplicity's sake, `libunwind` also uses lazy allocation now on SGX. This will require an update to the C definitions before merging this (CC `@raoulstrackx).`

r? `@m-ou-se`
2022-11-12 01:31:39 +00:00
Ibraheem Ahmed 209168655a tidy 2022-11-09 23:20:02 -05:00
Ibraheem Ahmed f2b5e27a60 spin less in mpsc::SyncSender::send 2022-11-09 23:20:02 -05:00
Ibraheem Ahmed f2966d1d0c remove extra spinning from mpsc parker 2022-11-09 23:20:02 -05:00
Ibraheem Ahmed 8dddb22943 sync::mpsc: quadratic backoff 2022-11-09 23:20:02 -05:00
Ibraheem Ahmed 7b721ed0cd sync::mpsc: reload state after spinning on CAS failure 2022-11-09 23:20:02 -05:00
Ibraheem Ahmed 8c17a3e7cb remove extra spinning from mpsc::Receiver::recv 2022-11-09 23:20:02 -05:00
Ibraheem Ahmed cb394c026a remove mention of rust-lang#39364 from mpsc docs 2022-11-09 23:20:02 -05:00
Ibraheem Ahmed 8a68b40432 add test case for rust-lang#39364 2022-11-09 23:20:02 -05:00
Ibraheem Ahmed 31dc5bba89 implement sync::mpsc as a wrapper around sync::mpmc 2022-11-09 23:20:00 -05:00
Ibraheem Ahmed a43da5a097 initial port of crossbeam-channel 2022-11-09 23:18:06 -05:00
Pietro Albini 807a7bfcee clarify licensing situation of mpsc and spsc queue 2022-11-08 09:36:08 +01:00
joboet 98815742cf std: remove lock wrappers in sys_common 2022-11-06 15:32:59 +01:00
Aleksey Kladov 3cddc8bff6 More inference-friendly API for lazy
The signature for new was

```
fn new<F>(f: F) -> Lazy<T, F>
```

Notably, with `F` unconstrained, `T` can be literally anything, and just
`let _ = Lazy::new(|| 92)` would not typecheck.

This historiacally was a necessity -- `new` is a `const` function, it
couldn't have any bounds. Today though, we can move `new` under the `F:
FnOnce() -> T` bound, which gives the compiler enough data to infer the
type of T from closure.
2022-10-29 09:56:20 +01:00
Rageking8 c246a3ddd4 more dupe typos again 2022-10-25 22:51:47 +08:00