예제 #1
0
        bool InsertInternal(uint key, TKey subKey, KeyValuePair <TKey, TValue> data, out Node current)
        {
            Node node = new Node().Init(ComputeRegularKey(key), subKey, data);

            uint b = key % (uint)size;
            Node bucket;

            if ((bucket = GetBucket(b)) == null)
            {
                bucket = InitializeBucket(b);
            }

            if (!ListInsert(node, bucket, out current))
            {
                return(false);
            }

            int csize = size;

            if (ClientInterlocked.Increment(ref count) / csize > MaxLoad && (csize & 0x40000000) == 0)
            {
                ClientInterlocked.CompareExchange(ref size, 2 * csize, csize);
            }

            current = node;

            return(true);
        }
        public void Push(T item)
        {
            Node temp = new Node();

            temp.Value = item;
            do
            {
                temp.Next = head;
            } while (ClientInterlocked.CompareExchange(ref head, temp, temp.Next) != temp.Next);

            ClientInterlocked.Increment(ref count);
        }
예제 #3
0
        public bool TryAdd(T data)
        {
            Node node = new Node();

            node.Data = data;
            node.Key  = comparer.GetHashCode(data);

            if (ListInsert(node))
            {
                ClientInterlocked.Increment(ref count);
                return(true);
            }

            return(false);
        }
예제 #4
0
 private IActionQueue GetActionQueue()
 {
     //二种策略,dispatch主动推送到处理线程或者线程主动取
     if (m_IsPassive)
     {
         //主动取策略,所有线程共享一个ActionQueue
         return(m_ActionQueue);
     }
     else
     {
         //主动推送策略,此情形每个线程各有一个ActionQueue
         int index = ClientInterlocked.Increment(ref m_TurnIndex) % m_ThreadNum;
         return(m_Threads[index]);
     }
 }
        public void Enqueue(T item)
        {
            Node node = new Node();

            node.Value = item;

            object oldTail = null;
            object oldNext = null;

            bool update = false;

            while (!update)
            {
                oldTail = tail;
                oldNext = ((Node)oldTail).Next;

                // Did tail was already updated ?
                if (tail == oldTail)
                {
                    if (oldNext == null)
                    {
                        // The place is for us
                        Node tailObj = (Node)tail;
                        update = ClientInterlocked.CompareExchange(ref tailObj.Next, node, null) == null;
                    }
                    else
                    {
                        // another Thread already used the place so give him a hand by putting tail where it should be
                        ClientInterlocked.CompareExchange(ref tail, oldNext, oldTail);
                    }
                }
            }
            // At this point we added correctly our node, now we have to update tail. If it fails then it will be done by another thread
            ClientInterlocked.CompareExchange(ref tail, node, oldTail);
            ClientInterlocked.Increment(ref count);
        }