Esempio n. 1
0
        public T Rent()
        {
            if (_disposed)
            {
                BuffersThrowHelper.ThrowDisposed <LockedObjectPool <T> >();
            }

            var objects = _objects;
            T   obj     = null;

            var allocate = false;

#if !NETCOREAPP
            try
#endif
            {
                var spinner = new SpinWait();
                while (0 != Interlocked.CompareExchange(ref _locker, 1, 0))
                {
                    spinner.SpinOnce();
                }

                if (_index < objects.Length)
                {
                    obj = objects[_index];
                    objects[_index++] = null;
                    allocate          = obj == null;
                }
            }
#if !NETCOREAPP
            finally
#endif
            {
                Volatile.Write(ref _locker, 0);
            }

            if (allocate || (obj == null && AllocateOnEmpty))
            {
                if (TraceLowCapacityAllocation && !allocate)
                {
                    DoTrace();
                }
                obj = CreateNewObject();
            }

            return(obj);
        }
Esempio n. 2
0
        internal T?Rent(int cpuId)
        {
            if (_disposed)
            {
                BuffersThrowHelper.ThrowDisposed <LockedObjectPool <T> >();
            }

            T?obj;

            for (int i = 0; i <= Cpu.CoreCount; i++)
            {
                if ((obj = _perCorePools[cpuId].Rent()) != null)
                {
                    return(obj);
                }

                if (++cpuId == Cpu.CoreCount)
                {
                    cpuId = 0;
                }
            }

            return(RentSlow());
        }