/// <summary> /// ExecuteLock accepts a delegate to execute in the context of a lock, releasing the lock when completed. /// </summary> /// <param name="opts"></param> /// <param name="action"></param> /// <returns></returns> public async Task ExecuteLocked(LockOptions opts, Action action, CancellationToken ct = default(CancellationToken)) { if (opts == null) { throw new ArgumentNullException(nameof(opts)); } if (action == null) { throw new ArgumentNullException(nameof(action)); } var l = await AcquireLock(opts, ct).ConfigureAwait(false); try { if (!l.IsHeld) { throw new LockNotHeldException("Could not obtain the lock"); } action(); } finally { await l.Release().ConfigureAwait(false); } }
public void ExecuteAbortableLocked(LockOptions opts, CancellationToken ct, Action action) { using (var l = AcquireLock(opts, ct)) { if (!l.IsHeld) { throw new LockNotHeldException("Could not obtain the lock"); } var thread = new Thread(() => { action(); }); thread.Start(); while (!l.CancellationToken.IsCancellationRequested && thread.IsAlive) { } if (!thread.IsAlive) { return; } var delayTask = Task.Delay(15000); while (!delayTask.IsCompleted && thread.IsAlive) { } if (!thread.IsAlive) { return; } // Now entering the "zone of danger" thread.Abort(); throw new TimeoutException("Thread was aborted because the lock was lost and the action did not complete within the lock delay"); } }
public Task ExecuteLocked(LockOptions opts, CancellationToken ct, Action action) { if (opts == null) { throw new ArgumentNullException(nameof(opts)); } return(ExecuteLocked(opts, action, ct)); }
/// <summary> /// ExecuteLock accepts a delegate to execute in the context of a lock, releasing the lock when completed. /// </summary> /// <param name="opts"></param> /// <param name="action"></param> /// <returns></returns> public void ExecuteLocked(LockOptions opts, Action action) { if (opts == null) { throw new ArgumentNullException("opts"); } ExecuteLocked(opts, CancellationToken.None, action); }
/// <summary> /// AcquireLock creates a lock that is already pre-acquired and implements IDisposable to be used in a "using" block /// </summary> /// <param name="opts"></param> /// <param name="ct"></param> /// <returns></returns> public IDisposableLock AcquireLock(LockOptions opts, CancellationToken ct) { if (opts == null) { throw new ArgumentNullException("opts"); } return(new DisposableLock(this, opts, ct)); }
/// <summary> /// AcquireLock creates a lock that is already pre-acquired and implements IDisposable to be used in a "using" block /// </summary> /// <param name="opts"></param> /// <param name="ct"></param> /// <returns></returns> public DisposableLock AcquireLock(LockOptions opts, CancellationToken ct) { if (opts == null) { throw new ArgumentNullException("opts"); } return new DisposableLock(this, opts, ct); }
/// <summary> /// CreateLock returns an unlocked lock which can be used to acquire and release the mutex. The key used must have write permissions. /// </summary> /// <param name="opts"></param> /// <returns></returns> public IDistributedLock CreateLock(LockOptions opts) { if (opts == null) { throw new ArgumentNullException(nameof(opts)); } return(new Lock(this) { Opts = opts }); }
/// <summary> /// AcquireLock creates a lock that is already acquired when this call returns. /// </summary> /// <param name="opts"></param> /// <param name="ct"></param> /// <returns></returns> public async Task <IDistributedLock> AcquireLock(LockOptions opts, CancellationToken ct = default(CancellationToken)) { if (opts == null) { throw new ArgumentNullException(nameof(opts)); } var l = CreateLock(opts); await l.Acquire(ct).ConfigureAwait(false); return(l); }
/// <summary> /// ExecuteLock accepts a delegate to execute in the context of a lock, releasing the lock when completed. /// </summary> /// <param name="opts"></param> /// <param name="ct"></param> /// <param name="action"></param> /// <returns></returns> public void ExecuteLocked(LockOptions opts, CancellationToken ct, Action action) { if (opts == null) { throw new ArgumentNullException("opts"); } if (action == null) { throw new ArgumentNullException("action"); } using (var l = AcquireLock(opts, ct)) { if (!l.IsHeld) { throw new LockNotHeldException("Could not obtain the lock"); } action(); } }
/// <summary> /// Do not use unless you need this. Executes an action in a new thread under a lock, ABORTING THE THREAD if the lock is lost and the action does not complete within the lock-delay. /// </summary> /// <param name="opts"></param> /// <param name="ct"></param> /// <param name="action"></param> public void ExecuteAbortableLocked(LockOptions opts, CancellationToken ct, Action action) { using (var l = AcquireLock(opts, ct)) { if (!l.IsHeld) { throw new LockNotHeldException("Could not obtain the lock"); } var thread = new Thread(() => { action(); }); thread.Start(); while (!l.CancellationToken.IsCancellationRequested && thread.IsAlive) { } if (!thread.IsAlive) { return; } var delayTask = Task.Delay(15000); while (!delayTask.IsCompleted && thread.IsAlive) { } if (!thread.IsAlive) { return; } // Now entering the "zone of danger" thread.Abort(); throw new TimeoutException("Thread was aborted because the lock was lost and the action did not complete within the lock delay"); } }
/// <summary> /// Do not use unless you need this. Executes an action in a new thread under a lock, ABORTING THE THREAD if the lock is lost and the action does not complete within the lock-delay. /// </summary> /// <param name="opts"></param> /// <param name="action"></param> public void ExecuteAbortableLocked(LockOptions opts, Action action) { if (opts == null) { throw new ArgumentNullException("opts"); } if (action == null) { throw new ArgumentNullException("action"); } ExecuteAbortableLocked(opts, CancellationToken.None, action); }
/// <summary> /// CreateLock returns an unlocked lock which can be used to acquire and release the mutex. The key used must have write permissions. /// </summary> /// <param name="opts"></param> /// <returns></returns> public IDistributedLock CreateLock(LockOptions opts) { if (opts == null) { throw new ArgumentNullException("opts"); } return new Lock(this) { Opts = opts }; }
internal DisposableLock(ConsulClient client, LockOptions opts, CancellationToken ct) : base(client) { Opts = opts; CancellationToken = Acquire(ct); }