From 07635ede37c13e81c8cd92c9b92907f0c2e05fdc Mon Sep 17 00:00:00 2001 From: Andy2 Date: Sat, 10 Dec 2011 08:42:22 -0800 Subject: [PATCH] add ability to delete a dict from a thread other than the one that created it -- by making env something that can be set rather than always pulled out of the dict jni struct. --- xwords4/android/XWords4/jni/anddict.c | 7 +++ xwords4/android/XWords4/jni/anddict.h | 2 + xwords4/android/XWords4/jni/jniutlswrapper.c | 3 +- xwords4/android/XWords4/jni/jniutlswrapper.h | 4 +- xwords4/android/XWords4/jni/xwjni.c | 51 +++++++++++++------ .../org/eehouse/android/xw4/jni/XwJNI.java | 1 + 6 files changed, 48 insertions(+), 20 deletions(-) diff --git a/xwords4/android/XWords4/jni/anddict.c b/xwords4/android/XWords4/jni/anddict.c index 2f986efd2..da9da772e 100644 --- a/xwords4/android/XWords4/jni/anddict.c +++ b/xwords4/android/XWords4/jni/anddict.c @@ -592,3 +592,10 @@ destroyDicts( PlayerDicts* dicts ) } } } + +void +setDictEnv( DictionaryCtxt* dict, JNIEnv* env ) +{ + AndDictionaryCtxt* anddict = (AndDictionaryCtxt*)dict; + anddict->env = env; +} diff --git a/xwords4/android/XWords4/jni/anddict.h b/xwords4/android/XWords4/jni/anddict.h index 4ab9987b0..595dfae7f 100644 --- a/xwords4/android/XWords4/jni/anddict.h +++ b/xwords4/android/XWords4/jni/anddict.h @@ -39,6 +39,8 @@ void makeDicts( MPFORMAL JNIEnv *env, JNIUtilCtxt* jniutil, void destroyDicts( PlayerDicts* dicts ); +void setDictEnv( DictionaryCtxt* dict, JNIEnv* env ); + DictionaryCtxt* and_dictionary_make_empty( MPFORMAL JNIEnv *env, JNIUtilCtxt* jniutil ); diff --git a/xwords4/android/XWords4/jni/jniutlswrapper.c b/xwords4/android/XWords4/jni/jniutlswrapper.c index 0c681b369..bfa03d78c 100644 --- a/xwords4/android/XWords4/jni/jniutlswrapper.c +++ b/xwords4/android/XWords4/jni/jniutlswrapper.c @@ -41,11 +41,10 @@ makeJNIUtil( MPFORMAL JNIEnv** envp, jobject jniutls ) } void -destroyJNIUtil( JNIUtilCtxt** ctxtp ) +destroyJNIUtil( JNIUtilCtxt** ctxtp, JNIEnv* env ) { JNIUtilCtxt* ctxt = *ctxtp; if ( !!ctxt ) { - JNIEnv* env = *ctxt->envp; (*env)->DeleteGlobalRef( env, ctxt->jjniutil ); XP_FREE( ctxt->mpool, ctxt ); *ctxtp = NULL; diff --git a/xwords4/android/XWords4/jni/jniutlswrapper.h b/xwords4/android/XWords4/jni/jniutlswrapper.h index 4e0dfea9a..1b2d65f96 100644 --- a/xwords4/android/XWords4/jni/jniutlswrapper.h +++ b/xwords4/android/XWords4/jni/jniutlswrapper.h @@ -1,4 +1,4 @@ -/* -*-mode: C; fill-column: 76; c-basic-offset: 4; -*- */ +/* -*-mode: C; compile-command: "../../scripts/ndkbuild.sh"; -*- */ /* * Copyright 2001-2010 by Eric House (xwords@eehouse.org). All rights * reserved. @@ -29,7 +29,7 @@ typedef struct JNIUtilCtxt JNIUtilCtxt; JNIUtilCtxt* makeJNIUtil( MPFORMAL JNIEnv** env, jobject jniutls ); -void destroyJNIUtil( JNIUtilCtxt** jniu ); +void destroyJNIUtil( JNIUtilCtxt** jniu, JNIEnv* env ); jobject and_util_makeJBitmap( JNIUtilCtxt* jniu, int nCols, int nRows, const jboolean* colors ); diff --git a/xwords4/android/XWords4/jni/xwjni.c b/xwords4/android/XWords4/jni/xwjni.c index 5f8a2b99f..2af11ea2f 100644 --- a/xwords4/android/XWords4/jni/xwjni.c +++ b/xwords4/android/XWords4/jni/xwjni.c @@ -290,7 +290,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_dict_1getInfo dict_destroy( dict ); result = true; } - destroyJNIUtil( &jniutil ); + destroyJNIUtil( &jniutil, env ); #ifdef MEM_DEBUG mpool_destroy( mpool ); @@ -447,7 +447,7 @@ JNIEXPORT void JNICALL Java_org_eehouse_android_xw4_jni_XwJNI_game_1dispose destroyDraw( &globals->dctx ); destroyXportProcs( &globals->xportProcs ); destroyUtil( &globals->util ); - destroyJNIUtil( &globals->jniutil ); + destroyJNIUtil( &globals->jniutil, env ); vtmgr_destroy( MPPARM(mpool) globals->vtMgr ); state->env = oldEnv; @@ -498,7 +498,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeFromStream destroyDicts( &dicts ); dict_destroy( dict ); destroyUtil( &globals->util ); - destroyJNIUtil( &globals->jniutil ); + destroyJNIUtil( &globals->jniutil, env ); destroyGI( MPPARM(mpool) &globals->gi ); } @@ -1305,7 +1305,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_dict_1iter_1init #endif closure = (int)data; } else { - destroyJNIUtil( &jniutil ); + destroyJNIUtil( &jniutil, env ); XP_FREE( mpool, data ); #ifdef MEM_DEBUG mpool_destroy( mpool ); @@ -1371,29 +1371,48 @@ Java_org_eehouse_android_xw4_jni_XwJNI_dict_1iter_1setMinMax } } +static void +destroy_dict( JNIEnv* env, DictIterData* data ) +{ +#ifdef MEM_DEBUG + MemPoolCtx* mpool = data->mpool; +#endif + dict_destroy( data->dict ); + destroyJNIUtil( &data->jniutil, env ); + freeIndices( data ); + vtmgr_destroy( MPPARM(mpool) data->vtMgr ); + XP_FREE( mpool, data ); +#ifdef MEM_DEBUG + mpool_destroy( mpool ); +#endif +} + JNIEXPORT void JNICALL Java_org_eehouse_android_xw4_jni_XwJNI_dict_1iter_1destroy ( JNIEnv* env, jclass C, jint closure ) { DictIterData* data = (DictIterData*)closure; if ( NULL != data ) { -#ifdef MEM_DEBUG - MemPoolCtx* mpool = data->mpool; -#endif - dict_destroy( data->dict ); - destroyJNIUtil( &data->jniutil ); - freeIndices( data ); - vtmgr_destroy( MPPARM(mpool) data->vtMgr ); - XP_FREE( mpool, data ); -#ifdef MEM_DEBUG - mpool_destroy( mpool ); -#endif + destroy_dict( env, data ); } } +JNIEXPORT void JNICALL +Java_org_eehouse_android_xw4_jni_XwJNI_dict_1iter_1backDestroy +( JNIEnv* env, jclass C, jint closure ) +{ + LOG_FUNC(); + DictIterData* data = (DictIterData*)closure; + if ( NULL != data ) { + setDictEnv( data->dict, env ); + destroy_dict( env, data ); + } + LOG_RETURN_VOID(); +} + JNIEXPORT jint JNICALL Java_org_eehouse_android_xw4_jni_XwJNI_dict_1iter_1wordCount -(JNIEnv* env, jclass C, jint closure ) +( JNIEnv* env, jclass C, jint closure ) { jint result = 0; DictIterData* data = (DictIterData*)closure; diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/XwJNI.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/XwJNI.java index 1e83cd137..2f5ba31cd 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/XwJNI.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/XwJNI.java @@ -243,6 +243,7 @@ public class XwJNI { public static native void dict_iter_setMinMax( int closure, int min, int max ); public static native void dict_iter_destroy( int closure ); + public static native void dict_iter_backDestroy( int closure ); public static native int dict_iter_wordCount( int closure ); public static native int[] dict_iter_getCounts( int closure ); public static native String dict_iter_nthWord( int closure, int nn );