Пример #1
0
        /// <summary>
        /// 获取数据分配缓冲区
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        internal static AutoCSer.CacheServer.BufferCount GetBufferCount(ref SubArray <byte> data)
        {
            if (data.Length > (int)AutoCSer.CacheServer.BufferCount.BufferSize)
            {
                data.Set(new byte[data.Length], 0, data.Length);
                return(null);
            }
            AutoCSer.CacheServer.BufferCount buffer;
            while (System.Threading.Interlocked.CompareExchange(ref bufferCountLock, 1, 0) != 0)
            {
                AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.MessageQueueGetBuffer);
            }
            buffer          = bufferCount.Get(ref data);
            bufferCountLock = 0;

            if (buffer == null)
            {
                Monitor.Enter(newBufferCountLock);
                AutoCSer.CacheServer.BufferCount oldBufferCount = bufferCount;
                try
                {
                    while (System.Threading.Interlocked.CompareExchange(ref bufferCountLock, 1, 0) != 0)
                    {
                        AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.MessageQueueGetBuffer);
                    }
                    buffer          = oldBufferCount.Get(ref data);
                    bufferCountLock = 0;
                    if (buffer == null)
                    {
                        AutoCSer.CacheServer.BufferCount newBufferCount = new AutoCSer.CacheServer.BufferCount();
                        buffer = newBufferCount.Get(ref data);
                        if (newBufferCount.Size > oldBufferCount.Size)
                        {
                            bufferCount = newBufferCount;
                            oldBufferCount.Free();
                        }
                        else
                        {
                            newBufferCount.Free();
                        }
                    }
                }
                finally { Monitor.Exit(newBufferCountLock); }
            }
            return(buffer);
        }
Пример #2
0
        /// <summary>
        /// 获取数据分配缓冲区
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        internal static AutoCSer.CacheServer.BufferCount GetBufferCount(ref SubArray <byte> data)
        {
            if (data.Length > (int)AutoCSer.CacheServer.BufferCount.BufferSize)
            {
                data.Set(new byte[data.Length], 0, data.Length);
                return(null);
            }
            AutoCSer.CacheServer.BufferCount buffer;
            bufferCountLock.EnterYield();
            buffer = bufferCount.Get(ref data);
            bufferCountLock.Exit();

            if (buffer == null)
            {
                Monitor.Enter(newBufferCountLock);
                AutoCSer.CacheServer.BufferCount oldBufferCount = bufferCount;
                try
                {
                    bufferCountLock.EnterYield();
                    buffer = oldBufferCount.Get(ref data);
                    bufferCountLock.Exit();
                    if (buffer == null)
                    {
                        AutoCSer.CacheServer.BufferCount newBufferCount = new AutoCSer.CacheServer.BufferCount();
                        buffer = newBufferCount.Get(ref data);
                        if (newBufferCount.Size > oldBufferCount.Size)
                        {
                            bufferCount = newBufferCount;
                            oldBufferCount.Free();
                        }
                        else
                        {
                            newBufferCount.Free();
                        }
                    }
                }
                finally { Monitor.Exit(newBufferCountLock); }
            }
            return(buffer);
        }