public ConcurrentListBasedMap(IComparer <K> comparer) { Guarder.CheckNull(comparer, "comparer"); this.comparer = comparer; head = new LockableNode(); tail = new LockableNode(); head.Next = tail; }
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; } }
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(); } } }
private bool Validate(LockableNode pred, LockableNode curr) { return(!pred.Marked && !curr.Marked && pred.Next == curr); }
private bool IsNotFound(LockableNode pred, LockableNode curr) { return(ReferenceEquals(pred.Next, tail) && ReferenceEquals(curr, tail)); }