Mutual Exclusion (Mutex) Lock

A synchronization mechanism granting exclusive access rights to a single process at a time across contested resources, harmonizing high-precision local benchmark measurements with continuous background operations.

General Meaning

In operating systems and concurrent computing, mutual exclusion (mutex) prevents multiple processes or threads from simultaneously accessing shared hardware, memory, or files. Without synchronization, concurrent mutations trigger race conditions, corrupted state, and unpredictable failures.

A mutex acts as a gatekeeper: a process acquires a lock before entering a critical section, forcing any contending process to wait, defer, or fail gracefully until the holder releases the token. This mechanism guarantees deterministic state isolation in resource-constrained environments.

Why It Matters

  • Preventing measurement noise and latency jitter: Running background batch jobs during AI inference speed benchmarks corrupts token-generation metrics and GPU timing data.
  • Eliminating memory exhaustion and OOM panics: Concurrently booting multiple large parameter models on the same host can overwhelm unified memory and trigger kernel panics or reboots.
  • Guarding shared directories and state files against corruption: Prevents multi-agent processes from interleaving file writes and corrupting repository state or queue manifests.

How the Site Factory Uses It

In the Site Factory, because the home Mac fleet (Studio 1 and Studio 2) simultaneously serves as a high-precision benchmarking environment for local LLM evaluation (llmonmac.com and mac-llm-bench), strict mutual exclusion locks are deployed to protect measurement integrity without halting background task progression (described in dashboard/site/index.html pattern 4).

Key lock primitives manage concurrency cleanly: mac.lock on Studio 1 uses OS-level fcntl.flock during benchmark runs, while background workers run with strict execution time limits (30-60 minutes) and unload models promptly; step.lock on Studio 2 guards benchmark stages, enabling realtime_runner.py to detect contention via [Errno 35] and automatically redirect workloads to an idle Mac; mq.py hold provides a manual queue pause token ahead of macOS updates or maintenance; and the CI gate gate.py checks lock states before executing self-hosted CI pipelines, waiting up to 45 minutes for benchmarks to clear.

Caveats & Operational Rules

  • Never delete lock files manually from the filesystem: Mutex state in the Site Factory relies on kernel file descriptor locks via fcntl.flock rather than simple file existence; deleting lock files with rm breaks isolation and causes race conditions.
  • Preventing deadlocks with bounded timeouts and automatic release: Relying on OS-level flock ensures kernel cleanup if processes crash abnormally, while all waiting loops enforce strict timeout boundaries.

Related Terms

References

  • Book: Martin Kleppmann, *Designing Data-Intensive Applications*, Chapter 8 (Trouble with Distributed Systems: Distributed Locks and Race Conditions)

Verified: 2026-11-08