Skip to content

Commit

Permalink
Merge pull request #550 from corda/chrisr3-exceptions-vmrun
Browse files Browse the repository at this point in the history
Refactor handling of uncaught exceptions with OpenJDK to use runRaw().
  • Loading branch information
dicej committed Sep 12, 2017
2 parents 0347a6d + cc128aa commit 6603c2f
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions src/classpath-openjdk.cpp
Expand Up @@ -296,9 +296,7 @@ object makeJconstructor(Thread* t, GcMethod* vmMethod, int index = -1);

object makeJfield(Thread* t, GcField* vmField, int index = -1);

void uncaughtException(Thread *t, GcThrowable *e);

void disposeThread(Thread *t);
static uint64_t uncaughtExceptionHandler(Thread*, uintptr_t*);

#ifdef AVIAN_OPENJDK_SRC
void interceptFileOperations(Thread*, bool);
Expand Down Expand Up @@ -597,13 +595,25 @@ class MyClasspath : public Classpath {
THREAD_RESOURCE0(t, {
GcThrowable* e = t->exception;
if (e != NULL) {
PROTECT(t, e);

t->exception = NULL;
uncaughtException(t, e);

// Prevent any exceptions thrown from the uncaught
// exception handler from unwinding the stack past
// this point. This allows us to continue cleaning
// up this resource.
uintptr_t argument = reinterpret_cast<uintptr_t>(e);
runRaw(t, uncaughtExceptionHandler, &argument);
}

disposeThread(t);
vm::acquire(t, t->javaThread);
t->clearFlag(Thread::ActiveFlag);
vm::notifyAll(t, t->javaThread);
vm::release(t, t->javaThread);

t->m->processor->invoke(t,
cast<GcMethod>(t, roots(t)->threadTerminated()),
t->javaThread->group(),
t->javaThread);
});

GcMethod* method = resolveMethod(
Expand Down Expand Up @@ -972,37 +982,20 @@ class EmbeddedFile {
unsigned pathLength;
};

void uncaughtException(Thread *t, GcThrowable *e)
static uint64_t uncaughtExceptionHandler(Thread* t, uintptr_t* arguments)
{
GcThrowable* exception = cast<GcThrowable>(t, reinterpret_cast<object>(arguments[0]));
PROTECT(t, exception);

GcMethod* dispatch = resolveMethod(t,
roots(t)->bootLoader(),
"java/lang/Thread",
"dispatchUncaughtException",
"(Ljava/lang/Throwable;)V");
if (dispatch != NULL) {
THREAD_RESOURCE0(t, {
if (t->exception != NULL) {
// The stack will be unwound when this resource is
// released, which means that uncaughtException()
// will not return. So repeat the thread clean-up here.
disposeThread(t);
}
});

t->m->processor->invoke(t, dispatch, t->javaThread, e);
t->m->processor->invoke(t, dispatch, t->javaThread, exception);
}
}

void disposeThread(Thread *t) {
vm::acquire(t, t->javaThread);
t->clearFlag(Thread::ActiveFlag);
vm::notifyAll(t, t->javaThread);
vm::release(t, t->javaThread);

t->m->processor->invoke(t,
cast<GcMethod>(t, roots(t)->threadTerminated()),
t->javaThread->group(),
t->javaThread);
return 0;
}

#ifdef AVIAN_OPENJDK_SRC
Expand Down

0 comments on commit 6603c2f

Please sign in to comment.