Skip to content

Commit

Permalink
intercept isInterrupted method.
Browse files Browse the repository at this point in the history
  • Loading branch information
aoli-al committed Jun 21, 2024
1 parent c362f50 commit ec0a924
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/src/main/kotlin/cmu/pasta/fray/core/GlobalContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ object GlobalContext {
registeredThreads[t.id]?.block()
}

fun threadIsInterrupted(t: Thread, result: Boolean): Boolean {
return result || registeredThreads[t.id]!!.interruptSignaled
}

fun threadGetState(t: Thread, state: Thread.State): Thread.State {
if (state == Thread.State.WAITING ||
state == Thread.State.TIMED_WAITING ||
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/kotlin/cmu/pasta/fray/core/RuntimeDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -683,4 +683,11 @@ class RuntimeDelegate : Delegate() {
return true
}
}

override fun onThreadIsInterrupted(result: Boolean, t: Thread): Boolean {
if (checkEntered()) return result
val isInterrupted = GlobalContext.threadIsInterrupted(t, result)
entered.set(false)
return isInterrupted
}
}
2 changes: 1 addition & 1 deletion examples/src/main/kotlin/JUnitRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fun main(args: Array<String>) {

val result = JUnitCore().run(request)
if (!result.wasSuccessful()) {
val failureReport = result.failures.joinToString { "\n =========== \n" }
val failureReport = result.failures.joinToString("\n =========== \n")
throw RuntimeException(failureReport)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class ThreadInstrumenter(cv: ClassVisitor) : ClassVisitorBase(cv, Thread::class.
return MethodExitVisitor(
mv, Runtime::onThreadGetAndClearInterrupt, access, name, descriptor, true, false, false)
}
if (name == "isInterrupted") {
return MethodExitVisitor(
mv, Runtime::onThreadIsInterrupted, access, name, descriptor, true, false, false)
}
if (name == "clearInterrupt") {
return MethodExitVisitor(
mv, Runtime::onThreadClearInterrupt, access, name, descriptor, true, false, false)
Expand Down
3 changes: 3 additions & 0 deletions runtime/src/main/java/cmu/pasta/fray/runtime/Delegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,5 +242,8 @@ public void onConditionAwaitUninterruptibly(Condition object) {
public void onConditionAwaitUninterruptiblyDone(Condition object) {
}

public boolean onThreadIsInterrupted(boolean result, Thread t) {
return result;
}
}

3 changes: 3 additions & 0 deletions runtime/src/main/java/cmu/pasta/fray/runtime/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,7 @@ public static void onConditionAwaitUninterruptiblyDone(Condition object) {
DELEGATE.onConditionAwaitUninterruptiblyDone(object);
}

public static boolean onThreadIsInterrupted(boolean result, Thread t) {
return DELEGATE.onThreadIsInterrupted(result, t);
}
}

0 comments on commit ec0a924

Please sign in to comment.