/// <summary> /// 申请锁,每间隔 4 次调用 1 次 Thread.Sleep(0),用于高频高冲突场景(不检测休眠标识,和 SpinLock.Enter4 效果一样) /// </summary> public void EnterNotCheckSleepFlag() { do { if (System.Threading.Interlocked.CompareExchange(ref lockValue, 1, 0) == 0) { return; } ThreadYield.YieldOnly(); if (System.Threading.Interlocked.CompareExchange(ref lockValue, 1, 0) == 0) { return; } ThreadYield.YieldOnly(); if (System.Threading.Interlocked.CompareExchange(ref lockValue, 1, 0) == 0) { return; } ThreadYield.YieldOnly(); if (System.Threading.Interlocked.CompareExchange(ref lockValue, 1, 0) == 0) { return; } ThreadYield.YieldOnly(); if (System.Threading.Interlocked.CompareExchange(ref lockValue, 1, 0) == 0) { return; } System.Threading.Thread.Sleep(0); }while (true); }
/// <summary> /// 添加链表 /// </summary> /// <param name="value">链表头部</param> /// <param name="end">链表尾部</param> /// <param name="count">数据数量</param> internal void PushLink(valueType value, valueType end, int count) { System.Threading.Interlocked.Add(ref this.count, count); valueType headValue; do { if ((headValue = head) == null) { end.LinkNext = null; if (System.Threading.Interlocked.CompareExchange(ref head, value, null) == null) { return; } } else { end.LinkNext = headValue; if (System.Threading.Interlocked.CompareExchange(ref head, value, headValue) == headValue) { return; } } ThreadYield.Yield(ThreadYield.Type.YieldLinkPush); }while (true); }
public static void CompareExchangeYield(ref int value) { while (System.Threading.Interlocked.CompareExchange(ref value, 1, 0) != 0) { ThreadYield.Yield(ThreadYield.Type.Unknown); } }
/// <summary> /// 添加节点 /// </summary> /// <param name="value"></param> internal void PushNotNull(valueType value) { if (count >= maxCount) { if (isDisponse) { ((IDisposable)value).Dispose(); } return; } System.Threading.Interlocked.Increment(ref count); valueType headValue; do { if ((headValue = head) == null) { value.LinkNext = null; if (System.Threading.Interlocked.CompareExchange(ref head, value, null) == null) { return; } } else { value.LinkNext = headValue; if (System.Threading.Interlocked.CompareExchange(ref head, value, headValue) == headValue) { return; } } ThreadYield.Yield(ThreadYield.Type.YieldLinkPush); }while (true); }
/// <summary> /// 添加节点 /// </summary> /// <param name="value"></param> /// <returns></returns> internal int IsPushNotNull(valueType value) { if (count >= maxCount) { return(0); } System.Threading.Interlocked.Increment(ref count); valueType headValue; do { if ((headValue = head) == null) { value.LinkNext = null; if (System.Threading.Interlocked.CompareExchange(ref head, value, null) == null) { return(1); } } else { value.LinkNext = headValue; if (System.Threading.Interlocked.CompareExchange(ref head, value, headValue) == headValue) { return(1); } } ThreadYield.Yield(ThreadYield.Type.YieldLinkPush); }while (true); }
/// <summary> /// 弹出节点 /// </summary> /// <returns></returns> public valueType Pop() { valueType headValue; while (System.Threading.Interlocked.CompareExchange(ref popLock, 1, 0) != 0) { AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.YieldLinkPop); } do { if ((headValue = head) == null) { System.Threading.Interlocked.Exchange(ref popLock, 0); return(null); } if (System.Threading.Interlocked.CompareExchange(ref head, headValue.LinkNext, headValue) == headValue) { System.Threading.Interlocked.Exchange(ref popLock, 0); System.Threading.Interlocked.Decrement(ref count); headValue.LinkNext = null; return(headValue); } ThreadYield.Yield(ThreadYield.Type.YieldLinkPop); }while (true); }
internal void PushNotNull(valueType value) { valueType headValue; do { if ((headValue = head) == null) { value.LinkNext = null; if (System.Threading.Interlocked.CompareExchange(ref head, value, null) == null) { return; } } else { value.LinkNext = headValue; if (System.Threading.Interlocked.CompareExchange(ref head, value, headValue) == headValue) { return; } } ThreadYield.Yield(ThreadYield.Type.YieldLinkPush); }while (true); }
/// <summary> /// 申请锁,每间隔 4 次调用 1 次 Thread.Sleep(0),用于高频一般冲突场景 /// </summary> public void EnterYield() { do { if (System.Threading.Interlocked.CompareExchange(ref Lock, 1, 0) == 0) { return; } ThreadYield.YieldOnly(); if (System.Threading.Interlocked.CompareExchange(ref Lock, 1, 0) == 0) { return; } ThreadYield.YieldOnly(); if (System.Threading.Interlocked.CompareExchange(ref Lock, 1, 0) == 0) { return; } ThreadYield.YieldOnly(); if (System.Threading.Interlocked.CompareExchange(ref Lock, 1, 0) == 0) { return; } ThreadYield.YieldOnly(); if (System.Threading.Interlocked.CompareExchange(ref Lock, 1, 0) == 0) { return; } System.Threading.Thread.Sleep(0); }while (true); }
internal void PushLink(T value, T end) { T headValue; do { if ((headValue = Head) == null) { end.LinkNext = null; if (System.Threading.Interlocked.CompareExchange(ref Head, value, null) == null) { return; } } else { end.LinkNext = headValue; if (System.Threading.Interlocked.CompareExchange(ref Head, value, headValue) == headValue) { return; } } ThreadYield.Yield(); }while (true); }
/// <summary> /// 获取下一个ID /// </summary> /// <returns></returns> public long GetNext() { while (System.Threading.Interlocked.CompareExchange(ref identityLock, 1, 0) != 0) { ThreadYield.YieldOnly(); } long timestamp = startTimestamp + Date.TimestampDifference; if (timestamp < maxTimestamp) { long identity = (currentIdentity += identityIncrement); if ((identity & mask) != 0) { System.Threading.Interlocked.Exchange(ref identityLock, 0); return(identity); } if (--timestampCount == 0) { maxTimestamp += Date.MillisecondTimestampDifferencePerSecond; timestampCount = 1000; } maxTimestamp += Date.TimestampPerMillisecond; System.Threading.Interlocked.Exchange(ref identityLock, 0); return(identity); } else if (timestamp == maxTimestamp) { long identity = (((currentIdentity >> bits) + 1) << bits) | distributed; if (--timestampCount == 0) { maxTimestamp += Date.MillisecondTimestampDifferencePerSecond; timestampCount = 1000; } currentIdentity = identity; maxTimestamp += Date.TimestampPerMillisecond; System.Threading.Interlocked.Exchange(ref identityLock, 0); return(identity); } else { long identity = Date.GetMillisecondsByTimestamp(timestamp), lastIdentity = currentIdentity >> bits; while (identity <= lastIdentity) { ++identity; } timestampCount = 1000; maxTimestamp = Date.GetTimestampByMilliseconds(identity + 1); currentIdentity = (identity = (identity << bits) | distributed); System.Threading.Interlocked.Exchange(ref identityLock, 0); return(identity); } }
public valueType SinglePop() { valueType headValue; do { if ((headValue = head) == null) { return(null); } if (System.Threading.Interlocked.CompareExchange(ref head, headValue.LinkNext, headValue) == headValue) { headValue.LinkNext = null; return(headValue); } ThreadYield.Yield(ThreadYield.Type.YieldLinkPop); }while (true); }
public T SinglePop() { T headValue; do { if ((headValue = Head) == null) { return(null); } if (System.Threading.Interlocked.CompareExchange(ref Head, headValue.LinkNext, headValue) == headValue) { headValue.LinkNext = null; return(headValue); } ThreadYield.Yield(); }while (true); }