/// <summary> /// 添加任务 /// </summary> /// <param name="value"></param> internal void Add(LinkQueueTaskNode value) { while (System.Threading.Interlocked.CompareExchange(ref queueLock, 1, 0) != 0) { AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.SqlLinkQueueTaskPush); } if (head == null) { head = end = value; } else { end.LinkNext = value; end = value; } queueLock = 0; if (System.Threading.Interlocked.CompareExchange(ref isThread, 1, 0) == 0) { try { threadPool.FastStart(run); } catch (Exception error) { AutoCSer.Log.Pub.Log.add(Log.LogType.Error, error); run(); } } }
private void run() { START: DbConnection connection = null; do { while (System.Threading.Interlocked.CompareExchange(ref queueLock, 1, 0) != 0) { AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.SqlLinkQueueTaskPop); } LinkQueueTaskNode value = head; head = end = null; queueLock = 0; if (value == null) { System.Threading.Thread.Sleep(0); while (System.Threading.Interlocked.CompareExchange(ref queueLock, 1, 0) != 0) { AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.SqlLinkQueueTaskPop); } value = head; head = end = null; queueLock = 0; if (value == null) { client.FreeConnection(ref connection); //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 = value.RunLinkQueueTask(ref connection); }while (value != null); break; } catch (Exception error) { client.CloseErrorConnection(ref connection); AutoCSer.Log.Pub.Log.add(Log.LogType.Error, error); } LinkQueueTaskNode next = value.LinkNext; value.LinkNext = null; value = next; }while (value != null); }while (true); }