Skip to content

Conversation

@connortsui20
Copy link
Contributor

@connortsui20 connortsui20 commented Oct 31, 2025

Feature: #![feature(nonpoison_condvar)]
Tracking Issue: #134645
Specific comment: #134645 (comment)

Changes the nonpoison::Condvar API to take MutexGuard by reference instead of ownership.

I'm actually not entirely sure why the current poison variant of Condvar takes by ownership, is it just legacy reasons?

Additionally, the nonpoison_and_poison_unwrap_test macro doesn't really make sense anymore now that the APIs are completely different, so this reverts that change from a few months ago.

r? @Amanieu

Setup for writing different tests for the `nonpoison::Condvar` since it
will have a different API.

Signed-off-by: Connor Tsui <connor.tsui20@gmail.com>
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 31, 2025
@connortsui20 connortsui20 changed the title Guard ref condvar nonpoison::Condvar should take MutexGuard by reference Oct 31, 2025
@Amanieu
Copy link
Member

Amanieu commented Oct 31, 2025

I'm actually not entirely sure why the current poison variant of Condvar takes by ownership, is it just legacy reasons?

The poisoning version had to return a LockResult<MutexGuard> because the mutex may have gotten poisoned while it was unlocked. However with non-poisoning mutexes you are guaranteed to always have a valid MutexGuard after waiting, so we can provide a more flexible API by taking the guard by reference instead.

Since non-poisoning `Condvar` take non-poisoing `Mutex`es when
`wait`ing, we do not need to take by ownership since a poison error
cannot occur while we wait.

Signed-off-by: Connor Tsui <connor.tsui20@gmail.com>
Signed-off-by: Connor Tsui <connor.tsui20@gmail.com>
@connortsui20
Copy link
Contributor Author

Ah I was thinking theoretically it could also have just returned a LockResult<()> with the poison flag turned on inside the mutex guard, but I guess returning ownership makes sense.

@Amanieu
Copy link
Member

Amanieu commented Oct 31, 2025

There is no poison flag in the MutexGuard. Instead, the poison flag in the Mutex is what prevents you from getting a MutexGuard in the first place.

@connortsui20
Copy link
Contributor Author

Thanks for the quick review! I just changed a few other things on top of the comments (use deref_mut() instead of the reborrow and a while loop instead of the loop since I think that is more readable in a function called wait_while (and as far as I can tell that doesn't change performance)

@connortsui20 connortsui20 requested a review from Amanieu October 31, 2025 19:58
@Amanieu
Copy link
Member

Amanieu commented Oct 31, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Oct 31, 2025

📌 Commit c1153b0 has been approved by Amanieu

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 31, 2025
@Amanieu
Copy link
Member

Amanieu commented Oct 31, 2025

Make sure to also update the signatures in the tracking issue.

Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 1, 2025
…Amanieu

`nonpoison::Condvar` should take `MutexGuard` by reference

Feature: `#![feature(nonpoison_condvar)]`
Tracking Issue: rust-lang#134645
Specific comment: rust-lang#134645 (comment)

Changes the `nonpoison::Condvar` API to take `MutexGuard` by reference instead of ownership.

I'm actually not entirely sure why the current poison variant of `Condvar` takes by ownership, is it just legacy reasons?

Additionally, the `nonpoison_and_poison_unwrap_test` macro doesn't really make sense anymore now that the APIs are completely different, so this reverts that change from a few months ago.

r? `@Amanieu`
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 1, 2025
…Amanieu

`nonpoison::Condvar` should take `MutexGuard` by reference

Feature: `#![feature(nonpoison_condvar)]`
Tracking Issue: rust-lang#134645
Specific comment: rust-lang#134645 (comment)

Changes the `nonpoison::Condvar` API to take `MutexGuard` by reference instead of ownership.

I'm actually not entirely sure why the current poison variant of `Condvar` takes by ownership, is it just legacy reasons?

Additionally, the `nonpoison_and_poison_unwrap_test` macro doesn't really make sense anymore now that the APIs are completely different, so this reverts that change from a few months ago.

r? ``@Amanieu``
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 1, 2025
…Amanieu

`nonpoison::Condvar` should take `MutexGuard` by reference

Feature: `#![feature(nonpoison_condvar)]`
Tracking Issue: rust-lang#134645
Specific comment: rust-lang#134645 (comment)

Changes the `nonpoison::Condvar` API to take `MutexGuard` by reference instead of ownership.

I'm actually not entirely sure why the current poison variant of `Condvar` takes by ownership, is it just legacy reasons?

Additionally, the `nonpoison_and_poison_unwrap_test` macro doesn't really make sense anymore now that the APIs are completely different, so this reverts that change from a few months ago.

r? ```@Amanieu```
bors added a commit that referenced this pull request Nov 1, 2025
Rollup of 10 pull requests

Successful merges:

 - #135602 (Tweak output of missing lifetime on associated type)
 - #139751 (Implement pin-project in pattern matching for `&pin mut|const T`)
 - #142682 (Update bundled musl to 1.2.5)
 - #148171 (Simplify code to generate line numbers in highlight)
 - #148263 (Unpin `libc` and `rustix` in `compiler` and `rustbook`)
 - #148301 ([rustdoc search] Include extern crates when filtering on `import`)
 - #148330 (Don't require dlltool with the dummy backend on MinGW)
 - #148338 (cleanup: upstream dropped amx-transpose functionality)
 - #148340 (Clippy subtree update)
 - #148343 (`nonpoison::Condvar` should take `MutexGuard` by reference)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Nov 1, 2025
Rollup of 10 pull requests

Successful merges:

 - #135602 (Tweak output of missing lifetime on associated type)
 - #139751 (Implement pin-project in pattern matching for `&pin mut|const T`)
 - #142682 (Update bundled musl to 1.2.5)
 - #148171 (Simplify code to generate line numbers in highlight)
 - #148263 (Unpin `libc` and `rustix` in `compiler` and `rustbook`)
 - #148301 ([rustdoc search] Include extern crates when filtering on `import`)
 - #148330 (Don't require dlltool with the dummy backend on MinGW)
 - #148338 (cleanup: upstream dropped amx-transpose functionality)
 - #148340 (Clippy subtree update)
 - #148343 (`nonpoison::Condvar` should take `MutexGuard` by reference)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 0ed8002 into rust-lang:master Nov 1, 2025
11 checks passed
@rustbot rustbot added this to the 1.93.0 milestone Nov 1, 2025
rust-timer added a commit that referenced this pull request Nov 1, 2025
Rollup merge of #148343 - connortsui20:guard-ref-condvar, r=Amanieu

`nonpoison::Condvar` should take `MutexGuard` by reference

Feature: `#![feature(nonpoison_condvar)]`
Tracking Issue: #134645
Specific comment: #134645 (comment)

Changes the `nonpoison::Condvar` API to take `MutexGuard` by reference instead of ownership.

I'm actually not entirely sure why the current poison variant of `Condvar` takes by ownership, is it just legacy reasons?

Additionally, the `nonpoison_and_poison_unwrap_test` macro doesn't really make sense anymore now that the APIs are completely different, so this reverts that change from a few months ago.

r? ````@Amanieu````
@connortsui20 connortsui20 deleted the guard-ref-condvar branch November 1, 2025 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants