JUCE icon indicating copy to clipboard operation
JUCE copied to clipboard

[Android] invoke juce::Thread runnable from within java.lang.Thread context.

Open atsushieno opened this issue 3 years ago • 0 comments

(redoing #1039, sorry for the noise here too.)

This fixes the core part of the issues exposed at https://forum.juce.com/t/env-findclass-does-not-work-inside-juce-app-very-strange/46886 The full details are explained at https://atsushieno.github.io/2022/03/16/juce-android-threads.html

The existing juce::Thread invokes its runnable (run()) target using pthread_create(). This is causing any JNI invocation that involves non-framework classes and non-registered JUCE Java classes not loading.

This Android NDK documentation explains that it is anticipated: https://developer.android.com/training/articles/perf-jni#threads

To fix this issue, java.lang.Thread context is needed.

Since there seems no way to first create pthread_create() and attach it to java.lang.Thread without already having it running, we need to have a java.lang.Thread and let it callback to native code (we can use Runnable proxy based on JNIClassBase), retrieve pthread_t ID via pthread_self() and proceeed to the actual runnable (run() on juce::Thread).

The JNI part is involved only at start() in this change - on Android thread cancellation is not really supported (not in JUCE on Android either).

atsushieno avatar Mar 17 '22 06:03 atsushieno