Example #1
0
        /// <summary>
        /// Attempt to release array to global storage
        /// </summary>
        /// <param name="array">Array</param>
        /// <returns>True if array was returned back to the pool storage, otherwise false</returns>
        internal bool TryReleaseGlobal(byte[] array)
        {
            int iteration       = 0;
            var unpackedIndices = new HeadTailIndicesStruct(_headTailIndices);

            while (unpackedIndices.HasFreeSpace(_arrayContainer.Length))
            {
                int tailIndex = unpackedIndices.MoveTailForward(_arrayContainer.Length);
                if (unpackedIndices.CompareExchangeCurState(ref _headTailIndices))
                {
                    Interlocked.Exchange(ref _arrayContainer[tailIndex], array);
                    return(true);
                }

                if (++iteration > 16)
                {
                    Thread.Yield();
                }
                else if (iteration > 4)
                {
                    Thread.SpinWait(iteration << 3);
                }

                unpackedIndices = new HeadTailIndicesStruct(_headTailIndices);
            }

            return(false);
        }
Example #2
0
        /// <summary>
        /// Attempts to rent from global array container
        /// </summary>
        /// <returns>Byte array if success, null otherwise</returns>
        internal byte[] TryRentGlobal()
        {
            int iteration       = 0;
            var unpackedIndices = new HeadTailIndicesStruct(_headTailIndices);

            while (unpackedIndices.HasAvailableElements(_arrayContainer.Length))
            {
                int headIndex = unpackedIndices.MoveHeadForward(_arrayContainer.Length);
                if (unpackedIndices.CompareExchangeCurState(ref _headTailIndices))
                {
                    return(Interlocked.Exchange(ref _arrayContainer[headIndex], null));
                }

                if (++iteration > 16)
                {
                    Thread.Yield();
                }
                else if (iteration > 4)
                {
                    Thread.SpinWait(iteration << 3);
                }

                unpackedIndices = new HeadTailIndicesStruct(_headTailIndices);
            }

            return(null);
        }