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 <thiago.macieira@intel.com>
This commit is contained in:
BogDan Vatra 2017-08-17 09:31:19 +03:00
parent 39852ce60f
commit b525ec2eb0
4 changed files with 48 additions and 40 deletions

View File

@ -3344,7 +3344,11 @@ bool qunsetenv(const char *varName)
#endif #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<QJNIObjectPrivate> 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) # 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. // older versions of INTEGRITY used a long instead of a uint for the seed.
@ -3355,10 +3359,6 @@ typedef uint SeedStorageType;
typedef QThreadStorage<SeedStorageType *> SeedStorage; typedef QThreadStorage<SeedStorageType *> SeedStorage;
Q_GLOBAL_STATIC(SeedStorage, randTLS) // Thread Local Storage for seed value Q_GLOBAL_STATIC(SeedStorage, randTLS) // Thread Local Storage for seed value
#elif defined(Q_OS_ANDROID)
typedef QThreadStorage<QJNIObjectPrivate> AndroidRandomStorage;
Q_GLOBAL_STATIC(AndroidRandomStorage, randomTLS)
#endif #endif
/*! /*!
@ -3378,21 +3378,7 @@ Q_GLOBAL_STATIC(AndroidRandomStorage, randomTLS)
*/ */
void qsrand(uint seed) void qsrand(uint seed)
{ {
#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)
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 (randomTLS->hasLocalData()) { if (randomTLS->hasLocalData()) {
randomTLS->localData().callMethod<void>("setSeed", "(J)V", jlong(seed)); randomTLS->localData().callMethod<void>("setSeed", "(J)V", jlong(seed));
return; return;
@ -3407,6 +3393,20 @@ void qsrand(uint seed)
} }
randomTLS->setLocalData(random); 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 #else
// On Windows srand() and rand() already use Thread-Local-Storage // On Windows srand() and rand() already use Thread-Local-Storage
// to store the seed between calls // to store the seed between calls
@ -3432,23 +3432,7 @@ void qsrand(uint seed)
*/ */
int qrand() int qrand()
{ {
#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)
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)
AndroidRandomStorage *randomStorage = randomTLS(); AndroidRandomStorage *randomStorage = randomTLS();
if (!randomStorage) if (!randomStorage)
return rand(); return rand();
@ -3468,6 +3452,22 @@ int qrand()
randomStorage->setLocalData(random); randomStorage->setLocalData(random);
return random.callMethod<jint>("nextInt", "(I)I", RAND_MAX); return random.callMethod<jint>("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 #else
// On Windows srand() and rand() already use Thread-Local-Storage // On Windows srand() and rand() already use Thread-Local-Storage
// to store the seed between calls // to store the seed between calls

View File

@ -398,7 +398,7 @@ QString QFileSystemEngine::resolveGroupName(uint groupId)
#if !defined(Q_OS_INTEGRITY) #if !defined(Q_OS_INTEGRITY)
struct group *gr = 0; 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); size_max = sysconf(_SC_GETGR_R_SIZE_MAX);
if (size_max == -1) if (size_max == -1)
size_max = 1024; size_max = 1024;

View File

@ -222,6 +222,14 @@ QT_END_NAMESPACE
#include <sys/inotify.h> #include <sys/inotify.h>
// 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 #endif
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -229,7 +237,7 @@ QT_BEGIN_NAMESPACE
QInotifyFileSystemWatcherEngine *QInotifyFileSystemWatcherEngine::create(QObject *parent) QInotifyFileSystemWatcherEngine *QInotifyFileSystemWatcherEngine::create(QObject *parent)
{ {
int fd = -1; int fd = -1;
#ifdef IN_CLOEXEC #if defined(IN_CLOEXEC)
fd = inotify_init1(IN_CLOEXEC); fd = inotify_init1(IN_CLOEXEC);
#endif #endif
if (fd == -1) { if (fd == -1) {

View File

@ -101,7 +101,7 @@
# define SCHED_IDLE 5 # define SCHED_IDLE 5
#endif #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 #define QT_HAS_THREAD_PRIORITY_SCHEDULING
#endif #endif