From b525ec2eb0bb441ac0dcd0a055efcad08fe6957c Mon Sep 17 00:00:00 2001 From: BogDan Vatra Date: Thu, 17 Aug 2017 09:31:19 +0300 Subject: [PATCH] Android: Fix compile with unified headers Unified headers now defines _POSIX_THREAD_SAFE_FUNCTIONS but not all libc functions are available in all Android API versions. Change-Id: I01c94f0b89e7f8aa8575e7bbda28d9fe41a68ff1 Reviewed-by: Thiago Macieira --- src/corelib/global/qglobal.cpp | 74 +++++++++---------- src/corelib/io/qfilesystemengine_unix.cpp | 2 +- src/corelib/io/qfilesystemwatcher_inotify.cpp | 10 ++- src/corelib/thread/qthread_unix.cpp | 2 +- 4 files changed, 48 insertions(+), 40 deletions(-) diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 12e908f09f..a9664e7564 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -3344,7 +3344,11 @@ bool qunsetenv(const char *varName) #endif } -#if defined(Q_OS_UNIX) && !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && (_POSIX_THREAD_SAFE_FUNCTIONS - 0 > 0) +#if defined(Q_OS_ANDROID) && (__ANDROID_API__ < 21) +typedef QThreadStorage AndroidRandomStorage; +Q_GLOBAL_STATIC(AndroidRandomStorage, randomTLS) + +#elif defined(Q_OS_UNIX) && !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && (_POSIX_THREAD_SAFE_FUNCTIONS - 0 > 0) # if defined(Q_OS_INTEGRITY) && defined(__GHS_VERSION_NUMBER) && (__GHS_VERSION_NUMBER < 500) // older versions of INTEGRITY used a long instead of a uint for the seed. @@ -3355,10 +3359,6 @@ typedef uint SeedStorageType; typedef QThreadStorage SeedStorage; Q_GLOBAL_STATIC(SeedStorage, randTLS) // Thread Local Storage for seed value - -#elif defined(Q_OS_ANDROID) -typedef QThreadStorage AndroidRandomStorage; -Q_GLOBAL_STATIC(AndroidRandomStorage, randomTLS) #endif /*! @@ -3378,21 +3378,7 @@ Q_GLOBAL_STATIC(AndroidRandomStorage, randomTLS) */ void qsrand(uint seed) { -#if defined(Q_OS_UNIX) && !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && (_POSIX_THREAD_SAFE_FUNCTIONS - 0 > 0) - SeedStorage *seedStorage = randTLS(); - if (seedStorage) { - SeedStorageType *pseed = seedStorage->localData(); - if (!pseed) - seedStorage->setLocalData(pseed = new SeedStorageType); - *pseed = seed; - } else { - //global static seed storage should always exist, - //except after being deleted by QGlobalStaticDeleter. - //But since it still can be called from destructor of another - //global static object, fallback to srand(seed) - srand(seed); - } -#elif defined(Q_OS_ANDROID) +#if defined(Q_OS_ANDROID) && (__ANDROID_API__ < 21) if (randomTLS->hasLocalData()) { randomTLS->localData().callMethod("setSeed", "(J)V", jlong(seed)); return; @@ -3407,6 +3393,20 @@ void qsrand(uint seed) } randomTLS->setLocalData(random); +#elif defined(Q_OS_UNIX) && !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && (_POSIX_THREAD_SAFE_FUNCTIONS - 0 > 0) + SeedStorage *seedStorage = randTLS(); + if (seedStorage) { + SeedStorageType *pseed = seedStorage->localData(); + if (!pseed) + seedStorage->setLocalData(pseed = new SeedStorageType); + *pseed = seed; + } else { + //global static seed storage should always exist, + //except after being deleted by QGlobalStaticDeleter. + //But since it still can be called from destructor of another + //global static object, fallback to srand(seed) + srand(seed); + } #else // On Windows srand() and rand() already use Thread-Local-Storage // to store the seed between calls @@ -3432,23 +3432,7 @@ void qsrand(uint seed) */ int qrand() { -#if defined(Q_OS_UNIX) && !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && (_POSIX_THREAD_SAFE_FUNCTIONS - 0 > 0) - SeedStorage *seedStorage = randTLS(); - if (seedStorage) { - SeedStorageType *pseed = seedStorage->localData(); - if (!pseed) { - seedStorage->setLocalData(pseed = new SeedStorageType); - *pseed = 1; - } - return rand_r(pseed); - } else { - //global static seed storage should always exist, - //except after being deleted by QGlobalStaticDeleter. - //But since it still can be called from destructor of another - //global static object, fallback to rand() - return rand(); - } -#elif defined(Q_OS_ANDROID) +#if defined(Q_OS_ANDROID) && (__ANDROID_API__ < 21) AndroidRandomStorage *randomStorage = randomTLS(); if (!randomStorage) return rand(); @@ -3468,6 +3452,22 @@ int qrand() randomStorage->setLocalData(random); return random.callMethod("nextInt", "(I)I", RAND_MAX); +#elif defined(Q_OS_UNIX) && !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && (_POSIX_THREAD_SAFE_FUNCTIONS - 0 > 0) + SeedStorage *seedStorage = randTLS(); + if (seedStorage) { + SeedStorageType *pseed = seedStorage->localData(); + if (!pseed) { + seedStorage->setLocalData(pseed = new SeedStorageType); + *pseed = 1; + } + return rand_r(pseed); + } else { + //global static seed storage should always exist, + //except after being deleted by QGlobalStaticDeleter. + //But since it still can be called from destructor of another + //global static object, fallback to rand() + return rand(); + } #else // On Windows srand() and rand() already use Thread-Local-Storage // to store the seed between calls diff --git a/src/corelib/io/qfilesystemengine_unix.cpp b/src/corelib/io/qfilesystemengine_unix.cpp index 6640e2b7e7..ac0c43d055 100644 --- a/src/corelib/io/qfilesystemengine_unix.cpp +++ b/src/corelib/io/qfilesystemengine_unix.cpp @@ -398,7 +398,7 @@ QString QFileSystemEngine::resolveGroupName(uint groupId) #if !defined(Q_OS_INTEGRITY) struct group *gr = 0; -#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD) && !defined(Q_OS_VXWORKS) +#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD) && !defined(Q_OS_VXWORKS) && (!defined(Q_OS_ANDROID) || defined(Q_OS_ANDROID) && (__ANDROID_API__ >= 24)) size_max = sysconf(_SC_GETGR_R_SIZE_MAX); if (size_max == -1) size_max = 1024; diff --git a/src/corelib/io/qfilesystemwatcher_inotify.cpp b/src/corelib/io/qfilesystemwatcher_inotify.cpp index 3cfc6a254f..048669b92f 100644 --- a/src/corelib/io/qfilesystemwatcher_inotify.cpp +++ b/src/corelib/io/qfilesystemwatcher_inotify.cpp @@ -222,6 +222,14 @@ QT_END_NAMESPACE #include +// see https://github.com/android-ndk/ndk/issues/394 +# if defined(Q_OS_ANDROID) && (__ANDROID_API__ < 21) +static inline int inotify_init1(int flags) +{ + return syscall(__NR_inotify_init1, flags); +} +# endif + #endif QT_BEGIN_NAMESPACE @@ -229,7 +237,7 @@ QT_BEGIN_NAMESPACE QInotifyFileSystemWatcherEngine *QInotifyFileSystemWatcherEngine::create(QObject *parent) { int fd = -1; -#ifdef IN_CLOEXEC +#if defined(IN_CLOEXEC) fd = inotify_init1(IN_CLOEXEC); #endif if (fd == -1) { diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp index f359d25a73..69a11b2b62 100644 --- a/src/corelib/thread/qthread_unix.cpp +++ b/src/corelib/thread/qthread_unix.cpp @@ -101,7 +101,7 @@ # define SCHED_IDLE 5 #endif -#if defined(Q_OS_DARWIN) || !defined(Q_OS_OPENBSD) && defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && (_POSIX_THREAD_PRIORITY_SCHEDULING-0 >= 0) +#if defined(Q_OS_DARWIN) || !defined(Q_OS_ANDROID) && !defined(Q_OS_OPENBSD) && defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && (_POSIX_THREAD_PRIORITY_SCHEDULING-0 >= 0) #define QT_HAS_THREAD_PRIORITY_SCHEDULING #endif