public override bool Release(int decrement)
        {
            if ((uint)(decrement - 1) > SharedConstants.TooBigOrNegative) // decrement <= 0
            {
                ThrowHelper.ThrowArgumentException_Positive(decrement, ExceptionArgument.decrement);
            }

            return(Release0(decrement));
        }
        public override IReferenceCounted Retain(int increment)
        {
            if ((uint)(increment - 1) > SharedConstants.TooBigOrNegative) // increment <= 0
            {
                ThrowHelper.ThrowArgumentException_Positive(increment, ExceptionArgument.increment);
            }

            return(Retain0(increment));
        }
        public ReceiveBufferSizeEstimate(int minimum = DefaultMinimum, int initial = DefaultInitial, int maximum = DefaultMaximum)
        {
            if ((uint)(minimum - 1) > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentException_Positive(minimum, ExceptionArgument.minimum);
            }
            if (initial < minimum)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.initial);
            }
            if (initial > maximum)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.maximum);
            }

            int min = GetSizeTableIndex(minimum);

            if (SizeTable[min] < minimum)
            {
                _minIndex = min + 1;
            }
            else
            {
                _minIndex = min;
            }

            int max = GetSizeTableIndex(maximum);

            if (SizeTable[max] > maximum)
            {
                _maxIndex = max - 1;
            }
            else
            {
                _maxIndex = max;
            }

            _index             = GetSizeTableIndex(initial);
            _receiveBufferSize = SizeTable[_index];
        }
Ejemplo n.º 4
0
        // TODO: Test if adding padding helps under contention
        //private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;

        internal PoolThreadCache(PoolArena <T> heapArena, PoolArena <T> directArena,
                                 int tinyCacheSize, int smallCacheSize, int normalCacheSize,
                                 int maxCachedBufferCapacity, int freeSweepAllocationThreshold)
        {
            if (maxCachedBufferCapacity < 0)
            {
                ThrowHelper.ThrowArgumentException_PositiveOrZero(maxCachedBufferCapacity, ExceptionArgument.maxCachedBufferCapacity);
            }

            _freeSweepAllocationThreshold = freeSweepAllocationThreshold;
            HeapArena   = heapArena;
            DirectArena = directArena;
            if (directArena is object)
            {
                tinySubPageDirectCaches = CreateSubPageCaches(
                    tinyCacheSize, PoolArena <T> .NumTinySubpagePools, SizeClass.Tiny);
                smallSubPageDirectCaches = CreateSubPageCaches(
                    smallCacheSize, directArena.NumSmallSubpagePools, SizeClass.Small);

                _numShiftsNormalDirect = Log2(directArena.PageSize);
                normalDirectCaches     = CreateNormalCaches(
                    normalCacheSize, maxCachedBufferCapacity, directArena);

                directArena.IncrementNumThreadCaches();
            }
            else
            {
                // No directArea is configured so just null out all caches
                tinySubPageDirectCaches  = null;
                smallSubPageDirectCaches = null;
                normalDirectCaches       = null;
                _numShiftsNormalDirect   = -1;
            }
            if (heapArena is object)
            {
                // Create the caches for the heap allocations
                tinySubPageHeapCaches = CreateSubPageCaches(
                    tinyCacheSize, PoolArena <T> .NumTinySubpagePools, SizeClass.Tiny);
                smallSubPageHeapCaches = CreateSubPageCaches(
                    smallCacheSize, heapArena.NumSmallSubpagePools, SizeClass.Small);

                _numShiftsNormalHeap = Log2(heapArena.PageSize);
                normalHeapCaches     = CreateNormalCaches(
                    normalCacheSize, maxCachedBufferCapacity, heapArena);

                heapArena.IncrementNumThreadCaches();
            }
            else
            {
                // No heapArea is configured so just null out all caches
                tinySubPageHeapCaches  = null;
                smallSubPageHeapCaches = null;
                normalHeapCaches       = null;
                _numShiftsNormalHeap   = -1;
            }

            // We only need to watch the thread when any cache is used.
            if (tinySubPageDirectCaches is object || smallSubPageDirectCaches is object || normalDirectCaches is object ||
                tinySubPageHeapCaches is object || smallSubPageHeapCaches is object || normalHeapCaches is object)
            {
                if (freeSweepAllocationThreshold < 1)
                {
                    ThrowHelper.ThrowArgumentException_Positive(freeSweepAllocationThreshold, ExceptionArgument.freeSweepAllocationThreshold);
                }
                _freeTask         = Free0;
                _deathWatchThread = Thread.CurrentThread;

                // The thread-local cache will keep a list of pooled buffers which must be returned to
                // the pool when the thread is not alive anymore.
                ThreadDeathWatcher.Watch(_deathWatchThread, _freeTask);
            }
            else
            {
                _freeTask         = null;
                _deathWatchThread = null;
            }
        }