Search code examples
android-ndkarmreal-timerenderscriptandroid-renderscript

Overtaking max_priority with NDK


I am trying to replicate a little degree of RTOS conditions in Android devices, without having to re-install the whole kernel (as happens with RTDroid). One of these conditions is to ensure that the critical threads will have preference over normal threads, every time they need the CPU or memory.

I understand that the code written in C/C++, using NDK, is executed at the linux kernel level. Meaning that it should have the same priority than the DVM process (Dalvik Virtual Machine). Therefore, I infered that the C/C++ code is executed with a greater priority than any "MAX_PRIORITY" thread running at the DVM.

Can I replicate the same behavior using Renderscript? My intuition says no, as the main thread of execution in Renderscript scripts is controlled by a java application.

Thanks in advance for any insights you could provide me!


Solution

  • In short, no. There are also several faulty assumptions/statements here:

    ...code written in C/C++, using NDK, is executed at the Linux kernel level.

    This is incorrect. Any C/C++ code you build using the NDK is called from your Java code and is running within the context of the same process and thread executing the VM (Dalvik or ART.) There's nothing special about it priority wise. You can create threads (pthreads) at the native level as well. These are all running under the context of the same user space process and are scheduled by the Linux kernel powering the system.

    The Thread objects at the Java level are essentially wrappers around pthreads in the VM implementation (certainly in the case of Dalvik, ART may try to do something different - but I doubt it), so nothing special or fancy there.

    In the case of Renderscript, how it achieves parallelization is very device/platform dependent. On some devices this may be done using nothing more than pthreads on the CPU. On other devices it may leverage on onboard DSP or (typically) a GPU. But, it is not a general purpose threading environment or something where you can dictate/manage priorities. It is specifically geared at processing data in a parallel, asynchronous (relative to the Java on the CPU) manner.