Skip to content

Commit

Permalink
Fix bug in cdl.
Browse files Browse the repository at this point in the history
  • Loading branch information
aoli-al committed Nov 8, 2024
1 parent 5cfecb3 commit b51440a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
10 changes: 1 addition & 9 deletions core/src/main/kotlin/org/pastalab/fray/core/RunContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,7 @@ class RunContext(val config: Configuration) {
}
}
is CountDownLatchAwaitBlocking -> {
context.pendingOperation = ThreadResumeOperation(true)
context.state = ThreadState.Enabled
latchManager.unblockThread(pendingOperation.latch, t.id, false, true)
waitingObject = pendingOperation.latch
}
is ParkBlocked -> {
Expand Down Expand Up @@ -518,13 +517,6 @@ class RunContext(val config: Configuration) {
}
is CountDownLatchAwaitBlocking -> {
latchManager.unblockThread(pendingOperation.latch, context.thread.id, true, false)
if (context.thread != Thread.currentThread()) {
syncManager.createWait(pendingOperation.latch, 1)
context.thread.interrupt()
syncManager.wait(pendingOperation.latch)
} else {
context.thread.interrupt()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,6 @@ class RuntimeDelegate(val context: RunContext) : org.pastalab.fray.runtime.Deleg
onSkipMethodDone("Latch.await")
return latch.await(timeout, unit)
}
latch.await()
} catch (e: InterruptedException) {
// Do nothing
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package org.pastalab.fray.core.concurrency.locks

import org.pastalab.fray.core.ThreadContext
import org.pastalab.fray.core.ThreadState
import org.pastalab.fray.core.concurrency.operations.CountDownLatchAwaitBlocking
import org.pastalab.fray.core.concurrency.operations.ThreadResumeOperation

class CountDownLatchContext(var count: Long) : Interruptible {
class CountDownLatchContext(var count: Long) {
val latchWaiters = mutableMapOf<Long, LockWaiter>()

fun await(canInterrupt: Boolean, thread: ThreadContext): Boolean {
Expand All @@ -19,18 +20,21 @@ class CountDownLatchContext(var count: Long) : Interruptible {
return false
}

fun unblockThread(tid: Long, isTimeout: Boolean, isInterrupt: Boolean) {
val lockWaiter = latchWaiters[tid] ?: return
fun unblockThread(tid: Long, isTimeout: Boolean, isInterrupt: Boolean): Boolean {
val lockWaiter = latchWaiters[tid] ?: return false
if (isInterrupt && !lockWaiter.canInterrupt) {
return
return false
}
val pendingOperation = lockWaiter.thread.pendingOperation
assert(pendingOperation is CountDownLatchAwaitBlocking)
lockWaiter.thread.pendingOperation = ThreadResumeOperation(!isTimeout)
lockWaiter.thread.state = ThreadState.Enabled
latchWaiters.remove(tid)
}

override fun interrupt(tid: Long) {
unblockThread(tid, false, true)
if ((pendingOperation as CountDownLatchAwaitBlocking).timed) {
return false
} else {
return true
}
}

fun release(): Int {
Expand All @@ -40,8 +44,9 @@ class CountDownLatchContext(var count: Long) : Interruptible {
count = 0
var threads = 0
for (lockWaiter in latchWaiters.values.toList()) {
unblockThread(lockWaiter.thread.thread.id, false, false)
threads += 1
if (unblockThread(lockWaiter.thread.thread.id, false, false)) {
threads += 1
}
}
return threads
}
Expand All @@ -59,8 +64,9 @@ class CountDownLatchContext(var count: Long) : Interruptible {
if (count == 0L) {
var threads = 0
for (lockWaiter in latchWaiters.values.toList()) {
unblockThread(lockWaiter.thread.thread.id, false, false)
threads += 1
if (unblockThread(lockWaiter.thread.thread.id, false, false)) {
threads += 1
}
}
return threads
}
Expand Down

0 comments on commit b51440a

Please sign in to comment.