⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lockfreequeue!1.cs

📁 破解的飞信源代码
💻 CS
字号:
namespace NCindy.DataStructures
{
    using NCindy.Threading;
    using System;
    using System.Runtime.InteropServices;
    using System.Threading;

    public sealed class LockFreeQueue<T>
    {
        private LockFreeNode<T> m_head;
        private NodeManager<T> m_nodeManager;
        private LockFreeNode<T> m_tail;

        public LockFreeQueue() : this(null)
        {
        }

        public LockFreeQueue(NodeManager<T> nodeManager)
        {
            this.m_nodeManager = (nodeManager == null) ? NodeManager<T>.Default : nodeManager;
            this.m_head = this.m_nodeManager.Allocate();
            this.m_tail = this.m_head;
        }

        public void Clear()
        {
            Thread.MemoryBarrier();
            Interlocked.Exchange<LockFreeNode<T>>(ref this.m_head, this.m_nodeManager.Allocate());
            Interlocked.Exchange<LockFreeNode<T>>(ref this.m_tail, this.m_head);
        }

        public T Dequeue()
        {
            T item;
            if (!this.TryDequeue(out item))
            {
                throw new InvalidOperationException("Queue is empty");
            }
            return item;
        }

        public void Enqueue(T item)
        {
            LockFreeNode<T> then = this.m_nodeManager.Allocate(item);
            LockFreeNode<T> @if = null;
            bool flag = false;
            while (!flag)
            {
                @if = this.m_tail;
                LockFreeNode<T> next = @if.Next;
                Thread.MemoryBarrier();
                if (@if == this.m_tail)
                {
                    if (next != null)
                    {
                        InterlockedEx.IfThen<LockFreeNode<T>>(ref this.m_tail, @if, next);
                    }
                    else
                    {
                        flag = InterlockedEx.IfThen<LockFreeNode<T>>(ref @if.Next, null, then);
                    }
                }
            }
            InterlockedEx.IfThen<LockFreeNode<T>>(ref this.m_tail, @if, then);
        }

        public bool TryDequeue(out T item)
        {
            item = default(T);
            bool flag = false;
            while (!flag)
            {
                LockFreeNode<T> @if = this.m_head;
                LockFreeNode<T> tail = this.m_tail;
                LockFreeNode<T> then = @if.Next;
                Thread.MemoryBarrier();
                if (@if == this.m_head)
                {
                    if (@if == tail)
                    {
                        if (then == null)
                        {
                            return false;
                        }
                        InterlockedEx.IfThen<LockFreeNode<T>>(ref this.m_tail, tail, then);
                    }
                    else
                    {
                        item = then.Item;
                        if (InterlockedEx.IfThen<LockFreeNode<T>>(ref this.m_head, @if, then))
                        {
                            this.m_nodeManager.Free(@if);
                        }
                    }
                }
            }
            return true;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -