/// <summary> /// 超时检测 /// </summary> protected internal override void OnTimer() { do { long value = System.Threading.Interlocked.Read(ref this.secondIndex.Value); SecondIndex secondIndex = new SecondIndex { Value = value }; if (System.Threading.Interlocked.CompareExchange(ref this.secondIndex.Value, secondIndex.Next(Counts.CurrentIndex), value) == value) { secondIndex.Value = value; int count = System.Threading.Interlocked.Exchange(ref Counts.Int[secondIndex.Index], 0); if (count > 0) { if (secondIndex.Second != 0) { OnTimeout(secondIndex.Second); } else { AutoCSer.LogHelper.Fatal("非法超时秒计数 0,请检查 Decrement 调用是否有 0 传参", LogLevel.Fatal | LogLevel.Error | LogLevel.AutoCSer); } } return; } AutoCSer.Threading.ThreadYield.Yield(); }while (true); }
/// <summary> /// 增加超时计数 /// </summary> /// <param name="seconds">超时秒数,必须大于 0</param> /// <returns>超时秒计数</returns> internal uint Increment(ushort seconds) { SecondIndex secondIndex = new SecondIndex { Value = System.Threading.Interlocked.Read(ref this.secondIndex.Value) }; int index = secondIndex.Index + seconds; uint timeoutSeconds = secondIndex.Second + seconds; if (index >= Counts.CurrentIndex) { index -= Counts.CurrentIndex; } if (timeoutSeconds != 0) { System.Threading.Interlocked.Increment(ref Counts.Int[index]); return(timeoutSeconds); } //屏蔽 0 if (++index == Counts.CurrentIndex) { index = 0; } System.Threading.Interlocked.Increment(ref Counts.Int[index]); return(1); }
/// <summary> /// 减少超时计数 /// </summary> /// <param name="seconds">超时秒计数</param> internal void Decrement(uint seconds) { SecondIndex secondIndex = new SecondIndex { Value = System.Threading.Interlocked.Read(ref this.secondIndex.Value) }; int index = (int)(seconds - secondIndex.Second); if (index > 0) { if (index <= Counts.CurrentIndex) { index += secondIndex.Index; if (index >= Counts.CurrentIndex) { index -= Counts.CurrentIndex; } System.Threading.Interlocked.Decrement(ref Counts.Int[index]); } } else if ((uint)index == 0 && System.Threading.Interlocked.Decrement(ref Counts.Int[secondIndex.Index]) < 0) { System.Threading.Interlocked.Increment(ref Counts.Int[secondIndex.Index]); } }
/// <summary> /// 超时检测 /// </summary> internal void OnTimer() { do { long value = System.Threading.Interlocked.Read(ref this.secondIndex.Value); SecondIndex secondIndex = new SecondIndex { Value = value }; if (System.Threading.Interlocked.CompareExchange(ref this.secondIndex.Value, secondIndex.Next(Counts.CustomSize), value) == value) { secondIndex.Value = value; int count = System.Threading.Interlocked.Exchange(ref Counts.Int[secondIndex.Index], 0); if (count > 0) { if (secondIndex.Second != 0) { OnTimeout(secondIndex.Second); } else { AutoCSer.Log.Pub.Log.Add(Log.LogType.Fatal, "非法超时秒计数 0,请检查 Decrement 调用是否有 0 传参"); } } return; } AutoCSer.Threading.ThreadYield.YieldOnly(); }while (true); }