Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NDK builder r15 finds neither HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC nor pthread_condattr_setclock for some build targets; Build fails

I have some native code in my project. I use pthread with monotonic time. But I'm not good in NDK development.

C code to initialise and use 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"
                }
            }
        }
    }
.....
}

Recently I've updated Android Studio and all the SDK stuff to newer version. And ndk build to r15, I guess. And now I'm getting and error when building:

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

After some research I've fount that 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 defined. But it is not defined now.

So, "armeabi-v7a", "x86", "mips" do not have defined nither HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC nor pthread_condattr_setclock defined, so my project can't be built for theese targets.

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

Should I not use monothonic wait for that targets some way?

Should I not build for those targets?

Should I revert to older NDK?

Or should I write to google groups about that?

like image 456
babay Avatar asked Jan 18 '26 09:01

babay


1 Answers

pthread_condattr_setclock was added in android-21: https://android.googlesource.com/platform/bionic/+/master/libc/libc.map.txt#780, so that's why you can't access it on older releases.

HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC was apparently defined in the old headers. It really shouldn't have been (not with that name, anyway). Names like that are the convention used by autoconf generated things, and we shouldn't be overlapping because that can cause amcro redefinition warnings. The better way to write this check is:

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

That's still not quite enough to get you building again though, since the declaration for pthread_cond_timedwait_monotonic_np disappeared from the headers when the actual POSIX APIs for this got added. I've just uploaded a change to get the declaration re-added for the sake of compatibility: https://android-review.googlesource.com/420945

Unfortunately it's too late for this to make it into r15b. What you could do in the meantime is add your own declaration for that function:

extern "C" int pthread_cond_timedwait_monotonic_np(
    pthread_cond_t*, pthread_mutex_t*, const struct timespec*);
like image 161
Dan Albert Avatar answered Jan 21 '26 01:01

Dan Albert