Skip to content

Commit

Permalink
jni: Allow other threads to call setLogLevel()
Browse files Browse the repository at this point in the history
It is useful to allow the user to toggle PRG_TRACE logging on an active
connection for debugging purposes.  But currently this would involve
releasing the class lock held by the mainloop.  So we will use asyncLock
to protect the shared variable.

Timings conducted on a Nexus 7 (2012) show that the new
MonitorEnter/MonitorExit pair adds about 700ns to each progress_cb (which
is called on each packet).  This isn't great, but it's probably small
enough to make it worth doing things the right way (using a lock) instead
of just declaring loglevel as volatile.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
  • Loading branch information
cernekee committed Apr 26, 2014
1 parent 3e5f76c commit e0a43fb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion java/src/org/infradead/libopenconnect/LibOpenConnect.java
Expand Up @@ -92,6 +92,7 @@ public boolean isCanceled() {

public native void pause();
public native void requestStats();
public native void setLogLevel(int level);

/* control operations */

Expand All @@ -108,7 +109,6 @@ public boolean isCanceled() {

/* connection settings */

public synchronized native void setLogLevel(int level);
public synchronized native int passphraseFromFSID();
public synchronized native void setCertExpiryWarning(int seconds);
public synchronized native int setHTTPProxy(String proxy);
Expand Down
11 changes: 9 additions & 2 deletions jni.c
Expand Up @@ -488,10 +488,14 @@ static void progress_cb(void *privdata, int level, const char *fmt, ...)
va_list ap;
char *msg;
jstring jmsg;
int ret;
int ret, loglevel;
jmethodID mid;

if (level > ctx->loglevel)
(*ctx->jenv)->MonitorEnter(ctx->jenv, ctx->async_lock);
loglevel = ctx->loglevel;
(*ctx->jenv)->MonitorExit(ctx->jenv, ctx->async_lock);

if (level > loglevel)
return;

va_start(ap, fmt);
Expand Down Expand Up @@ -930,7 +934,10 @@ JNIEXPORT void JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_setLogLe

if (!ctx)
return;

(*ctx->jenv)->MonitorEnter(ctx->jenv, ctx->async_lock);
ctx->loglevel = arg;
(*ctx->jenv)->MonitorExit(ctx->jenv, ctx->async_lock);
}

JNIEXPORT jint JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_setupTunFD(
Expand Down

0 comments on commit e0a43fb

Please sign in to comment.