Esempio n. 1
0
        /// <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);
            }
        }
Esempio n. 2
0
 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");
     }
 }
Esempio n. 3
0
 public Task ExecuteLocked(LockOptions opts, CancellationToken ct, Action action)
 {
     if (opts == null)
     {
         throw new ArgumentNullException(nameof(opts));
     }
     return(ExecuteLocked(opts, action, ct));
 }
Esempio n. 4
0
 /// <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);
 }
Esempio n. 5
0
 /// <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));
 }
Esempio n. 6
0
 /// <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);
 }
Esempio n. 7
0
 /// <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
     });
 }
Esempio n. 8
0
        /// <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);
        }
Esempio n. 9
0
 /// <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();
     }
 }
Esempio n. 10
0
 /// <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");
     }
 }
Esempio n. 11
0
 /// <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);
 }
Esempio n. 12
0
 /// <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();
     }
 }
Esempio n. 13
0
 /// <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 };
 }
Esempio n. 14
0
 internal DisposableLock(ConsulClient client, LockOptions opts, CancellationToken ct)
     : base(client)
 {
     Opts = opts;
     CancellationToken = Acquire(ct);
 }
Esempio n. 15
0
 internal DisposableLock(ConsulClient client, LockOptions opts, CancellationToken ct)
     : base(client)
 {
     Opts = opts;
     CancellationToken = Acquire(ct);
 }