NamedLock class
A NamedLock can be used to control access to a resource across processes and isolates.
A NamedLock uses a combination of a UDP socket and a files to provide a locking mechanism that is guarenteed to work across isolates in the same process as well as between processes.
If you only need locking 'within' an isolate that you should avoid using NamedLock as it is a realitively slow locking mechanism as it creates a file to represent a lock.
To ensure that NamedLocks hold across processes and isolates we use a two part locking mechanism. The first part is a UDP socket (on port 63424) that we refere to a hard lock. The same hard lock is used for all NamedLocks and as such is a potential bottle neck. To limit this bottle neck we hold the hard lock for as short a period as possible. The hard lock is only used to create and delete the file based lock. As soon as a file based lock transition completes the hard lock is released.
On linux a traditional file lock will not block isolates in the same process from locking the same file hence we need
Constructors
- NamedLock({required String name, String? lockPath, String description = '', Duration timeout = const Duration(seconds: 30)})
-
lockPath
is the path of the directory used to store the lock file. If no lockPath is given then Directory.systemTemp/dcli/locks is used to store locks. All code that shares the lock MUST use the samelockPath
. It is recommended that you pass an absolute path to ensure that the same path is used. Thename
is used as the suffix of the lockfile. The suffix allows multiple locks to share a single lockPath. Thedescription
, if passed, is used in error messages to describe the lock. Thetimeout
defines how long we will wait for a lock to become available. The defaulttimeout
is infinite (null).
Properties
- decLockCount → int
-
decrements the lock count and returns
the new lock count.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- incLockCount → int
-
increments the lock count and returns
the new lock count.
no setter
- name → String
-
The name of the lock.
final
- port → int
-
The tcp socket port we use to implement
a hard lock. A port can only be opened once
so its the perfect way to create a lock that works
across processes and isolates.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
withLock(
void fn(), {String? waiting}) → void -
creates a lock file and then calls
fn
oncefn
returns the lock is released. Ifwaiting
is passed it will be used to write a log message to the console.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited