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

std: use sync::Mutex for internal statics #100579

Merged
merged 1 commit into from
Oct 16, 2022

Conversation

joboet
Copy link
Member

@joboet joboet commented Aug 15, 2022

Since sync::Mutex is now const-constructible, it can be used for internal statics, removing the need for sys_common::StaticMutex. This adds some extra allocations on platforms which need to box their mutexes (currently SGX and some UNIX), but these will become unnecessary with the lock improvements tracked in #93740.

I changed the program argument implementation on Hermit, it does not need Mutex but can use atomics like some UNIX systems (ping @mkroening @stlankes).

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Aug 15, 2022
@rustbot
Copy link
Collaborator

rustbot commented Aug 15, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive
Copy link
Collaborator

r? @m-ou-se

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 15, 2022
library/std/src/backtrace.rs Outdated Show resolved Hide resolved
library/std/src/backtrace.rs Outdated Show resolved Hide resolved
@joshtriplett
Copy link
Member

LGTM

@mkroening
Copy link
Contributor

The hermit changes look good to me. 👍

@stlankes
Copy link
Contributor

Looks also good for me!

@thomcc
Copy link
Member

thomcc commented Sep 18, 2022

As mentioned in #100581 I'm not a fan of this. I care more about this on Darwin than on other platforms, and a bit more about this for StaticMutex than StaticRwlock.

I also would eventually like to move StaticMutex on Darwin to os_unfair_lock which is much more efficient (this is blocked by rust-lang/compiler-team#556). This change would make that harder.

@m-ou-se
Copy link
Member

m-ou-se commented Sep 18, 2022

I also would eventually like to move StaticMutex on Darwin to os_unfair_lock which is much more efficient (this is blocked by rust-lang/compiler-team#556).

I agree that that'd be good.

If we decouple the mutex and condition variable on macOS, we could possibly also make the standard Mutex a os_unfair_lock, which would be compatible with this PR. (Not sure yet if that's a good idea though, but it might be.)

@thomcc
Copy link
Member

thomcc commented Sep 18, 2022

I guess it wouldn't be the end of the world. It looks like on Darwin the only time we actually would be taking a StaticMutex is when producing a backtrace and spawning processes (it has 64 bit atomics, and doesnt require synchronized create for TLS). These aren't so performance sensitive that an additional allocation will matter.

@thomcc
Copy link
Member

thomcc commented Sep 18, 2022

r? @thomcc

@rust-highfive rust-highfive assigned thomcc and unassigned m-ou-se Sep 18, 2022
@thomcc
Copy link
Member

thomcc commented Sep 18, 2022

This looks fine. This takes locks in the same cases as before (well, strictly fewer, actually -- hermit will use atomics with this PR), so there should be no concerns about additional deadlocks here (mentioned by @m-ou-se on Zulip). No rollup for similar reasons to the rwlock changes.

@bors r+ rollup=never

@bors
Copy link
Contributor

bors commented Sep 18, 2022

📌 Commit dce2d6a65d5bf154003eb95f1cc152b46097921c has been approved by thomcc

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 Sep 18, 2022
@joboet
Copy link
Member Author

joboet commented Sep 18, 2022

I also would eventually like to move StaticMutex on Darwin to os_unfair_lock which is much more efficient (this is blocked by rust-lang/compiler-team#556).

I agree that that'd be good.

If we decouple the mutex and condition variable on macOS, we could possibly also make the standard Mutex a os_unfair_lock, which would be compatible with this PR. (Not sure yet if that's a good idea though, but it might be.)

That would be great, but it probably requires Apple to promise that the os_unfair_lock is movable when unlocked.

@m-ou-se
Copy link
Member

m-ou-se commented Sep 18, 2022

That would be great, but it probably requires Apple to promise that the os_unfair_lock is movable when unlocked.

os_unfair_lock doesn't have constructor nor a destructor function, which is nearly enough to guarantee it's movable: moving a freshly initialized os_unfair_lock is equivalent to just destructing it in place and creating a new one in a new location. I guess the only thing that's technically missing is the guarantee that this still works after locking and unlocking. But without a destructor, it's safe to assume there's no external pointers pointing into it. And with its tiny size (1 pointer), internal pointers or alignment-dependent operation also seem very unlikely. (But yes, official confirmation from Apple would be nice. ^^)

@bors
Copy link
Contributor

bors commented Sep 18, 2022

⌛ Testing commit dce2d6a65d5bf154003eb95f1cc152b46097921c with merge fc4d094db22a53d020f3486c334dd9bee77b503a...

@thomcc
Copy link
Member

thomcc commented Sep 18, 2022

It's hard to get Apple to update their documentation, so I wouldn't hold my breath there honestly. If we want to block using it on that, I could file an issue with them to ask though (I have a dev account and don't mind). That said, I agree it's unclear on whether or not it's worth using for Mutex, given the trickiness it adds to condvars (it's hard for me to imagine we'd have an impl that doesn't penalize condvar-heavy code, for example).

I agree that it's hard to imagine this being immovable in any way, especially given that it's intended as a replacement for OSSpinLock.

@joboet
Copy link
Member Author

joboet commented Sep 18, 2022

I agree it's unclear on whether or not it's worth using for Mutex, given the trickiness it adds to condvars (it's hard for me to imagine we'd have an impl that doesn't penalize condvar-heavy code, for example).

If the condvar implementation is changed to a parking-lot or atomic queue (see usync), it does not matter what the underlying mutex is, so this is definitely worth doing once condvars have been optimized.

@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Oct 13, 2022
@bors
Copy link
Contributor

bors commented Oct 13, 2022

⌛ Testing commit 2d2c9e4 with merge 3993d24c2658c1b2e4a9e0600bf6cbec13a246f1...

@bors
Copy link
Contributor

bors commented Oct 13, 2022

💥 Test timed out

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 13, 2022
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

albertlarsan68 added a commit to albertlarsan68/rust that referenced this pull request Oct 14, 2022
…omcc

std: use `sync::Mutex` for internal statics

Since `sync::Mutex` is now `const`-constructible, it can be used for internal statics, removing the need for `sys_common::StaticMutex`. This adds some extra allocations on platforms which need to box their mutexes (currently SGX and some UNIX), but these will become unnecessary with the lock improvements tracked in rust-lang#93740.

I changed the program argument implementation on Hermit, it does not need `Mutex` but can use atomics like some UNIX systems (ping `@mkroening` `@stlankes).`
@joboet
Copy link
Member Author

joboet commented Oct 15, 2022

This seems like a spurious failure, as the tests passed before and nothing was changed for macOS since.

@thomcc
Copy link
Member

thomcc commented Oct 15, 2022

@bors delegate+

@bors
Copy link
Contributor

bors commented Oct 15, 2022

✌️ @joboet can now approve this pull request

@joboet
Copy link
Member Author

joboet commented Oct 15, 2022

@bors r+ rollup=iffy

@bors
Copy link
Contributor

bors commented Oct 15, 2022

💡 This pull request was already approved, no need to approve it again.

  • This pull request previously failed. You should add more commits to fix the bug, or use retry to trigger a build again.

@bors
Copy link
Contributor

bors commented Oct 15, 2022

📌 Commit 2d2c9e4 has been approved by joboet

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 15, 2022
@joboet
Copy link
Member Author

joboet commented Oct 15, 2022

@bors r- r=thomcc

@bors
Copy link
Contributor

bors commented Oct 15, 2022

📌 Commit 2d2c9e4 has been approved by thomcc

It is now in the queue for this repository.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 15, 2022
@bors
Copy link
Contributor

bors commented Oct 15, 2022

⌛ Testing commit 2d2c9e4 with merge ddc7fd9...

@bors
Copy link
Contributor

bors commented Oct 16, 2022

☀️ Test successful - checks-actions
Approved by: thomcc
Pushing ddc7fd9 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Oct 16, 2022
@bors bors merged commit ddc7fd9 into rust-lang:master Oct 16, 2022
@rustbot rustbot added this to the 1.66.0 milestone Oct 16, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (ddc7fd9): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean1 range count2
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-5.6% [-5.6%, -5.6%] 1
Improvements ✅
(secondary)
-2.3% [-2.8%, -1.7%] 4
All ❌✅ (primary) -5.6% [-5.6%, -5.6%] 1

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean1 range count2
Regressions ❌
(primary)
2.9% [2.4%, 3.5%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.3% [-1.3%, -1.3%] 1
Improvements ✅
(secondary)
-2.1% [-2.1%, -2.1%] 1
All ❌✅ (primary) 1.5% [-1.3%, 3.5%] 3

Footnotes

  1. the arithmetic mean of the percent change 2

  2. number of relevant changes 2

@joboet joboet deleted the sync_mutex_everywhere branch October 16, 2022 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.