Skip to content

Add value accessor methods to Mutex and RwLock#133406

Merged
bors merged 1 commit intorust-lang:masterfrom
EFanZh:lock-value-accessors
Dec 15, 2024
Merged

Add value accessor methods to Mutex and RwLock#133406
bors merged 1 commit intorust-lang:masterfrom
EFanZh:lock-value-accessors

Conversation

@EFanZh
Copy link
Copy Markdown
Contributor

@EFanZh EFanZh commented Nov 24, 2024

This PR adds get, set and replace methods to the Mutex and RwLock types for quick access to their contained values.

One possible optimization would be to check for poisoning first and return an error immediately, without attempting to acquire the lock. I didn’t implement this because I consider poisoning to be relatively rare, adding this extra check could slow down common use cases.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Nov 24, 2024

r? @Noratrieb

rustbot has assigned @Noratrieb.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 Nov 24, 2024
@EFanZh EFanZh force-pushed the lock-value-accessors branch from 5d2f4af to 2c98df2 Compare November 24, 2024 05:08
@EFanZh EFanZh marked this pull request as ready for review November 24, 2024 06:17
Comment thread library/std/src/sync/mutex.rs
Comment thread library/std/src/sync/mutex/tests.rs
Comment thread library/std/src/sync/rwlock.rs
@Noratrieb Noratrieb added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 24, 2024
@EFanZh EFanZh force-pushed the lock-value-accessors branch from 2c98df2 to b6446a1 Compare November 24, 2024 11:59
@EFanZh
Copy link
Copy Markdown
Contributor Author

EFanZh commented Nov 24, 2024

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 24, 2024
Comment thread library/std/src/sync/mutex.rs
@EFanZh EFanZh force-pushed the lock-value-accessors branch 2 times, most recently from f0d47da to 85fd82b Compare November 30, 2024 11:20
@EFanZh EFanZh force-pushed the lock-value-accessors branch from 85fd82b to 242c6c3 Compare November 30, 2024 13:06
Copy link
Copy Markdown
Member

@Noratrieb Noratrieb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs changes to the lock result are fine, moving the writing about the guard being contained to the methods explicitly probably makes it more clear even without the new use of the result.

@Noratrieb
Copy link
Copy Markdown
Member

@bors r+

@bors
Copy link
Copy Markdown
Collaborator

bors commented Dec 14, 2024

📌 Commit 242c6c3 has been approved by Noratrieb

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 Dec 14, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 15, 2024
Rollup of 7 pull requests

Successful merges:

 - rust-lang#130361 (std::net: Solaris supports `SOCK_CLOEXEC` as well since 11.4.)
 - rust-lang#133406 (Add value accessor methods to `Mutex` and `RwLock`)
 - rust-lang#133633 (don't show the full linker args unless `--verbose` is passed)
 - rust-lang#134285 (Add some convenience helper methods on `hir::Safety`)
 - rust-lang#134310 (Add clarity to the examples of some `Vec` & `VecDeque` methods)
 - rust-lang#134313 (Don't make a def id for `impl_trait_in_bindings`)
 - rust-lang#134315 (A couple of polonius fact generation cleanups)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 6667908 into rust-lang:master Dec 15, 2024
@rustbot rustbot added this to the 1.85.0 milestone Dec 15, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Dec 15, 2024
Rollup merge of rust-lang#133406 - EFanZh:lock-value-accessors, r=Noratrieb

Add value accessor methods to `Mutex` and `RwLock`

- ACP: rust-lang/libs-team#485.
- Tracking issue: rust-lang#133407.

This PR adds `get`, `set` and `replace` methods to the `Mutex` and `RwLock` types for quick access to their contained values.

One possible optimization would be to check for poisoning first and return an error immediately, without attempting to acquire the lock. I didn’t implement this because I consider poisoning to be relatively rare, adding this extra check could slow down common use cases.
@EFanZh EFanZh deleted the lock-value-accessors branch December 15, 2024 14:15
tautschnig added a commit to tautschnig/verify-rust-std that referenced this pull request Jan 17, 2025