Skip to content

Commit

Permalink
update.
Browse files Browse the repository at this point in the history
  • Loading branch information
aoli-al committed Jun 20, 2024
1 parent 5c8445e commit d7f2a4c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
42 changes: 21 additions & 21 deletions core/src/main/kotlin/cmu/pasta/fray/core/GlobalContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import java.util.concurrent.Executors
import java.util.concurrent.Semaphore
import java.util.concurrent.locks.Condition
import java.util.concurrent.locks.Lock
import java.util.concurrent.locks.LockSupport
import java.util.concurrent.locks.ReentrantLock
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock
Expand Down Expand Up @@ -167,51 +168,52 @@ object GlobalContext {
fun threadPark() {
val t = Thread.currentThread()
val context = registeredThreads[t.id]!!

context.state = ThreadState.Enabled
context.pendingOperation = ParkOperation()
scheduleNextOperation(true)

context.checkInterrupt()
if (!context.unparkSignaled) {
context.pendingOperation = ParkOperation()
context.state = ThreadState.Paused
scheduleNextOperation(false)
} else {
context.unparkSignaled = false
}
// Well, unpark is signaled everywhere. We cannot really rely on it to
// block the thread.
LockSupport.unpark(t)
}

fun threadParkDone() {
val t = Thread.currentThread()
val context = registeredThreads[t.id]!!
// If the thread is still running, it means
// that the thread is unparked before it is parked.
if (context.state == ThreadState.Running) {
return

if (!context.unparkSignaled) {
context.pendingOperation = ParkOperation()
context.state = ThreadState.Paused
scheduleNextOperation(true)
if (context.unparkSignaled) {
context.checkInterrupt()
}
} else {
context.unparkSignaled = false
}
val state = context.state
val operation = context.pendingOperation
if (state != ThreadState.Enabled) {
println(state)
println(operation)
return
}
assert(state == ThreadState.Enabled)
syncManager.signal(t)
context.block()
}

fun threadUnpark(t: Thread) {
val context = registeredThreads[t.id]!!
if (context.state == ThreadState.Paused && context.pendingOperation is ParkOperation) {
syncManager.createWait(t, 1)
context.state = ThreadState.Enabled
context.pendingOperation = ThreadResumeOperation()
} else if (context.state == ThreadState.Enabled || context.state == ThreadState.Running) {
context.unparkSignaled = true
}
}

fun threadUnparkDone(t: Thread) {
// SFuzz only needs to wait if `t` is parked and then
// waken up by this `unpark` operation.
syncManager.wait(t)
}
fun threadUnparkDone(t: Thread) {}

fun threadRun() {
var t = Thread.currentThread()
Expand Down Expand Up @@ -360,9 +362,7 @@ object GlobalContext {
}

// A thread is interrupted during park.

if (context.state == ThreadState.Paused && context.pendingOperation is ParkOperation) {
syncManager.createWait(t, 1)
context.pendingOperation = ThreadResumeOperation()
context.state = ThreadState.Enabled
}
Expand Down
11 changes: 8 additions & 3 deletions core/src/main/kotlin/cmu/pasta/fray/core/RuntimeDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,18 @@ class RuntimeDelegate : Delegate() {

override fun onThreadParkDone() {
if (checkEntered()) return
GlobalContext.threadParkDone()
entered.set(false)
try {
GlobalContext.threadParkDone()
} finally {
entered.set(false)
}
}

override fun onThreadUnpark(t: Thread?) {
if (t == null) return
if (checkEntered()) return
if (checkEntered()) {
return
}
GlobalContext.threadUnpark(t)
entered.set(false)
}
Expand Down

0 comments on commit d7f2a4c

Please sign in to comment.