从本机pthread调用Java方法时的SIGSEGV

时间:2015-06-20 12:28:32

标签: java c multithreading java-native-interface pthreads

在通过JNI使用C代码的Java项目中,我有一段本机C代码,它获取对象及其方法之一的引用,然后启动本机线程,在结构中将这些引用传递给它。当线程尝试调用该方法时,代码将与SIGSEGV崩溃。从主线程调用相同的方法。

进行一些研究我了解到env引用仅在线程内有效,并且必须首先附加任何其他本机线程。我做了这个,但代码仍然在第一次调用方法时崩溃。

奇怪的是,当我在创建另一个线程之前从主线程调用相同的方法(只是取消注释主代码中的行)时,事情会有一段时间。输出线程在崩溃之前循环了大约10,000次。

方法调用是DataOutputStream.writeShort()。有问题的帖子是写DataOutputStream的唯一一个。但是,DataOutputStream已连接到DataInputStream

简化的原生代码:

static void write_output(struct output_state *s) {
    int i;
    jint sample;
    for (i = 0; i < 2 * s->result_len; i += 2) {
        sample = (s->result[i] << 8) + s->result[i+1];
        (*(s->env))->CallVoidMethod(s->env, s->tunerOut, s->writeShort, sample);
    }
}

static void *output_thread_fn(void *arg)
{
    struct output_state *s = arg;
    (*(s->jvm))->AttachCurrentThread(s->jvm, &(s->env), NULL);
    while (!do_exit) {
        // use timedwait and pad out under runs
        safe_cond_wait(&s->ready, &s->ready_m);
        pthread_rwlock_rdlock(&s->rw); //sync access to s with producer thread
        write_output(s);
        pthread_rwlock_unlock(&s->rw);
    }
    (*(s->jvm))->DetachCurrentThread(s->jvm);
    return 0;
}

JNIEXPORT jboolean JNICALL Java_eu_jacquet80_rds_input_SdrGroupReader_open
  (JNIEnv *env, jobject self) {
    jclass clsSelf = (*env)->GetObjectClass(env, self);
    jfieldID fTunerOut = (*env)->GetFieldID(env, clsSelf, "tunerOut", "Ljava/io/DataOutputStream;");
    jobject tunerOut = (*env)->GetObjectField(env, self, fTunerOut);
    jclass cls = (*env)->GetObjectClass(env, tunerOut);
    jmethodID writeShortID = (*env)->GetMethodID(env, cls, "writeShort", "(I)V");

    if (!writeShortID || !cls)
        return 0;

    (*env)->GetJavaVM(env, &(output.jvm));
    output.tunerOut = tunerOut;
    output.writeShort = writeShortID;

    // (*env)->CallVoidMethod(env, tunerOut, writeShortID, 0); // just for testing

    pthread_create(&controller.thread, NULL, controller_thread_fn, (void *)(&controller));
    usleep(100000);
    pthread_create(&output.thread, NULL, output_thread_fn, (void *)(&output));
    pthread_create(&demod.thread, NULL, demod_thread_fn, (void *)(&demod));
    pthread_create(&dongle.thread, NULL, dongle_thread_fn, (void *)(&dongle));

    return 1;
}

JNI引用(例如jclassjfieldIDjobjectjmethodID是否与JNIEnv具有相同的限制,即仅在同一线程内有效?

怀疑这一点,我在调用open()后立即将JNI参考资料从output_thread()移至AttachCurrentThread()。但是,我仍需要跨越线程边框传递jobject引用(self),并且对GetObjectClass()的调用崩溃。

创建线程本机代码并让该线程调用给定类实例的特定方法的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

证明我的怀疑是正确的:jobjectjclass引用确实是本地的,即仅在同一线程内有效,并且只有当前的本机方法返回时才有效。请参阅http://developer.android.com/training/articles/perf-jni.html#local_and_global_references

我将引用相关代码移动到线程函数的方法是正确的,但我需要先调用selfNewGlobalRef()转换为全局引用。

更新的代码:

static void write_output(struct output_state *s) {
    int i;
    jint sample;
    for (i = 0; i < 2 * s->result_len; i += 2) {
        sample = (s->result[i] << 8) + s->result[i+1];
        (*(s->env))->CallVoidMethod(s->env, s->tunerOut, s->writeShort, sample);
    }
}

static void *output_thread_fn(void *arg)
{
    struct output_state *s = arg;
    (*(s->jvm))->AttachCurrentThread(s->jvm, &(s->env), NULL);
    jclass clsSelf = (*(s->env))->GetObjectClass(s->env, s->self);
    jfieldID fTunerOut = (*(s->env))->GetFieldID(s->env, clsSelf, "tunerOut", "Ljava/io/DataOutputStream;");
    s->tunerOut = (*(s->env))->GetObjectField(s->env, s->self, fTunerOut);
    jclass cls = (*(s->env))->GetObjectClass(s->env, s->tunerOut);
    s->writeShort = (*(s->env))->GetMethodID(s->env, cls, "writeShort", "(I)V");
    while (!do_exit) {
        // use timedwait and pad out under runs
        safe_cond_wait(&s->ready, &s->ready_m);
        pthread_rwlock_rdlock(&s->rw); //sync access to s with producer thread
        write_output(s);
        pthread_rwlock_unlock(&s->rw);
    }
    (*(s->jvm))->DetachCurrentThread(s->jvm);
    return 0;
}

JNIEXPORT jboolean JNICALL Java_eu_jacquet80_rds_input_SdrGroupReader_open
  (JNIEnv *env, jobject self) {
    jclass clsSelf = (*env)->GetObjectClass(env, self);

    if (!writeShortID || !cls)
        return 0;

    output.self = (*env)->NewGlobalRef(env, self);
    (*env)->GetJavaVM(env, &(output.jvm));

    (*env)->CallVoidMethod(env, tunerOut, writeShortID, 0); // just for testing

    pthread_create(&controller.thread, NULL, controller_thread_fn, (void *)(&controller));
    usleep(100000);
    pthread_create(&output.thread, NULL, output_thread_fn, (void *)(&output));
    pthread_create(&demod.thread, NULL, demod_thread_fn, (void *)(&demod));
    pthread_create(&dongle.thread, NULL, dongle_thread_fn, (void *)(&dongle));

    return 1;
}

还有一件事是在输出线程完成时调用DeleteGlobalRef()。这是为了确保在不再需要时释放全局引用,以便垃圾收集器可以获取它。