Ejemplo n.º 1
0
        CommunicationWaitResult AbortCore(CancellationToken token)
        {
            ICommunicationWaiter    busyWaiter = null;
            CommunicationWaitResult result     = CommunicationWaitResult.Succeeded;

            lock (ThisLock)
            {
                if (_busyCount > 0)
                {
                    if (_busyWaiter != null)
                    {
                        busyWaiter = _busyWaiter;
                    }
                    else
                    {
                        busyWaiter  = new AsyncCommunicationWaiter(ThisLock);
                        _busyWaiter = busyWaiter;
                    }
                    Interlocked.Increment(ref _busyWaiterCount);
                }
            }

            if (busyWaiter != null)
            {
                result = busyWaiter.Wait(true, token);
                if (Interlocked.Decrement(ref _busyWaiterCount) == 0)
                {
                    busyWaiter.Dispose();
                    _busyWaiter = null;
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <CommunicationWaitResult> CloseCoreAsync(bool aborting, CancellationToken token)
        {
            token.ThrowIfCancellationRequested();
            ICommunicationWaiter    busyWaiter = null;
            CommunicationWaitResult result     = CommunicationWaitResult.Succeeded;

            lock (ThisLock)
            {
                if (BusyCount > 0)
                {
                    if (_busyWaiter != null)
                    {
                        if (!aborting && _aborted)
                        {
                            return(CommunicationWaitResult.Aborted);
                        }

                        busyWaiter = _busyWaiter;
                    }
                    else
                    {
                        busyWaiter  = new AsyncCommunicationWaiter(ThisLock);
                        _busyWaiter = busyWaiter;
                    }
                    Interlocked.Increment(ref _busyWaiterCount);
                }
            }

            if (busyWaiter != null)
            {
                result = await busyWaiter.WaitAsync(aborting, token);

                if (Interlocked.Decrement(ref _busyWaiterCount) == 0)
                {
                    busyWaiter.Dispose();
                    _busyWaiter = null;
                }
            }

            return(result);
        }