Fork me on GitHub
  • API

    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
    object
    Lock
    Implements
    IDisposable
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    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
    MakeLock(string)

    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
    MakeLock(string)

    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
    MakeLock(string)

    Methods

    Dispose()

    Releases exclusive access.

    Declaration
    public void Dispose()
    See Also
    MakeLock(string)

    Dispose(bool)

    Releases exclusive access.

    Declaration
    protected abstract void Dispose(bool disposing)
    Parameters
    Type Name Description
    bool disposing
    See Also
    MakeLock(string)

    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
    MakeLock(string)

    NewAnonymous<T>(Lock, int, 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

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

    Type Parameters
    Name Description
    T
    See Also
    MakeLock(string)

    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
    MakeLock(string)

    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

    true if lock was obtained

    Exceptions
    Type Condition
    LockObtainFailedException

    if lock wait times out

    ArgumentOutOfRangeException

    if lockWaitTimeout is out of bounds

    IOException

    if Obtain() throws IOException

    See Also
    MakeLock(string)

    Implements

    IDisposable

    See Also

    MakeLock(string)
    Back to top Copyright © 2024 The Apache Software Foundation, Licensed under the Apache License, Version 2.0
    Apache Lucene.Net, Lucene.Net, Apache, the Apache feather logo, and the Apache Lucene.Net project logo are trademarks of The Apache Software Foundation.
    All other marks mentioned may be trademarks or registered trademarks of their respective owners.