Exemple #1
0
        /// <summary>
        /// 获取数据缓冲区
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        internal static Buffer GetBuffer(int size)
        {
            if (size > (int)BufferSize)
            {
                return(new Buffer(size));
            }
            Buffer buffer;

            bufferCountLock.EnterYield();
            try
            {
                buffer = bufferCount.get(size);
            }
            finally { bufferCountLock.Exit(); }

            if (buffer == null)
            {
                byte        step = 0xff;
                BufferCount newBufferCount = null, oldBufferCount;
                Monitor.Enter(newBufferCountLock);
                oldBufferCount = bufferCount;
                try
                {
                    bufferCountLock.EnterYield();
                    step   = 0;
                    buffer = oldBufferCount.get(size);
                    bufferCountLock.Exit();
                    step = 0xff;

                    if (buffer == null)
                    {
                        newBufferCount = new BufferCount();
                        step           = 1;
                        buffer         = newBufferCount.get(size);
                        if (newBufferCount.Size > oldBufferCount.Size)
                        {
                            step        = 0xff;
                            bufferCount = newBufferCount;
                            oldBufferCount.Free();
                        }
                    }
                }
                finally
                {
                    switch (step)
                    {
                    case 0: bufferCountLock.Exit(); break;

                    case 1: newBufferCount.Free(); break;
                    }
                    Monitor.Exit(newBufferCountLock);
                }
            }
            return(buffer);
        }
        /// <summary>
        /// 获取数据缓冲区
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        internal static Buffer GetBuffer(int size)
        {
            if (size > (int)BufferSize) return new Buffer(size);
            Buffer buffer;
            while (System.Threading.Interlocked.CompareExchange(ref bufferCountLock, 1, 0) != 0) AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.CacheServerGetBuffer);
            try
            {
                buffer = bufferCount.get(size);
            }
            finally { System.Threading.Interlocked.Exchange(ref bufferCountLock, 0); }

            if (buffer == null)
            {
                byte step = 0xff;
                BufferCount newBufferCount = null, oldBufferCount;
                Monitor.Enter(newBufferCountLock);
                oldBufferCount = bufferCount;
                try
                {
                    while (System.Threading.Interlocked.CompareExchange(ref bufferCountLock, 1, 0) != 0) AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.CacheServerGetBuffer);
                    step = 0;
                    buffer = oldBufferCount.get(size);
                    System.Threading.Interlocked.Exchange(ref bufferCountLock, 0);
                    step = 0xff;

                    if (buffer == null)
                    {
                        newBufferCount = new BufferCount();
                        step = 1;
                        buffer = newBufferCount.get(size);
                        if (newBufferCount.Size > oldBufferCount.Size)
                        {
                            step = 0xff;
                            bufferCount = newBufferCount;
                            oldBufferCount.Free();
                        }
                    }
                }
                finally
                {
                    switch (step)
                    {
                        case 0: System.Threading.Interlocked.Exchange(ref bufferCountLock, 0); break;
                        case 1: newBufferCount.Free(); break;
                    }
                    Monitor.Exit(newBufferCountLock);
                }
            }
            return buffer;
        }