Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="pool"></param>
        public PooledObject(ObjectPool <T> pool)
        {
            Assert.IsNotNull(pool);

            _pool = pool;
            _item = pool.CreateObject(CancellationToken.None);
        }
Пример #2
0
 private void FactoryDestroyObject(ObjectPoolItem <T> item)
 {
     _factory.DestroyObject(item.Value);
     Log.Trace("ObjectPool item(key:{0}) destroyed.", item.Key);
 }
Пример #3
0
        internal ObjectPoolItem <T> CreateObject(CancellationToken cancellationToken)
        {
            ObjectPoolItem <T> item = null;

            while (true)
            {
                if (_idleItems.Count > 0)
                {
                    lock (_idleItems)
                    {
                        if (_idleItems.Count > 0)
                        {
                            var last = _idleItems.Last;
                            _idleItems.RemoveLast();
                            item = last.Value;
                            Log.Trace("Item(key:{0}) reused from object pool. idle: {1}, active: {2}.", item.Key, _idleItems.Count, _activeItems.Count);
                        }
                    }

                    lock (_activeItems)
                    {
                        _activeItems.Add(item.Key, item);
                    }

                    _factory.InitializeObject(item.Value);
                    break;
                }
                else
                {
                    var count = _idleItems.Count + _activeItems.Count;

                    if (count < MaxSize)
                    {
                        var creationDate = LocalTime.Default.Now;
                        var value        = _factory.CreateObject();
                        var key          = Interlocked.Increment(ref _key);
                        item = new ObjectPoolItem <T>(key, value, creationDate);

                        lock (_activeItems)
                        {
                            _activeItems.Add(item.Key, item);
                        }

                        Log.Trace("New item(key:{0}) created in object pool. idle: {1}, active: {2}.", key, _idleItems.Count, _activeItems.Count);
                        break;
                    }
                    else
                    {
                        Log.Trace("object pool is active. Waiting for idle item...");
                        _idleEvent.Reset();

                        WaitHandle[] waitHandles =
                        {
                            cancellationToken.WaitHandle,
                            _idleEvent
                        };

                        var i = WaitHandle.WaitAny(waitHandles, 30000, false);

                        if (i == 0)
                        {
                            break;
                        }
                        else if (i == 1)
                        {
                        }
                        else if (i == WaitHandle.WaitTimeout)
                        {
                            throw new ApplicationException("ObjectPool.CreateObject timeout.");
                        }
                    }
                }
            }

            return(item);
        }