Consider dropping mozilla::ThreadLocal and using native thread_local instead
Categories
(Core :: MFBT, defect)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox53 | --- | affected |
People
(Reporter: xidorn, Unassigned)
References
Details
Comment 1•9 years ago
|
||
| Reporter | ||
Comment 2•9 years ago
|
||
Comment 3•9 years ago
|
||
Comment 4•9 years ago
|
||
Comment 5•9 years ago
|
||
Comment 6•9 years ago
|
||
Comment 7•9 years ago
|
||
Updated•9 years ago
|
Comment 9•9 years ago
|
||
Comment 11•9 years ago
|
||
Comment 12•7 years ago
|
||
Comment 13•7 years ago
|
||
Comment 14•7 years ago
|
||
Comment 15•7 years ago
|
||
Comment 16•7 years ago
|
||
Comment 17•6 years ago
|
||
It seems to pass try with at least one instance replace with thread_local:
https://treeherder.mozilla.org/#/jobs?repo=try&revision=d8e0ea47ba9d0dc026c82bb071afc4ebe4d79f4f
Comment 18•6 years ago
|
||
(In reply to Jeff Gilbert [:jgilbert] from comment #17)
It seems to pass try with at least one instance replace with
thread_local:
https://treeherder.mozilla.org/#/jobs?repo=try&revision=d8e0ea47ba9d0dc026c82bb071afc4ebe4d79f4f
I don't see Android builds on that push? Or do Android builds not matter for that particular instance that was replaced?
Comment 19•6 years ago
|
||
You're right, that one missed the Android builds. Triggering those now.
Comment 20•6 years ago
|
||
Android builds, even API16.
Comment 21•6 years ago
|
||
The problem is not whether they build, it's whether they work. And if they do on all the supported versions of Android. And I'm sure we don't even need to go to the latter question.
Comment 22•6 years ago
|
||
Google's current C++ style guide entry sounds promising, though not free of caveats:
https://google.github.io/styleguide/cppguide.html#thread_local
thread_local variables that aren't declared inside a function must be initialized with a true compile-time constant, and this must be enforced by using the ABSL_CONST_INIT attribute. Prefer thread_local over other ways of defining thread-local data.
This thread sounds generally promising that issues are fixed for Chromium as of early 2019:
https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/2msN8k3Xzgs
I'm not sure how to test this, since we don't have e.g. gtests for API16+ builds, seems like.
Comment 23•6 years ago
|
||
It sounds to me that this was an issue fixed by updating the NDK, and that old versions of Android shouldn't cause this to not work, so long as the NDK generally works with them still.
Comment 24•6 years ago
|
||
Except for comment 13...
Comment 25•5 years ago
|
||
(In reply to Mike Hommey [:glandium] from comment #24)
Except for comment 13...
Hm, Bug 1403715 is closed as WONTFIX. What's the status of that? Is the custom linker still used? Is there a bug tracking its removal?
Comment 26•5 years ago
|
||
It is still used. But even without the custom linker, though, only Android versions >= 10 support thread local storage in the system linker. Google removed their version usage dashboard, and I don't have Android Studio handy, but this screenshot from April says more than 90% of devices have a version < 10. But it may or may not be relevant, because the question from comment 13 still applies: does thread_local require TLS on Android or not?
Comment 27•5 years ago
|
||
(In reply to Mike Hommey [:glandium] from comment #26)
because the question from comment 13 still applies: does thread_local require TLS on Android or not?
I am not familiar with the details here, but would that be achieved by adding -femulated-tls (https://reviews.llvm.org/D10522) ?
Comment 28•5 years ago
|
||
Probably.
Comment 29•5 years ago
|
||
Note that we do need to keep ThreadLocal at least for the memory allocator, and that the reason we do probably applies to emulated TLS, so the memory allocator would probably need some #ifdefs adjusted to include/exclude Android.
Comment 30•4 years ago
|
||
Offhand remark: MOZ_THREAD_LOCAL on macOS appears to be quite slow. Moving a thread-local access off a hot path onto that path slowed a microbenchmark down by 33% on macOS; comparable code on Linux actually appears to have sped up. (Bug 1742779 comment 4, still looking into this, including for Windows.)
Comment 31•4 years ago
|
||
MOZ_THREAD_LOCAL on macOS uses thread_local.
Comment 32•4 years ago
|
||
Is there any progress in verifying if we can fix the issues with thread_local on Android?
My app is running out of PTHREAD_KEYS_MAX keys when integrating GeckoView, as GeckoView makes heavy use of pthread_key_create (at least 27-32 keys from what I could tell), while on Android the limit is 128. This, coupled with Unity Engine using up to ~65 on its own too, and the OS itself using 15+ keys before my app runs, means it's bound to be a disaster for any non-trivial apps to integrate.
Locally forcing the thread_local or __thread branch in mfbt/ThreadLocal.h results in GeckoView silently stopping during initialization.
The C++ Portability Guide does mention it's not supported on Android, but isn't clear on why. It works in a simple C++ Android app.
https://firefox-source-docs.mozilla.org/code-quality/coding-style/using_cxx_in_firefox_code.html?highlight=c%2011#notes
"Thread locals: thread_local is not supported on Android."
Updated•4 years ago
|
Comment 33•4 years ago
|
||
It works in a simple C++ Android app.
Does it work on Android 4.1?
Comment 34•4 years ago
|
||
(In reply to Mike Hommey [:glandium] from comment #33)
It works in a simple C++ Android app.
Does it work on Android 4.1?
I didn't test, and not sure how this goes, but I'd say it's super valuable to get it to work optionally in GeckoView so if someone does target only modern-ish Android OSes they can avoid this limitation by changing the flag and compiling GeckoView themselves :)
Updated•3 years ago
|
Comment 35•2 years ago
|
||
It would be cool if we could do this, but I don't think it's actionable by me.
Description
•