summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2011-03-11 18:34:35 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2011-03-11 18:34:35 +0000
commit66f2a08f8a37933b9eff79ceabdc2cb42706e48c (patch)
tree704cd88300e33a99335adf1d5e3e4ff6b71a2aed
parent2e5b7aebde6426058ea7b69424b0335a844a6a18 (diff)
Android: Don't share the JNI environment across threads, but obtain it the
correct way git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29569 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/hosted/android/keyboard.c5
-rw-r--r--apps/hosted/android/notification.c5
-rw-r--r--apps/hosted/android/yesno.c14
-rw-r--r--firmware/target/hosted/android/button-android.c1
-rw-r--r--firmware/target/hosted/android/lcd-android.c7
-rw-r--r--firmware/target/hosted/android/pcm-android.c11
-rw-r--r--firmware/target/hosted/android/powermgmt-android.c6
-rw-r--r--firmware/target/hosted/android/system-android.c7
-rw-r--r--firmware/target/hosted/android/system-target.h16
-rw-r--r--firmware/target/hosted/android/telephony-android.c4
10 files changed, 60 insertions, 16 deletions
diff --git a/apps/hosted/android/keyboard.c b/apps/hosted/android/keyboard.c
index 7c16cff079..460011c4f7 100644
--- a/apps/hosted/android/keyboard.c
+++ b/apps/hosted/android/keyboard.c
@@ -26,8 +26,8 @@
#include "string-extra.h"
#include "kernel.h"
#include "lang.h"
+#include "system.h"
-extern JNIEnv *env_ptr;
static jclass RockboxKeyboardInput_class;
static jobject RockboxKeyboardInput_instance;
static jmethodID kbd_inputfunc;
@@ -53,7 +53,9 @@ Java_org_rockbox_RockboxKeyboardInput_put_1result(JNIEnv *env, jobject this,
static void kdb_init(void)
{
+ JNIEnv *env_ptr = getJavaEnvironment();
JNIEnv e = *env_ptr;
+
static jmethodID kbd_is_usable;
if (RockboxKeyboardInput_class == NULL)
{
@@ -87,6 +89,7 @@ static void kdb_init(void)
int kbd_input(char* text, int buflen)
{
+ JNIEnv *env_ptr = getJavaEnvironment();
JNIEnv e = *env_ptr;
jstring str = e->NewStringUTF(env_ptr, text);
jstring ok_text = e->NewStringUTF(env_ptr, str(LANG_KBD_OK));
diff --git a/apps/hosted/android/notification.c b/apps/hosted/android/notification.c
index 33a7717012..1c89c51357 100644
--- a/apps/hosted/android/notification.c
+++ b/apps/hosted/android/notification.c
@@ -29,8 +29,8 @@
#include "misc.h"
#include "thread.h"
#include "debug.h"
+#include "system.h"
-extern JNIEnv *env_ptr;
extern jclass RockboxService_class;
extern jobject RockboxService_instance;
@@ -47,6 +47,7 @@ static const struct dim dim = { .width = 200, .height = 200 };
static void track_changed_callback(void *param)
{
struct mp3entry* id3 = (struct mp3entry*)param;
+ JNIEnv *env_ptr = getJavaEnvironment();
JNIEnv e = *env_ptr;
if (id3)
{
@@ -109,6 +110,7 @@ static void track_changed_callback(void *param)
static void track_finished_callback(void *param)
{
(void)param;
+ JNIEnv *env_ptr = getJavaEnvironment();
JNIEnv e = *env_ptr;
e->CallVoidMethod(env_ptr, NotificationManager_instance,
finishNotification);
@@ -122,6 +124,7 @@ static void track_finished_callback(void *param)
void notification_init(void)
{
+ JNIEnv *env_ptr = getJavaEnvironment();
JNIEnv e = *env_ptr;
jfieldID nNM = e->GetFieldID(env_ptr, RockboxService_class,
"fg_runner", "Lorg/rockbox/Helper/RunForegroundManager;");
diff --git a/apps/hosted/android/yesno.c b/apps/hosted/android/yesno.c
index 7aca3f7f79..de6ec99a3c 100644
--- a/apps/hosted/android/yesno.c
+++ b/apps/hosted/android/yesno.c
@@ -28,8 +28,8 @@
#include "settings.h"
#include "lang.h"
#include "kernel.h"
+#include "system.h"
-extern JNIEnv *env_ptr;
static jobject RockboxYesno_instance = NULL;
static jmethodID yesno_func;
static struct semaphore yesno_done;
@@ -44,7 +44,7 @@ Java_org_rockbox_RockboxYesno_put_1result(JNIEnv *env, jobject this, jboolean re
semaphore_release(&yesno_done);
}
-static void yesno_init(void)
+static void yesno_init(JNIEnv *env_ptr)
{
JNIEnv e = *env_ptr;
static jmethodID yesno_is_usable;
@@ -75,7 +75,7 @@ static void yesno_init(void)
sleep(HZ/10);
}
-jstring build_message(const struct text_message *message)
+static jstring build_message(JNIEnv *env_ptr, const struct text_message *message)
{
char msg[1024] = "";
JNIEnv e = *env_ptr;
@@ -99,10 +99,12 @@ enum yesno_res gui_syncyesno_run(const struct text_message * main_message,
{
(void)yes_message;
(void)no_message;
- yesno_init();
-
+ JNIEnv *env_ptr = getJavaEnvironment();
+
+ yesno_init(env_ptr);
+
JNIEnv e = *env_ptr;
- jstring message = build_message(main_message);
+ jstring message = build_message(env_ptr, main_message);
jstring yes = (*env_ptr)->NewStringUTF(env_ptr, str(LANG_SET_BOOL_YES));
jstring no = (*env_ptr)->NewStringUTF(env_ptr, str(LANG_SET_BOOL_NO));
diff --git a/firmware/target/hosted/android/button-android.c b/firmware/target/hosted/android/button-android.c
index ed1e125223..e7a3d00a65 100644
--- a/firmware/target/hosted/android/button-android.c
+++ b/firmware/target/hosted/android/button-android.c
@@ -29,7 +29,6 @@
#include "system.h"
#include "touchscreen.h"
-extern JNIEnv *env_ptr;
static int last_y, last_x;
static int last_btns;
diff --git a/firmware/target/hosted/android/lcd-android.c b/firmware/target/hosted/android/lcd-android.c
index 08a4075795..66ddcd5e3e 100644
--- a/firmware/target/hosted/android/lcd-android.c
+++ b/firmware/target/hosted/android/lcd-android.c
@@ -28,7 +28,6 @@
#include "lcd.h"
#include "button.h"
-extern JNIEnv *env_ptr;
extern jobject RockboxService_instance;
static jobject RockboxFramebuffer_instance;
@@ -94,6 +93,8 @@ void connect_with_java(JNIEnv* env, jobject fb_instance)
void lcd_deinit(void)
{
+ JNIEnv *env_ptr = getJavaEnvironment();
+
(*env_ptr)->DeleteGlobalRef(env_ptr, RockboxFramebuffer_instance);
(*env_ptr)->DeleteGlobalRef(env_ptr, native_buffer);
}
@@ -107,6 +108,8 @@ void lcd_init_device(void)
void lcd_update(void)
{
+ JNIEnv *env_ptr = getJavaEnvironment();
+
if (display_on)
(*env_ptr)->CallVoidMethod(env_ptr, RockboxFramebuffer_instance,
java_lcd_update);
@@ -114,6 +117,8 @@ void lcd_update(void)
void lcd_update_rect(int x, int y, int width, int height)
{
+ JNIEnv *env_ptr = getJavaEnvironment();
+
if (display_on)
(*env_ptr)->CallVoidMethod(env_ptr, RockboxFramebuffer_instance,
java_lcd_update_rect, x, y, width, height);
diff --git a/firmware/target/hosted/android/pcm-android.c b/firmware/target/hosted/android/pcm-android.c
index f4ed4b1b33..4b6df7f878 100644
--- a/firmware/target/hosted/android/pcm-android.c
+++ b/firmware/target/hosted/android/pcm-android.c
@@ -25,8 +25,6 @@
#include "debug.h"
#include "pcm.h"
-extern JNIEnv *env_ptr;
-
/* infos about our pcm chunks */
static size_t pcm_data_size;
static char *pcm_data_start;
@@ -117,6 +115,8 @@ void pcm_play_dma_start(const void *addr, size_t size)
void pcm_play_dma_stop(void)
{
+ JNIEnv *env_ptr = getJavaEnvironment();
+
(*env_ptr)->CallVoidMethod(env_ptr,
RockboxPCM_instance,
stop_method);
@@ -124,6 +124,8 @@ void pcm_play_dma_stop(void)
void pcm_play_dma_pause(bool pause)
{
+ JNIEnv *env_ptr = getJavaEnvironment();
+
(*env_ptr)->CallVoidMethod(env_ptr,
RockboxPCM_instance,
play_pause_method,
@@ -152,6 +154,7 @@ void pcm_play_dma_init(void)
* Luckily we only reference the PCM object from here, so it's safe (and
* clean) to allocate it here
**/
+ JNIEnv *env_ptr = getJavaEnvironment();
JNIEnv e = *env_ptr;
/* get the class and its constructor */
jclass RockboxPCM_class = e->FindClass(env_ptr, "org/rockbox/RockboxPCM");
@@ -169,6 +172,8 @@ void pcm_play_dma_init(void)
void pcm_deinit(void)
{
+ JNIEnv *env_ptr = getJavaEnvironment();
+
(*env_ptr)->DeleteGlobalRef(env_ptr, RockboxPCM_instance);
}
@@ -178,5 +183,7 @@ void pcm_postinit(void)
void pcm_set_mixer_volume(int volume)
{
+ JNIEnv *env_ptr = getJavaEnvironment();
+
(*env_ptr)->CallVoidMethod(env_ptr, RockboxPCM_instance, set_volume_method, volume);
}
diff --git a/firmware/target/hosted/android/powermgmt-android.c b/firmware/target/hosted/android/powermgmt-android.c
index d23fece39a..222212f9c8 100644
--- a/firmware/target/hosted/android/powermgmt-android.c
+++ b/firmware/target/hosted/android/powermgmt-android.c
@@ -23,8 +23,8 @@
#include <jni.h>
#include <stdbool.h>
#include "config.h"
+#include "system.h"
-extern JNIEnv *env_ptr;
extern jclass RockboxService_class;
extern jobject RockboxService_instance;
@@ -32,6 +32,8 @@ static jfieldID _battery_level;
void powermgmt_init_target(void)
{
+ JNIEnv *env_ptr = getJavaEnvironment();
+
jmethodID initBatteryMonitor = (*env_ptr)->GetMethodID(env_ptr,
RockboxService_class,
"initBatteryMonitor",
@@ -50,6 +52,8 @@ void powermgmt_init_target(void)
int battery_level(void)
{
+ JNIEnv *env_ptr = getJavaEnvironment();
+
return (*env_ptr)->GetIntField(env_ptr, RockboxService_instance, _battery_level);
}
diff --git a/firmware/target/hosted/android/system-android.c b/firmware/target/hosted/android/system-android.c
index 66de4e0ebb..f9f0605b49 100644
--- a/firmware/target/hosted/android/system-android.c
+++ b/firmware/target/hosted/android/system-android.c
@@ -27,7 +27,7 @@
/* global fields for use with various JNI calls */
-JNIEnv *env_ptr;
+JavaVM *vm_ptr;
jobject RockboxService_instance;
jclass RockboxService_class;
@@ -44,8 +44,11 @@ void system_reboot(void) { }
void power_off(void)
{
+ JNIEnv *env_ptr = getJavaEnvironment();
+
lcd_deinit();
pcm_deinit();
+
(*env_ptr)->DeleteGlobalRef(env_ptr, RockboxService_class);
}
@@ -68,8 +71,8 @@ Java_org_rockbox_RockboxService_main(JNIEnv *env, jobject this)
volatile uintptr_t stack = 0;
stackbegin = stackend = (uintptr_t*) &stack;
- env_ptr = env;
+ (*env)->GetJavaVM(env, &vm_ptr);
RockboxService_instance = (*env)->NewGlobalRef(env, this);
RockboxService_class = (*env)->NewGlobalRef(env, class);
diff --git a/firmware/target/hosted/android/system-target.h b/firmware/target/hosted/android/system-target.h
index 3938ad5fba..9145ab1e84 100644
--- a/firmware/target/hosted/android/system-target.h
+++ b/firmware/target/hosted/android/system-target.h
@@ -21,6 +21,8 @@
#ifndef __SYSTEM_TARGET_H__
#define __SYSTEM_TARGET_H__
+#include <jni.h>
+
#define disable_irq()
#define enable_irq()
#define disable_irq_save() 0
@@ -30,6 +32,20 @@ void power_off(void);
void wait_for_interrupt(void);
void interrupt(void);
+/* A JNI environment is specific to its thread, so use the correct way to
+ * obtain it: share a pointer to the JavaVM structure and ask that the JNI
+ * environment attached to the current thread. */
+static inline JNIEnv* getJavaEnvironment(void)
+{
+ extern JavaVM *vm_ptr;
+ JNIEnv *env = NULL;
+
+ if (vm_ptr)
+ (*vm_ptr)->GetEnv(vm_ptr, (void**) &env, JNI_VERSION_1_2);
+
+ return env;
+}
+
#endif /* __SYSTEM_TARGET_H__ */
#define NEED_GENERIC_BYTESWAPS
diff --git a/firmware/target/hosted/android/telephony-android.c b/firmware/target/hosted/android/telephony-android.c
index 64ad436ca7..fb2dc37623 100644
--- a/firmware/target/hosted/android/telephony-android.c
+++ b/firmware/target/hosted/android/telephony-android.c
@@ -22,14 +22,16 @@
#include <jni.h>
#include "kernel.h"
+#include "system.h"
-extern JNIEnv *env_ptr;
extern jobject RockboxService_instance;
void telephony_init_device(void)
{
+ JNIEnv *env_ptr = getJavaEnvironment();
JNIEnv e = *env_ptr;
+
jclass class = e->FindClass(env_ptr, "org/rockbox/RockboxTelephony");
jmethodID constructor = e->GetMethodID(env_ptr, class, "<init>", "(Landroid/content/Context;)V");