[PATCH] [libc++] Fix _SC_NPROCESSORS_ONLN detection

Matthew Dempsky matthew at dempsky.org
Fri Jun 7 19:48:58 PDT 2013


POSIX defines that the _POSIX_C_SOURCE macros are to be set by user
code to specify what version of POSIX the system should provide.  If
you want to check what version of POSIX is actually available, you're
supposed to test _POSIX_VERSION.

However, since sysconf() has been in POSIX since 1995, it's probably
safe to assume it's available on any system with a C++11 compiler,
especially if _SC_NPROCESSORS_ONLN is defined too.  So no point in a
complicated preprocessor rule if just we unconditionally include
<unistd.h> (on non-Windows systems).

Also, I've added a #warning for to help porters detect when a suitable
implementation isn't detected at compile-time.


Index: src/thread.cpp
===================================================================
--- src/thread.cpp	(revision 183583)
+++ src/thread.cpp	(working copy)
@@ -16,9 +16,8 @@
 #if !defined(_WIN32)
 #if !defined(__sun__) && !defined(__linux__)
 #include <sys/sysctl.h>
-#else
+#endif // !__sun__ && !__linux__
 #include <unistd.h>
-#endif // !__sun__ && !__linux__
 #endif // !_WIN32
 
 #if defined(__NetBSD__)
@@ -71,7 +70,7 @@ thread::hardware_concurrency() _NOEXCEPT
     std::size_t s = sizeof(n);
     sysctl(mib, 2, &n, &s, 0, 0);
     return n;
-#elif (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && defined(_SC_NPROCESSORS_ONLN)) || defined(EMSCRIPTEN)
+#elif defined(_SC_NPROCESSORS_ONLN)
     long result = sysconf(_SC_NPROCESSORS_ONLN);
     // sysconf returns -1 if the name is invalid, the option does not exist or
     // does not have a definite limit.
@@ -83,6 +82,7 @@ thread::hardware_concurrency() _NOEXCEPT
 #else  // defined(CTL_HW) && defined(HW_NCPU)
     // TODO: grovel through /proc or check cpuid on x86 and similar
     // instructions on other architectures.
+    #warning hardware_concurrency not yet implemented
     return 0;  // Means not computable [thread.thread.static]
 #endif  // defined(CTL_HW) && defined(HW_NCPU)
 }



More information about the cfe-commits mailing list