/// <summary> /// 添加任务 /// </summary> /// <param name="value"></param> internal void Add(ILinkTask value) { value.LinkTaskTicks = AutoCSer.Pub.Stopwatch.ElapsedTicks; ILinkTask headValue; do { if ((headValue = head) == null) { value.NextLinkTask = null; if (System.Threading.Interlocked.CompareExchange(ref head, value, null) == null) { if (System.Threading.Interlocked.CompareExchange(ref isThread, 1, 0) == 0) { runThread(); } return; } } else { value.NextLinkTask = headValue; if (System.Threading.Interlocked.CompareExchange(ref head, value, headValue) == headValue) { if (System.Threading.Interlocked.CompareExchange(ref isThread, 1, 0) == 0) { runThread(); } return; } } AutoCSer.Threading.ThreadYield.YieldOnly(); }while (true); }
/// <summary> /// 添加任务 /// </summary> /// <param name="value"></param> /// <returns></returns> private int addNullHead(ILinkTask value) { if (head != null) { System.Threading.Thread.Sleep(0); } value.LinkTaskTicks = AutoCSer.Pub.Stopwatch.ElapsedTicks; ILinkTask headValue; do { if ((headValue = head) == null) { value.NextLinkTask = null; if (System.Threading.Interlocked.CompareExchange(ref head, value, null) == null) { if (System.Threading.Interlocked.CompareExchange(ref isThread, 1, 0) == 0) { runThread(); } return(1); } AutoCSer.Threading.ThreadYield.YieldOnly(); } else { return(0); } }while (true); }
/// <summary> /// 线程切换检测 /// </summary> private static void check() { ILinkTask value = Task.head; if (value != null && LinkTaskConfig.Default.IsCheck(value.LinkTaskTicks)) { if (isAllTask) { if (++taskIndex == tasks.Length) { taskIndex = 0; } Task = tasks[taskIndex]; } else { try { Task = new LinkTask(); tasks[++taskIndex] = Task; if (taskIndex + 1 == tasks.Length) { isAllTask = true; } } catch (Exception error) { AutoCSer.Log.Pub.Log.Add(Log.LogType.Error, error); } } } }
internal void Run() { do { START: ILinkTask value = System.Threading.Interlocked.Exchange(ref head, null); if (value == null) { System.Threading.Thread.Sleep(0); if ((value = System.Threading.Interlocked.Exchange(ref head, null)) == null) { //isThread = 0; System.Threading.Interlocked.Exchange(ref isThread, 0); if (head != null && System.Threading.Interlocked.CompareExchange(ref isThread, 1, 0) == 0) { goto START; } return; } } do { value = value.SingleRunLinkTask(); }while (value != null); }while (true); }
internal bool CheckAdd(ILinkTask value) { if (value.NextLinkTask == null) { Add(value); return(true); } return(false); }
internal void Run() { do { START: ILinkTask value = System.Threading.Interlocked.Exchange(ref head, null); if (value == null) { System.Threading.Thread.Sleep(0); if ((value = System.Threading.Interlocked.Exchange(ref head, null)) == null) { //isThread = 0; System.Threading.Interlocked.Exchange(ref isThread, 0); if (head != null && System.Threading.Interlocked.CompareExchange(ref isThread, 1, 0) == 0) { goto START; } return; } } do { try { do { value.RunTask(ref value); }while (value != null); break; } catch (Exception error) { AutoCSer.Log.Pub.Log.Add(Log.LogType.Error, error); } }while (value != null); }while (true); }
/// <summary> /// 执行任务 /// </summary> /// <param name="next">下一个任务节点</param> void ILinkTask.RunTask(ref ILinkTask next) { next = LinkNext; LinkNext = null; RunTask(); }