boostorg / thread

Boost.org thread module
http://boost.org/libs/thread
199 stars 161 forks source link

Android < 21 must not use pthread_condattr_setclock #215

Closed gjasny closed 6 years ago

gjasny commented 6 years ago

Hello,

the pthread_condattr_setclock function is only available for __ANDROID_API__ >= 21. The unconditional use in pthread_helpers.hpp leads to an compile error.

What would be the proper way to handle it? Should the preprocessor condition in boost::pthread::cond_init be tightened or BOOST_THREAD_INTERNAL_CLOCK_IS_MONO not be defined on Android < 21?

Would the following patch make sense?

diff --git a/boost/thread/detail/config.hpp b/boost/thread/detail/config.hpp
index 70c74687f6..aae25268d3 100644
--- a/boost/thread/detail/config.hpp
+++ b/boost/thread/detail/config.hpp
@@ -417,6 +417,11 @@
   #define BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
 #elif defined(BOOST_THREAD_CHRONO_MAC_API)
   #define BOOST_THREAD_HAS_MONO_CLOCK
+#elif defined(__ANDROID__)
+  #define BOOST_THREAD_HAS_MONO_CLOCK
+  #if defined(__ANDROID_API__) && __ANDROID_API__ >= 21
+    #define BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
+  #endif
 #else
   #include <time.h> // check for CLOCK_MONOTONIC
   #if defined(CLOCK_MONOTONIC)

Thanks, Gregor

viboes commented 6 years ago

Hi,

for completion, could you add the compiler error?

Yes, please, could you provide a PR?

gjasny commented 6 years ago

Compile error:

build   11-Apr-2018 07:46:57    clang-linux.compile.c++.without-pth /home/bamboo/bamboo-agent-home/xml-data/build-dir/EL-BOOST7-AC/_output/Applications_Android_armeabi-v7a_cxx14/boost/bin.v2/libs/type_erasure/build/clang-linux-android/debug/link-static/pch-off/target-os-android/threadapi-pthread/threading-multi/dynamic_binding.o
build   11-Apr-2018 07:46:57    In file included from libs/type_erasure/src/dynamic_binding.cpp:14:
build   11-Apr-2018 07:46:57    In file included from ./boost/thread/shared_mutex.hpp:28:
build   11-Apr-2018 07:46:57    In file included from ./boost/thread/pthread/shared_mutex.hpp:14:
build   11-Apr-2018 07:46:57    In file included from ./boost/thread/mutex.hpp:16:
build   11-Apr-2018 07:46:57    In file included from ./boost/thread/pthread/mutex.hpp:26:
build   11-Apr-2018 07:46:57    ./boost/thread/pthread/pthread_helpers.hpp:28:15: error: use of undeclared identifier 'pthread_condattr_setclock'; did you mean 'pthread_condattr_setpshared'?
build   11-Apr-2018 07:46:57                  pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
build   11-Apr-2018 07:46:57                  ^~~~~~~~~~~~~~~~~~~~~~~~~
build   11-Apr-2018 07:46:57                  pthread_condattr_setpshared
build   11-Apr-2018 07:46:57    /home/bamboo/bamboo-agent-home/xml-data/build-dir/EL-BOOST7-AC/_toolchain_arm_android-14/bin/../sysroot/usr/include/pthread.h:122:5: note: 'pthread_condattr_setpshared' declared here
build   11-Apr-2018 07:46:57    int pthread_condattr_setpshared(pthread_condattr_t* __attr, int __shared);
build   11-Apr-2018 07:46:57        ^
build   11-Apr-2018 07:46:57    1 error generated.

In pthread.h:

#if __ANDROID_API__ >= 21
int pthread_condattr_setclock(pthread_condattr_t* __attr, clockid_t __clock) __INTRODUCED_IN(21);
#endif /* __ANDROID_API__ >= 21 */
huowa222 commented 5 years ago

in chromium build, there is a build config for android called config.gni try to get it and refresh the value of android api level.