This is an archive of the discontinued LLVM Phabricator instance.

libcxx: Define __STDCPP_THREADS__ to 1, not to __cplusplus.
ClosedPublic

Authored by thakis on Jul 27 2019, 10:24 AM.

Details

Reviewers
EricWF
Summary

[cpp.predefined]p2:

__STDCPP_THREADS__
 Defined, and has the value integer literal 1, if and only if a program
 can have more than one thread of execution .

Also define it only if it's not defined already, since it's supposed
to be defined by the compiler.

Part of PR33230.

Diff Detail

Event Timeline

thakis created this revision.Jul 27 2019, 10:24 AM
EricWF added inline comments.Jul 28 2019, 9:18 AM
libcxx/include/thread
17

This doesn't actually appear in the header synopsis, since it's supposed to be predefined by the compiler.
And if it's supposed to be predefined, I think __config may be a more appropriate place for it to live.

110

This should be defined by the compiler, not the library. So we should only define it if it hasn't already been defined.

libcxx/test/std/thread/macro.pass.cpp
21

Can you add a test that it's defined to 1 specifically?

This really should be defined by the compiler.

thakis updated this revision to Diff 212148.Jul 29 2019, 4:50 AM
thakis marked an inline comment as done.
thakis edited the summary of this revision. (Show Details)

remove from synopsis, define conditional, add test for value

Did most things, except that I kept it in thread for now. PTAL.

libcxx/include/thread
110

Currently it's only defined if thread is included; in __config it'd always be defined. Is the latter what we want? If the compiler doesn't define it, if <thread> is included seems like an ok signal.

EricWF accepted this revision.Jul 30 2019, 6:30 AM

LGTM after moving the define to <__config>.

libcxx/include/thread
110

What if you want to use __STDCPP_THREADS__ to determine if you can include thread?
Defining it in <__config> best matches the correct behavior of having the compiler predefine it.

This revision is now accepted and ready to land.Jul 30 2019, 6:30 AM

I started moving this into __config, but that raises several questions:

  1. We probably only want to set it there if _LIBCPP_HAS_NO_THREADS is not defined:
#if !defined(_LIBCPP_HAS_NO_THREADS) && !defined(__STDCPP_THREADS__)
#define __STDCPP_THREADS__ 1
#endif
  1. If the compiler does set __STDCPP_THREADS__ but _LIBCPP_HAS_NO_THREADS is set, that should probably be an error:
#if defined(__STDCPP_THREADS__) && defined(_LIBCPP_HAS_NO_THREADS)
#error _LIBCPP_HAS_NO_THREADS cannot be set when __STDCPP_THREADS__ is.
#endif

But now if a compiler does set __STDCPP_THREADS__ it's kind of out of libc++'s control. Once the clang half is implemented, maybe there's going to be some flag to cause it to not set that define and we could mention that here – but then again putting compiler-specific diags in libc++ is a bit strange as well.

So I guess I'll land this with those two tweaks and then maybe adjust when I move the define into clang.

thakis closed this revision.Jul 30 2019, 4:21 PM

Landed in r367316.