Interlocked/Atomic operations are CPU specific.
Is there any common baseline requirement by all Android supporting hardware as far as for interlocked operations?
Are there any interlocked operations supported by all Android devices?
Is there any Android equivalent to Windows' InterlockedExchange?
Any help will be appreciated.
You can use __atomic_swap for InterlockedExchange functionality.
Read docs/ANDROID-ATOMICS.html file from NDK distribution for more info.
You can also use GCC atomic builtins.
There's stdatomic header in latest editions of ndk, so if you use GNU or LLVM cxx module, it is possible to use this standard C++11 header. You cannot use it with STLport however - it has no such header. Do not forget to add libatomic to LOCAL_LDLIBS (see https://developer.android.com/ndk/guides/cpp-support.html for details).
Related
I'm compiling a 3rd-party Java library for Android that uses JNI. I read the relevant pages on adding C++ support on developer.android but I'm still confused about a couple of issues regarding C++ STL runtime that I was hoping I could clear up here:
1- My library has no control over the app it will be embedded in, so I don't know if there will be other libraries that might use a static/shared STLs. If I use a static C++ runtime with ANDROID_STL=c++_static, is it safe, or should I have to worry about another library that could be using something like gnustl_static which might conflict with mine?
2- If I use a shared C++ runtime with ANDROID_STL=c++_shared, is it a guarantee that a specific element in the STL will use the libc++ runtime or could it be possible to use gnustl if it doesn't exist? For example, If I was using std::string with a shared c++ runtime (c++_shared) in an app that has another library of gnustl_static, will my std::string implementation be taken from libc++ or gnustl?
Ideally, I'd like to have a very stripped down version of a static c++ runtime with (c++_static) that only includes std::vector, std::string and std::map. I was actually planning to use something like -ffunction-sections as described here and #768.
Please advise and thank you.
Environment Details
Pkg.Desc = Android NDK
Pkg.Revision = r15c
Android Studio = 3.1.2
system: cmake Host OS: Arch Linux ($ uname -r % 4.18.5-arch1-1-ARCH)
Compiler: Clang++
STL: c++_static/c++_shared
Your concern is a very real one. But if handled properly, you can find a robust way out.
The warnings about using single C++ runtime across all libraries in the app (and the whole idea to define C++ support in NDK as APP_STL vs. most other flags such as LOCAL_CFLAGS or LOCAL_SHARED_LIBRARIES, are relevant for the native libraries that are connected. JNI libraries that never communicate directly (except through their corresponding Java layers) can use different C++ runtimes. Another point is that normal build will only package one C++ runtime shared lib into the APK. Note that versioning is also a potential hazard: if a developer who adds your library uses a different NDK release, there might be collisions or unexpected side effects when his version of STL runtime works with your code.
Therefore, to achieve maximum flexibility, your library should use a static C++ runtime. This may effect the size of the binary, but if, as you say, you use only a limited subset of STL, this extra will be rather small.
The bottom line, you will have minimum to worry about if build your shared library with libc++_static.
We have an Android application with several native libraries. Currently, most of them use stlport_shared, but we are interested in upgrading to c++_shared (the LLVM libc++ runtime). If we were to upgrade some of them, but not others, we would have to load both stlport and llvm.
I imagined that loading two implementations of the STL might cause issues, but in practice the app seems to run correctly. Is this undefined behavior, or is it allowable to load more than one STL implementation?
I've read over https://developer.android.com/ndk/guides/cpp-support.html and some of the documentation supplied with the NDK, but can't seem to find a definitive answer. Thank you for your help!
It's best to avoid this if at all possible.
If an STL type (like a std::string, for example) is accessed by both an stlport_shared binary and a c++_static binary, as the two implementations of std::string are not compatible this will not work.
You also are vulnerable to the issues described by https://stackoverflow.com/a/25856207/632035
I working on android project and I need to access the wstring_convert class in c++11 in gcc but the problem is gcc does not have this in its standard library, so am thinking of using clang std library in gcc compiler of this possible, or if there is any other way to use wstring_convert from external library like stlport but stlport development latest update is in 2008 !
Any help is appreciated.
You can choose
NDK_TOOLCHAIN_VERSION := clang
in your Application.mk
The standard C++ library from the LLVM project is:
http://libcxx.llvm.org/
It is a standalone, separate project from clang, but I never heard of someone using it with GCC. There are parts of the standard library which rely on compiler-specific internals (e.g., some type traits), so I am not sure this is going to work.
edit
From the front page:
libc++ is known to work on the following platforms, using g++-4.2 and clang (lack of C++11 language support disables some functionality). Note that functionality provided by is only functional with clang.
So it looks like it might actually work with at least some versions of GCC.
Just wondering if andorid NDK supports direct embedding of asm?
asm{
//lots of strange asm instructions...
}
If so, I presume you'd have to get some refference to the instruction sets of the specfic arm achitecture - where would you find these?
Thanks,
Steve.
The Android NDK uses GCC, so you can safely look at GCC documentation.
As for the specific ARM instructions, something like ARM architecture may be a good starting point.
In addition to #NuSkooler this is another useful resource
ARM GCC inline assembler cookbook
Is there any info about the future of extending the safe C++ header list in the NDK (or maybe some hints to what might be safe to use) ? Or how soon we can expect the next NDK update? Also will there be any tutorials and documentation comming out for the NDK?
Thanks
If you are interested in using C++ with the Android NDK you should check out the modified NDK by CrystaX. He has rebuilt the Android NDK so that it has full support for C++, including C++ Exceptions, RTTI (Run Time Type Identification) and the Standard C++ Library.
There aren't that many changes, in fact CrystaX offers a link to the complete patch file from his website.
Check it out at http://www.crystax.net/android/ndk-r4.php
i'm not aware of any announcements regarding future plans.
the samples are here:
http://developer.android.com/sdk/ndk/1.6_r1/index.html#samples
and the NDK itself contains documentation in the form of .txt files in the docs/ directory.