Skip to content

Commit

Permalink
Treat sleep as a timed blocking operation. (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
aoli-al authored Jan 8, 2025
1 parent 888f370 commit 7284074
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
9 changes: 9 additions & 0 deletions core/src/main/kotlin/org/pastalab/fray/core/RunContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,15 @@ class RunContext(val config: Configuration) {
syncManager.wait(latch)
}

fun threadSleepOperation() {
val t = Thread.currentThread().threadId()
val context = registeredThreads[t]!!
context.checkInterrupt()
context.pendingOperation = ThreadSleepBlocking(context)
context.state = ThreadState.Paused
scheduleNextOperation(true)
}

fun scheduleNextOperationAndCheckDeadlock(shouldBlockCurrentThread: Boolean) {
try {
scheduleNextOperation(shouldBlockCurrentThread)
Expand Down
32 changes: 28 additions & 4 deletions core/src/main/kotlin/org/pastalab/fray/core/RuntimeDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -846,16 +846,40 @@ class RuntimeDelegate(val context: RunContext) : org.pastalab.fray.runtime.Deleg
return probe
}

override fun onThreadSleepDuration(duration: Duration?) {
Thread.yield()
override fun onThreadSleepDuration(duration: Duration) {
if (checkEntered()) {
Thread.sleep(duration)
return
}
try {
context.threadSleepOperation()
} finally {
entered.set(false)
}
}

override fun onThreadSleepMillis(millis: Long) {
Thread.yield()
if (checkEntered()) {
Thread.sleep(millis)
return
}
try {
context.threadSleepOperation()
} finally {
entered.set(false)
}
}

override fun onThreadSleepMillisNanos(millis: Long, nanos: Int) {
Thread.yield()
if (checkEntered()) {
Thread.sleep(millis, nanos)
return
}
try {
context.threadSleepOperation()
} finally {
entered.set(false)
}
}

override fun onStampedLockReadLock(lock: StampedLock) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.pastalab.fray.core.concurrency.operations

import org.pastalab.fray.core.ThreadContext
import org.pastalab.fray.core.concurrency.primitives.InterruptionType
import org.pastalab.fray.rmi.ThreadState

class ThreadSleepBlocking(val context: ThreadContext) : TimedBlockingOperation(true) {
override fun unblockThread(tid: Long, type: InterruptionType): Any? {
context.pendingOperation = ThreadResumeOperation(type != InterruptionType.TIMEOUT)
context.state = ThreadState.Enabled
return null
}
}

0 comments on commit 7284074

Please sign in to comment.