Record one-shot guard acquisitions and report whether a later attempt was blocked.

one-shot guard A coordination report can record nonblocking guard attempts. An `AtomicBoolean` started at `false` acts as an exclusive one-shot guard, and `compareAndSet(false, true)` succeeds only for the first attempt and fails for every later attempt, so it never blocks. The first acquire flips the flag, so a later attempt in the same run is reported as blocked. The status line summarizes whether the section stayed idle, guarded, or blocked a later acquire.

Atomic Guard Coordination Report

holds
AtomicGuardCoordinationReport.scala
Replay: real traced execution (multi-file project)
import java.util.concurrent.atomic.AtomicBoolean

object Main {
  def main(args: Array[String]): Unit = {
    val holds = 1
    val guard = new AtomicBoolean(false)
    var acquired = 0
    var blocked = 0

    for (i <- 0 until holds) {
      if (guard.compareAndSet(false, true)) {
        acquired = acquired + 1
      } else {
        blocked = blocked + 1
      }
    }

    var status = "guarded"
    if (acquired == 0) {
      status = "idle"
    }
    if (blocked > 0) {
      status = "blocked"
    }

    println("holds=" + holds + " acquired=" + acquired + " blocked=" + blocked + " " + status)
  }
}
import java.util.concurrent.atomic.AtomicBoolean

object Main {
  def main(args: Array[String]): Unit = {
    val holds = 0
    val guard = new AtomicBoolean(false)
    var acquired = 0
    var blocked = 0

    for (i <- 0 until holds) {
      if (guard.compareAndSet(false, true)) {
        acquired = acquired + 1
      } else {
        blocked = blocked + 1
      }
    }

    var status = "guarded"
    if (acquired == 0) {
      status = "idle"
    }
    if (blocked > 0) {
      status = "blocked"
    }

    println("holds=" + holds + " acquired=" + acquired + " blocked=" + blocked + " " + status)
  }
}
import java.util.concurrent.atomic.AtomicBoolean

object Main {
  def main(args: Array[String]): Unit = {
    val holds = 2
    val guard = new AtomicBoolean(false)
    var acquired = 0
    var blocked = 0

    for (i <- 0 until holds) {
      if (guard.compareAndSet(false, true)) {
        acquired = acquired + 1
      } else {
        blocked = blocked + 1
      }
    }

    var status = "guarded"
    if (acquired == 0) {
      status = "idle"
    }
    if (blocked > 0) {
      status = "blocked"
    }

    println("holds=" + holds + " acquired=" + acquired + " blocked=" + blocked + " " + status)
  }
}
  1. holds ← 1, guard ← false, acquired ← 0, blocked ← 0

    3object Main {4  def main(args: Array[String]): Unit = {5    val holds→ 1 = 1 //@holds=0, 26    val guard→ false = new AtomicBoolean(false)7    var acquired→ 0 = 08    var blocked→ 0 = 0910    for (i <- 0 until holds) {
  2. for (i <- 0 until holds)

    10for (i0 <- 0 until holds1) {11  if (guard.compareAndSet(false, true)) {
  3. acquired ← 1

    10for (i <- 0 until holds) {11  if (guardtrue.compareAndSet(false, true)) {12    acquired→ 1 = acquired + 113  } else {14    blocked = blocked + 1
  4. status ← guarded

    18    var status→ guarded = "guarded"19    if (acquired == 0) {20      status = "idle"21    }22    if (blocked > 0) {23      status = "blocked"24    }2526    println("holds=" + holds1 + " acquired=" + acquired1 + " blocked=" + blocked0 + " " + statusguarded)27  }28}
    outputholds=1 acquired=1 blocked=0 guarded
  1. holds ← 0, guard ← false, acquired ← 0, blocked ← 0, status ← guarded

    3object Main {4  def main(args: Array[String]): Unit = {5    val holds→ 0 = 06    val guard→ false = new AtomicBoolean(false)7    var acquired→ 0 = 08    var blocked→ 0 = 0910    for (i <- 0 until holds) {11      if (guard.compareAndSet(false, true)) {12        acquired = acquired + 113      } else {14        blocked = blocked + 115      }16    }1718    var status→ guarded = "guarded"19    if (acquired == 0) {20      status = "idle"
  2. status ← idle

    18var status = "guarded"19if (acquired0 == 0) {20  status→ idle = "idle"21}22if (blocked > 0) {
  3. println("holds=" + holds + " acquired=" + acquired + " blocked=" + blo…

    26    println("holds=" + holds0 + " acquired=" + acquired0 + " blocked=" + blocked0 + " " + statusidle)27  }28}
    outputholds=0 acquired=0 blocked=0 idle
  1. holds ← 2, guard ← false, acquired ← 0, blocked ← 0

    3object Main {4  def main(args: Array[String]): Unit = {5    val holds→ 2 = 26    val guard→ false = new AtomicBoolean(false)7    var acquired→ 0 = 08    var blocked→ 0 = 0910    for (i <- 0 until holds) {
  2. for (i <- 0 until holds)

    pass 1 of 2
    10for (i0 <- 0 until holds2) {11  if (guard.compareAndSet(false, true)) {
  3. acquired ← 1

    10for (i <- 0 until holds) {11  if (guardtrue.compareAndSet(false, true)) {12    acquired→ 1 = acquired + 113  } else {14    blocked = blocked + 1
  4. for (i <- 0 until holds)

    pass 2 of 2
    10for (i1 <- 0 until holds2) {11  if (guard.compareAndSet(false, true)) {
  5. blocked ← 1

    12    acquired = acquired + 113  } else {14    blocked→ 1 = blocked + 115  }16}
  6. status ← guarded

    18var status→ guarded = "guarded"19if (acquired == 0) {20  status = "idle"
  7. status ← blocked

    21}22if (blocked1 > 0) {23  status→ blocked = "blocked"24}
  8. println("holds=" + holds + " acquired=" + acquired + " blocked=" + blo…

    26    println("holds=" + holds2 + " acquired=" + acquired1 + " blocked=" + blocked1 + " " + statusblocked)27  }28}
    outputholds=2 acquired=1 blocked=1 blocked