Show / Hide Table of Contents

    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();
    Inheritance
    System.Object
    Lock
    Namespace: Lucene.Net.Store
    Assembly: Lucene.Net.dll
    Syntax
    public abstract class Lock : IDisposable

    Fields

    | Improve this Doc View Source

    LOCK_OBTAIN_WAIT_FOREVER

    Pass this value to Obtain(Int64) to try forever to obtain the lock.

    Declaration
    public const long LOCK_OBTAIN_WAIT_FOREVER = null
    Field Value
    Type Description
    System.Int64
    | Improve this Doc View Source

    LOCK_POLL_INTERVAL

    How long Obtain(Int64) waits, in milliseconds, in between attempts to acquire the lock.

    Declaration
    public static long LOCK_POLL_INTERVAL
    Field Value
    Type Description
    System.Int64

    Properties

    | Improve this Doc View Source

    FailureReason

    If a lock obtain called, this failureReason may be set with the "root cause" as to why the lock was not obtained.

    Declaration
    protected Exception FailureReason { get; set; }
    Property Value
    Type Description
    Exception

    Methods

    | Improve this Doc View Source

    Dispose()

    Releases exclusive access.

    Declaration
    public void Dispose()
    | Improve this Doc View Source

    Dispose(Boolean)

    Releases exclusive access.

    Declaration
    protected abstract void Dispose(bool disposing)
    Parameters
    Type Name Description
    System.Boolean disposing
    | Improve this Doc View Source

    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
    System.Boolean
    | Improve this Doc View Source

    NewAnonymous<T>(Lock, Int32, Func<T>)

    Creates a new instance with the ability to specify the DoBody() method through the doBody argument

    Simple example:

        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 determines the return type of the operation.

    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

    System.Int32 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 doBody delegate method (i.e. () => { return theObject; })

    Type Parameters
    Name Description
    T
    | Improve this Doc View Source

    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
    System.Boolean

    true iff exclusive access is obtained

    | Improve this Doc View Source

    Obtain(Int64)

    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
    System.Int64 lockWaitTimeout

    length of time to wait in milliseconds or LOCK_OBTAIN_WAIT_FOREVER to retry forever

    Returns
    Type Description
    System.Boolean

    true if lock was obtained

    Exceptions
    Type Condition
    LockObtainFailedException

    if lock wait times out

    See Also

    MakeLock(System.String)
    • Improve this Doc
    • View Source
    Back to top Copyright © 2020 Licensed to the Apache Software Foundation (ASF)