internal SecondTimerTaskNode GetClear() { SecondTimerTaskNode value = head; head = end = null; return(value); }
/// <summary> /// 添加定时任务节点 /// </summary> /// <param name="node"></param> internal void Append(SecondTimerTaskNode node) { long timeoutSeconds = node.TimeoutSeconds; TimerLinkLock.Enter(); long index = timeoutSeconds - linkArrayBaseSeconds; if (index < linkArrayCapacity) { if (index >= linkArrayIndex) { linkArray[(int)index].Append(node); TimerLinkLock.Exit(); return; } } else { index = ((index - linkArrayCapacity) >> linkArrayBitSize) + nextLinkArrayIndex; if (index < linkArrayCapacity) { nextLinkArray[(int)index].Append(node); } else { timerLink.Append(node); } TimerLinkLock.Exit(); return; } TimerLinkLock.Exit(); node.AppendCall(); }
internal void Append(SecondTimerTaskNode next) { if (end == null) { head = end = next; } else { next.DoubleLinkPrevious = end; end.DoubleLinkNext = next; end = next; } }
/// <summary> /// 将另外一个链表的首节点添加到尾节点并返回下一个节点 /// </summary> /// <param name="otherHead"></param> /// <returns></returns> internal SecondTimerTaskNode AppendOtherHead(SecondTimerTaskNode otherHead) { SecondTimerTaskNode next = otherHead.DoubleLinkNext; if (next == null) { if (otherHead.KeepMode != SecondTimerKeepMode.Canceled) { Append(otherHead); } } else { if (otherHead.KeepMode != SecondTimerKeepMode.Canceled) { otherHead.DoubleLinkNext = null; Append(otherHead); } next.DoubleLinkPrevious = null; } return(next); }
/// <summary> /// 尝试执行定时任务 /// </summary> internal void OnTimer() { SecondTimerNode node = NodeLink.End; if (node != null) { SecondTimerNode.LinkOnTimer(node); } SecondTimerTaskNode head; do { TimerLinkLock.Enter(); long seconds = linkArrayBaseSeconds + linkArrayIndex; if (seconds < SecondTimer.CurrentSeconds) { head = linkArray[linkArrayIndex++].GetClear(); if (linkArrayIndex == linkArrayCapacity) { linkArrayIndex = 0; linkArrayBaseSeconds += linkArrayCapacity; SecondTimerTaskNode nextHead = nextLinkArray[nextLinkArrayIndex++].GetClear(); while (nextHead != null) { nextHead = linkArray[(int)(nextHead.TimeoutSeconds - linkArrayBaseSeconds)].AppendOtherHead(nextHead); } if (nextLinkArrayIndex == linkArrayCapacity) { nextLinkArrayIndex = 0; long baseSeconds = linkArrayBaseSeconds + linkArrayCapacity, maxSeconds = baseSeconds + (linkArrayCapacity << linkArrayBitSize); nextHead = timerLink.GetClear(); while (nextHead != null) { if (nextHead.TimeoutSeconds < maxSeconds) { nextHead = nextLinkArray[(int)(nextHead.TimeoutSeconds - baseSeconds) >> linkArrayBitSize].AppendOtherHead(nextHead); } else { nextHead = timerLink.AppendOtherHead(nextHead); } } } } TimerLinkLock.Exit(); while (head != null) { try { do { head.Call(ref head); }while (head != null); } catch (Exception exception) { AutoCSer.LogHelper.Exception(exception, null, LogLevel.AutoCSer | LogLevel.Exception); } } } else { TimerLinkLock.Exit(); return; } }while (true); }