private CachedItem LockedGetByNode(InterlockedLinkedListNode node, int millisecondsTimeout) { SpinWait wait = new SpinWait(); long startTicks = 0; if (millisecondsTimeout != -1 && millisecondsTimeout != 0) { startTicks = DateTime.UtcNow.Ticks; } do { CachedItem item = node.item; if (item != InterlockedLinkedListNode.InvalidCachedItem) { if( Interlocked.CompareExchange(ref node.item, InterlockedLinkedListNode.InvalidCachedItem, item) == item) { return item; } } wait.SpinOnce(); } while (millisecondsTimeout != 0 && ((millisecondsTimeout == -1 || !wait.NextSpinWillYield) || !TimeoutExpired(startTicks, millisecondsTimeout))) ; //if (Interlocked.CompareExchange(ref failureCount2, 0, 10) == 10) //{ // XMS.Core.Container.LogService.Warn(String.Format("为指定缓存项的前一节点获取锁的等待时间超过 {0}ms 时间限制的次数超过 100 次的限制,为 {1} 次。", millisecondsTimeout, failureCount2)); //} //Interlocked.Increment(ref failureCount2); if (XMS.Core.Container.LogService.IsDebugEnabled) { XMS.Core.Container.LogService.Debug(String.Format("未能在指定的超时时间 {0}ms 内获取可用于提升当前命中缓存项位置的前一节点的锁,缓存项的位置保持不变。", millisecondsTimeout)); } return null; }
internal void Invalidate() { this.list = null; this.next = null; this.prev = null; }
internal void ValidateNode(InterlockedLinkedListNode node) { if (node == null) { throw new ArgumentNullException("node"); } if (node.list != this) { throw new InvalidOperationException("移除的节点不属于当前集合。"); } }
public void Clear() { InterlockedLinkedListNode head = this.head; while (head != null) { InterlockedLinkedListNode node2 = head; head = head.next; node2.Invalidate(); } this.head = null; this.count = 0; this.version++; }
internal void InternalRemoveNode(InterlockedLinkedListNode node) { if (node.next == node) { this.head = null; } else { node.next.prev = node.prev; node.prev.next = node.next; if (this.head == node) { this.head = node.next; } } node.Invalidate(); this.count--; this.version++; }
public void Remove(InterlockedLinkedListNode node) { this.ValidateNode(node); this.InternalRemoveNode(node); }
private void InternalInsertNodeBefore(InterlockedLinkedListNode node, InterlockedLinkedListNode newNode) { newNode.next = node; newNode.prev = node.prev; node.prev.next = newNode; node.prev = newNode; this.version++; this.count++; }
private void InternalInsertNodeToEmptyList(InterlockedLinkedListNode newNode) { newNode.next = newNode; newNode.prev = newNode; this.head = newNode; this.version++; this.count++; }
public InterlockedLinkedListNode AddFirst(CachedItem value) { InterlockedLinkedListNode newNode = new InterlockedLinkedListNode(this, value); if (this.head == null) { this.InternalInsertNodeToEmptyList(newNode); return newNode; } this.InternalInsertNodeBefore(this.head, newNode); this.head = newNode; return newNode; }