cross-locks provides a single-file, zero-dependency¹ implementation of named, FIFO, re-entrant global locks that Just Work everywhere – native back-ends, WASM in the browser, and single-threaded test binaries – with identical APIs and semantics.
¹ Apart from the optional runtime adapters (tokio, wasm-bindgen) and the tiny derive helpers async-trait /
thiserror.
✨ Why?
- Cross-platform parity – ship the same locking guarantees in browsers (via
Navigator.locks) or on the server without extra feature flags in your application code. - FIFO fairness on native (using a Tokio queue); browser fairness delegated to the spec.
- Zero-timeout “try-lock” semantics that mirror Supabase / GoTrue behaviour.
- Task-local re-entrancy – a lock holder can call itself recursively without dead-locking.
Arc<T>auto-impl – store the lock directly in shared state (e.g. AxumExtension).- Compile-time selection – one concrete backend is compiled in; no runtime branches.
🚀 Quick start
# Cargo.toml
[]
= { = "0.1", = false, = ["native"] } # or "browser"
= { = "1", = true, = ["sync", "rt-multi-thread"] }
use ;
use Duration;
async
Feature matrix
| Target | --features |
Crate size | Guarantees |
|---|---|---|---|
| Native (Tokio multi-thread) | native |
tiny | FIFO, timeout, re-entry |
| Browser WASM (2022+) | browser |
tiny | Delegates to Navigator.locks |
| Tests / single-thread CLI | (none) | tiny | No-op passthrough |
Safari ≥ 16.4, Chrome ≥ 69, Firefox ≥ 94 already ship the Web Locks API.
🧩 Using your own runtime
GlobalLock is an async-trait with a blanket impl for Arc<T>, so you can plug in bespoke back-ends (e.g. a Postgres
advisory lock):
🛠️ Tooling & tests
# exhaustive suite on native
# smoke-test in headless Firefox
The repository ships a tiny dual_test! macro so you can write once, run everywhere:
dual_test!
📜 License
Licensed under either 🅰 Apache-2.0 or 🅱 MIT at your option – see LICENSE-* for details.
❤️ Acknowledgements
Inspired by Supabase’s JS SDK and the GoTrue service; thanks to the Web Locks API editors for a sane browser primitive.
Happy hacking & safe locking! — @geoffreygarrett