1
0
Fork 0
forked from len0rd/rockbox

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
This commit is contained in:
Maurus Cuelenaere 2011-03-11 18:34:35 +00:00
parent 2e5b7aebde
commit 66f2a08f8a
10 changed files with 60 additions and 16 deletions

View file

@ -29,7 +29,6 @@
#include "system.h"
#include "touchscreen.h"
extern JNIEnv *env_ptr;
static int last_y, last_x;
static int last_btns;

View file

@ -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);

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);

View file

@ -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

View file

@ -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");