1

我正在使用 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。示例应打印:

接下来

主要的

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

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

有什么想法吗?

4

1 回答 1

0

您不应该尝试创建jmethodID一个全局参考。它是一个方法 ID,而不是参考。编译器应该抱怨这一点。尝试将方法 id 全局保存在您的模块中。

于 2012-08-06T16:18:21.073 回答