private CommunicationWaitResult CloseCore(TimeSpan timeout, bool aborting)
 {
     ICommunicationWaiter busyWaiter = null;
     CommunicationWaitResult succeeded = CommunicationWaitResult.Succeeded;
     lock (this.ThisLock)
     {
         if (this.busyCount > 0)
         {
             if (this.busyWaiter != null)
             {
                 if (!aborting && this.aborted)
                 {
                     return CommunicationWaitResult.Aborted;
                 }
                 busyWaiter = this.busyWaiter;
             }
             else
             {
                 busyWaiter = new SyncCommunicationWaiter(this.ThisLock);
                 this.busyWaiter = busyWaiter;
             }
             Interlocked.Increment(ref this.busyWaiterCount);
         }
     }
     if (busyWaiter != null)
     {
         succeeded = busyWaiter.Wait(timeout, aborting);
         if (Interlocked.Decrement(ref this.busyWaiterCount) == 0)
         {
             busyWaiter.Dispose();
             this.busyWaiter = null;
         }
     }
     return succeeded;
 }
Exemplo n.º 2
0
        public void DecrementActivityCount()
        {
            ICommunicationWaiter activityWaiter = null;
            bool empty = false;

            lock (ThisLock)
            {
                if (!(ActivityCount > 0))
                {
                    Fx.Assert("ServiceChannelManager.DecrementActivityCount: (this.activityCount > 0)");
                }
                if (--ActivityCount == 0)
                {
                    if (_activityWaiter != null)
                    {
                        activityWaiter = _activityWaiter;
                        Interlocked.Increment(ref _activityWaiterCount);
                    }
                    if (BusyCount == 0)
                    {
                        empty = true;
                    }
                }
            }

            if (activityWaiter != null)
            {
                activityWaiter.Signal();
                if (Interlocked.Decrement(ref _activityWaiterCount) == 0)
                {
                    activityWaiter.Dispose();
                    _activityWaiter = null;
                }
            }

            if (empty && State == LifetimeState.Opened)
            {
                OnEmpty();
            }
        }
Exemplo n.º 3
0
        private CommunicationWaitResult CloseCore(TimeSpan timeout, bool aborting)
        {
            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 SyncCommunicationWaiter(ThisLock);
                        _busyWaiter = busyWaiter;
                    }
                    Interlocked.Increment(ref _busyWaiterCount);
                }
            }

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

            return(result);
        }
Exemplo n.º 4
0
        public async Task CloseInputAsync(CancellationToken token)
        {
            AsyncCommunicationWaiter activityWaiter = null;

            lock (ThisLock)
            {
                if (ActivityCount > 0)
                {
                    activityWaiter = new AsyncCommunicationWaiter(ThisLock);
                    if (!(_activityWaiter == null))
                    {
                        Fx.Assert("ServiceChannelManager.CloseInput: (this.activityWaiter == null)");
                    }
                    _activityWaiter = activityWaiter;
                    Interlocked.Increment(ref _activityWaiterCount);
                }
            }

            if (activityWaiter != null)
            {
                CommunicationWaitResult result = await activityWaiter.WaitAsync(false, token);

                if (Interlocked.Decrement(ref _activityWaiterCount) == 0)
                {
                    activityWaiter.Dispose();
                    _activityWaiter = null;
                }

                switch (result)
                {
                case CommunicationWaitResult.Expired:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(SR.SfxCloseTimedOutWaitingForDispatchToComplete));

                case CommunicationWaitResult.Aborted:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(GetType().ToString()));
                }
            }
        }
Exemplo n.º 5
0
        protected virtual IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
        {
            CloseCommunicationAsyncResult result = null;

            lock (this.ThisLock)
            {
                if (this.busyCount > 0)
                {
                    if (this.busyWaiter != null)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(base.GetType().ToString()));
                    }
                    result          = new CloseCommunicationAsyncResult(timeout, callback, state, this.ThisLock);
                    this.busyWaiter = result;
                    Interlocked.Increment(ref this.busyWaiterCount);
                }
            }
            if (result != null)
            {
                return(result);
            }
            return(new CompletedAsyncResult(callback, state));
        }
Exemplo n.º 6
0
        protected void DecrementBusyCount()
        {
            ICommunicationWaiter busyWaiter = null;
            bool empty = false;

            lock (ThisLock)
            {
                if (BusyCount <= 0)
                {
                    throw Fx.AssertAndThrow("LifetimeManager.DecrementBusyCount: (this.busyCount > 0)");
                }
                if (--BusyCount == 0)
                {
                    if (_busyWaiter != null)
                    {
                        busyWaiter = _busyWaiter;
                        Interlocked.Increment(ref _busyWaiterCount);
                    }
                    empty = true;
                }
            }

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

            if (empty && State == LifetimeState.Opened)
            {
                OnEmpty();
            }
        }
Exemplo n.º 7
0
        public void CloseInput(TimeSpan timeout)
        {
            SyncCommunicationWaiter activityWaiter = null;

            lock (this.ThisLock)
            {
                if (this.activityCount > 0)
                {
                    activityWaiter = new SyncCommunicationWaiter(this.ThisLock);
                    if (!(this.activityWaiter == null))
                    {
                        Fx.Assert("ServiceChannelManager.CloseInput: (this.activityWaiter == null)");
                    }
                    this.activityWaiter = activityWaiter;
                    Interlocked.Increment(ref this.activityWaiterCount);
                }
            }

            if (activityWaiter != null)
            {
                CommunicationWaitResult result = activityWaiter.Wait(timeout, false);
                if (Interlocked.Decrement(ref this.activityWaiterCount) == 0)
                {
                    activityWaiter.Dispose();
                    this.activityWaiter = null;
                }

                switch (result)
                {
                case CommunicationWaitResult.Expired:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(SR.GetString(SR.SfxCloseTimedOutWaitingForDispatchToComplete)));

                case CommunicationWaitResult.Aborted:
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(this.GetType().ToString()));
                }
            }
        }
Exemplo n.º 8
0
 protected virtual void OnEndClose(IAsyncResult result)
 {
     if (result is CloseCommunicationAsyncResult)
     {
         CloseCommunicationAsyncResult.End(result);
         if (Interlocked.Decrement(ref _busyWaiterCount) == 0)
         {
             _busyWaiter.Dispose();
             _busyWaiter = null;
         }
     }
     else
         CompletedAsyncResult.End(result);
 }
Exemplo n.º 9
0
        protected virtual IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
        {
            CloseCommunicationAsyncResult closeResult = null;

            lock (this.ThisLock)
            {
                if (_busyCount > 0)
                {
                    if (_busyWaiter != null)
                    {
                        Fx.Assert(_aborted, "LifetimeManager.OnBeginClose: (this.aborted == true)");
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(this.GetType().ToString()));
                    }
                    else
                    {
                        closeResult = new CloseCommunicationAsyncResult(timeout, callback, state, this.ThisLock);
                        Fx.Assert(_busyWaiter == null, "LifetimeManager.OnBeginClose: (this.busyWaiter == null)");
                        _busyWaiter = closeResult;
                        Interlocked.Increment(ref _busyWaiterCount);
                    }
                }
            }

            if (closeResult != null)
            {
                return closeResult;
            }
            else
            {
                return new CompletedAsyncResult(callback, state);
            }
        }
Exemplo n.º 10
0
        protected void DecrementBusyCount()
        {
            ICommunicationWaiter busyWaiter = null;
            bool empty = false;

            lock (this.ThisLock)
            {
                if (_busyCount <= 0)
                {
                    throw Fx.AssertAndThrow("LifetimeManager.DecrementBusyCount: (this.busyCount > 0)");
                }
                if (--_busyCount == 0)
                {
                    if (_busyWaiter != null)
                    {
                        busyWaiter = _busyWaiter;
                        Interlocked.Increment(ref _busyWaiterCount);
                    }
                    empty = true;
                }
            }

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

            if (empty && this.State == LifetimeState.Opened)
                OnEmpty();
        }
 protected virtual IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
 {
     CloseCommunicationAsyncResult result = null;
     lock (this.ThisLock)
     {
         if (this.busyCount > 0)
         {
             if (this.busyWaiter != null)
             {
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(base.GetType().ToString()));
             }
             result = new CloseCommunicationAsyncResult(timeout, callback, state, this.ThisLock);
             this.busyWaiter = result;
             Interlocked.Increment(ref this.busyWaiterCount);
         }
     }
     if (result != null)
     {
         return result;
     }
     return new CompletedAsyncResult(callback, state);
 }