Page MenuHomePhabricator

[libcxx] Use C11 thread API on Fuchsia

Authored by phosek on Jul 8 2019, 4:12 PM.



On Fuchsia, pthread API is emulated on top of C11 thread API. Using C11
thread API directly is more efficient.

While this implementation is only used by Fuchsia at the moment, it's
not Fuchsia specific, and could be used by other platforms that use C11
threads rather than pthreads in the future.

Diff Detail

Event Timeline

phosek created this revision.Jul 8 2019, 4:12 PM
jfb added inline comments.Jul 8 2019, 4:30 PM

What does this do? Docs say "Enable extensions for AIX 3, and for Interix."

None of these calls (__libcpp_tls_create, for instance) are templates, or marked as inline candidates.
Should they be in a source file, and baked into the dylib instead of a header file?
(like we do in support/win32/thread_win32.cpp, for example)

EricWF added a comment.Jul 8 2019, 7:52 PM

If this is using the C11 threading API and isn't Fuchsia specific, then would the macro name reflect that?


What happens when threads.h is included before any libc++ headers?
We might have to lift this define into the compiler, like we've had to do with _GNU_SOURCE.

EricWF added inline comments.Jul 8 2019, 7:55 PM

Should this not also check for __has_include(<threads.h>)?

EricWF added inline comments.Jul 8 2019, 7:59 PM

Switching the implementation used by Fuchsia is ABI breaking. Are we 100% certain that all of our Fuchsia users don't expect ABI stability?


Can you point towards the C11 source for mtx_destroy and cnd_destroy?

EricWF added inline comments.Jul 8 2019, 8:24 PM

After switching to the _LIBCPP_HAS_THREAD_API_C11 this check should include _LIBCPP_HAS_MUSL_LIBC, because AFAIK, the trivial destruction property is an implementation detail of MUSL.


After some searching, I'm assuming the libc Fucshia uses is MUSL?

If so, then MTX_INIT is defined as simply {}. Writing that in source is going to be less problematic than ensuring _ALL_SOURCE is always defined.


Just use const here, it has the same effect but it works in C++03.

phosek updated this revision to Diff 208862.Jul 9 2019, 6:33 PM
phosek marked 11 inline comments as done.
phosek added inline comments.

Yes, that's fundamental part of Fuchsia's packaging story.


I've added guard for Fuchsia since this is implementation of details our libc.


I've instead added a check for defined(__Fuchsia__), Fuchsia's libc started as a fork of musl but it has sufficiently diverged at this point that I don't think it's safe to consider it being musl-compatible.

Ping? Anything else you'd like me to change?

@EricWF friendly ping?

EricWF accepted this revision.Aug 30 2019, 11:13 AM

LGTM apart from inline comments.


Could we add a comment explaining why we think using {} is safe here? So we don't have to do the archaeology in the future.


There is a std::call_once, should this be qualified as ::call_once?


This code is duplicated from the pthread version, right? Could we generalize the "convert chrono duration to a timespec like thing", and de-duplicate it?


ts_sec needs a reserved name.

This revision is now accepted and ready to land.Aug 30 2019, 11:13 AM
phosek updated this revision to Diff 232209.Dec 4 2019, 2:14 PM
phosek marked 4 inline comments as done.
phosek edited the summary of this revision. (Show Details)
Herald added a project: Restricted Project. · View Herald TranscriptDec 4 2019, 2:14 PM
phosek updated this revision to Diff 238083.Jan 14 2020, 1:18 PM
This revision was automatically updated to reflect the committed changes.