Skip to content

Commit

Permalink
atomic bool
Browse files Browse the repository at this point in the history
Signed-off-by: Zach Puller <[email protected]>
  • Loading branch information
zpuller committed Dec 17, 2024
1 parent 2cf6f30 commit 4e3624e
Showing 1 changed file with 8 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import java.nio.file.StandardOpenOption
import java.util
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicBoolean

import scala.collection.mutable

Expand Down Expand Up @@ -177,8 +178,7 @@ trait SpillableHandle extends StoreHandle {
*/
def spill(): Long

private var spilling = false
private val spillLock = new Object()
private val spilling = new AtomicBoolean(false)

/**
* Method used to atomically check and set the spilling state, so that anyone who wants to
Expand All @@ -191,20 +191,14 @@ trait SpillableHandle extends StoreHandle {
* @param s whether the caller is trying to spill or not (ie finished)
* @return whether the caller is allowed to spill (or true if s is false)
*/
def setSpilling(s: Boolean): Boolean = spillLock.synchronized {
def setSpilling(s: Boolean): Boolean = {
if (!s) {
// done spilling, nothing to check
spilling = false
if (!spilling.getAndSet(false)) {
throw new IllegalStateException("tried to setSpilling to false while not spilling!")
}
true
} else {
if (!spilling) {
// we may spill
spilling = true
true
} else {
// someone else is already spilling
false
}
!spilling.getAndSet(true)
}
}

Expand All @@ -217,9 +211,7 @@ trait SpillableHandle extends StoreHandle {
*/
private[spill] def spillable: Boolean = {
if (approxSizeInBytes > 0) {
spillLock.synchronized {
!spilling
}
!spilling.get()
} else {
false
}
Expand Down

0 comments on commit 4e3624e

Please sign in to comment.