Skip to content

Commit

Permalink
support thread.interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
guoyiteng committed Dec 18, 2019
1 parent f53e498 commit 9c28b84
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
5 changes: 5 additions & 0 deletions runtime/native/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void Polyglot_jlang_runtime_Exceptions_throwNewThrowable__Ljava_lang_Class_2Ljav
jclass clazz, jstring str);
void Polyglot_jlang_runtime_Exceptions_throwThrowable__Ljava_lang_Throwable_2(
jthrowable obj);
void Polyglot_jlang_runtime_Exceptions_throwInterruptedException__();
// A distinct integer identifying our own exceptions.
const uint64_t javaExceptionClass = 8101813523428701805ll;

Expand Down Expand Up @@ -520,3 +521,7 @@ void throwThrowable(JNIEnv *env, jthrowable obj) {
Polyglot_jlang_runtime_Exceptions_throwThrowable__Ljava_lang_Throwable_2(
obj);
}

void throwInterruptedException(JNIEnv *env) {
Polyglot_jlang_runtime_Exceptions_throwInterruptedException__();
}
1 change: 1 addition & 0 deletions runtime/native/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
void throwClassNotFoundException(JNIEnv *env, const char *name);
void throwNewThrowable(JNIEnv *env, jclass clazz, const char *msg);
void throwThrowable(JNIEnv *env, jthrowable obj);
void throwInterruptedException(JNIEnv *env);

extern "C" {

Expand Down
16 changes: 9 additions & 7 deletions runtime/native/jvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ jstring internJString(jstring str) {
}
}

void throwInterruptedException(JNIEnv *env, const char* msg) {
jclass clazz = FindClass("java.lang.InterruptedException");
throwNewThrowable(env, clazz, msg);
}

extern "C" {
// we copied this number from open JDK -> not sure the implications
#define JVM_INTERFACE_VERSION 4
Expand All @@ -128,6 +123,11 @@ jint JVM_IHashCode(JNIEnv *env, jobject obj) {
}

void JVM_MonitorWait(JNIEnv *env, jobject obj, jlong ms) {
// check interrupted before waiting
if (Threads::Instance().threads[currentThread].interrupted) {
Threads::Instance().threads[currentThread].interrupted = false;
throwInterruptedException(env);
}
Monitor::Instance().wait(obj, ms);
}

Expand Down Expand Up @@ -384,12 +384,14 @@ jint JVM_CountStackFrames(JNIEnv *env, jobject thread) {
}

void JVM_Interrupt(JNIEnv *env, jobject thread) {
throwInterruptedException(env, "test");
Threads::Instance().threads[thread].interrupted = true;
}

jboolean JVM_IsInterrupted(JNIEnv *env, jobject thread,
jboolean clearInterrupted) {
JvmUnimplemented("JVM_IsInterrupted");
bool interrupted = Threads::Instance().threads[thread].interrupted;
Threads::Instance().threads[thread].interrupted &= !static_cast<bool>(clearInterrupted);
return static_cast<jboolean>(interrupted);
}

jboolean JVM_HoldsLock(JNIEnv *env, jclass threadClass, jobject obj) {
Expand Down
1 change: 1 addition & 0 deletions runtime/native/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern thread_local jobject currentThread;
struct NativeThread {
pthread_t tid;
bool threadStatus;
bool interrupted;
};

class Threads {
Expand Down
1 change: 1 addition & 0 deletions runtime/src/jlang/runtime/Exceptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ static void throwNewThrowable(Class<? extends Throwable> clazz, String msg) thro
static void throwThrowable(Throwable t) throws Throwable {
throw t;
}
static void throwInterruptedException() throws InterruptedException { throw new InterruptedException(); }
}

0 comments on commit 9c28b84

Please sign in to comment.