コード例 #1
0
 public ConcurrentListBasedMap(IComparer <K> comparer)
 {
     Guarder.CheckNull(comparer, "comparer");
     this.comparer = comparer;
     head          = new LockableNode();
     tail          = new LockableNode();
     head.Next     = tail;
 }
コード例 #2
0
 private void Find(K key, out LockableNode pred, out LockableNode curr)
 {
     pred = head;
     curr = head.Next;
     while (!ReferenceEquals(curr, tail) && comparer.Compare(curr.Key, key) < 0)
     {
         pred = curr;
         curr = curr.Next;
     }
 }
コード例 #3
0
        public bool TryAdd(K key, V value)
        {
            Guarder.CheckNull(key, "key");
            Guarder.CheckNull(value, "value");
            while (true)
            {
                LockableNode pred, curr;
                Find(key, out pred, out curr);

                pred.Lock();
                try
                {
                    curr.Lock();
                    try
                    {
                        if (Validate(pred, curr))
                        {
                            if ((ReferenceEquals(pred.Next, tail) && ReferenceEquals(curr, tail)) || (comparer.Compare(curr.Key, key) != 0))
                            {
                                var entry = new LockableNode {
                                    Key = key, Value = value, Marked = false, Next = curr
                                };
                                pred.Next = entry;
                                return(true);
                            }
                            else
                            {
                                return(false);
                            }
                        }
                    }
                    finally
                    {
                        curr.Unlock();
                    }
                }
                finally
                {
                    pred.Unlock();
                }
            }
        }
コード例 #4
0
 private bool Validate(LockableNode pred, LockableNode curr)
 {
     return(!pred.Marked && !curr.Marked && pred.Next == curr);
 }
コード例 #5
0
 private bool IsNotFound(LockableNode pred, LockableNode curr)
 {
     return(ReferenceEquals(pred.Next, tail) && ReferenceEquals(curr, tail));
 }