-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Conversation
This comment has been minimized.
This comment has been minimized.
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 Oh. ^^' |
/// 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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]
?
Also, this |
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.) |
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 |
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. |
☔ The latest upstream changes (presumably #95742) made this pull request unmergeable. Please resolve the merge conflicts. |
0865b1f
to
0664b8a
Compare
@bors r+ |
📌 Commit 0664b8a has been approved by |
@bors rollup=never Just in case. |
…nieu Replace ReentrantMutex by a futex-based one on Linux. Tracking issue: rust-lang#93740 r? `@Amanieu`
⌛ Testing commit 0664b8a with merge 939bbea31a5449d8e597e9daa1b866d9dbe3183c... |
💔 Test failed - checks-actions |
Hm, src/test/ui/issues/issue-70093.rs is segfaulting on host=x86_64-unknown-linux-gnu target=i686-unknown-linux-musl. rust/src/test/ui/issues/issue-70093.rs Lines 1 to 9 in 4e1927d
|
The job Click to see the possible cause of the failure (guessed by this bot)
|
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 |
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); |
I don't think this test shoud've worked at all when cross compiling *-gnu to *-musl. It's using the |
cc @jsgf who wrote this test. Should we add |
…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.
💡 This pull request was already approved, no need to approve it again.
|
📌 Commit d4e44a6 has been approved by |
@bors retry |
☀️ Test successful - checks-actions |
Finished benchmarking commit (ab33f71): comparison url. Summary:
If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf. @rustbot label: -perf-regression Footnotes |
/// 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 } } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
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.) |
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`
Tracking issue: #93740
r? @Amanieu