Пример #1
0
        public Task <IDisposable> Lock(CancellationToken cancellationToken = default)
        {
            LinkedListNode <CancelableCompletionSource <IDisposable> >?node = null;

            lock (_pending)
            {
                if (_isDisposed)
                {
                    throw new ObjectDisposedException(nameof(AsyncLock));
                }

                if (_semaphoreSlim.CurrentCount == 1) //Lock is free
                {
                    _semaphoreSlim.Wait();            //Will never block
                    return(_completedTask);
                }

                //Lock was not free
                var ccs = new CancelableCompletionSource <IDisposable>();
                node = _pending.AddLast(ccs);
            }

            cancellationToken.Register(() => Cancel(node));

            return(node.Value.Task);
        }
Пример #2
0
        public Task <IDisposable> Lock(CancellationToken cancellationToken)
        {
            LinkedListNode <CancelableCompletionSource <IDisposable> >?node = null;

            lock (_pending)
            {
                ThrowIfDisposed();

                if (_semaphoreSlim.CurrentCount == 1)       //Lock is free
                {
                    _semaphoreSlim.Wait(cancellationToken); //Will never block
                    return(_completedTask);
                }

                //Lock was not free
                var ccs = new CancelableCompletionSource <IDisposable>();
                node = _pending.AddLast(ccs);
            }

            cancellationToken.Register(() => Cancel(node));

            return(node.Value.Task);
        }
Пример #3
0
 public StateTask(TState state, StateChanged change)
 {
     _state  = state;
     _change = change;
     CancelableCompletionSource = new CancelableCompletionSource <TState>();
 }