Beispiel #1
0
        /// <summary>
        /// Creates a new entry and queues it to this wait queue. If the cancellation token is already canceled, this method immediately returns a canceled task without modifying the wait queue.
        /// </summary>
        /// <param name="this">The wait queue.</param>
        /// <param name="syncObject">A synchronization object taken while cancelling the entry.</param>
        /// <param name="token">The token used to cancel the wait.</param>
        /// <returns>The queued task.</returns>
        public static Task <T> Enqueue <T>(this IAsyncWaitQueue <T> @this, object syncObject, CancellationToken token)
        {
            if (token.IsCancellationRequested)
            {
                return(Task.FromCanceled <T>(token));
            }

            var ret = @this.Enqueue();

            if (!token.CanBeCanceled)
            {
                return(ret);
            }

            var registration = token.Register(() =>
            {
                IDisposable finish;
                lock (syncObject)
                    finish = @this.TryCancel(ret);
                finish.Dispose();
            }, useSynchronizationContext: false);

            ret.ContinueWith(_ => registration.Dispose(), CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
            return(ret);
        }
Beispiel #2
0
        /// <summary>
        ///     Creates a new entry and queues it to this wait queue. If the cancellation token is already canceled, this method
        ///     immediately returns a canceled task without modifying the wait queue.
        /// </summary>
        /// <param name="this">The wait queue.</param>
        /// <param name="mutex">A synchronization object taken while cancelling the entry.</param>
        /// <param name="token">The token used to cancel the wait.</param>
        /// <returns>The queued task.</returns>
        public static Task <T> Enqueue <T>(this IAsyncWaitQueue <T> @this, object mutex, CancellationToken token)
        {
            if (token.IsCancellationRequested)
            {
                return(Task.FromCanceled <T>(token));
            }

            Task <T> ret = @this.Enqueue();

            if (!token.CanBeCanceled)
            {
                return(ret);
            }

            CancellationTokenRegistration registration = token.Register(() =>
            {
                lock (mutex)
                {
                    @this.TryCancel(ret, token);
                }
            },
                                                                        false);

            ret.ContinueWith(_ => registration.Dispose(),
                             CancellationToken.None,
                             TaskContinuationOptions.ExecuteSynchronously,
                             TaskScheduler.Default);
            return(ret);
        }
 public Task WaitAsync(CancellationToken cancellationToken)
 {
     lock (_mutex)
     {
         if (!_set)
         {
             return(_queue.Enqueue(_mutex, cancellationToken));
         }
         _set = false;
         return(TaskConstants.Completed);
     }
 }
Beispiel #4
0
 private Task <IDisposable> RequestLockAsync(CancellationToken cancellationToken)
 {
     lock (_mutex)
     {
         if (_taken)
         {
             return(_queue.Enqueue(_mutex, cancellationToken));
         }
         _taken = true;
         return(Task.FromResult((IDisposable) new Key(this)));
     }
 }
Beispiel #5
0
        /// <summary>
        /// Creates a new entry and queues it to this wait queue. If the cancellation token is already canceled, this method immediately returns a canceled task without modifying the wait queue.
        /// </summary>
        /// <param name="this">The wait queue.</param>
        /// <param name="token">The token used to cancel the wait.</param>
        /// <returns>The queued task.</returns>
        public static Task <T> Enqueue <T>(this IAsyncWaitQueue <T> @this, CancellationToken token)
        {
            if (token.IsCancellationRequested)
            {
                return(TaskConstants <T> .Canceled);
            }

            var ret = @this.Enqueue();

            if (token.CanBeCanceled)
            {
                var registration = token.Register(() => @this.TryCancel(ret).Dispose(), useSynchronizationContext: false);
                ret.ContinueWith(_ => registration.Dispose(), CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
            }
            return(ret);
        }