Example #1
0
        public async Task ExecuteInSemaphore(SemaphoreOptions opts, Action a, CancellationToken ct = default(CancellationToken))
        {
            if (opts == null)
            {
                throw new ArgumentNullException(nameof(opts));
            }
            if (a == null)
            {
                throw new ArgumentNullException(nameof(a));
            }

            var semaphore = await AcquireSemaphore(opts, ct).ConfigureAwait(false);

            try
            {
                if (!semaphore.IsHeld)
                {
                    throw new LockNotHeldException("Could not obtain the lock");
                }
                a();
            }
            finally
            {
                await semaphore.Release(ct).ConfigureAwait(false);
            }
        }
Example #2
0
 /// <summary>
 /// SemaphoreOpts is used to create a Semaphore with the given options.
 /// The prefix must have write privileges, and the limit must be agreed upon by all contenders.
 /// If a Session is not provided, one will be created.
 /// </summary>
 /// <param name="opts">The semaphore options</param>
 /// <returns>An unlocked semaphore</returns>
 public IDistributedSemaphore Semaphore(SemaphoreOptions opts)
 {
     if (opts == null)
     {
         throw new ArgumentNullException(nameof(opts));
     }
     return(new Semaphore(this)
     {
         Opts = opts
     });
 }
Example #3
0
        public async Task <IDistributedSemaphore> AcquireSemaphore(SemaphoreOptions opts, CancellationToken ct = default(CancellationToken))
        {
            if (opts == null)
            {
                throw new ArgumentNullException(nameof(opts));
            }

            var semaphore = Semaphore(opts);
            await semaphore.Acquire(ct).ConfigureAwait(false);

            return(semaphore);
        }