Java green threads using UNIX ucontext library via JNI. Is it possible?

314 views Asked by At

I'm developing a simple, educational example of green (cooperative) quantum-based threads using ucontext. But I'm facing problems. The example provided below is very easy to follow amd I'll thank any help you can provide me with:

TestApp.java:

public class TestApp extends UThreadApp {
    public static void umain() {
        System.out.println("umain");
    }
}

UThreadApp.java:

import java.io.*;

public class UThreadApp {
    public static void main(String[] args) {    
        long kernel = newContext();
        long umain = newUThread("TestApp", "umain");
        swap(kernel, umain);
        System.out.println("main");
    }

    native static long newContext();
    native static void swap(long oldc, long newc);
    native static long newUThread(String className, String method);

    static {
        System.loadLibrary("uthread");
    }
}

UThreadApp.c:

#include <stdlib.h>
#include <ucontext.h>

#include "UThreadApp.h"

typedef struct {
    ucontext_t context;
    unsigned char stack[SIGSTKSZ];
} *Context;

ucontext_t *kernelContext;

/*
 * Class:     UThreadApp
 * Method:    newContext
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_UThreadApp_newContext
  (JNIEnv *env, jclass cls) {
    kernelContext = malloc(sizeof(ucontext_t));
    if (kernelContext == NULL)
        RTError("newContext: malloc: couldn't allocate memory for kernelContext");
    return (jlong) kernelContext;
}

/*
 * Class:     UThreadApp
 * Method:    swap
 * Signature: (JJ)V
 */
JNIEXPORT void JNICALL Java_UThreadApp_swap
  (JNIEnv *env, jclass cls, jlong oldc, jlong newc) {
    if (swapcontext((ucontext_t *)oldc, (ucontext_t *)newc) == -1)
        SysError("swap: swapcontext: couldn't switch context");
}

static JavaVM *jvm = NULL;

static void callback(jclass cls, jmethodID mid) {
    if (jvm == NULL)
        return;

    JNIEnv *env = NULL;
    jint res;
    res = (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
    if (res < 0)
        RTError("Attach VM Thread failed");

    /* JVM segfaults here */
    (*env)->CallStaticVoidMethod(env, cls, mid);
    //printf("umain\n");

    (*jvm)->DetachCurrentThread(jvm);
}

typedef void (*uthread_func)();

/*
 * Class:     UThreadApp
 * Method:    newUThread
 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
 */
JNIEXPORT jlong JNICALL Java_UThreadApp_newUThread
  (JNIEnv *env, jclass cls, jstring className, jstring method) {

    /* find the current jvm */
    (*env)->GetJavaVM(env, &jvm);

    Context ctx = malloc(sizeof(*ctx));
    if (getcontext((ucontext_t *)ctx) == -1)
        RTError("newUThread: getcontext");
    sigdelset(&ctx->context.uc_sigmask, SIGALRM);
    ctx->context.uc_link = kernelContext;
    ctx->context.uc_stack.ss_sp = ctx->stack;
    ctx->context.uc_stack.ss_size = SIGSTKSZ;

    const char *str;
    str = (*env)->GetStringUTFChars(env, className, NULL);
    if (str == NULL)
        RTError("newUThread: className: GetStringUTFChars");
    jclass kls = (*env)->FindClass(env, str);
    if (kls == NULL)
        RTError("newUThread: FindClass: class not found");
    (*env)->ReleaseStringUTFChars(env, className, str);

    str = (*env)->GetStringUTFChars(env, method, NULL);
    if (str == NULL)
        RTError("newUThread: method: GetStringUTFChars");
    jmethodID mid = (*env)->GetStaticMethodID(env, kls, str, "()V");
    if (mid == NULL) {
        RTError("newUThread: GetStaticMethodID: method not found");
    }
    (*env)->ReleaseStringUTFChars(env, method, str);

    /* make global references */
    jclass gkls = (*env)->NewGlobalRef(env, kls);
    jmethodID gmid = (*env)->NewGlobalRef(env, mid);

    makecontext((ucontext_t *)ctx, (uthread_func)callback, 2, gkls, gmid);

    return (jlong) ctx;
}

Code creates kernel and main contexts and prepares umain method as new user thread. swap switches contexts, saves kernel and runs main. Example should print:

umain

and next

main

but segfaults when executing Java callback at (*env)->CallStaticVoidMethod(env, cls, mid). This kind of code is easy to program in C but I'm facing subtle problems when trying to develop it in Java.

Apache's javaflow isn't an option because I find it extremely difficult to develop a scheduler with time-quantum (using SIGALRM).

Any thoughts?

1

There are 1 answers

0
maba On

You should not try to make a jmethodID a global ref. It is a method ID not a reference. The compiler should complain about that. Try to keep the method id globally in your module instead.