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)
downloadrockbox-66f2a08f8a37933b9eff79ceabdc2cb42706e48c.tar.gz
rockbox-66f2a08f8a37933b9eff79ceabdc2cb42706e48c.zip
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 @@
26#include "string-extra.h" 26#include "string-extra.h"
27#include "kernel.h" 27#include "kernel.h"
28#include "lang.h" 28#include "lang.h"
29#include "system.h"
29 30
30extern JNIEnv *env_ptr;
31static jclass RockboxKeyboardInput_class; 31static jclass RockboxKeyboardInput_class;
32static jobject RockboxKeyboardInput_instance; 32static jobject RockboxKeyboardInput_instance;
33static jmethodID kbd_inputfunc; 33static jmethodID kbd_inputfunc;
@@ -53,7 +53,9 @@ Java_org_rockbox_RockboxKeyboardInput_put_1result(JNIEnv *env, jobject this,
53 53
54static void kdb_init(void) 54static void kdb_init(void)
55{ 55{
56 JNIEnv *env_ptr = getJavaEnvironment();
56 JNIEnv e = *env_ptr; 57 JNIEnv e = *env_ptr;
58
57 static jmethodID kbd_is_usable; 59 static jmethodID kbd_is_usable;
58 if (RockboxKeyboardInput_class == NULL) 60 if (RockboxKeyboardInput_class == NULL)
59 { 61 {
@@ -87,6 +89,7 @@ static void kdb_init(void)
87 89
88int kbd_input(char* text, int buflen) 90int kbd_input(char* text, int buflen)
89{ 91{
92 JNIEnv *env_ptr = getJavaEnvironment();
90 JNIEnv e = *env_ptr; 93 JNIEnv e = *env_ptr;
91 jstring str = e->NewStringUTF(env_ptr, text); 94 jstring str = e->NewStringUTF(env_ptr, text);
92 jstring ok_text = e->NewStringUTF(env_ptr, str(LANG_KBD_OK)); 95 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 @@
29#include "misc.h" 29#include "misc.h"
30#include "thread.h" 30#include "thread.h"
31#include "debug.h" 31#include "debug.h"
32#include "system.h"
32 33
33extern JNIEnv *env_ptr;
34extern jclass RockboxService_class; 34extern jclass RockboxService_class;
35extern jobject RockboxService_instance; 35extern jobject RockboxService_instance;
36 36
@@ -47,6 +47,7 @@ static const struct dim dim = { .width = 200, .height = 200 };
47static void track_changed_callback(void *param) 47static void track_changed_callback(void *param)
48{ 48{
49 struct mp3entry* id3 = (struct mp3entry*)param; 49 struct mp3entry* id3 = (struct mp3entry*)param;
50 JNIEnv *env_ptr = getJavaEnvironment();
50 JNIEnv e = *env_ptr; 51 JNIEnv e = *env_ptr;
51 if (id3) 52 if (id3)
52 { 53 {
@@ -109,6 +110,7 @@ static void track_changed_callback(void *param)
109static void track_finished_callback(void *param) 110static void track_finished_callback(void *param)
110{ 111{
111 (void)param; 112 (void)param;
113 JNIEnv *env_ptr = getJavaEnvironment();
112 JNIEnv e = *env_ptr; 114 JNIEnv e = *env_ptr;
113 e->CallVoidMethod(env_ptr, NotificationManager_instance, 115 e->CallVoidMethod(env_ptr, NotificationManager_instance,
114 finishNotification); 116 finishNotification);
@@ -122,6 +124,7 @@ static void track_finished_callback(void *param)
122 124
123void notification_init(void) 125void notification_init(void)
124{ 126{
127 JNIEnv *env_ptr = getJavaEnvironment();
125 JNIEnv e = *env_ptr; 128 JNIEnv e = *env_ptr;
126 jfieldID nNM = e->GetFieldID(env_ptr, RockboxService_class, 129 jfieldID nNM = e->GetFieldID(env_ptr, RockboxService_class,
127 "fg_runner", "Lorg/rockbox/Helper/RunForegroundManager;"); 130 "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 @@
28#include "settings.h" 28#include "settings.h"
29#include "lang.h" 29#include "lang.h"
30#include "kernel.h" 30#include "kernel.h"
31#include "system.h"
31 32
32extern JNIEnv *env_ptr;
33static jobject RockboxYesno_instance = NULL; 33static jobject RockboxYesno_instance = NULL;
34static jmethodID yesno_func; 34static jmethodID yesno_func;
35static struct semaphore yesno_done; 35static struct semaphore yesno_done;
@@ -44,7 +44,7 @@ Java_org_rockbox_RockboxYesno_put_1result(JNIEnv *env, jobject this, jboolean re
44 semaphore_release(&yesno_done); 44 semaphore_release(&yesno_done);
45} 45}
46 46
47static void yesno_init(void) 47static void yesno_init(JNIEnv *env_ptr)
48{ 48{
49 JNIEnv e = *env_ptr; 49 JNIEnv e = *env_ptr;
50 static jmethodID yesno_is_usable; 50 static jmethodID yesno_is_usable;
@@ -75,7 +75,7 @@ static void yesno_init(void)
75 sleep(HZ/10); 75 sleep(HZ/10);
76} 76}
77 77
78jstring build_message(const struct text_message *message) 78static jstring build_message(JNIEnv *env_ptr, const struct text_message *message)
79{ 79{
80 char msg[1024] = ""; 80 char msg[1024] = "";
81 JNIEnv e = *env_ptr; 81 JNIEnv e = *env_ptr;
@@ -99,10 +99,12 @@ enum yesno_res gui_syncyesno_run(const struct text_message * main_message,
99{ 99{
100 (void)yes_message; 100 (void)yes_message;
101 (void)no_message; 101 (void)no_message;
102 yesno_init(); 102 JNIEnv *env_ptr = getJavaEnvironment();
103 103
104 yesno_init(env_ptr);
105
104 JNIEnv e = *env_ptr; 106 JNIEnv e = *env_ptr;
105 jstring message = build_message(main_message); 107 jstring message = build_message(env_ptr, main_message);
106 jstring yes = (*env_ptr)->NewStringUTF(env_ptr, str(LANG_SET_BOOL_YES)); 108 jstring yes = (*env_ptr)->NewStringUTF(env_ptr, str(LANG_SET_BOOL_YES));
107 jstring no = (*env_ptr)->NewStringUTF(env_ptr, str(LANG_SET_BOOL_NO)); 109 jstring no = (*env_ptr)->NewStringUTF(env_ptr, str(LANG_SET_BOOL_NO));
108 110
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 @@
29#include "system.h" 29#include "system.h"
30#include "touchscreen.h" 30#include "touchscreen.h"
31 31
32extern JNIEnv *env_ptr;
33static int last_y, last_x; 32static int last_y, last_x;
34static int last_btns; 33static int last_btns;
35 34
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 @@
28#include "lcd.h" 28#include "lcd.h"
29#include "button.h" 29#include "button.h"
30 30
31extern JNIEnv *env_ptr;
32extern jobject RockboxService_instance; 31extern jobject RockboxService_instance;
33 32
34static jobject RockboxFramebuffer_instance; 33static jobject RockboxFramebuffer_instance;
@@ -94,6 +93,8 @@ void connect_with_java(JNIEnv* env, jobject fb_instance)
94 93
95void lcd_deinit(void) 94void lcd_deinit(void)
96{ 95{
96 JNIEnv *env_ptr = getJavaEnvironment();
97
97 (*env_ptr)->DeleteGlobalRef(env_ptr, RockboxFramebuffer_instance); 98 (*env_ptr)->DeleteGlobalRef(env_ptr, RockboxFramebuffer_instance);
98 (*env_ptr)->DeleteGlobalRef(env_ptr, native_buffer); 99 (*env_ptr)->DeleteGlobalRef(env_ptr, native_buffer);
99} 100}
@@ -107,6 +108,8 @@ void lcd_init_device(void)
107 108
108void lcd_update(void) 109void lcd_update(void)
109{ 110{
111 JNIEnv *env_ptr = getJavaEnvironment();
112
110 if (display_on) 113 if (display_on)
111 (*env_ptr)->CallVoidMethod(env_ptr, RockboxFramebuffer_instance, 114 (*env_ptr)->CallVoidMethod(env_ptr, RockboxFramebuffer_instance,
112 java_lcd_update); 115 java_lcd_update);
@@ -114,6 +117,8 @@ void lcd_update(void)
114 117
115void lcd_update_rect(int x, int y, int width, int height) 118void lcd_update_rect(int x, int y, int width, int height)
116{ 119{
120 JNIEnv *env_ptr = getJavaEnvironment();
121
117 if (display_on) 122 if (display_on)
118 (*env_ptr)->CallVoidMethod(env_ptr, RockboxFramebuffer_instance, 123 (*env_ptr)->CallVoidMethod(env_ptr, RockboxFramebuffer_instance,
119 java_lcd_update_rect, x, y, width, height); 124 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 @@
25#include "debug.h" 25#include "debug.h"
26#include "pcm.h" 26#include "pcm.h"
27 27
28extern JNIEnv *env_ptr;
29
30/* infos about our pcm chunks */ 28/* infos about our pcm chunks */
31static size_t pcm_data_size; 29static size_t pcm_data_size;
32static char *pcm_data_start; 30static char *pcm_data_start;
@@ -117,6 +115,8 @@ void pcm_play_dma_start(const void *addr, size_t size)
117 115
118void pcm_play_dma_stop(void) 116void pcm_play_dma_stop(void)
119{ 117{
118 JNIEnv *env_ptr = getJavaEnvironment();
119
120 (*env_ptr)->CallVoidMethod(env_ptr, 120 (*env_ptr)->CallVoidMethod(env_ptr,
121 RockboxPCM_instance, 121 RockboxPCM_instance,
122 stop_method); 122 stop_method);
@@ -124,6 +124,8 @@ void pcm_play_dma_stop(void)
124 124
125void pcm_play_dma_pause(bool pause) 125void pcm_play_dma_pause(bool pause)
126{ 126{
127 JNIEnv *env_ptr = getJavaEnvironment();
128
127 (*env_ptr)->CallVoidMethod(env_ptr, 129 (*env_ptr)->CallVoidMethod(env_ptr,
128 RockboxPCM_instance, 130 RockboxPCM_instance,
129 play_pause_method, 131 play_pause_method,
@@ -152,6 +154,7 @@ void pcm_play_dma_init(void)
152 * Luckily we only reference the PCM object from here, so it's safe (and 154 * Luckily we only reference the PCM object from here, so it's safe (and
153 * clean) to allocate it here 155 * clean) to allocate it here
154 **/ 156 **/
157 JNIEnv *env_ptr = getJavaEnvironment();
155 JNIEnv e = *env_ptr; 158 JNIEnv e = *env_ptr;
156 /* get the class and its constructor */ 159 /* get the class and its constructor */
157 jclass RockboxPCM_class = e->FindClass(env_ptr, "org/rockbox/RockboxPCM"); 160 jclass RockboxPCM_class = e->FindClass(env_ptr, "org/rockbox/RockboxPCM");
@@ -169,6 +172,8 @@ void pcm_play_dma_init(void)
169 172
170void pcm_deinit(void) 173void pcm_deinit(void)
171{ 174{
175 JNIEnv *env_ptr = getJavaEnvironment();
176
172 (*env_ptr)->DeleteGlobalRef(env_ptr, RockboxPCM_instance); 177 (*env_ptr)->DeleteGlobalRef(env_ptr, RockboxPCM_instance);
173} 178}
174 179
@@ -178,5 +183,7 @@ void pcm_postinit(void)
178 183
179void pcm_set_mixer_volume(int volume) 184void pcm_set_mixer_volume(int volume)
180{ 185{
186 JNIEnv *env_ptr = getJavaEnvironment();
187
181 (*env_ptr)->CallVoidMethod(env_ptr, RockboxPCM_instance, set_volume_method, volume); 188 (*env_ptr)->CallVoidMethod(env_ptr, RockboxPCM_instance, set_volume_method, volume);
182} 189}
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 @@
23#include <jni.h> 23#include <jni.h>
24#include <stdbool.h> 24#include <stdbool.h>
25#include "config.h" 25#include "config.h"
26#include "system.h"
26 27
27extern JNIEnv *env_ptr;
28extern jclass RockboxService_class; 28extern jclass RockboxService_class;
29extern jobject RockboxService_instance; 29extern jobject RockboxService_instance;
30 30
@@ -32,6 +32,8 @@ static jfieldID _battery_level;
32 32
33void powermgmt_init_target(void) 33void powermgmt_init_target(void)
34{ 34{
35 JNIEnv *env_ptr = getJavaEnvironment();
36
35 jmethodID initBatteryMonitor = (*env_ptr)->GetMethodID(env_ptr, 37 jmethodID initBatteryMonitor = (*env_ptr)->GetMethodID(env_ptr,
36 RockboxService_class, 38 RockboxService_class,
37 "initBatteryMonitor", 39 "initBatteryMonitor",
@@ -50,6 +52,8 @@ void powermgmt_init_target(void)
50 52
51int battery_level(void) 53int battery_level(void)
52{ 54{
55 JNIEnv *env_ptr = getJavaEnvironment();
56
53 return (*env_ptr)->GetIntField(env_ptr, RockboxService_instance, _battery_level); 57 return (*env_ptr)->GetIntField(env_ptr, RockboxService_instance, _battery_level);
54} 58}
55 59
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 @@
27 27
28 28
29/* global fields for use with various JNI calls */ 29/* global fields for use with various JNI calls */
30JNIEnv *env_ptr; 30JavaVM *vm_ptr;
31jobject RockboxService_instance; 31jobject RockboxService_instance;
32jclass RockboxService_class; 32jclass RockboxService_class;
33 33
@@ -44,8 +44,11 @@ void system_reboot(void) { }
44 44
45void power_off(void) 45void power_off(void)
46{ 46{
47 JNIEnv *env_ptr = getJavaEnvironment();
48
47 lcd_deinit(); 49 lcd_deinit();
48 pcm_deinit(); 50 pcm_deinit();
51
49 (*env_ptr)->DeleteGlobalRef(env_ptr, RockboxService_class); 52 (*env_ptr)->DeleteGlobalRef(env_ptr, RockboxService_class);
50} 53}
51 54
@@ -68,8 +71,8 @@ Java_org_rockbox_RockboxService_main(JNIEnv *env, jobject this)
68 71
69 volatile uintptr_t stack = 0; 72 volatile uintptr_t stack = 0;
70 stackbegin = stackend = (uintptr_t*) &stack; 73 stackbegin = stackend = (uintptr_t*) &stack;
71 env_ptr = env;
72 74
75 (*env)->GetJavaVM(env, &vm_ptr);
73 RockboxService_instance = (*env)->NewGlobalRef(env, this); 76 RockboxService_instance = (*env)->NewGlobalRef(env, this);
74 RockboxService_class = (*env)->NewGlobalRef(env, class); 77 RockboxService_class = (*env)->NewGlobalRef(env, class);
75 78
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 @@
21#ifndef __SYSTEM_TARGET_H__ 21#ifndef __SYSTEM_TARGET_H__
22#define __SYSTEM_TARGET_H__ 22#define __SYSTEM_TARGET_H__
23 23
24#include <jni.h>
25
24#define disable_irq() 26#define disable_irq()
25#define enable_irq() 27#define enable_irq()
26#define disable_irq_save() 0 28#define disable_irq_save() 0
@@ -30,6 +32,20 @@ void power_off(void);
30void wait_for_interrupt(void); 32void wait_for_interrupt(void);
31void interrupt(void); 33void interrupt(void);
32 34
35/* A JNI environment is specific to its thread, so use the correct way to
36 * obtain it: share a pointer to the JavaVM structure and ask that the JNI
37 * environment attached to the current thread. */
38static inline JNIEnv* getJavaEnvironment(void)
39{
40 extern JavaVM *vm_ptr;
41 JNIEnv *env = NULL;
42
43 if (vm_ptr)
44 (*vm_ptr)->GetEnv(vm_ptr, (void**) &env, JNI_VERSION_1_2);
45
46 return env;
47}
48
33#endif /* __SYSTEM_TARGET_H__ */ 49#endif /* __SYSTEM_TARGET_H__ */
34 50
35#define NEED_GENERIC_BYTESWAPS 51#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 @@
22 22
23#include <jni.h> 23#include <jni.h>
24#include "kernel.h" 24#include "kernel.h"
25#include "system.h"
25 26
26extern JNIEnv *env_ptr;
27extern jobject RockboxService_instance; 27extern jobject RockboxService_instance;
28 28
29 29
30void telephony_init_device(void) 30void telephony_init_device(void)
31{ 31{
32 JNIEnv *env_ptr = getJavaEnvironment();
32 JNIEnv e = *env_ptr; 33 JNIEnv e = *env_ptr;
34
33 jclass class = e->FindClass(env_ptr, "org/rockbox/RockboxTelephony"); 35 jclass class = e->FindClass(env_ptr, "org/rockbox/RockboxTelephony");
34 jmethodID constructor = e->GetMethodID(env_ptr, class, "<init>", "(Landroid/content/Context;)V"); 36 jmethodID constructor = e->GetMethodID(env_ptr, class, "<init>", "(Landroid/content/Context;)V");
35 37