Commit Graph

49 Commits

Author SHA1 Message Date
наб a0111ec7a1 awhile -> a while where appropriate 2025-07-02 20:17:29 +02:00
Dannyyy93 d6dc08c3f4 docs: fix typos 2025-05-22 22:47:36 +08:00
Christopher Durham 4d93f60568 use generic Atomic type where possible
in core/alloc/std only for now, and ignoring test files

Co-authored-by: Pavel Grigorenko <GrigorenkoPV@ya.ru>
2025-04-27 02:18:08 +03:00
Petros Angelatos b9e2ac5c7b sync::mpsc: prevent double free on Drop
This PR is fixing a regression introduced by #121646 that can lead to a
double free when dropping the channel.

The details of the bug can be found in the corresponding crossbeam PR
https://github.com/crossbeam-rs/crossbeam/pull/1187

Signed-off-by: Petros Angelatos <petrosagg@gmail.com>
2025-04-11 15:33:09 +03:00
Petros Angelatos 9eb6a5446a sync::mpsc: add miri reproducer of double free
Signed-off-by: Petros Angelatos <petrosagg@gmail.com>
2025-04-11 15:23:12 +03:00
Kornel ad566646cf Use faster thread_local in current_thread_id() 2025-02-21 13:09:16 +00:00
Michael Goulet 4312d7b541 Fix pattern matching mode changes and unsafe_op_in_unsafe_fn 2025-02-09 17:11:13 +00:00
Jacob Pratt d2aa3dec8a Rollup merge of #135621 - bjorn3:move_tests_to_stdtests, r=Noratrieb
Move some std tests to integration tests

Unit tests directly inside of standard library crates require a very fragile way of building that is hard to reproduce outside of bootstrap.

Follow up to https://github.com/rust-lang/rust/pull/133859
2025-02-04 05:36:50 -05:00
usamoi 05364239a8 fix doc for std::sync::mpmc 2025-01-27 11:42:16 +08:00
bjorn3 b8ae372e48 Move std::sync unit tests to integration tests
This removes two minor OnceLock tests which test private methods. The
rest of the tests should be more than enough to catch mistakes in those
private methods. Also makes ReentrantLock::try_lock public. And finally
it makes the mpmc tests actually run.
2025-01-26 10:28:05 +00:00
joboet 5a856b82f3 std: allow after-main use of synchronization primitives
By creating an unnamed thread handle when the actual one has already been destroyed, synchronization primitives using thread parking can be used even outside the Rust runtime.

This also fixes an inefficiency in the queue-based `RwLock`: if `thread::current` was not initialized yet, it will create a new handle on every parking attempt without initializing `thread::current`. The private `current_or_unnamed` function introduced here fixes this.
2024-11-18 17:55:36 +01:00
Matthias Krüger 0555bb2a1b Rollup merge of #132869 - lolbinarycat:library-fix-too_long_first_doc_paragraph, r=tgross35
split up the first paragraph of doc comments for better summaries

used `./x clippy -Aclippy::all '-Wclippy::too_long_first_doc_paragraph' library/core library/alloc` to find these issues.
2024-11-12 06:27:19 +01:00
binarycat ae3c68db34 split up the first paragraph of doc comments for better summaries 2024-11-10 13:22:58 -06:00
Josh Stone 03383ad102 Initialize channel Blocks directly on the heap
The channel's `Block::new` was causing a stack overflow because it held
32 item slots, instantiated on the stack before moving to `Box::new`.
The 32x multiplier made modestly-large item sizes untenable.

That block is now initialized directly on the heap.

Fixes #102246
2024-11-07 10:09:45 -08:00
Ralf Jung 854e3c43e0 library: consistently use American spelling for 'behavior' 2024-10-25 12:02:47 +02:00
Ralf Jung 9fa120593e mpmc doctest: make sure main thread waits for child threads 2024-10-02 08:00:17 +02:00
Obei Sideg 041e76b7cd Add multi-producer, multi-consumer channel (mpmc) 2024-09-30 20:43:51 +03:00
Josh Stone f4d9d1a0ea Use &raw in the standard library
Since the stabilization in #127679 has reached stage0, 1.82-beta, we can
start using `&raw` freely, and even the soft-deprecated `ptr::addr_of!`
and `ptr::addr_of_mut!` can stop allowing the unstable feature.

I intentionally did not change any documentation or tests, but the rest
of those macro uses are all now using `&raw const` or `&raw mut` in the
standard library.
2024-09-25 17:03:20 -07:00
Michael Goulet c682aa162b Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
ranger-ross 24ad26db3b Fixed some typos in the standard library documentation/comments 2024-08-31 14:41:01 +09:00
Nicholas Nethercote 84ac80f192 Reformat use declarations.
The previous commit updated `rustfmt.toml` appropriately. This commit is
the outcome of running `x fmt --all` with the new formatting options.
2024-07-29 08:26:52 +10:00
Jubilee Young e32460276c std: Unsafe-wrap std::sync 2024-07-14 17:59:37 -07:00
Ibraheem Ahmed 7c9fa952c3 fix typos
Co-authored-by: Ralf Jung <post@ralfj.de>
2024-02-29 01:33:02 -05:00
Ibraheem Ahmed 9c6a0766be document potential memory leak in unbounded channel 2024-02-29 00:56:31 -05:00
Ibraheem Ahmed 580b003edd fix race between block initialization and receiver disconnection 2024-02-26 13:53:35 -05:00
Pavel Grigorenko ff187a92d8 library: use addr_of! 2024-02-24 16:02:17 +03:00
Ibraheem Ahmed fb31a1ac21 avoid tls access while iterating through mpsc thread entries 2023-07-19 11:50:29 -04:00
chenx97 d3727148a0 support for mips32r6 as a target_arch value 2023-07-18 18:58:18 +08:00
chenx97 c6e03cd951 support for mips64r6 as a target_arch value 2023-07-18 18:58:18 +08:00
Gil Shoshan af5de855a3 Remove unnecessary Send bound 2023-05-03 15:47:11 +03:00
Petros Angelatos f0d487dce5 sync::mpsc: synchronize receiver disconnect with initialization
Receiver disconnection relies on the incorrect assumption that
`head.index != tail.index` implies that the channel is initialized (i.e
`head.block` and `tail.block` point to allocated blocks). However, it
can happen that `head.index != tail.index` and `head.block == null` at
the same time which leads to a segfault when a channel is dropped in
that state.

This can happen because initialization is performed in two steps. First,
the tail block is allocated and the `tail.block` is set. If that is
successful `head.block` is set to the same pointer. Importantly,
initialization is skipped if `tail.block` is not null.

Therefore we can have the following situation:

1. Thread A starts to send the first value of the channel, observes that
   `tail.block` is null and begins initialization. It sets `tail.block`
   to point to a newly allocated block and then gets preempted.
   `head.block` is still null at this point.
2. Thread B starts to send the second value of the channel, observes
   that `tail.block` *is not* null and proceeds with writing its value
   in the allocated tail block and sets `tail.index` to 1.
3. Thread B drops the receiver of the channel which observes that
   `head.index != tail.index` (0 and 1 respectively), therefore there
   must be messages to drop. It starts traversing the linked list from
   `head.block` which is still a null pointer, leading to a segfault.

This PR fixes this problem by waiting for initialization to complete
when `head.index != tail.index` and the `head.block` is still null. A
similar check exists in `start_recv` for similar reasons.

Fixes #110001

Signed-off-by: Petros Angelatos <petrosagg@gmail.com>
2023-04-08 19:57:22 +02:00
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
joboet 746331edf3 std: drop all messages in bounded channel when destroying the last receiver 2023-02-17 15:47:58 +01:00
Ibraheem Ahmed ab20f8d5ba remove optimistic spinning from mpsc::SyncSender 2023-01-14 00:36:51 -05: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
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
Rageking8 58110572fb fix dupe word typos 2022-12-05 16:42:36 +08:00
Jonas Spinner 70bba3b62a rustdoc: Fix backoff doc to match implementation 2022-11-22 11:06:57 +01:00
Ibraheem Ahmed a22426916d avoid calling thread::current in channel destructor 2022-11-12 23:13:58 -05: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 a43da5a097 initial port of crossbeam-channel 2022-11-09 23:18:06 -05:00