Class Lock
An interprocess mutex lock.
Typical use might look like:var result = Lock.With.NewAnonymous<string>(
@lock: directory.MakeLock("my.lock"),
lockWaitTimeout: Lock.LOCK_OBTAIN_WAIT_FOREVER,
doBody: () =>
{
//... code to execute while locked ...
return "the result";
}).Run();
Implements
Inherited Members
Namespace: Lucene.Net.Store
Assembly: Lucene.Net.dll
Syntax
public abstract class Lock : IDisposable
Fields
LOCK_OBTAIN_WAIT_FOREVER
Pass this value to Obtain(long) to try forever to obtain the lock.
Declaration
public const long LOCK_OBTAIN_WAIT_FOREVER = -1
Field Value
Type | Description |
---|---|
long |
See Also
LOCK_POLL_INTERVAL
How long Obtain(long) waits, in milliseconds, in between attempts to acquire the lock.
Declaration
public static long LOCK_POLL_INTERVAL
Field Value
Type | Description |
---|---|
long |
See Also
Properties
FailureReason
If a lock obtain called, this failureReason may be set with the "root cause" Exception as to why the lock was not obtained.
Declaration
protected Exception FailureReason { get; set; }
Property Value
Type | Description |
---|---|
Exception |
See Also
Methods
Dispose()
Releases exclusive access.
Declaration
public void Dispose()
See Also
Dispose(bool)
Releases exclusive access.
Declaration
protected abstract void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing |
See Also
IsLocked()
Returns true
if the resource is currently locked. Note that one must
still call Obtain() before using the resource.
Declaration
public abstract bool IsLocked()
Returns
Type | Description |
---|---|
bool |
See Also
NewAnonymous<T>(Lock, int, Func<T>)
Creates a new instance with the ability to specify the DoBody() method
through the doBody
argument
var result = Lock.With.NewAnonymous<string>(
@lock: directory.MakeLock("my.lock"),
lockWaitTimeout: Lock.LOCK_OBTAIN_WAIT_FOREVER,
doBody: () =>
{
//... code to execute while locked ...
return "the result";
}).Run();
The result of the operation is the value that is returned from doBody
(i.e. () => { return "the result"; }). The type of Declaration
public static Lock.With<T> NewAnonymous<T>(Lock @lock, int lockWaitTimeout, Func<T> doBody)
Parameters
Type | Name | Description |
---|---|---|
Lock | lock | the Lock instance to use |
int | lockWaitTimeout | length of time to wait in milliseconds or LOCK_OBTAIN_WAIT_FOREVER to retry forever |
Func<T> | doBody | a delegate method that |
Returns
Type | Description |
---|---|
Lock.With<T> | The value that is returned from the |
Type Parameters
Name | Description |
---|---|
T |
See Also
Obtain()
Attempts to obtain exclusive access and immediately return upon success or failure. Use Dispose() to release the lock.
Declaration
public abstract bool Obtain()
Returns
Type | Description |
---|---|
bool | true iff exclusive access is obtained |
See Also
Obtain(long)
Attempts to obtain an exclusive lock within amount of
time given. Polls once per LOCK_POLL_INTERVAL
(currently 1000) milliseconds until lockWaitTimeout
is
passed.
Declaration
public bool Obtain(long lockWaitTimeout)
Parameters
Type | Name | Description |
---|---|---|
long | lockWaitTimeout | length of time to wait in milliseconds or LOCK_OBTAIN_WAIT_FOREVER to retry forever |
Returns
Type | Description |
---|---|
bool |
|
Exceptions
Type | Condition |
---|---|
LockObtainFailedException | if lock wait times out |
ArgumentOutOfRangeException | if |
IOException | if Obtain() throws IOException |