/// <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 Task ExecuteLocked(LockOptions opts, CancellationToken ct, Action action) { if (opts == null) { throw new ArgumentNullException(nameof(opts)); } return(ExecuteLocked(opts, action, 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); }