GlobalLock class abstract
A lock shared beyond the current isolate, identified by its name.
Two GlobalLock objects with the same name exclude each other:
- io (Dart VM, Flutter mobile and desktop): across all the isolates and
processes of the machine run by the same user. The lock is a file
created exclusively in
/tmp/synchronized_global_lock_<user>(Linux, macOS) or the system temp folder (elsewhere), touched every second by a heartbeat isolate, so the lock stays held while the event loop of the holder is blocked (synchronous work). The lock is released at once when the holder isolate exits without releasing it; a waiter takes over a file it saw untouched for 10 seconds (timed on its own clock, so a clock change or a suspended machine never breaks a live lock), so a holder process that died (crash, kill) blocks the others for about 10 seconds. - web: across the tabs and workers of the same origin, using the Web Locks
API (
navigator.locks); the browser releases it when the tab closes. - node (its Web Locks API, node 24+): within the current process only (and its worker threads), not across processes.
- elsewhere (no Web Locks API): within the current isolate only.
Not reentrant: acquiring it again while holding it waits for itself (until the timeout, if any).
final lock = GlobalLock('my_app_firebase_emulator');
// Released when the computation completes.
await lock.synchronized(() async {
// ...
}, timeout: const Duration(minutes: 10));
// Or held across calls: always release the handle.
var handle = await lock.acquire(timeout: const Duration(minutes: 10));
try {
// ...
} finally {
await handle.release();
}
There is nothing to close: a lock that is not held uses no resource.
- Implementers
Constructors
- GlobalLock(String name)
-
Creates a lock named
name.factory
Properties
Methods
-
acquire(
{Duration? timeout}) → Future< GlobalLockHandle> - Waits for the lock and holds it until GlobalLockHandle.release.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
synchronized<
T> (FutureOr< T> computation(), {Duration? timeout}) → Future<T> -
Runs
computationholding the lock, released when it completes. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited