示例#1
0
        /// <summary>
        /// Acquires an object from the pool. The method is blocked when there is no object available
        /// in the pool. When there is no object, it waits until any object is returned to the pool.
        /// The method is recommended to use when the objects are sufficient.
        /// </summary>
        /// <returns>The pooled object</returns>
        public T Acquire()
        {
            AtomicInt32 acquiredInvalidCounter = AtomicInt32.From(0);

            T    obj;
            var  spin = new SpinWait();
            bool localValidateOnAcquire = validator.ValidateOnAcquire;

            bool acquired = false;

            TryAcquire(-1, out obj);
            do
            {
                spin.SpinOnce();
                if (null == obj)
                {
                    TryAcquire(-1, out obj);
                }
                else
                {
                    if (localValidateOnAcquire && !validator.Validate(obj))
                    {
                        acquiredInvalidCounter.Increment();
                        locker.EnterWriteLock();
                        try
                        {
                            DoInvalidateObject(obj);
                        }
                        finally
                        {
                            obj = null;
                            locker.ExitWriteLock();
                        }

                        if (acquiredInvalidCounter.Value >= AcquiredInvalidLimit)
                        {
                            throw new InvalidOperationException($"Unable to create a valid object after {acquiredInvalidCounter.Value} attempts.");
                        }
                    }
                    else
                    {
                        acquired = true;
                    }
                }
            } while (!acquired);

            return(obj);
        }
示例#2
0
 public void Handle(T message)
 {
     requestCount.Increment();
 }
示例#3
0
 /// <summary>
 /// This asynchronous method is invoked by an action on its owning queue when the action gets canceled while in the Started state.
 /// This allows the queue to quickly determine when it needs to sweep through the queued actions looking for cancled ones (and when not).
 /// </summary>
 public void NoteCancelHasBeenRequestedOnRelatedAction()
 {
     cancelRequestCount.Increment();
 }