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

Replace ReentrantMutex by a futex-based one on Linux. #95727

Merged
merged 9 commits into from
Apr 13, 2022

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Apr 6, 2022

Tracking issue: #93740

r? @Amanieu

@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 Apr 6, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 6, 2022
@rust-log-analyzer

This comment has been minimized.

@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 6, 2022

test/ui/runtime/stdout-during-shutdown/stdout-during-shutdown.rs

Hm, who thought it was a good idea to test that we allow std to be used in an atexit() handler?

*runs git blame*

Oh. ^^'

@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 Apr 6, 2022
/// The 'owner' field tracks which thread has locked the mutex.
///
/// We use current_thread_unique_ptr() as the thread identifier,
/// which is just the address of a thread local variable.
Copy link
Member

Choose a reason for hiding this comment

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

That might not work if a thread doesn't unlock and then exits. Another new thread may then suddenly acquire the lock if the allocator happens to return the same pointer.

Copy link
Member

Choose a reason for hiding this comment

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

There is a ThreadId type you can use instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

That might not work if a thread doesn't unlock and then exits. Another new thread may then suddenly acquire the lock if the allocator happens to return the same pointer.

That should work out fine, as the old thread doesn't exist anymore, it's fine for the new thread to take over ownership of the lock.

There is a ThreadId type you can use instead.

I am aware, but the ThreadId type is 64-bit, which is problematic on some 32-bit platforms.

Copy link
Member

@Amanieu Amanieu left a comment

Choose a reason for hiding this comment

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

Should all these methods be marked as #[inline]?

library/std/src/sys_common/thread_info.rs Outdated Show resolved Hide resolved
library/std/src/sys/unix/locks/futex.rs Show resolved Hide resolved
library/std/src/sys/unix/locks/futex.rs Show resolved Hide resolved
@Amanieu
Copy link
Member

Amanieu commented Apr 6, 2022

Also, this ReentrantMutex implementation is not specific to any OS so we could use it everywhere and remove all the OS-specific ReentrantMutex implementations. I think this would be a good improvement towards simplifying the codebase.

@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 6, 2022

Also, this ReentrantMutex implementation is not specific to any OS so we could use it everywhere and remove all the OS-specific ReentrantMutex implementations. I think this would be a good improvement towards simplifying the codebase.

Yes! After updating everything on Linux, my next step will using these implementations on as many platforms as possible. (So the Mutex and Condvar and RwLock on all platforms that support futexes, and this ReentrantMutex on every platform.)

@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 6, 2022

Should all these methods be marked as #[inline]?

Maybe? They're more complicated than a simple single compare_exchange like in Mutex though. So I guess it depends on how simple the generated code for current_thread_unique_ptr() will be. If that always results in a call, then I guess not.

@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 6, 2022

Should all these methods be marked as #[inline]?

Maybe?

Oh and we only use this type within std, and not expose it anywhere. So rustc/llvm can already decide to inline them in the places we use them, regardless of the attribute.

@bors
Copy link
Contributor

bors commented Apr 7, 2022

☔ The latest upstream changes (presumably #95742) made this pull request unmergeable. Please resolve the merge conflicts.

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

Amanieu commented Apr 8, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Apr 8, 2022

📌 Commit 0664b8a 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-review Status: Awaiting review from the assignee but also interested parties. labels Apr 8, 2022
@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 8, 2022

@bors rollup=never

Just in case.

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Apr 8, 2022
…nieu

Replace ReentrantMutex by a futex-based one on Linux.

Tracking issue: rust-lang#93740

r? `@Amanieu`
@bors
Copy link
Contributor

bors commented Apr 9, 2022

⌛ Testing commit 0664b8a with merge 939bbea31a5449d8e597e9daa1b866d9dbe3183c...

@bors
Copy link
Contributor

bors commented Apr 12, 2022

💔 Test failed - checks-actions

@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 Apr 12, 2022
@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 12, 2022

Hm, src/test/ui/issues/issue-70093.rs is segfaulting on host=x86_64-unknown-linux-gnu target=i686-unknown-linux-musl.

// run-pass
// compile-flags: -Zlink-native-libraries=no -Cdefault-linker-libraries=yes
// ignore-windows - this will probably only work on unixish systems
// ignore-fuchsia - missing __libc_start_main for some reason (#84733)
#[link(name = "some-random-non-existent-library", kind = "static")]
extern "C" {}
fn main() {}

@rust-log-analyzer
Copy link
Collaborator

The job dist-i586-gnu-i586-i686-musl failed! Check out the build log: (web) (plain)

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

---- [ui] src/test/ui/issues/issue-70093.rs stdout ----

error: test run failed!
status: signal: 11 (core dumped)
command: "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/issues/issue-70093/a"
stdout: none
stderr: none


failures:
    [ui] src/test/ui/issues/issue-70093.rs

@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 12, 2022

I can reproduce it on x86_64-unknown-linux-musl.

Program stopped.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x00007ffff7fe2b0c in __register_frame_info ()
#2  0x00007ffff7fe34ad in libc_start_init ()
#3  0x00007ffff7fa8c50 in ?? ()
#4  0x00007ffff7fe34d2 in libc_start_main_stage2 ()
#5  0x00007ffff7fe34b5 in libc_start_init ()
#6  0x0000000000000000 in ?? ()
(gdb) frame 1
#1  0x00007ffff7fe2b0c in __register_frame_info ()
(gdb) disassemble
Dump of assembler code for function __register_frame_info:
[...]
   0x00007ffff7fe2afa <+74>:	test   %rbp,%rbp
   0x00007ffff7fe2afd <+77>:	je     0x7ffff7fe2b0c <__register_frame_info+92>
   0x00007ffff7fe2aff <+79>:	lea    0x1a89a(%rip),%rdi        # 0x7ffff7ffd3a0 <object_mutex>
   0x00007ffff7fe2b06 <+86>:	call   *0x1a3a4(%rip)            # 0x7ffff7ffceb0
=> 0x00007ffff7fe2b0c <+92>:	mov    0x1a8c5(%rip),%rax        # 0x7ffff7ffd3d8 <unseen_objects>
[...]
End of assembler dump.
(gdb) x/g 0x7ffff7ffceb0
0x7ffff7ffceb0:	0x0

@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 12, 2022

That call seems to be this line: https://github.com/gcc-mirror/gcc/blob/95533fe4f014c10dd18de649927668aba6117daf/libgcc/unwind-dw2-fde.c#L103

__gthread_mutex_lock (&object_mutex);

@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 12, 2022

I don't think this test shoud've worked at all when cross compiling *-gnu to *-musl. It's using the *-gnu C compiler as linker for a *-musl target while enabling its default libs. It in turn then passes -lgcc -lgcc_eh -lc to ld, making it link glibc in a musl binary.

@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 12, 2022

cc @jsgf who wrote this test. Should we add // ignore-cross-compile to that test? Or is it expected to always work when cross compiling?

m-ou-se added a commit to m-ou-se/rust that referenced this pull request Apr 12, 2022
…triplett

Don't test -Cdefault-linker-libraries=yes when cross compiling.

See rust-lang#95727 (comment) and the five comments below that.

Unblocks rust-lang#95727.
@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 13, 2022

Disabled the test for cross compiling in #95975

@bors r=Amanieu

@bors
Copy link
Contributor

bors commented Apr 13, 2022

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

@bors
Copy link
Contributor

bors commented Apr 13, 2022

📌 Commit d4e44a6 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-review Status: Awaiting review from the assignee but also interested parties. labels Apr 13, 2022
@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 13, 2022

@bors retry

@bors
Copy link
Contributor

bors commented Apr 13, 2022

⌛ Testing commit d4e44a6 with merge ab33f71...

@bors
Copy link
Contributor

bors commented Apr 13, 2022

☀️ Test successful - checks-actions
Approved by: Amanieu
Pushing ab33f71 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 13, 2022
@bors bors merged commit ab33f71 into rust-lang:master Apr 13, 2022
@rustbot rustbot added this to the 1.62.0 milestone Apr 13, 2022
@m-ou-se m-ou-se deleted the futex-reentrantmutex branch April 13, 2022 16:10
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (ab33f71): comparison url.

Summary:

  • Primary benchmarks: no relevant changes found
  • Secondary benchmarks: mixed results
Regressions 😿
(primary)
Regressions 😿
(secondary)
Improvements 🎉
(primary)
Improvements 🎉
(secondary)
All 😿 🎉
(primary)
count1 0 1 0 2 0
mean2 N/A 0.2% N/A -0.4% N/A
max N/A 0.2% N/A -0.4% N/A

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

@rustbot label: -perf-regression

Footnotes

  1. number of relevant changes

  2. the arithmetic mean of the percent change

/// This can be used as a non-null usize-sized ID.
pub fn current_thread_unique_ptr() -> usize {
// Use a non-drop type to make sure it's still available during thread destruction.
thread_local! { static X: u8 = const { 0 } }
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we avoid creating new thread_local!? Since pthread keys number available on some platforms is rather small, it would be beneficial for std to use as few pthread keys as possible.

Copy link
Member Author

Choose a reason for hiding this comment

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

That'd be nice.

I used the address of the THREAD_INFO thread local in an earlier version, to avoid creating a new one, but that one needs Drop, which makes it inaccessible during thread shutdown. We don't have a good way of getting the address of such a thread local's storage after its destructor ran, since its getter function just returns None after destruction.

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like LOCAL_PANIC_COUNT meets this requirement, but it's a little weird to use it directly. maybe it would be nice to combine the KEYS of map.rs and LOCAL_PANIC_COUNT into one LOCAL_THREAD_STATE .

@jsgf
Copy link
Contributor

jsgf commented Apr 17, 2022

@m-ou-se:

cc @jsgf who wrote this test. Should we add // ignore-cross-compile to that test? Or is it expected to always work when cross compiling?

Yeah, I wouldn't expect that test to necessarily work cross-compiling, so I think it's fine to exclude it. I was always concerned that test was a bit fragile. (Though I don't really see why it would cause a segfault.)

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 18, 2022
Use a single ReentrantMutex implementation on all platforms.

This replaces all platform specific ReentrantMutex implementations by the one I added in rust-lang#95727 for Linux, since that one does not depend on any platform specific details.

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

10 participants