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

Lock Guard Coordination Report

lock_guard_coordination_report.swift
import Foundation

let attempts = 
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 = 
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 = 
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)
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.