Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazily allocate and initialize pthread locks. #97647

Merged
merged 2 commits into from
Jun 4, 2022

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Jun 2, 2022

Lazily allocate and initialize pthread locks.

This allows {Mutex, Condvar, RwLock}::new() to be const, while still using the platform's native locks for features like priority inheritance and debug tooling. E.g. on macOS, we cannot directly use the (private) APIs that pthread's locks are implemented with, making it impossible for us to use anything other than pthread while still preserving priority inheritance, etc.

This PR doesn't yet make the public APIs const. That's for a separate PR with an FCP.

Tracking issue: #93740

@m-ou-se m-ou-se added A-concurrency Area: Concurrency T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 2, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 2, 2022
@m-ou-se m-ou-se force-pushed the lazy-box-locks branch 9 times, most recently from 72b67ce to 6d2c27d Compare June 2, 2022 11:40
_phantom: PhantomData<T>,
}

pub(crate) unsafe trait LazyInit {
Copy link
Member Author

@m-ou-se m-ou-se Jun 2, 2022

Choose a reason for hiding this comment

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

I had to make this trait pub(crate) instead of pub, otherwise rustc will start suggesting to implement std::sys_common::lazy_box::LazyInit every time a user tries to call a non-existent new function. And due to it being a bound on LazyBox, the change from pub to pub(crate) went a bit viral through std::sys.

See #95080

library/std/src/sys/sgx/condvar.rs Outdated Show resolved Hide resolved
unsafe fn init(&mut self);
/// This is called before deallocating the box, with the same address for
/// `self` as used in `init()`.
unsafe fn destroy(&mut self);
Copy link
Member

Choose a reason for hiding this comment

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

Why is destroy still needed? Can't we just use the Drop impl?

Copy link
Member Author

@m-ou-se m-ou-se Jun 3, 2022

Choose a reason for hiding this comment

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

Why is destroy still needed? Can't we just use the Drop impl?

That's a leftover from the very weird interface of sys's Mutex: init() and destroy() are "optional".

Since #77147, however, the only cases where destroy() is not called are for statics, which don't get dropped anyway. So moving this to a Drop impl should work now.

library/std/src/sys_common/lazy_box.rs Outdated Show resolved Hide resolved
library/std/src/sys_common/lazy_box.rs Outdated Show resolved Hide resolved
library/std/src/sys_common/lazy_box.rs Outdated Show resolved Hide resolved
@nbdd0121
Copy link
Contributor

nbdd0121 commented Jun 2, 2022

How about just making LazyInit have a single function fn new() -> Pin<Box<Self>>?

@m-ou-se m-ou-se 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 Jun 2, 2022
@Amanieu
Copy link
Member

Amanieu commented Jun 2, 2022

How about just making LazyInit have a single function fn new() -> Pin<Box<Self>>?

At that point, why both with a trait? Just have LazyBox::new take an impl FnMut() -> Pin<Box<Self>>.

@m-ou-se
Copy link
Member Author

m-ou-se commented Jun 3, 2022

At that point, why both with a trait? Just have LazyBox::new take an impl FnMut() -> Pin<Box<Self>>.

Because that new function is called as MovableMutex::new(), and MovableMutex is either an alias for sys::locks::Mutex, or for LazyBox<sys::locks::Mutex>, depending on the platform. So calling <_>::new() has to work on both.

@Amanieu
Copy link
Member

Amanieu commented Jun 3, 2022

@bors r+

Looking forward to finally making Mutex::new const!

@bors
Copy link
Contributor

bors commented Jun 3, 2022

📌 Commit 6a417d4 has been approved by Amanieu

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 3, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Jun 4, 2022
Lazily allocate and initialize pthread locks.

Lazily allocate and initialize pthread locks.

This allows {Mutex, Condvar, RwLock}::new() to be const, while still using the platform's native locks for features like priority inheritance and debug tooling. E.g. on macOS, we cannot directly use the (private) APIs that pthread's locks are implemented with, making it impossible for us to use anything other than pthread while still preserving priority inheritance, etc.

This PR doesn't yet make the public APIs const. That's for a separate PR with an FCP.

Tracking issue: rust-lang#93740
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 4, 2022
Rollup of 5 pull requests

Successful merges:

 - rust-lang#96642 (Avoid zero-sized allocs in ThinBox if T and H are both ZSTs.)
 - rust-lang#97647 (Lazily allocate and initialize pthread locks.)
 - rust-lang#97715 (Support the `#[expect]` attribute on fn parameters (RFC-2383))
 - rust-lang#97716 (Fix reachability analysis for const methods)
 - rust-lang#97722 (Tighten spans for bad fields in struct deriving `Copy`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit e9ec022 into rust-lang:master Jun 4, 2022
@rustbot rustbot added this to the 1.63.0 milestone Jun 4, 2022
@m-ou-se m-ou-se deleted the lazy-box-locks branch June 4, 2022 14:26
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 19, 2022
Make {Mutex, Condvar, RwLock}::new() const.

This makes it possible to have `static M: Mutex<_> = Mutex::new(..);` 🎉

Our implementations [on Linux](rust-lang#95035), [on Windows](rust-lang#77380), and various BSDs and some tier 3 platforms have already been using a non-allocating const-constructible implementation. As of rust-lang#97647, the remaining platforms (most notably macOS) now have a const-constructible implementation as well. This means we can finally make these functions publicly const.

Tracking issue: rust-lang#93740
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Jun 28, 2022
Hermit: Fix initializing lazy locks

Closes hermit-os/hermit-rs#322.

The initialization function of hermit's `Condvar` is not called since rust-lang#97647 and was erroneously removed in rust-lang#97879.

r? `@m-ou-se`

CC: `@stlankes`
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Jun 28, 2022
Hermit: Fix initializing lazy locks

Closes hermit-os/hermit-rs#322.

The initialization function of hermit's `Condvar` is not called since rust-lang#97647 and was erroneously removed in rust-lang#97879.

r? ``@m-ou-se``

CC: ``@stlankes``
workingjubilee pushed a commit to tcdi/postgrestd that referenced this pull request Sep 15, 2022
Make {Mutex, Condvar, RwLock}::new() const.

This makes it possible to have `static M: Mutex<_> = Mutex::new(..);` 🎉

Our implementations [on Linux](rust-lang/rust#95035), [on Windows](rust-lang/rust#77380), and various BSDs and some tier 3 platforms have already been using a non-allocating const-constructible implementation. As of rust-lang/rust#97647, the remaining platforms (most notably macOS) now have a const-constructible implementation as well. This means we can finally make these functions publicly const.

Tracking issue: rust-lang/rust#93740
workingjubilee pushed a commit to tcdi/postgrestd that referenced this pull request Sep 15, 2022
Hermit: Fix initializing lazy locks

Closes hermit-os/hermit-rs#322.

The initialization function of hermit's `Condvar` is not called since rust-lang/rust#97647 and was erroneously removed in rust-lang/rust#97879.

r? ``@m-ou-se``

CC: ``@stlankes``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-concurrency Area: Concurrency 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.

8 participants