Concurrency Coordination Reports
Lock Guard Coordination Report
Record one-shot guard acquisitions behind a lock and report whether a later attempt was blocked.
locked one-shot guard
A coordination report can record nonblocking guard attempts. An `NSLock` wraps a critical section, and a `taken` flag started at `false` acts as an exclusive one-shot guard: the first attempt inside the lock flips the flag and is acquired, and every later attempt finds the flag already set and is reported as blocked. The lock serializes the check so the counts stay deterministic across runs. With no attempts the section is idle, with one attempt it is guarded, and a later attempt in the same run is blocked. The status line summarizes the outcome.
Lock Guard Coordination Report
lock_guard_coordination_report.swift
Replay: real traced execution (multi-file project)
import Foundation
let attempts = 1
let lock = NSLock()
var taken = false
var acquired = 0
var blocked = 0
for _ in 0..<attempts {
lock.lock()
if !taken {
taken = true
acquired += 1
} else {
blocked += 1
}
lock.unlock()
}
var status = "guarded"
if acquired == 0 {
status = "idle"
}
if blocked > 0 {
status = "blocked"
}
let line = "attempts=\(attempts) acquired=\(acquired) blocked=\(blocked) \(status)"
print(line)
import Foundation
let attempts = 0
let lock = NSLock()
var taken = false
var acquired = 0
var blocked = 0
for _ in 0..<attempts {
lock.lock()
if !taken {
taken = true
acquired += 1
} else {
blocked += 1
}
lock.unlock()
}
var status = "guarded"
if acquired == 0 {
status = "idle"
}
if blocked > 0 {
status = "blocked"
}
let line = "attempts=\(attempts) acquired=\(acquired) blocked=\(blocked) \(status)"
print(line)
import Foundation
let attempts = 2
let lock = NSLock()
var taken = false
var acquired = 0
var blocked = 0
for _ in 0..<attempts {
lock.lock()
if !taken {
taken = true
acquired += 1
} else {
blocked += 1
}
lock.unlock()
}
var status = "guarded"
if acquired == 0 {
status = "idle"
}
if blocked > 0 {
status = "blocked"
}
let line = "attempts=\(attempts) acquired=\(acquired) blocked=\(blocked) \(status)"
print(line)
attempts ← 1, lock ← <NSLock: ⟨addr A⟩>, taken ← false, acquired ← 0
3let attempts→ 1 = 1 //@attempts=0, 24let lock→ <NSLock: ⟨addr A⟩> = NSLock()5var taken→ false = false6var acquired→ 0 = 07var blocked→ 0 = 0for _ in 0..<attempts
9for _ in 0..<attempts1 {10 lock<NSLock: ⟨addr A⟩>.lock()11 if !taken {if !taken
10lock.lock()11if !takenfalse {12 taken = true13 acquired0 += 114} else {lock.unlock()
16 }17 lock<NSLock: ⟨addr A⟩>.unlock()18}status ← guarded, line ← attempts=1 acquired=1 blocked=0 guarded
20var status→ guarded = "guarded"21if acquired == 0 {22 status = "idle"23}24if blocked > 0 {25 status = "blocked"26}2728let line→ attempts=1 acquired=1 blocked=0 guarded = "attempts=\(attempts1) acquired=\(acquired1) blocked=\(blocked0) \(statusguarded)"2930print(lineattempts=1 acquired=1 blocked=0 guarded)outputattempts=1 acquired=1 blocked=0 guarded
attempts ← 0, lock ← <NSLock: ⟨addr A⟩>, taken ← false, acquired ← 0
3let attempts→ 0 = 04let lock→ <NSLock: ⟨addr A⟩> = NSLock()5var taken→ false = false6var acquired→ 0 = 07var blocked→ 0 = 089for _ in 0..<attempts {10 lock.lock()11 if !taken {12 taken = true13 acquired += 114 } else {15 blocked += 116 }17 lock.unlock()18}1920var status→ guarded = "guarded"21if acquired == 0 {if acquired == 0
20var status = "guarded"21if acquired0 == 0 {22 status = "idle"23}line ← attempts=0 acquired=0 blocked=0 idle
28let line→ attempts=0 acquired=0 blocked=0 idle = "attempts=\(attempts0) acquired=\(acquired0) blocked=\(blocked0) \(statusidle)"2930print(lineattempts=0 acquired=0 blocked=0 idle)outputattempts=0 acquired=0 blocked=0 idle
attempts ← 2, lock ← <NSLock: ⟨addr A⟩>, taken ← false, acquired ← 0
3let attempts→ 2 = 24let lock→ <NSLock: ⟨addr A⟩> = NSLock()5var taken→ false = false6var acquired→ 0 = 07var blocked→ 0 = 0for _ in 0..<attempts
pass 1 of 29for _ in 0..<attempts2 {10 lock<NSLock: ⟨addr A⟩>.lock()11 if !taken {if !taken
10lock.lock()11if !takenfalse {12 taken = true13 acquired0 += 114} else {lock.unlock()
16 }17 lock<NSLock: ⟨addr A⟩>.unlock()18}for _ in 0..<attempts
pass 2 of 29for _ in 0..<attempts2 {10 lock<NSLock: ⟨addr A⟩>.lock()11 if !taken {else
13 acquired += 114} else {15 blocked0 += 116}lock.unlock()
16 }17 lock<NSLock: ⟨addr A⟩>.unlock()18}status ← guarded
20var status→ guarded = "guarded"21if acquired == 0 {if blocked > 0
23}24if blocked1 > 0 {25 status = "blocked"26}line ← attempts=2 acquired=1 blocked=1 blocked
28let line→ attempts=2 acquired=1 blocked=1 blocked = "attempts=\(attempts2) acquired=\(acquired1) blocked=\(blocked1) \(statusblocked)"2930print(lineattempts=2 acquired=1 blocked=1 blocked)outputattempts=2 acquired=1 blocked=1 blocked