Java绿色线程通过JNI使用UNIX ucontext库。可能吗?

时间:2012-07-30 16:33:47

标签: java java-native-interface green-threads

我正在开发一个使用ucontext的绿色(协作)基于量子线程的简单教育示例。但是我遇到了问题。下面提供的示例很容易理解,我会感谢您提供给我的任何帮助:

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

代码创建kernelmain上下文,并准备umain方法作为新用户线程。 swap切换上下文,保存内核并运行main。示例应该打印:

umain

和下一个

主要

但在(*env)->CallStaticVoidMethod(env, cls, mid)执行Java回调时会出现段错误。这种代码很容易用C语言编写,但在尝试用Java开发时我遇到了一些细微的问题。

Apache的javaflow不是一个选项,因为我发现使用时间量程(使用SIGALRM)开发调度程序非常困难。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您不应该尝试将jmethodID作为全局参考。它是一个方法ID而不是参考。编译器应该抱怨这一点。尝试将方法ID全局保留在模块中。

相关问题