ndk pthread vs java thread on android when calling jni function - android

I need to call a JNI C function:
When should I call it from a java thread (Runnable, AsyncTask, etc)?
When should i call a C function that uses pthread ?

Using a Java background thread to call a native method (directly with (new Thread( () -> { nativeMethod() } )).start() or indirectly via AsyncTask or some Executor) may be easier in many scenarios, especially if your C code should return its results to Java, or to issue Java callbacks.

Related

jni: calling a java method in its appartment thread

I'm pretty new to jni.
I need to invoke a native method from java code, which should perform a time-consuming operation asynchronously (in another thread), and at the end it should invoke a java callback. However I need this callback to be invoked in the same java thread that originated the operation. That is, the java application should be single-threaded, and all callbacks should be invoked in the context of this thread.
I've read tutorials on callback invocations, the stuff about keeping global refs to the object, getting the appropriate methodid and invoking it.
I've also found stuff about how to call a java method from another thread (AttachCurrentThread), however this is not what I need. I need to make the java call within the originating thread, so the question is how do I switch to that thread?
For instance, in Win32 I'd use something like PostMessage/PostThreadMessage to invoke a code in the specified thread, assuming it runs the message-loop. There're also alternatives like QueueUserAPC which is applicable if the thread waits for events in an alertable state.
So, what are the options at my disposal? Is there a jni method for posting execution of a java method in the specified thread? Or perhaps a linux equivalent of PostThreadMessage? I can also think about workarounds within the java code, but prefer to solve this on the native side.
Thanks in advance.
Please see a relevant recent discussion: How do I post code to be run on the Android main thread from a separate thread in C++?. The most interesting part is this thread in android-ndk group from 2012.
TL;NR: from JNI_OnLoad() or other call that happens early enough, and comes from the UI thread, you call
pipe2(messagePipe, O_NONBLOCK | O_CLOEXEC);
ALooper_addFd(ALooper_forThread(), messagePipe[0], 0, ALOOPER_EVENT_INPUT, handler, data);
Elsewhere you define a simple handler function:
int handler(int fd, int, void* pHandler) {
int what = 0;
read(fd, &what, sizeof(what));
static_cast<MyHandler *>(pHandler)->handle(what));
return 1;
}
Now the native version of Handler.sendEmptyMessage(), and a close analog of CWinThread::PostThreadMessage() (without wParam and lParam), could then be as simple as
MyHandler::sendEmptyMessage(int what) {
write(messagePipe[1], &what, sizeof(what));
}

Unity3D & Android: Difference between "UnityMain" and "main" threads?

TLDR: I'm using JNI to call into my custom JAR from Unity C#. But the android library says it's running on the "UnityMain" thread, while the actual ui thread for the activity is called "main". What is the difference between the two?
Details:
This is a problem for me since I get the error "Can't create handler inside thread that has not called Looper.prepare()". Here's the output I get when printing the two threads from Java:
/// Java Output
Current Thread: Thread[UnityMain,5,main]
MainLooper Thread: Thread[main,5,main]
To resolve this, I'm running the JNI calls using the Activity.runOnUiThread method:
/// Unity C# Code
activityObj.Call("runOnUiThread", new AndroidJavaRunnable(() => {
// JNI calls and other stuff
}
Now I get the following output when printing the two threads from Java:
/// Java Output
Current Thread: Thread[main,5,main]
MainLooper Thread: Thread[main,5,main]
Only problem now is that I can't make Unity Coroutine or Invoke calls from the "main" thread (i.e. inside the "runOnUiThread" callback). I get the following Unity error:
/// Unity C# Output
E/Unity (21048): Invoke can only be called from the main thread.
E/Unity (21048): Constructors and field initializers will be executed from the loading thread when loading a scene.
E/Unity (21048): Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
So what is the difference between the "UnityMain" and "main" threads? And why is the Java "main" thread different from that of Unity?
Unity runs its own thread to handle its processing. It doesn't usurp the Android main thread created by the Android OS when launching the app. Typically when you write a traditional Android app there is only one main thread, and everything that deals with the UI must run on the main thread. To use a second "main" thread, was a design choice made by Unity, probably so it can do whatever it wants without messing with the apps Android main thread. If you want to do anything in the Android UI outside of Unity you'll need to have your code run on the main thread. You can do this from anywhere using the below snippet:
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
Log.d("MAIN", "Thread? " + Thread.currentThread());
}
});
If the Android/Java code you are calling into has access to the Application context or an Activity context you can use runOnUiThread as well.

Difference between MainThread, UiThread, WorkerThread, BinderThread in Android Annotation

As i read in the android annotations for thread document
We have four types of thread,
#MainThread
#UiThread
#WorkerThread
#BinderThread
What is differences?
The differences is:
#MainThread, first Thread run when app started,
#UiThread, run from MainThread for UI works,
#WorkerThread, that run when programmer define a thread
#BinderThread,uses for something like query()/insert()/update()/delete() methods in ContentProvider.

JNI Env in different threads and exceptions

Say that I have two native threads, A and B. In thread A I use JNI methods to get a Java object by invoking a java method (constructor, static method, etc) and get a global reference to it.
java_vm->AttachCurrentThread(&env,NULL);
j_ref_inputStream = env->CallObjectMethod(g_j_ref_btSocket,j_mid_getInputStream);
g_j_ref_inputStream = env->NewGlobalRef(j_ref_inputStream);
Now I pass my global reference to Thread B where I call a method on it:
java_vm->AttachCurrentThread(&env2,NULL);
jint b = env2->CallIntMethod(g_j_ref_inputStream,j_mid_read);
Calling read() on an InputStream will block Thread B (right?). To forcefully unblock Thread B I can try generating an IOException. So in Thread A I do:
env->callVoidMethod(g_j_ref_btSocket,j_mid_close);
But where do I catch this exception? I'm not too clear on JNI and native threads. Do I call the following in Thread A:
j_exception = env->ExceptionOccurred();
if(j_exception) // handle exception
Or in Thread B:
j_exception = env2->ExceptionOccurred();
if(j_exception) // handle exception
Or does it not matter? (please elaborate on why in either case)
Forget the JNI aspect of it. Just consider what you're trying to do. think of it in pure Java if it helps. You are trying to unblock thread B by closing a stream it is blocked reading. So B will get an exception.
In reality both threads should call 'ExceptionOccurred()' after every JNI call, not just the ones you're 'expecting' exceptions on. But thread B will get this exception.

Android: Synchronization of native thread with main thread

In my android application I've got a callback from a native thread into Java code which needs to be synchronized with the main UI thread. The intention is that the UI thread display a list of options based on information returned from the native thread. Until the user selects an option the native thread needs to block. After the user selects an option the native thread reads the value and continues running.
I've tried to implement this solution using a ConditionVariable however I get a VM error with the comment indicating "Fatal spin-on-suspend, dumping threads".
It looks as if it's not possible to use a Java based synchronization object to synchronize these threads. The code works perfectly in the case where I've got two Java threads.
In general is there any way to use a Java based synchronization object to synchronize a Java and native thread, or does this need to be implemented using the NDK with a call from the Java thread into an NDK function that implements the synchronization?
The way to do this is not to use a Java based sync object but rather an NDK based sync object as follows:
static pthread_cond_t uiConditionVariable = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t uiConditionMutex = PTHREAD_MUTEX_INITIALIZER;
/**
* This function opens the condition variable which releases waiting threads.
*/
JNIEXPORT void JNICALL
Java_com_Xxxx_openConditionVariable(JNIEnv *env,jobject o)
{
pthread_mutex_lock(&uiConditionMutex);
pthread_cond_signal(&uiConditionVariable);
pthread_mutex_unlock(&uiConditionMutex);
}
/**
* This function blocks on the condition variable associated with the
*/
JNIEXPORT void JNICALL
Java_com_Xxxx_blockConditionVariable(JNIEnv *env,jobject o)
{
pthread_mutex_lock(&uiConditionMutex);
pthread_cond_wait(&uiConditionVariable,&uiConditionMutex);
pthread_mutex_unlock(&uiConditionMutex);
}

Categories

Resources