NDK r15 constructor finds neither HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC nor pthread_condattr_setclock for some build targets; Build failure

I have my own code in my project. I am using pthread with monotonic time. But I am not good at NDK development.

C code to initialize and use the condition with monotonic clock:

int initMonotonicCond(pthread_cond_t *cond) {
    int result = 0;
#ifdef HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC
    result = pthread_cond_init(cond, NULL);
#else
    pthread_condattr_t cond1attr;
    result |= pthread_condattr_init(&cond1attr);
    result |= pthread_condattr_setclock(&cond1attr, CLOCK_MONOTONIC);
    result |= pthread_cond_init(cond, &cond1attr);
    pthread_condattr_destroy(&cond1attr);
#endif
    return result;
}

void monothonicWait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *ts) {
#ifdef HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC
    pthread_cond_timedwait_monotonic_np(cond, mutex, ts);
#else
    pthread_cond_timedwait(cond, mutex, ts);
#endif
}

      

Gradle builds ndk project with

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 24
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            externalNativeBuild {
                cmake {
                    cppFlags "-fexceptions -frtti -fPIE -fPIC"
                    abiFilters "armeabi-v7a", "armeabi", "arm64-v8a", "x86", "x86_64", "mips", "mips64"
                }
            }
        }
        debug {
            externalNativeBuild {
                cmake {
                    cppFlags "-fexceptions -frtti -fPIE -funwind-tables -DDEBUG -fPIC"
                    abiFilters "armeabi"
                }
            }
        }
    }
.....
}

      

I recently updated Android Studio and all SDK stuff to a newer version. And ndk build up to r15 i guess. And now I am getting an error on creation:

Error: (155, 15) error: using undeclared identifier 'pthread_condattr_setclock'; did you mean "pthread_condattr_setpshared"?

After some research I got what now HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC

(and pthread_cond_timedwait_monotonic_np

) should be defined for non-x64 targets ("armeabi-v7a", "armeabi", "x86", "mips"). And it was determined. But now it is not defined.

So, "armeabi-v7a", "x86", "mips" are neither defined HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC

nor pthread_condattr_setclock

, therefore my project cannot be created for these purposes.

So what's the reason for this and what options do I have?

Do I have to use monotonous waiting for these goals in some way?

Should I not build for these purposes?

Should I go back to an earlier NDK?

Or should I post to google groups about this?

+3


source to share


1 answer


pthread_condattr_setclock

was added in android-21: https://android.googlesource.com/platform/bionic/+/master/libc/libc.map.txt#780 so you can't access it in older versions.

HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC

appears to have been defined in older titles. It really shouldn't have been (not with that name, anyway). Names like this are a convention used by autoconf-generated stuff and we shouldn't be duplicated because it might cause amcro override warnings. The best way to write this check is:

#if defined(__ANDROID_API__) && __ANDROID_API__ >= 21

      



This is still not enough for you to build the building again, as the declaration for pthread_cond_timedwait_monotonic_np

disappeared from the headers when the actual POSIX APIs were added for this. I just added a change to re-add the declaration for convenience: https://android-review.googlesource.com/420945

Unfortunately, it's too late to do this in r15b. In the meantime, you can add your own declaration for this function:

extern "C" int pthread_cond_timedwait_monotonic_np(
    pthread_cond_t*, pthread_mutex_t*, const struct timespec*);

      

+3


source







All Articles