Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified header system doesn't provide pthread_mutex_lock_timeout_np #420

Closed
Tapped opened this issue Jun 12, 2017 · 13 comments
Closed

Unified header system doesn't provide pthread_mutex_lock_timeout_np #420

Tapped opened this issue Jun 12, 2017 · 13 comments
Assignees
Labels
Milestone

Comments

@Tapped
Copy link

Tapped commented Jun 12, 2017

Description

usr/include/pthread.h in the unified sysroot folder doesn't have pthread_mutex_lock_timeout_np, which was the standard way to lock a mutex with timeout on platforms less than android-21. Now the only method to lock a mutex with timeout is pthread_mutex_timedlock which is only available in android-21 and above. I suspect this is a bug with the unified header system, which I would expect to provide pthread_mutex_lock_timeout_np for __ANDROID_API__ < 21. Right now there is no way to target versions less than android-21 that uses a system based mutex lock with timeout.

We have made a workaround (that still uses the unified header system) for it, that emulates the behavior, but is probably slower than what the kernel can provide.

    long long timeout = get_ticks() + millisecondsTimeout * 10000ll;
    while (pthread_mutex_trylock(mutexHandle) == EBUSY)
    {
        long long now = get_ticks(); // Replace get_ticks with your method for getting CPU ticks.
        if (now >= timeout)
            return false;

        pthread_yield();
    }
    return true;

Environment Details

  • NDK Version: 15.0.4075724
  • Build system: Gradle - CMake
  • Host OS: Windows and macOS
  • Compiler: Clang
  • ABI: armeabi-v7a
  • STL: libstlport
  • NDK API level: android-16
@DanAlbert DanAlbert added this to the r15b milestone Jun 12, 2017
@DanAlbert DanAlbert assigned DanAlbert and unassigned enh Jun 12, 2017
hubot pushed a commit to aosp-mirror/platform_bionic that referenced this issue Jun 15, 2017
The proper API for this isn't available until L, so expose this for
API levels earlier than that.

Test: make checkbuild
Bug: android/ndk#420
Change-Id: I382b8f557be9530f3e13aaae353b4a6e7f9301ab
@DanAlbert
Copy link
Member

The fix has been merged into r15b. I'm sending build 4113355 of ndk-r15-release off to QA here in a bit if you want to grab the RC from the build server (https://android.googlesource.com/platform/ndk/+/master/docs/ContinuousBuilds.md), otherwise it should be out some time this week.

@rcdailey
Copy link

@DanAlbert: I just installed build from here:

https://partner.android.com/build#p:id=builddetail&pp=branch%253Daosp-ndk-r15-release%2526buildnumber%253D4119039%2526target%253Dwin64&a=100621237

This does not seem to fix my problem. Boost Thread isn't able to find it:

In file included from E:/code/frontend/source/Core/ThirdParty/boost/include\boost/thread/mutex.hpp:16:

E:/code/frontend/source/Core/ThirdParty/boost/include\boost/thread/pthread/mutex.hpp:251:25: error: use of undeclared identifier 'pthread_mutex_timedlock'

          int const res=pthread_mutex_timedlock(&m,&timeout);

                        ^

I'm using API 15. This worked fine when I wasn't using the unified headers. What do I need to do to get this working again?

@DanAlbert
Copy link
Member

DanAlbert commented Jun 22, 2017

That code shouldn't work for either set of headers if you're targeting API 15 (it should work for 64-bit ABIs, but that's because 15 should be translated to 21 for those). pthread_mutex_timedlock didn't exist until android-21.

What ABI are you targeting, and what build system are you using?

@DanAlbert
Copy link
Member

The check in boost looks fine: https://github.com/boostorg/thread/blob/develop/include/boost/thread/pthread/mutex.hpp#L30

Are you manually passing -DBOOST_PTHREAD_HAS_TIMEDLOCK or something?

@rcdailey
Copy link

Using CMake 3.8.1. ABI is armeabi-v7a. My CMake toolchain is below:

set( CMAKE_SYSTEM_NAME Android )
set( CMAKE_SYSTEM_VERSION 15 ) # API level
set( CMAKE_ANDROID_ARCH_ABI armeabi-v7a )
set( CMAKE_ANDROID_STL_TYPE gnustl_shared )
set( CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION clang )
set( CMAKE_ANDROID_NDK_DEPRECATED_HEADERS 1 )

@rcdailey
Copy link

@DanAlbert I'm not specifying that compiler definition myself. But in boost/thread/pthread/mutex.hpp it does this at the top:

#if (defined(_POSIX_TIMEOUTS) && (_POSIX_TIMEOUTS-0)>=200112L) \
 || (defined(__ANDROID__) && defined(__ANDROID_API__) && __ANDROID_API__ >= 21)
#ifndef BOOST_PTHREAD_HAS_TIMEDLOCK
#define BOOST_PTHREAD_HAS_TIMEDLOCK
#endif
#endif

Could this be the cause?

@DanAlbert
Copy link
Member

No, that check is fine. The first half of the or fails because _POSIX_TIMEOUTS-0 will be -1 unless you're targeting android-21+ https://android.googlesource.com/platform/bionic/+/master/libc/include/bits/posix_limits.h#108

The second half is fine because you're not targeting 21+, so neither case should be true, and it shouldn't be defined.

Can you provide the failing compilation command? The clang++ command from the log (ninja -v if you're using ninja, pass -DCMAKE_VERBOSE_MAKEFILE=ON when running cmake if you're using make, idk what for anything else)

@alexcohn
Copy link

@DanAlbert As I showed in #423, this patch is not enough. Please supply all defines and functions that were declared in pthread.h before android-21.

@enh
Copy link
Contributor

enh commented Jun 22, 2017

i don't think that's true... i built boost with just that patch (with https://gist.github.com/enh/b2dc8e2cbbce7fffffde2135271b10fd and a locally modified toolchain).

@enh
Copy link
Contributor

enh commented Jun 22, 2017

yeah, boost 1.64.0 builds fine for me if i replace my hacked r15 with clean r15b (though note that the bootstrap step requires that "clang" is a working host clang, so i had to have a symlink to /usr/bin/clang-3.8 on my ubuntu box).

@rcdailey
Copy link

I'm using boost 1.63. I'll get you the command invocation later.

@DanAlbert
Copy link
Member

@alexcohn: sorry for missing all the details in the other bug, I've uploaded a change to re-expose some more legacy APIs. That should actually be affecting this bug though because the missing API here is the one we did expose.

@rcdailey: Alright, please do. That should help figure out what's going on.

@rcdailey
Copy link

@DanAlbert I must have had some other issue, because I'm not able to reproduce this anymore. Sorry for the confusion. This looks to be working fine under API 15.

sepehrst pushed a commit to spsforks/android-bionic-libc that referenced this issue Apr 22, 2024
The proper API for this isn't available until L, so expose this for
API levels earlier than that.

Test: make checkbuild
Bug: android/ndk#420
Change-Id: I382b8f557be9530f3e13aaae353b4a6e7f9301ab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants