Skip to content

Commit

Permalink
jni: Change cancelLock so it can be used from native code
Browse files Browse the repository at this point in the history
Rename cancelLock to asyncLock and store a global reference so it can
be acquired by various JNI functions.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
  • Loading branch information
cernekee committed Apr 26, 2014
1 parent 36b60c0 commit 3e5f76c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions java/src/org/infradead/libopenconnect/LibOpenConnect.java
Expand Up @@ -76,7 +76,7 @@ public synchronized void destroy() {
/* async requests (safe to call from any thread) */

public void cancel() {
synchronized (cancelLock) {
synchronized (asyncLock) {
if (!canceled) {
doCancel();
canceled = true;
Expand All @@ -85,7 +85,7 @@ public void cancel() {
}

public boolean isCanceled() {
synchronized (cancelLock) {
synchronized (asyncLock) {
return canceled;
}
}
Expand Down Expand Up @@ -255,7 +255,7 @@ public static class VPNStats {

long libctx;
boolean canceled = false;
Object cancelLock = new Object();
Object asyncLock = new Object();

static synchronized native void globalInit();
static {
Expand Down
18 changes: 18 additions & 0 deletions jni.c
Expand Up @@ -28,6 +28,7 @@
struct libctx {
JNIEnv *jenv;
jobject jobj;
jobject async_lock;
struct openconnect_info *vpninfo;
OPENCONNECT_X509 *cert;
int cmd_fd;
Expand Down Expand Up @@ -520,6 +521,17 @@ static void progress_cb(void *privdata, int level, const char *fmt, ...)

/* Library init/uninit */

static jobject init_async_lock(struct libctx *ctx)
{
jclass jcls = (*ctx->jenv)->GetObjectClass(ctx->jenv, ctx->jobj);
jfieldID jfld = (*ctx->jenv)->GetFieldID(ctx->jenv, jcls, "asyncLock", "Ljava/lang/Object;");
jobject jobj = (*ctx->jenv)->GetObjectField(ctx->jenv, ctx->jobj, jfld);

if (jobj)
jobj = (*ctx->jenv)->NewGlobalRef(ctx->jenv, jobj);
return jobj;
}

JNIEXPORT jlong JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_init(
JNIEnv *jenv, jobject jobj, jstring juseragent)
{
Expand All @@ -533,6 +545,9 @@ JNIEXPORT jlong JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_init(
ctx->jobj = (*jenv)->NewGlobalRef(jenv, jobj);
if (!ctx->jobj)
goto bad_free_ctx;
ctx->async_lock = init_async_lock(ctx);
if (!ctx->async_lock)
goto bad_delete_obj_ref;

useragent = (char *)(*jenv)->GetStringUTFChars(jenv, juseragent, NULL);
if (!useragent)
Expand All @@ -559,6 +574,8 @@ JNIEXPORT jlong JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_init(
bad_free_vpninfo:
openconnect_vpninfo_free(ctx->vpninfo);
bad_delete_ref:
(*jenv)->DeleteGlobalRef(jenv, ctx->async_lock);
bad_delete_obj_ref:
(*jenv)->DeleteGlobalRef(jenv, ctx->jobj);
bad_free_ctx:
free(ctx);
Expand All @@ -575,6 +592,7 @@ JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_free(
if (!ctx)
return;
openconnect_vpninfo_free(ctx->vpninfo);
(*jenv)->DeleteGlobalRef(jenv, ctx->async_lock);
(*jenv)->DeleteGlobalRef(jenv, ctx->jobj);
free(ctx);
}
Expand Down

0 comments on commit 3e5f76c

Please sign in to comment.